range.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <div class="map-range">
  3. <div class="fwlxrange">
  4. <div :class="model.xzmj == 0 ? 'pointer' : ''" @click="drawMap">
  5. <el-button plain v-if="model.xzmj == 0" icon="edit-pen" size="mini"
  6. >绘制</el-button
  7. >
  8. <span v-if="model.xzmj != 0">{{ model.xzmj }} km²</span>
  9. </div>
  10. <el-upload
  11. class="upload-demo"
  12. :on-change="handleChange"
  13. :auto-upload="false"
  14. :show-file-list="false"
  15. :file-list="fileList"
  16. :limit="1"
  17. accept=".zip"
  18. >
  19. <el-tooltip
  20. v-if="fileList.length > 0"
  21. :content="fileList[0].name"
  22. placement="bottom-start"
  23. effect="light"
  24. >
  25. <span class="title-item" style="display: inline-block; width: 100%">{{
  26. fileList[0].name
  27. }}</span>
  28. </el-tooltip>
  29. <el-button v-else class="upload-btn" icon="Upload" size="mini"
  30. >导入</el-button
  31. >
  32. </el-upload>
  33. <!-- <div class="clear" @click="clearAll">清除</div> -->
  34. <div>
  35. <el-button size="mini" @click="clearAll">清除</el-button>
  36. </div>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import { ShapeUpload } from "@/api/ghss/hgxfx.js";
  42. import { ElMessage } from "element-ui";
  43. // import { polygon } from "@turf/turf";
  44. import * as turf from "@turf/turf";
  45. import parse from "wellknown";
  46. export default {
  47. components: {},
  48. props: {
  49. //启用的组件,不传为所有
  50. keys: {
  51. type: Array,
  52. default: ["xzq", "hx", "sc", "zbd"],
  53. },
  54. activeTabs: {
  55. type: String,
  56. },
  57. type: {
  58. type: String,
  59. },
  60. },
  61. data() {
  62. return {
  63. userXZQ: "", //store.state.user.user.dept.district,
  64. fwlist: {
  65. xzq: {
  66. id: 0,
  67. name: "行政区",
  68. icon: "wind-power",
  69. },
  70. hx: {
  71. id: 1,
  72. name: "绘制",
  73. icon: "Share",
  74. },
  75. sc: {
  76. id: 2,
  77. name: "上传",
  78. icon: "Upload-filled",
  79. },
  80. zbd: {
  81. id: 3,
  82. name: "坐标点",
  83. icon: "Location",
  84. },
  85. },
  86. model: {
  87. fwlx: 0,
  88. xzfw: "",
  89. xzmj: 0,
  90. feature: null,
  91. },
  92. handlerDraw: null,
  93. draw: null, //绘制
  94. fileList: [], //文件
  95. fileDataID: "", //存储上传文件获取到的id
  96. dataSources: null,
  97. };
  98. },
  99. mounted() {
  100. // this.model.fwlx = this.fwlist[this.$props.keys[0]].id;
  101. },
  102. methods: {
  103. //上传文件
  104. handleChange(file, fileList) {
  105. if (fileList.length > 0) {
  106. this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
  107. this.clearAll(false);
  108. }
  109. const formdata = new FormData();
  110. formdata.append("file", file.raw);
  111. formdata.append("fromType", 2);
  112. formdata.append("fromRoute", this.$route.path);
  113. ShapeUpload(formdata).then((res) => {
  114. if (res.success) {
  115. this.model.xzmj = (res.data.area / 1000000).toFixed(2);
  116. // viewer.entities.removeAll();
  117. this.resetDataSources();
  118. this.fileDataID = res.data.id;
  119. this.model.xzfw = res.data.id;
  120. this.addPolygon(parse(res.data.geom));
  121. // let geoms = res.data.geom
  122. // .substring(
  123. // res.data.geom.indexOf("(((") + 3,
  124. // res.data.geom.length - 3
  125. // )
  126. // .split(")),((");
  127. // for (let i = 0; i < geoms.length; i++) {
  128. // let geom = geoms[i].split(",");
  129. // let points = [];
  130. // for (let j = 0; j < geom.length; j++) {
  131. // points.push(parseFloat(geom[j].split(" ")[0]));
  132. // points.push(parseFloat(geom[j].split(" ")[1]));
  133. // }
  134. // points.push(parseFloat(geom[0].split(" ")[0]));
  135. // points.push(parseFloat(geom[0].split(" ")[1]));
  136. // viewer.entities.add({
  137. // // polyline: new Cesium.PolylineGraphics({
  138. // // positions: Cesium.Cartesian3.fromDegreesArray(points),
  139. // // width: 3,
  140. // // material: Cesium.Color.BLUE.withAlpha(0.9),
  141. // // clampToGround: true,
  142. // // }),
  143. // polygon: {
  144. // hierarchy: {
  145. // positions: Cesium.Cartesian3.fromDegreesArray(points),
  146. // },
  147. // material: Cesium.Color.WHITE.withAlpha(0.3),
  148. // clampToGround: true,
  149. // outline: true,
  150. // outlineWidth: 5,
  151. // outlineColor: Cesium.Color.RED,
  152. // },
  153. // });
  154. // }
  155. // viewer.flyTo(viewer.entities);
  156. }
  157. });
  158. },
  159. //上传文件
  160. handleChange1(file, fileList) {
  161. if (fileList.length > 0) {
  162. this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
  163. this.clearAll(false);
  164. }
  165. const formdata = new FormData();
  166. formdata.append("file", file.raw);
  167. this.addPolygon();
  168. // ShapeUpload(formdata).then((res) => {
  169. // if (res.success) {
  170. // // var layer = myMap.addGeoJson("common_layer", res.data.geojson);
  171. // // var url = `${SYS_LAYERS.XZQXZ}/0/query`;
  172. // // var geom = layer.getSource().getFeatures()[0].getGeometry();
  173. // // 判断绘制范围是否在行政区范围内
  174. // let withinRange = true;
  175. // // arcMap.SearchWfsData(url, geom, function (fs) {
  176. // // if (fs.length) {
  177. // // fs.map((res) => {
  178. // // let xzqdm = res.get("XZQDM");
  179. // // if (!xzqdm.startsWith(this.userXZQ)) {
  180. // // withinRange = false;
  181. // // }
  182. // // });
  183. // // } else {
  184. // // withinRange = false;
  185. // // }
  186. // if (withinRange) {
  187. // this.model.xzfw = res.data.filepath;
  188. // // this.model.feature = layer.getSource().getFeatures()[0];
  189. // // this.model.xzmj = new OLTool().Tools.formatArea(
  190. // // this.model.feature.getGeometry(),
  191. // // true
  192. // // );
  193. // // myMap.zoomToextent(layer.getSource().getExtent());
  194. // } else {
  195. // // this.clearAll();
  196. // // ElMessage.warning("分析范围超出了权限范围,请重新上传文件!");
  197. // }
  198. // // });
  199. // }
  200. // });
  201. },
  202. // 加载GeoJSON数据
  203. addPolygon(geojson) {
  204. // let geojson =
  205. // this.$props.type == "hegxfx"
  206. // ? "/static/data/ghss/导入.geojson"
  207. // : "static/data/draw.geojson";
  208. let polygon = Cesium.GeoJsonDataSource.load(geojson, {
  209. // clampToGround: true
  210. stroke: Cesium.Color.RED,
  211. fill: Cesium.Color.WHITE.withAlpha(0.3),
  212. strokeWidth: 5,
  213. });
  214. polygon.then(function (dataSource) {
  215. // 将数据源添加到Cesium Viewer
  216. viewer.dataSources.add(dataSource);
  217. viewer.zoomTo(dataSource);
  218. this.dataSources = dataSource;
  219. });
  220. },
  221. resetDataSources() {
  222. if (this.dataSources) {
  223. viewer.dataSources.remove(this.dataSources);
  224. this.dataSources = null;
  225. }
  226. },
  227. //绘制
  228. drawMap() {
  229. if (!window.handlerPolygon) {
  230. common.initHandler("Polygon");
  231. }
  232. common.handlerDrawing("Polygon").then(
  233. (res) => {
  234. // console.log(res.positions, "------");
  235. //过滤掉高度
  236. const filteredArr = res.positions.filter((item, index) => {
  237. if ((index + 1) % 3 !== 0) {
  238. return item;
  239. }
  240. });
  241. // 数组变二维数组方法
  242. function listToMatrix(list, elementsPerSubArray) {
  243. var matrix = [],
  244. i,
  245. k;
  246. for (i = 0, k = -1; i < list.length; i++) {
  247. if (i % elementsPerSubArray === 0) {
  248. k++;
  249. matrix[k] = [];
  250. }
  251. matrix[k].push(list[i]);
  252. }
  253. return matrix;
  254. }
  255. var matrix = listToMatrix(filteredArr, 2);
  256. // console.log(matrix, "max-----");
  257. matrix.push(matrix[0]);
  258. const geojsonPolygon = turf.polygon([matrix]);
  259. var parse = require("wellknown"); //引入wellknow
  260. const wktPolygon = parse.stringify(geojsonPolygon);
  261. // console.log("WKT Polygon:", wktPolygon);
  262. const formdata = new FormData();
  263. // formdata.append("file", file.raw);
  264. formdata.append("fromType", 1);
  265. formdata.append("geom", wktPolygon);
  266. formdata.append("fromRoute", this.$route.path);
  267. // this.addPolygon();
  268. ShapeUpload(formdata).then((res) => {
  269. if (res.success) {
  270. this.fileDataID = res.data.id;
  271. this.model.xzmj = (res.data.area / 1000000).toFixed(2);
  272. this.model.xzfw = res.data.id;
  273. this.$message({
  274. message: "绘制成功!",
  275. type: "success",
  276. });
  277. }
  278. });
  279. },
  280. (err) => {
  281. console.log(err);
  282. }
  283. );
  284. window.handlerPolygon.activate();
  285. },
  286. clear() {
  287. if (this.handlerDraw != null) {
  288. this.handlerDraw.clear();
  289. viewer.scene.globe.removeAllExcavationRegion();
  290. this.handlerDraw.deactivate();
  291. this.handlerDraw = null;
  292. this.result = null;
  293. }
  294. },
  295. //清除
  296. clearAll(clearfile = true) {
  297. //销毁上传创建的面
  298. // viewer.entities.removeAll();
  299. this.resetDataSources();
  300. this.model.xzfw = "";
  301. this.model.xzmj = 0;
  302. if (clearfile) {
  303. this.fileDataID = "";
  304. this.fileList = [];
  305. }
  306. common.clearHandlerDrawing("Polygon");
  307. },
  308. //输出:重置
  309. reset() {
  310. // this.model = {
  311. // fwlx: this.fwlist[this.$props.keys[0]].id,
  312. // xzfw: "",
  313. // xzmj: 0,
  314. // };
  315. // this.fileList = []; //文件
  316. this.clearAll();
  317. },
  318. //输出:获取范围
  319. getRange() {
  320. return this.model;
  321. },
  322. },
  323. };
  324. </script>
  325. <style lang="scss" scoped>
  326. .map-range {
  327. width: 100px;
  328. // height: 100px;
  329. color: #fff;
  330. .xz_type {
  331. margin-bottom: 10px;
  332. // display: flex;
  333. // justify-content: space-between;
  334. .keyitem {
  335. width: 100px;
  336. height: 30px;
  337. font-size: 16px;
  338. line-height: 30px;
  339. display: inline-block;
  340. cursor: pointer;
  341. }
  342. }
  343. .fwlxrange {
  344. display: flex;
  345. justify-content: space-between;
  346. text-align: center;
  347. .clear {
  348. width: 80px;
  349. }
  350. /deep/ .el-button--mini {
  351. background: #0f7ac8;
  352. border: none;
  353. border-radius: 0;
  354. color: #fff;
  355. width: 60px;
  356. height: 26px;
  357. font-size: 14px;
  358. // line-height: 32px;
  359. // text-align: center;
  360. }
  361. }
  362. }
  363. </style>