range.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <div class="map-range">
  3. <div class="fwlxrange">
  4. <div :class="model.xzmj == 0 ? 'pointer' : ''" @click="drawMap">
  5. <el-button plain icon="edit-pen" size="mini">绘制</el-button>
  6. </div>
  7. <el-upload
  8. class="upload-demo"
  9. :on-change="handleChange"
  10. :auto-upload="false"
  11. :show-file-list="false"
  12. :file-list="fileList"
  13. :limit="1"
  14. accept=".zip"
  15. >
  16. <el-button class="upload-btn" icon="Upload" size="mini">导入</el-button>
  17. </el-upload>
  18. <!-- <div class="clear" @click="clearAll">清除</div> -->
  19. <div>
  20. <el-button size="mini" @click="clearAll">清除</el-button>
  21. </div>
  22. </div>
  23. <div v-if="type != 'cutfill'">
  24. <span v-if="model.xzmj != 0">{{ model.xzmj }} 亩</span>
  25. <el-tooltip
  26. v-if="fileList.length > 0"
  27. :content="fileList[0].name"
  28. placement="bottom-start"
  29. >
  30. <span class="title-item" style="display: inline-block; width: 100%">{{
  31. fileList[0].name
  32. }}</span>
  33. </el-tooltip>
  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. import parse from "wellknown";
  43. let now_dataSources = null;
  44. export default {
  45. components: {},
  46. props: {
  47. //启用的组件,不传为所有
  48. keys: {
  49. type: Array,
  50. default: ["xzq", "hx", "sc", "zbd"],
  51. },
  52. activeTabs: {
  53. type: String,
  54. },
  55. type: {
  56. type: String,
  57. },
  58. },
  59. data() {
  60. return {
  61. userXZQ: "", //store.state.user.user.dept.district,
  62. fwlist: {
  63. xzq: {
  64. id: 0,
  65. name: "行政区",
  66. icon: "wind-power",
  67. },
  68. hx: {
  69. id: 1,
  70. name: "绘制",
  71. icon: "Share",
  72. },
  73. sc: {
  74. id: 2,
  75. name: "上传",
  76. icon: "Upload-filled",
  77. },
  78. zbd: {
  79. id: 3,
  80. name: "坐标点",
  81. icon: "Location",
  82. },
  83. },
  84. model: {
  85. fwlx: 0,
  86. xzfw: "",
  87. xzmj: 0,
  88. feature: null,
  89. },
  90. handlerDraw: null,
  91. draw: null, //绘制
  92. fileList: [], //文件
  93. fileDataID: "", //存储上传文件获取到的id
  94. dataSources: null,
  95. };
  96. },
  97. mounted() {
  98. // this.model.fwlx = this.fwlist[this.$props.keys[0]].id;
  99. },
  100. methods: {
  101. //上传文件
  102. handleChange(file, fileList) {
  103. if (fileList.length > 0) {
  104. this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
  105. this.clearAll(false);
  106. }
  107. const formdata = new FormData();
  108. formdata.append("file", file.raw);
  109. formdata.append("fromType", 2);
  110. formdata.append("fromRoute", this.$route.path);
  111. ShapeUpload(formdata).then((res) => {
  112. if (res.success) {
  113. this.model.xzmj = res.data.muArea.toFixed(2);
  114. // viewer.entities.removeAll();
  115. this.resetDataSources();
  116. this.fileDataID = res.data.id;
  117. this.model.xzfw = res.data.id;
  118. this.model.geom = res.data.geom;
  119. this.addPolygon(parse(res.data.geom));
  120. // let geoms = res.data.geom
  121. // .substring(
  122. // res.data.geom.indexOf("(((") + 3,
  123. // res.data.geom.length - 3
  124. // )
  125. // .split(")),((");
  126. // for (let i = 0; i < geoms.length; i++) {
  127. // let geom = geoms[i].split(",");
  128. // let points = [];
  129. // for (let j = 0; j < geom.length; j++) {
  130. // points.push(parseFloat(geom[j].split(" ")[0]));
  131. // points.push(parseFloat(geom[j].split(" ")[1]));
  132. // }
  133. // points.push(parseFloat(geom[0].split(" ")[0]));
  134. // points.push(parseFloat(geom[0].split(" ")[1]));
  135. // viewer.entities.add({
  136. // // polyline: new Cesium.PolylineGraphics({
  137. // // positions: Cesium.Cartesian3.fromDegreesArray(points),
  138. // // width: 3,
  139. // // material: Cesium.Color.BLUE.withAlpha(0.9),
  140. // // clampToGround: true,
  141. // // }),
  142. // polygon: {
  143. // hierarchy: {
  144. // positions: Cesium.Cartesian3.fromDegreesArray(points),
  145. // },
  146. // material: Cesium.Color.WHITE.withAlpha(0.3),
  147. // clampToGround: true,
  148. // outline: true,
  149. // outlineWidth: 5,
  150. // outlineColor: Cesium.Color.RED,
  151. // },
  152. // });
  153. // }
  154. // viewer.flyTo(viewer.entities);
  155. }
  156. });
  157. },
  158. //上传文件
  159. handleChange1(file, fileList) {
  160. if (fileList.length > 0) {
  161. this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
  162. this.clearAll(false);
  163. }
  164. const formdata = new FormData();
  165. formdata.append("file", file.raw);
  166. this.addPolygon();
  167. // ShapeUpload(formdata).then((res) => {
  168. // if (res.success) {
  169. // // var layer = myMap.addGeoJson("common_layer", res.data.geojson);
  170. // // var url = `${SYS_LAYERS.XZQXZ}/0/query`;
  171. // // var geom = layer.getSource().getFeatures()[0].getGeometry();
  172. // // 判断绘制范围是否在行政区范围内
  173. // let withinRange = true;
  174. // // arcMap.SearchWfsData(url, geom, function (fs) {
  175. // // if (fs.length) {
  176. // // fs.map((res) => {
  177. // // let xzqdm = res.get("XZQDM");
  178. // // if (!xzqdm.startsWith(this.userXZQ)) {
  179. // // withinRange = false;
  180. // // }
  181. // // });
  182. // // } else {
  183. // // withinRange = false;
  184. // // }
  185. // if (withinRange) {
  186. // this.model.xzfw = res.data.filepath;
  187. // // this.model.feature = layer.getSource().getFeatures()[0];
  188. // // this.model.xzmj = new OLTool().Tools.formatArea(
  189. // // this.model.feature.getGeometry(),
  190. // // true
  191. // // );
  192. // // myMap.zoomToextent(layer.getSource().getExtent());
  193. // } else {
  194. // // this.clearAll();
  195. // // ElMessage.warning("分析范围超出了权限范围,请重新上传文件!");
  196. // }
  197. // // });
  198. // }
  199. // });
  200. },
  201. // 加载GeoJSON数据
  202. addPolygon(geojson) {
  203. // let geojson =
  204. // this.$props.type == "hegxfx"
  205. // ? "/static/data/ghss/导入.geojson"
  206. // : "static/data/draw.geojson";
  207. let _this = this;
  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. now_dataSources = dataSource;
  219. });
  220. },
  221. resetDataSources() {
  222. if (now_dataSources) {
  223. viewer.dataSources.remove(now_dataSources);
  224. now_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. if (this.$props.type == "cutfill") {
  263. this.model.geom = wktPolygon;
  264. return;
  265. }
  266. const formdata = new FormData();
  267. // formdata.append("file", file.raw);
  268. formdata.append("fromType", 1);
  269. formdata.append("geom", wktPolygon);
  270. formdata.append("fromRoute", this.$route.path);
  271. // this.addPolygon();
  272. ShapeUpload(formdata).then((res) => {
  273. if (res.success) {
  274. this.fileDataID = res.data.id;
  275. this.model.xzmj = (res.data.area * 0.0015).toFixed(2);
  276. this.model.xzfw = res.data.id;
  277. this.model.geom = res.data.geom;
  278. this.$message({
  279. message: "绘制成功!",
  280. type: "success",
  281. });
  282. }
  283. });
  284. },
  285. (err) => {
  286. console.log(err);
  287. }
  288. );
  289. window.handlerPolygon.activate();
  290. },
  291. clear() {
  292. if (this.handlerDraw != null) {
  293. this.handlerDraw.clear();
  294. viewer.scene.globe.removeAllExcavationRegion();
  295. this.handlerDraw.deactivate();
  296. this.handlerDraw = null;
  297. this.result = null;
  298. }
  299. },
  300. //清除
  301. clearAll(clearfile = true) {
  302. //销毁上传创建的面
  303. // viewer.entities.removeAll();
  304. this.resetDataSources();
  305. this.model.xzfw = "";
  306. this.model.xzmj = 0;
  307. if (clearfile) {
  308. this.fileDataID = "";
  309. this.fileList = [];
  310. }
  311. common.clearHandlerDrawing("Polygon");
  312. },
  313. //输出:重置
  314. reset() {
  315. // this.model = {
  316. // fwlx: this.fwlist[this.$props.keys[0]].id,
  317. // xzfw: "",
  318. // xzmj: 0,
  319. // };
  320. // this.fileList = []; //文件
  321. this.clearAll();
  322. },
  323. //输出:获取范围
  324. getRange() {
  325. return this.model;
  326. },
  327. },
  328. beforeDestroy() {
  329. this.clearAll();
  330. },
  331. watch: {
  332. // 监听,当路由发生变化的时候执行
  333. $route: {
  334. // $route可以用引号,也可以不用引号//to 老路由信息,from 新路由信息
  335. handler(to, from) {
  336. this.clearAll();
  337. },
  338. deep: true, // 深度观察监听
  339. immediate: true, // 第一次初始化渲染就可以监听到
  340. },
  341. },
  342. };
  343. </script>
  344. <style lang="scss" scoped>
  345. .map-range {
  346. width: 100px;
  347. // height: 100px;
  348. color: #fff;
  349. .xz_type {
  350. margin-bottom: 10px;
  351. // display: flex;
  352. // justify-content: space-between;
  353. .keyitem {
  354. width: 100px;
  355. height: 30px;
  356. font-size: 16px;
  357. line-height: 30px;
  358. display: inline-block;
  359. cursor: pointer;
  360. }
  361. }
  362. .fwlxrange {
  363. display: flex;
  364. justify-content: space-between;
  365. text-align: center;
  366. .clear {
  367. width: 80px;
  368. }
  369. /deep/ .el-button--mini {
  370. background: #0f7ac8;
  371. border: none;
  372. border-radius: 0;
  373. color: #fff;
  374. width: 60px;
  375. height: 26px;
  376. font-size: 14px;
  377. // line-height: 32px;
  378. // text-align: center;
  379. }
  380. }
  381. }
  382. </style>