forked from mokrayaGISka/cesium_measurementTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement.html
233 lines (213 loc) · 10.5 KB
/
measurement.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Use correct character set. -->
<meta charset="utf-8">
<!-- Tell IE to use the latest, best version. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>3D Measure</title>
<script src="Cesium/Cesium.js"></script>
<style>
@import url(Cesium/Widgets/widgets.css);
html, body, #cesiumContainer {
width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0ZDIwYzM1MC1kYWNmLTQyMWQtYWFlNi02YWY2MjFjZDZkZmMiLCJpZCI6ODc5MSwic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTU1MjgyMzI3N30.FVbhAcjUughhOY044nMiv9gYLXf8pQI7kFnvGtjNPGE';
// Construct the viewer, with a high-res terrain source pre-selected.
var viewer = new Cesium.Viewer('cesiumContainer');
var camera = viewer.camera;
var scene = viewer.scene;
var globe = scene.globe;
var ellipsoid = Cesium.Ellipsoid.WGS84;
var geodesic = new Cesium.EllipsoidGeodesic();
var tileset = scene.primitives.add(new Cesium.Cesium3DTileset({
url: 'Tileset/tileset.json'
}));
tileset.readyPromise.then(function() {
// console.log(tileset);
var boundingSphere = tileset.boundingSphere;
camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.5, -0.2, boundingSphere.radius * 4.0));
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function(error) {
throw(error);
});
tileset.allTilesLoaded.addEventListener(function() {
console.log('All tiles are loaded');
});
var points = scene.primitives.add(new Cesium.PointPrimitiveCollection());
var point1, point2;
var point1GeoPosition, point2GeoPosition;
var polylines = scene.primitives.add(new Cesium.PolylineCollection());
var polyline1, polyline2, polyline3;
var distanceLabel, verticalLabel, horizontalLabel;
var LINEPOINTCOLOR = Cesium.Color.RED;
// var labels = scene.primitives.add(new Cesium.LabelCollection({scene: scene}));
var label = {
font : '14px monospace',
showBackground : true,
horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
verticalOrigin : Cesium.VerticalOrigin.CENTER,
pixelOffset : new Cesium.Cartesian2(0, 0),
eyeOffset: new Cesium.Cartesian3(0,0,-50),
fillColor: Cesium.Color.WHITE,
};
function addDistanceLabel(point1, point2, height) {
point1.cartographic = ellipsoid.cartesianToCartographic(point1.position);
point2.cartographic = ellipsoid.cartesianToCartographic(point2.position);
point1.longitude = parseFloat(Cesium.Math.toDegrees(point1.position.x));
point1.latitude = parseFloat(Cesium.Math.toDegrees(point1.position.y));
point2.longitude = parseFloat(Cesium.Math.toDegrees(point2.position.x));
point2.latitude = parseFloat(Cesium.Math.toDegrees(point2.position.y));
label.text = getHorizontalDistanceString(point1, point2);
horizontalLabel = viewer.entities.add({
position: getMidpoint(point1, point2, point1GeoPosition.height),
label: label
});
label.text = getDistanceString(point1, point2);
distanceLabel = viewer.entities.add({
position: getMidpoint(point1, point2, height),
label: label
});
label.text = getVerticalDistanceString();
verticalLabel = viewer.entities.add({
position: getMidpoint(point2, point2, height),
label: label
});
};
function getHorizontalDistanceString(point1, point2) {
geodesic.setEndPoints(point1.cartographic, point2.cartographic);
var meters = geodesic.surfaceDistance.toFixed(2);
if (meters >= 1000) {
return (meters / 1000).toFixed(1) + ' км';
}
return meters + ' м';
};
function getVerticalDistanceString() {
var heights = [point1GeoPosition.height, point2GeoPosition.height];
var meters = Math.max.apply(Math, heights) - Math.min.apply(Math, heights);
if (meters >= 1000) {
return (meters / 1000).toFixed(1) + ' км';
}
return meters.toFixed(2) + ' м';
};
function getDistanceString(point1, point2) {
geodesic.setEndPoints(point1.cartographic, point2.cartographic);
var horizontalMeters = geodesic.surfaceDistance.toFixed(2);
var heights = [point1GeoPosition.height, point2GeoPosition.height];
var verticalMeters = Math.max.apply(Math, heights) - Math.min.apply(Math, heights);
var meters = Math.pow((Math.pow(horizontalMeters, 2) + Math.pow(verticalMeters, 2)), 0.5);
if (meters >= 1000) {
return (meters / 1000).toFixed(1) + ' км';
}
return meters.toFixed(2) + ' м';
};
function getMidpoint(point1, point2, height) {
var scratch = new Cesium.Cartographic();
geodesic.setEndPoints(point1.cartographic, point2.cartographic);
var midpointCartographic = geodesic.interpolateUsingFraction(0.5, scratch);
return Cesium.Cartesian3.fromRadians(midpointCartographic.longitude, midpointCartographic.latitude, height);
};
// Mouse over the globe to see the cartographic position
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(click) {
if (scene.mode !== Cesium.SceneMode.MORPHING) {
var pickedObject = scene.pick(click.position);
if (scene.pickPositionSupported && Cesium.defined(pickedObject)) {
var cartesian = viewer.scene.pickPosition(click.position);
// console.log(cartesian);
if (Cesium.defined(cartesian)) {
if (points.length === 2) {
points.removeAll();
polylines.removeAll();
viewer.entities.remove(distanceLabel);
viewer.entities.remove(horizontalLabel);
viewer.entities.remove(verticalLabel);
}
//add first point
if (points.length === 0) {
point1 = points.add({
position : new Cesium.Cartesian3(cartesian.x, cartesian.y, cartesian.z),
color : LINEPOINTCOLOR
});
} //add second point and lines
else if (points.length === 1) {
point2 = points.add({
position : new Cesium.Cartesian3(cartesian.x, cartesian.y, cartesian.z),
color : LINEPOINTCOLOR
});
point1GeoPosition = Cesium.Cartographic.fromCartesian(point1.position);
point2GeoPosition = Cesium.Cartographic.fromCartesian(point2.position);
point3GeoPosition = Cesium.Cartographic.fromCartesian(new Cesium.Cartesian3(point2.position.x, point2.position.y, point1.position.z));
var pl1Positions = [
new Cesium.Cartesian3.fromRadians(point1GeoPosition.longitude, point1GeoPosition.latitude, point1GeoPosition.height),
new Cesium.Cartesian3.fromRadians(point2GeoPosition.longitude, point2GeoPosition.latitude, point2GeoPosition.height)
];
var pl2Positions = [
new Cesium.Cartesian3.fromRadians(point2GeoPosition.longitude, point2GeoPosition.latitude, point2GeoPosition.height),
new Cesium.Cartesian3.fromRadians(point2GeoPosition.longitude, point2GeoPosition.latitude, point1GeoPosition.height)
];
var pl3Positions = [
new Cesium.Cartesian3.fromRadians(point1GeoPosition.longitude, point1GeoPosition.latitude, point1GeoPosition.height),
new Cesium.Cartesian3.fromRadians(point2GeoPosition.longitude, point2GeoPosition.latitude, point1GeoPosition.height)
];
polyline1 = polylines.add({
show : true,
positions : pl1Positions,
width : 1,
material: new Cesium.Material({
fabric : {
type : 'Color',
uniforms : {
color : LINEPOINTCOLOR
}
}
})
});
polyline2 = polylines.add({
show : true,
positions : pl2Positions,
width : 1,
material: new Cesium.Material({
fabric : {
type : 'PolylineDash',
uniforms : {
color : LINEPOINTCOLOR,
}
},
})
});
polyline3 = polylines.add({
show : true,
positions : pl3Positions,
width : 1,
material: new Cesium.Material({
fabric : {
type : 'PolylineDash',
uniforms : {
color : LINEPOINTCOLOR,
}
},
})
});
var labelZ;
if (point2GeoPosition.height >= point1GeoPosition.height) {
labelZ = point1GeoPosition.height + (point2GeoPosition.height - point1GeoPosition.height)/2.0;
} else {
labelZ = point2GeoPosition.height + (point1GeoPosition.height - point2GeoPosition.height)/2.0;
};
addDistanceLabel(point1, point2, labelZ);
}
}
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
</script>
</body>
</html>