surprise
2024-01-09 9398f14feea7ad8b7d00ce7d2bb2e5362cec59d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import parse from "qs/lib/parse";
import store from "../../../store";
import * as turf from '@turf/turf';
const menuGrid = {
    geom: [],
    init() {
        var that = this;
        var grid = sgworld.Creator.createSimpleGraphic(
            "point",
            {
                // clampToGround: false
            }, (entity) => {
                var val = that.setCartesianToEightFour(entity.position.getValue())
                that.geom.push([parseFloat(val.lng), parseFloat(val.lat)])
                sgworld.Creator.SimpleGraphic.remove(entity.id);
                if (this.geom.length < 2) {
                    that.init();
                } else {
                    store.state.isshowGrid = true;
                }
            })
    },
    setTurfGrid(res, geom) {
 
        var box = [parseFloat(geom[0][0]), parseFloat(geom[0][1]), parseFloat(geom[1][0]), parseFloat(geom[1][1])]
        this.geom = [];
        var options = { units: "meters" };
        var squareGrid = turf.squareGrid(box, res.cellSide, options);
        var features = [];
        for (var i = 0; i < squareGrid.features.length; i++) {
            var coord = squareGrid.features[i].geometry.coordinates[0];
            var coordinates = [];
            for (var j in coord) {
                coordinates.push([coord[j][0], coord[j][1], 18])
            }
            features.push({
                "type": "Feature", "properties": {
                    "NAME": '新建格网' + (i + 1),
                    "bak": ''
                },
                "geometry": {
                    "type": "Polygon", "coordinates": [
                        coordinates
                    ]
                }
            })
        }
        var obj = {
            "type": "FeatureCollection",
            "name": "新建格网",
            "crs": {
                "type": "name",
                "properties": {
                    "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
                }
            },
            "features": features
        }
        this.adddDataSource(obj)
    },
 
    adddDataSource(res) {
        var data = Cesium.GeoJsonDataSource.load(res, //要加载的 url、GeoJSON 对象或 TopoJSON 对象。
            {
                stroke: Cesium.Color.RED, //折线和多边形轮廓的默认颜色。
                fill: Cesium.Color.WHITE.withAlpha(0.2), //多边形内部的默认颜色。
                strokeWidth: 3, //折线和多边形轮廓的默认宽度。
            }
        )
        data.then((dataSource) => {
            Viewer.dataSources.add(
                dataSource
            );
        })
    },
 
    setOtherGrid(res, geom) {
        var maxlon, minlon, maxlat, minlat;
        console.log(geom)
        if (parseFloat(geom[0][0]) > parseFloat(geom[1][0])) {
            maxlon = parseFloat(geom[0][0]);
            minlon = parseFloat(geom[1][0]);
        } else {
            maxlon = parseFloat(geom[1][0]);
            minlon = parseFloat(geom[0][0]);
        }
        if (parseFloat(geom[0][1]) > parseFloat(geom[1][1])) {
            maxlat = geom[0][1];
            minlat = geom[1][1];
        } else {
            maxlat = geom[1][1];
            minlat = geom[0][1];
        }
        var row = 3;
        var col = 3;
        var lon_dis = (parseFloat(maxlon) - parseFloat(minlon)) / 3;
        var lat_dis = (parseFloat(maxlat) - parseFloat(minlat)) / 3;
        console.log(lon_dis, minlon)
        for (var i = 0; i < col; i++) {
            for (var j = 0; j < row; j++) {
                var startlon = parseFloat(minlon + (j * lon_dis)) 
                var startlat = parseFloat(minlat + (i * lat_dis)) 
                var endlon = parseFloat(minlon + ((j + 1) * lon_dis)) 
                var endlat = parseFloat(minlat + ((i + 1) * lat_dis)) 
                var positions = [
                    parseFloat(startlon), parseFloat(startlat),  parseFloat(endlon), parseFloat(startlat),  
                    parseFloat(endlon), parseFloat(endlat),  parseFloat(startlon), parseFloat(endlat),   parseFloat(startlon), parseFloat(startlat) 
                ]
            
                   
                Viewer.entities.add({
                    name: "polygon_height",
                    polygon: {
                        show: true,
                        hierarchy: new Cesium.Cartesian3.fromDegreesArray(positions),
                        height: 18,
                        material: Cesium.Color.CYAN.withAlpha(0.5),
                        outline: true,
                        outlineColor: Cesium.Color.BLACK,
                    }
                })
          
 
 
            }
        }
    },
 
 
    setGridRowCol(res) {
        store.state.isshowGrid = false;
        var geom = this.geom;
        // this.setTurfGrid(res,geom)
        this.setOtherGrid(res, geom);
 
    },
    toFixed(res) {
        return parseFloat(res).toFixed(6)
    },
    setdistance(start, end) {
        var from = turf.point(start);
        var to = turf.point(end);
        var distance = turf.distance(from, to, {
            units: 'meters'
        });
        return distance
    },
 
 
    setCartesianToEightFour(res) {
        var std = {};
        let ellipsoid = window.Viewer.scene.globe.ellipsoid;
        let cartographic = ellipsoid.cartesianToCartographic(res);
        std.lat = Cesium.Math.toDegrees(cartographic.latitude);
        std.lng = Cesium.Math.toDegrees(cartographic.longitude);
        std.alt = cartographic.height;
        return std;
    }
}
export default menuGrid;