123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <template>
- <div class="map-range">
- <div class="fwlxrange">
- <div :class="model.xzmj == 0 ? 'pointer' : ''" @click="drawMap">
- <el-button plain v-if="model.xzmj == 0" icon="edit-pen" size="mini"
- >绘制</el-button
- >
- <span v-if="model.xzmj != 0">{{ model.xzmj }} m²</span>
- </div>
- <el-upload
- class="upload-demo"
- :on-change="handleChange"
- :auto-upload="false"
- :show-file-list="false"
- :file-list="fileList"
- :limit="1"
- accept=".zip"
- >
- <el-tooltip
- v-if="fileList.length > 0"
- :content="fileList[0].name"
- placement="bottom-start"
- effect="light"
- >
- <span class="title-item" style="display: inline-block; width: 100%">{{
- fileList[0].name
- }}</span>
- </el-tooltip>
- <el-button v-else class="upload-btn" icon="Upload" size="mini"
- >导入</el-button
- >
- </el-upload>
- <div class="clear" @click="clearAll">清除</div>
- </div>
- </div>
- </template>
- <script>
- import { ShapeUpload } from "@/api/ghss/hgxfx.js";
- import { ElMessage } from "element-ui";
- // import { polygon } from "@turf/turf";
- import * as turf from "@turf/turf";
- export default {
- components: {},
- props: {
- //启用的组件,不传为所有
- keys: {
- type: Array,
- default: ["xzq", "hx", "sc", "zbd"],
- },
- activeTabs: {
- type: String,
- },
- type: {
- type: String,
- },
- },
- data() {
- return {
- userXZQ: "", //store.state.user.user.dept.district,
- fwlist: {
- xzq: {
- id: 0,
- name: "行政区",
- icon: "wind-power",
- },
- hx: {
- id: 1,
- name: "绘制",
- icon: "Share",
- },
- sc: {
- id: 2,
- name: "上传",
- icon: "Upload-filled",
- },
- zbd: {
- id: 3,
- name: "坐标点",
- icon: "Location",
- },
- },
- model: {
- fwlx: 0,
- xzfw: "",
- xzmj: 0,
- feature: null,
- },
- handlerDraw: null,
- draw: null, //绘制
- fileList: [], //文件
- fileDataID: "", //存储上传文件获取到的id
- };
- },
- mounted() {
- // this.model.fwlx = this.fwlist[this.$props.keys[0]].id;
- },
- methods: {
- //上传文件
- handleChange(file, fileList) {
- if (fileList.length > 0) {
- this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
- this.clearAll(false);
- }
- const formdata = new FormData();
- formdata.append("file", file.raw);
- formdata.append("fromType", 2);
- formdata.append("fromRoute", this.$route.path);
- // this.addPolygon();
- ShapeUpload(formdata).then((res) => {
- if (res.success) {
- viewer.entities.removeAll();
- this.fileDataID = res.data.id;
- let geoms = res.data.geom
- .substring(
- res.data.geom.indexOf("(((") + 3,
- res.data.geom.length - 3
- )
- .split(")),((");
- for (let i = 0; i < geoms.length; i++) {
- let geom = geoms[i].split(",");
- let points = [];
- for (let j = 0; j < geom.length; j++) {
- points.push(parseFloat(geom[j].split(" ")[0]));
- points.push(parseFloat(geom[j].split(" ")[1]));
- }
- points.push(parseFloat(geom[0].split(" ")[0]));
- points.push(parseFloat(geom[0].split(" ")[1]));
- viewer.entities.add({
- // polyline: new Cesium.PolylineGraphics({
- // positions: Cesium.Cartesian3.fromDegreesArray(points),
- // width: 3,
- // material: Cesium.Color.BLUE.withAlpha(0.9),
- // clampToGround: true,
- // }),
- polygon: {
- hierarchy: {
- positions: Cesium.Cartesian3.fromDegreesArray(points),
- },
- material: Cesium.Color.WHITE.withAlpha(0.3),
- clampToGround: true,
- outline: true,
- outlineWidth: 5,
- outlineColor: Cesium.Color.RED,
- },
- });
- }
- viewer.flyTo(viewer.entities);
- }
- });
- },
- //上传文件
- handleChange1(file, fileList) {
- if (fileList.length > 0) {
- this.fileList = [fileList[fileList.length - 1]]; //这一步,是展示最后一次选择文件
- this.clearAll(false);
- }
- const formdata = new FormData();
- formdata.append("file", file.raw);
- this.addPolygon();
- // ShapeUpload(formdata).then((res) => {
- // if (res.success) {
- // // var layer = myMap.addGeoJson("common_layer", res.data.geojson);
- // // var url = `${SYS_LAYERS.XZQXZ}/0/query`;
- // // var geom = layer.getSource().getFeatures()[0].getGeometry();
- // // 判断绘制范围是否在行政区范围内
- // let withinRange = true;
- // // arcMap.SearchWfsData(url, geom, function (fs) {
- // // if (fs.length) {
- // // fs.map((res) => {
- // // let xzqdm = res.get("XZQDM");
- // // if (!xzqdm.startsWith(this.userXZQ)) {
- // // withinRange = false;
- // // }
- // // });
- // // } else {
- // // withinRange = false;
- // // }
- // if (withinRange) {
- // this.model.xzfw = res.data.filepath;
- // // this.model.feature = layer.getSource().getFeatures()[0];
- // // this.model.xzmj = new OLTool().Tools.formatArea(
- // // this.model.feature.getGeometry(),
- // // true
- // // );
- // // myMap.zoomToextent(layer.getSource().getExtent());
- // } else {
- // // this.clearAll();
- // // ElMessage.warning("分析范围超出了权限范围,请重新上传文件!");
- // }
- // // });
- // }
- // });
- },
- // 加载GeoJSON数据
- addPolygon() {
- let geojson =
- this.$props.type == "hegxfx"
- ? "/static/data/ghss/导入.geojson"
- : "static/data/draw.geojson";
- let polygon = Cesium.GeoJsonDataSource.load(geojson, {
- // clampToGround: true
- stroke: Cesium.Color.RED,
- fill: Cesium.Color.WHITE.withAlpha(0.3),
- strokeWidth: 5,
- });
- polygon.then(function (dataSource) {
- // 将数据源添加到Cesium Viewer
- viewer.dataSources.add(dataSource);
- viewer.zoomTo(dataSource);
- // 可以获取实体并进行操作
- // var entities = dataSource.entities.values;
- // for (var i = 0; i < entities.length; i++) {
- // var entity = entities[i];
- // // 你可以在这里设置实体的属性,例如位置、颜色等
- // }
- });
- },
- //绘制
- drawMap() {
- if (!window.handlerPolygon) {
- common.initHandler("Polygon");
- }
- common.handlerDrawing("Polygon").then(
- (res) => {
- // console.log(res.positions, "------");
- //过滤掉高度
- const filteredArr = res.positions.filter((item, index) => {
- if ((index + 1) % 3 !== 0) {
- return item;
- }
- });
- console.log(filteredArr, "filteredArr");
- // 数组变二维数组方法
- function listToMatrix(list, elementsPerSubArray) {
- var matrix = [],
- i,
- k;
- for (i = 0, k = -1; i < list.length; i++) {
- if (i % elementsPerSubArray === 0) {
- k++;
- matrix[k] = [];
- }
- matrix[k].push(list[i]);
- }
- return matrix;
- }
- var matrix = listToMatrix(filteredArr, 2);
- // console.log(matrix, "max-----");
- matrix.push(matrix[0]);
- const geojsonPolygon = turf.polygon([matrix]);
- var parse = require("wellknown"); //引入wellknow
- const wktPolygon = parse.stringify(geojsonPolygon);
- // console.log("WKT Polygon:", wktPolygon);
- const formdata = new FormData();
- // formdata.append("file", file.raw);
- formdata.append("fromType", 1);
- formdata.append("geom", wktPolygon);
- formdata.append("fromRoute", this.$route.path);
- // this.addPolygon();
- ShapeUpload(formdata).then((res) => {
- if (res.success) {
- this.$message({
- message: "绘制成功!",
- type: "success",
- });
- }
- });
- },
- (err) => {
- console.log(err);
- }
- );
- window.handlerPolygon.activate();
- },
- clear() {
- if (this.handlerDraw != null) {
- this.handlerDraw.clear();
- viewer.scene.globe.removeAllExcavationRegion();
- this.handlerDraw.deactivate();
- this.handlerDraw = null;
- this.result = null;
- }
- },
- //清除
- clearAll(clearfile = true) {
- //销毁上传创建的面
- viewer.entities.removeAll();
- this.model.xzfw = "";
- this.model.xzmj = 0;
- if (clearfile) {
- this.fileDataID = "";
- this.fileList = [];
- }
- common.clearHandlerDrawing("Polygon");
- },
- //输出:重置
- reset() {
- // this.model = {
- // fwlx: this.fwlist[this.$props.keys[0]].id,
- // xzfw: "",
- // xzmj: 0,
- // };
- // this.fileList = []; //文件
- this.clearAll();
- },
- //输出:获取范围
- getRange() {
- return this.model;
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .map-range {
- width: 100px;
- // height: 100px;
- color: #fff;
- .xz_type {
- margin-bottom: 10px;
- // display: flex;
- // justify-content: space-between;
- .keyitem {
- width: 100px;
- height: 30px;
- font-size: 16px;
- line-height: 30px;
- display: inline-block;
- cursor: pointer;
- }
- }
- .fwlxrange {
- display: flex;
- justify-content: space-between;
- text-align: center;
- .clear {
- width: 80px;
- }
- /deep/ .el-button--mini {
- background: #0F7AC8;
- border: none;
- border-radius: 0;
- color: #fff;
- width: 60px;
- height: 32px;
- font-size: 14px;
- // line-height: 32px;
- // text-align: center;
- }
- }
- }
- </style>
|