-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwktTools.js
98 lines (88 loc) · 2.72 KB
/
wktTools.js
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
const PresType = {"polygon":1, "marker":2};
Object.freeze(PresType);
function drawGeometry() {
let wkt_string = document.getElementById('wktInput').value;
wkt_string = wkt_string.replace(/ +(?= )/g,'');
wkt_string = wkt_string.replace(/(\r\n|\n|\r)/gm, "");
let geometry = wellknown.parse(wkt_string.trim());
geoms.forEach(function(item, i, arr) {
if (item[1] == PresType.polygon)
item[0].destroy();
else
item[0].removeFrom(earth);
});
geoms = [];
let geomArray = wkt2geoArray(geometry);
geomArray.forEach(function(item, i, arr) {
let gObj = item[0].addTo(earth);
geoms.push([gObj, item[1]]);
});
if (geomArray.length > 0) {
// let viewPoint = geomArray[0][0];
// earth.setView(viewPoint, 5);
}
}
function wkt2geoArray(geometry)
{
let res = [];
if (geometry.type === "Polygon") {
res.push([wkt2Polygon(geometry), PresType.polygon]);
} else if (geometry.type === "Point") {
res.push([wkt2Point(geometry), PresType.marker]);
} else if (geometry.type === "LineString") {
res.push([wkt2Line(geometry), PresType.polygon]);
} else if (geometry.type === "GeometryCollection") {
geometry.geometries.forEach(function(item, i, arr) {
res = res.concat(wkt2geoArray(item));
});
} else {
alert("geometry type " + geometry.type + " not supported");
}
return res;
}
function wkt2Line(geometry) {
let points_array = geometry.coordinates;
points_array.forEach(function(item, i){
let buf = item[0];
item[0] = item[1];
item[1] = buf;
});
let copy = points_array.slice();
copy.reverse();
let prevLat = copy[1][0];
let prevLon = copy[1][1];
let lastLat = copy[0][0];
let lastLon = copy[0][1];
let addLat = lastLat - (prevLat - lastLat)*0.000001;
let addLon = lastLon - (prevLon - lastLon)*0.000001;
let addPoint = [addLat, addLon];
points_array.push(addPoint);
points_array = points_array.concat(copy);
let line = WE.polygon(points_array, {
color: '#0412d6',
opacity: 1,
fillColor: '#ffffff',
fillOpacity: 0,
weight: 2
});
return line;
}
function wkt2Point(geometry) {
let points_array = geometry.coordinates;
return WE.marker([points_array[1], points_array[0]]);
}
function wkt2Polygon(geometry) {
let points_array = geometry.coordinates[0];
points_array.forEach(function(item, i){
let buf = item[0];
item[0] = item[1];
item[1] = buf;
});
return pol = WE.polygon(points_array, {
color: '#0412d6',
opacity: 1,
fillColor: '#0412d6',
fillOpacity: 0.1,
weight: 2
});
}