range.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.area * 0.0015).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. if (this.$props.type == "cutfill") {
  121. this.$emit("cutfill", this.model.geom);
  122. }
  123. }
  124. });
  125. },
  126. //上传文件
  127. handleChange1(file, fileList) {
  128. if (fileList.length > 0) {
  129. this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
  130. this.clearAll(false);
  131. }
  132. const formdata = new FormData();
  133. formdata.append("file", file.raw);
  134. this.addPolygon();
  135. // ShapeUpload(formdata).then((res) => {
  136. // if (res.success) {
  137. // // var layer = myMap.addGeoJson("common_layer", res.data.geojson);
  138. // // var url = `${SYS_LAYERS.XZQXZ}/0/query`;
  139. // // var geom = layer.getSource().getFeatures()[0].getGeometry();
  140. // // 判断绘制范围是否在行政区范围内
  141. // let withinRange = true;
  142. // // arcMap.SearchWfsData(url, geom, function (fs) {
  143. // // if (fs.length) {
  144. // // fs.map((res) => {
  145. // // let xzqdm = res.get("XZQDM");
  146. // // if (!xzqdm.startsWith(this.userXZQ)) {
  147. // // withinRange = false;
  148. // // }
  149. // // });
  150. // // } else {
  151. // // withinRange = false;
  152. // // }
  153. // if (withinRange) {
  154. // this.model.xzfw = res.data.filepath;
  155. // // this.model.feature = layer.getSource().getFeatures()[0];
  156. // // this.model.xzmj = new OLTool().Tools.formatArea(
  157. // // this.model.feature.getGeometry(),
  158. // // true
  159. // // );
  160. // // myMap.zoomToextent(layer.getSource().getExtent());
  161. // } else {
  162. // // this.clearAll();
  163. // // ElMessage.warning("分析范围超出了权限范围,请重新上传文件!");
  164. // }
  165. // // });
  166. // }
  167. // });
  168. },
  169. // 加载GeoJSON数据
  170. addPolygon(geojson) {
  171. // let geojson =
  172. // this.$props.type == "hegxfx"
  173. // ? "/static/data/ghss/导入.geojson"
  174. // : "static/data/draw.geojson";
  175. let _this = this;
  176. let polygon = Cesium.GeoJsonDataSource.load(geojson, {
  177. clampToGround: true,
  178. stroke: Cesium.Color.RED,
  179. fill: Cesium.Color.WHITE.withAlpha(0.3),
  180. strokeWidth: 5,
  181. });
  182. polygon.then(function (dataSource) {
  183. // 将数据源添加到Cesium Viewer
  184. viewer.dataSources.add(dataSource);
  185. viewer.zoomTo(dataSource);
  186. now_dataSources = dataSource;
  187. });
  188. },
  189. resetDataSources() {
  190. if (now_dataSources) {
  191. viewer.dataSources.remove(now_dataSources);
  192. now_dataSources = null;
  193. }
  194. },
  195. //绘制
  196. drawMap() {
  197. if (!window.handlerPolygon) {
  198. common.initHandler("Polygon");
  199. }
  200. common.handlerDrawing("Polygon").then(
  201. (res) => {
  202. // console.log(res.positions, "------");
  203. //过滤掉高度
  204. const filteredArr = res.positions.filter((item, index) => {
  205. if ((index + 1) % 3 !== 0) {
  206. return item;
  207. }
  208. });
  209. // 数组变二维数组方法
  210. function listToMatrix(list, elementsPerSubArray) {
  211. var matrix = [],
  212. i,
  213. k;
  214. for (i = 0, k = -1; i < list.length; i++) {
  215. if (i % elementsPerSubArray === 0) {
  216. k++;
  217. matrix[k] = [];
  218. }
  219. matrix[k].push(list[i]);
  220. }
  221. return matrix;
  222. }
  223. var matrix = listToMatrix(filteredArr, 2);
  224. // console.log(matrix, "max-----");
  225. matrix.push(matrix[0]);
  226. const geojsonPolygon = turf.polygon([matrix]);
  227. // var parse = require("wellknown"); //引入wellknow
  228. const wktPolygon = parse.stringify(geojsonPolygon);
  229. // console.log("WKT Polygon:", wktPolygon);
  230. if (this.$props.type == "cutfill") {
  231. this.model.geom = wktPolygon;
  232. this.$emit("cutfill", this.model.geom);
  233. return;
  234. }
  235. const formdata = new FormData();
  236. // formdata.append("file", file.raw);
  237. formdata.append("fromType", 1);
  238. formdata.append("geom", wktPolygon);
  239. formdata.append("fromRoute", this.$route.path);
  240. // this.addPolygon();
  241. ShapeUpload(formdata).then((res) => {
  242. if (res.success) {
  243. this.fileDataID = res.data.id;
  244. this.model.xzmj = (res.data.area * 0.0015).toFixed(2);
  245. this.model.xzfw = res.data.id;
  246. this.model.geom = res.data.geom;
  247. this.$message({
  248. message: "绘制成功!",
  249. type: "success",
  250. });
  251. }
  252. });
  253. },
  254. (err) => {
  255. console.log(err);
  256. }
  257. );
  258. window.handlerPolygon.activate();
  259. },
  260. clear() {
  261. if (this.handlerDraw != null) {
  262. this.handlerDraw.clear();
  263. viewer.scene.globe.removeAllExcavationRegion();
  264. this.handlerDraw.deactivate();
  265. this.handlerDraw = null;
  266. this.result = null;
  267. }
  268. },
  269. //清除
  270. clearAll(clearfile = true) {
  271. //销毁上传创建的面
  272. // viewer.entities.removeAll();
  273. this.resetDataSources();
  274. this.model.xzfw = "";
  275. this.model.xzmj = 0;
  276. if (clearfile) {
  277. this.fileDataID = "";
  278. this.fileList = [];
  279. }
  280. common.clearHandlerDrawing("Polygon");
  281. },
  282. //输出:重置
  283. reset() {
  284. // this.model = {
  285. // fwlx: this.fwlist[this.$props.keys[0]].id,
  286. // xzfw: "",
  287. // xzmj: 0,
  288. // };
  289. // this.fileList = []; //文件
  290. this.clearAll();
  291. },
  292. //输出:获取范围
  293. getRange() {
  294. return this.model;
  295. },
  296. },
  297. beforeDestroy() {
  298. this.clearAll();
  299. },
  300. watch: {
  301. // 监听,当路由发生变化的时候执行
  302. $route: {
  303. // $route可以用引号,也可以不用引号//to 老路由信息,from 新路由信息
  304. handler(to, from) {
  305. this.clearAll();
  306. },
  307. deep: true, // 深度观察监听
  308. immediate: true, // 第一次初始化渲染就可以监听到
  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: 26px;
  345. font-size: 14px;
  346. // line-height: 32px;
  347. // text-align: center;
  348. }
  349. }
  350. }
  351. </style>