Parallels and Meridians.html 9.73 KB
Newer Older
Schaaf's avatar
Schaaf committed
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <meta
      name="description"
      content="Use polylines to draw parallels and meridians on the globe."
    />
    <meta name="cesium-sandcastle-labels" content="Geometries" />
    <title>Cesium Demo</title>
    <script type="text/javascript" src="../Sandcastle-header.js"></script>
    <script
      type="text/javascript"
      src="../../../Build/CesiumUnminified/Cesium.js"
      nomodule
    ></script>
    <script type="module" src="../load-cesium-es6.js"></script>
  </head>
  <body
    class="sandcastle-loading"
    data-sandcastle-bucket="bucket-requirejs.html"
  >
    <style>
      @import url(../templates/bucket.css);
    </style>
    <div id="cesiumContainer" class="fullSize"></div>
    <div id="loadingOverlay"><h1>Loading...</h1></div>
    <div id="toolbar"></div>
    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        var viewer = new Cesium.Viewer("cesiumContainer");
        var toDegrees = Cesium.Math.toDegrees;

        function parallel(latitude, color, granularity) {
          var name = "Parallel " + latitude;
          return viewer.entities.add({
            name: name,
            polyline: {
              positions: Cesium.Cartesian3.fromDegreesArray([
                -180,
                latitude,
                -90,
                latitude,
                0,
                latitude,
                90,
                latitude,
                180,
                latitude,
              ]),
              width: 2,
              arcType: Cesium.ArcType.RHUMB,
              material: color,
              granularity: granularity,
            },
          });
        }

        function meridian(longitude, color, granularity) {
          var name = "Meridian " + longitude;
          return viewer.entities.add({
            name: name,
            polyline: {
              positions: Cesium.Cartesian3.fromDegreesArray([
                longitude,
                90,
                longitude,
                0,
                longitude,
                -90,
              ]),
              width: 2,
              arcType: Cesium.ArcType.RHUMB,
              material: color,
              granularity: granularity,
            },
          });
        }

        function labelCoordinates(cartographic) {
          var position = Cesium.Cartographic.toCartesian(cartographic);
          var latitude = toDegrees(cartographic.latitude).toFixed(4);
          var longitude = toDegrees(cartographic.longitude).toFixed(4);
          var label = "lat: " + latitude + "°\nlon: " + longitude + "°";

          return viewer.entities.add({
            position: position,
            label: {
              text: label,
              showBackground: true,
              font: "14px monospace",
            },
          });
        }

        function makeGrid(numberOfDivisions, color, show) {
          var parallels = makeParallelsRecursive(
            -90,
            90,
            numberOfDivisions,
            color
          );
          var meridians = makeMeridiansRecursive(
            -180,
            180,
            numberOfDivisions,
            color
          );
          meridians.push(meridian(180, color));

          var allLines = parallels.concat(meridians);
          allLines.forEach(function (line) {
            line.show = show;
          });

          return allLines;
        }

        function makeParallelsRecursive(
          minLatitude,
          maxLatitude,
          depth,
          color
        ) {
          var result = [];
          var midpoint = (minLatitude + maxLatitude) / 2;
          result.push(parallel(midpoint, color));

          if (depth > 0) {
            var southernLines = makeParallelsRecursive(
              minLatitude,
              midpoint,
              depth - 1,
              color
            );
            var northernLines = makeParallelsRecursive(
              midpoint,
              maxLatitude,
              depth - 1,
              color
            );
            result = southernLines.concat(result, northernLines);
          }

          return result;
        }

        function makeMeridiansRecursive(
          minLongitude,
          maxLongitude,
          depth,
          color
        ) {
          var result = [];
          var midpoint = (minLongitude + maxLongitude) / 2;
          result.push(meridian(midpoint, color));

          if (depth > 0) {
            var westernLines = makeMeridiansRecursive(
              minLongitude,
              midpoint,
              depth - 1,
              color
            );
            var easternLines = makeMeridiansRecursive(
              midpoint,
              maxLongitude,
              depth - 1,
              color
            );
            result = westernLines.concat(result, easternLines);
          }

          return result;
        }

        var showAntipodalPoint = false;
        var primitives = {
          equator: parallel(0, Cesium.Color.BLUE),
          primeMeridian: meridian(0, Cesium.Color.BLUE),
          selectedPoint: {
            meridian: undefined,
            parallel: undefined,
            label: undefined,
          },
          antipodalPoint: {
            meridian: undefined,
            parallel: undefined,
            label: undefined,
          },
          lowResolutionGrid: makeGrid(2, Cesium.Color.PALEGREEN, false),
          higherResolutionGrid: makeGrid(5, Cesium.Color.DARKORANGE, false),
        };

        function updateCrosshairs(cartographic) {
          var selectedPoint = primitives.selectedPoint;
          var antipodalPoint = primitives.antipodalPoint;
          if (Cesium.defined(selectedPoint.parallel)) {
            viewer.entities.remove(selectedPoint.parallel);
            viewer.entities.remove(selectedPoint.meridian);
            viewer.entities.remove(selectedPoint.label);
            viewer.entities.remove(antipodalPoint.parallel);
            viewer.entities.remove(antipodalPoint.meridian);
            viewer.entities.remove(antipodalPoint.label);
          }

          var pointLatitude = toDegrees(cartographic.latitude);
          var antipodeLatitude = -pointLatitude;

          var pointLongitude = toDegrees(cartographic.longitude);
          var antipodeLongitude = (pointLongitude + 180) % 360;

          // Increase the granularity to improve accuracy when zoomed in
          var finerGranularity = 0.001;
          var red = Cesium.Color.RED;
          selectedPoint.parallel = parallel(
            toDegrees(cartographic.latitude),
            red,
            finerGranularity
          );
          selectedPoint.meridian = meridian(
            toDegrees(cartographic.longitude),
            red,
            finerGranularity
          );
          selectedPoint.label = labelCoordinates(cartographic);

          var cyan = Cesium.Color.CYAN;
          var antipode = new Cesium.Cartographic.fromDegrees(
            antipodeLongitude,
            antipodeLatitude,
            0
          );
          antipodalPoint.parallel = parallel(
            antipodeLatitude,
            cyan,
            finerGranularity
          );
          antipodalPoint.meridian = meridian(
            antipodeLongitude,
            cyan,
            finerGranularity
          );
          antipodalPoint.label = labelCoordinates(antipode);

          antipodalPoint.parallel.show = showAntipodalPoint;
          antipodalPoint.meridian.show = showAntipodalPoint;
          antipodalPoint.label.show = showAntipodalPoint;
        }

        // Click to shift the cross-hairs
        viewer.screenSpaceEventHandler.setInputAction(function (mouse) {
          viewer.scene.pick(mouse.position);
          var ray = viewer.camera.getPickRay(mouse.position);
          var globe = viewer.scene.globe;
          var cartesian = globe.pick(ray, viewer.scene);

          if (!Cesium.defined(cartesian)) {
            return;
          }

          var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
          updateCrosshairs(cartographic);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

        Sandcastle.addToggleButton("Show equator", true, function (checked) {
          primitives.equator.show = checked;
        });

        Sandcastle.addToggleButton("Show prime meridian", true, function (
          checked
        ) {
          primitives.primeMeridian.show = checked;
        });

        Sandcastle.addToggleButton("Show low-resolution grid", false, function (
          checked
        ) {
          primitives.lowResolutionGrid.forEach(function (line) {
            line.show = checked;
          });
        });

        Sandcastle.addToggleButton(
          "Show higher-resolution grid",
          false,
          function (checked) {
            primitives.higherResolutionGrid.forEach(function (line) {
              line.show = checked;
            });
          }
        );

        Sandcastle.addToggleButton("Show antipodal point", false, function (
          checked
        ) {
          showAntipodalPoint = checked;
          var antipodalPoint = primitives.antipodalPoint;

          if (Cesium.defined(antipodalPoint.parallel)) {
            antipodalPoint.parallel.show = showAntipodalPoint;
            antipodalPoint.meridian.show = showAntipodalPoint;
            antipodalPoint.label.show = showAntipodalPoint;
          }
        });
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>