range.vue 10 KB

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