range.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 }} 亩</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. >
  24. <span class="title-item" style="display: inline-block; width: 100%">{{
  25. fileList[0].name
  26. }}</span>
  27. </el-tooltip>
  28. <el-button v-else class="upload-btn" icon="Upload" size="mini"
  29. >导入</el-button
  30. >
  31. </el-upload>
  32. <!-- <div class="clear" @click="clearAll">清除</div> -->
  33. <div>
  34. <el-button size="mini" @click="clearAll">清除</el-button>
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import { ShapeUpload } from "@/api/ghss/hgxfx.js";
  41. import { ElMessage } from "element-ui";
  42. // import { polygon } from "@turf/turf";
  43. import * as turf from "@turf/turf";
  44. import parse from "wellknown";
  45. let now_dataSources = null;
  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 * 0.0015).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 _this = this;
  209. let polygon = Cesium.GeoJsonDataSource.load(geojson, {
  210. clampToGround: true,
  211. stroke: Cesium.Color.RED,
  212. fill: Cesium.Color.WHITE.withAlpha(0.3),
  213. strokeWidth: 5,
  214. });
  215. polygon.then(function (dataSource) {
  216. // 将数据源添加到Cesium Viewer
  217. viewer.dataSources.add(dataSource);
  218. viewer.zoomTo(dataSource);
  219. now_dataSources = dataSource;
  220. });
  221. },
  222. resetDataSources() {
  223. if (now_dataSources) {
  224. viewer.dataSources.remove(now_dataSources);
  225. now_dataSources = null;
  226. }
  227. },
  228. //绘制
  229. drawMap() {
  230. if (!window.handlerPolygon) {
  231. common.initHandler("Polygon");
  232. }
  233. common.handlerDrawing("Polygon").then(
  234. (res) => {
  235. // console.log(res.positions, "------");
  236. //过滤掉高度
  237. const filteredArr = res.positions.filter((item, index) => {
  238. if ((index + 1) % 3 !== 0) {
  239. return item;
  240. }
  241. });
  242. // 数组变二维数组方法
  243. function listToMatrix(list, elementsPerSubArray) {
  244. var matrix = [],
  245. i,
  246. k;
  247. for (i = 0, k = -1; i < list.length; i++) {
  248. if (i % elementsPerSubArray === 0) {
  249. k++;
  250. matrix[k] = [];
  251. }
  252. matrix[k].push(list[i]);
  253. }
  254. return matrix;
  255. }
  256. var matrix = listToMatrix(filteredArr, 2);
  257. // console.log(matrix, "max-----");
  258. matrix.push(matrix[0]);
  259. const geojsonPolygon = turf.polygon([matrix]);
  260. // var parse = require("wellknown"); //引入wellknow
  261. const wktPolygon = parse.stringify(geojsonPolygon);
  262. // console.log("WKT Polygon:", wktPolygon);
  263. const formdata = new FormData();
  264. // formdata.append("file", file.raw);
  265. formdata.append("fromType", 1);
  266. formdata.append("geom", wktPolygon);
  267. formdata.append("fromRoute", this.$route.path);
  268. // this.addPolygon();
  269. ShapeUpload(formdata).then((res) => {
  270. if (res.success) {
  271. this.fileDataID = res.data.id;
  272. this.model.xzmj = (res.data.area * 0.0015).toFixed(2);
  273. this.model.xzfw = res.data.id;
  274. this.$message({
  275. message: "绘制成功!",
  276. type: "success",
  277. });
  278. }
  279. });
  280. },
  281. (err) => {
  282. console.log(err);
  283. }
  284. );
  285. window.handlerPolygon.activate();
  286. },
  287. clear() {
  288. if (this.handlerDraw != null) {
  289. this.handlerDraw.clear();
  290. viewer.scene.globe.removeAllExcavationRegion();
  291. this.handlerDraw.deactivate();
  292. this.handlerDraw = null;
  293. this.result = null;
  294. }
  295. },
  296. //清除
  297. clearAll(clearfile = true) {
  298. //销毁上传创建的面
  299. // viewer.entities.removeAll();
  300. this.resetDataSources();
  301. this.model.xzfw = "";
  302. this.model.xzmj = 0;
  303. if (clearfile) {
  304. this.fileDataID = "";
  305. this.fileList = [];
  306. }
  307. common.clearHandlerDrawing("Polygon");
  308. },
  309. //输出:重置
  310. reset() {
  311. // this.model = {
  312. // fwlx: this.fwlist[this.$props.keys[0]].id,
  313. // xzfw: "",
  314. // xzmj: 0,
  315. // };
  316. // this.fileList = []; //文件
  317. this.clearAll();
  318. },
  319. //输出:获取范围
  320. getRange() {
  321. return this.model;
  322. },
  323. },
  324. };
  325. </script>
  326. <style lang="scss" scoped>
  327. .map-range {
  328. width: 100px;
  329. // height: 100px;
  330. color: #fff;
  331. .xz_type {
  332. margin-bottom: 10px;
  333. // display: flex;
  334. // justify-content: space-between;
  335. .keyitem {
  336. width: 100px;
  337. height: 30px;
  338. font-size: 16px;
  339. line-height: 30px;
  340. display: inline-block;
  341. cursor: pointer;
  342. }
  343. }
  344. .fwlxrange {
  345. display: flex;
  346. justify-content: space-between;
  347. text-align: center;
  348. .clear {
  349. width: 80px;
  350. }
  351. /deep/ .el-button--mini {
  352. background: #0f7ac8;
  353. border: none;
  354. border-radius: 0;
  355. color: #fff;
  356. width: 60px;
  357. height: 26px;
  358. font-size: 14px;
  359. // line-height: 32px;
  360. // text-align: center;
  361. }
  362. }
  363. }
  364. </style>