forked from ProjectionWizard/projectionwizard.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
previewMap.js
615 lines (544 loc) · 17.3 KB
/
previewMap.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* PROJECTION WIZARD v2.0
* Map Projection Selection Tool
*
* Author: Bojan Savric, Jacob Wasilkowski
* Date: May, 2020
*
*/
var world110m, world50m;
/***MAP DRAW FUNCTION FOR SMALL-SCALE***/
function addWorldMapPreview(center, projection, currentlyDragging) {
var textAreaParams = document.getElementById("textAreaParams");
textAreaParams.innerHTML = "";
var activeDistortion = $('input[name=distortion]:checked').val();
// Updates the active projection
activeProjection = projection;
// Updates default displays for the 3 world projections per distortion
// property (this helps maintain the most recently chosen world projection
// by the user in the d3 canvas map and the html text styling)
if (activeDistortion === 'Equalarea') {
activeWorldEqAreaProj = projection;
} else if (activeDistortion === 'Equidistant') {
activeWorldEqDistProj = projection;
} else if (activeDistortion === 'Compromise') {
activeWorldComproProj = projection;
}
highlightActiveProjectionNode();
//Creating canvas HTML element
if ( projection == 'Two-point equidistant' ) {
addCanvasMap(lat1_eq, lng1_eq, projection, 1, currentlyDragging);
}
else if ( projection == 'Oblique azimuthal equidistant' )
{
addCanvasMap(latC_eq, lngC_eq, projection, 1, currentlyDragging);
}
else {
addCanvasMap(0, center.lng, projection, 1, currentlyDragging);
}
//adding class to split text and map preview
$("#result").addClass("results");
}
/***MAP DRAW FUNCTIONS FOR onmouseover***/
function updateWorldMap(projection) {
//getting a center of the map
var center = rectangle.getBounds().getCenter();
addWorldMapPreview(center, projection, false);
}
function updateEquidistantMap(projection) {
//getting a center of the map
var center = rectangle.getBounds().getCenter();
// Updates the active projection and default display
// for the equidistant projection between pole and equator
activeEqDistProj = activeProjection = projection;
previewMapProjection = projection;
addMapPreview(center, false);
}
function highlightActiveProjectionNode() {
// remove ".active-projection" class from all projection title DOM nodes that have a custom proj-name data property
var optionalProjectionsNodeList = document.querySelectorAll('[data-proj-name]');
for (var i = 0; i < optionalProjectionsNodeList.length; i++) {
var node = optionalProjectionsNodeList[i];
node.classList.remove('active-projection');
}
// add ".active-projection" class to the matching projection title DOM node if it was found
var activeProjectionNode = document.querySelector('[data-proj-name="' + activeProjection + '"]');
if (activeProjectionNode) {
activeProjectionNode.classList.add('active-projection');
}
}
/***MAIN MAP DRAW FUNCTION***/
function addMapPreview(center, currentlyDragging) {
//Creating canvas HTML element
addCanvasMap(previewMapLat0, center.lng, previewMapProjection, 0, currentlyDragging);
highlightActiveProjectionNode();
//adding class to split text and map preview
$("#result").addClass("results");
addClippingParams();
}
/* Setting a new projection with D3 */
function pickProjection(lat0, lon0, projectionString) {
//Definding D3 projection
if (projectionString == 'Azimuthal equal area') {
return d3.geoAzimuthalEqualArea()
.clipAngle(180 - 1e-3)
.precision(.1)
.rotate([-lon0, -lat0]);
}
else if (projectionString == 'Azimuthal equidistant') {
return d3.geoAzimuthalEquidistant()
.clipAngle(180 - 1e-3)
.precision(.1)
.rotate([-lon0, -lat0]);
}
else if (projectionString == 'Orthographic') {
return d3.geoOrthographic()
.clipAngle(90)
.rotate([-lon0, -lat0]);
}
else if (projectionString == 'Stereographic') {
return d3.geoStereographic()
.rotate([-lon0, -lat0])
.clipAngle(90)
//.clipExtent([[0, 0], [width, height]])
.precision(.1);
}
else if (projectionString == 'Mercator') {
return d3.geoMercator()
.precision(.1)
.rotate([-lon0, 0]);
}
else if (projectionString == 'Transverse Mercator') {
return d3.geoTransverseMercator()
.precision(.1)
.rotate([-lon0, 0]);
}
else if (projectionString == 'Cylindrical equal area') {
var interval = (latmax - latmin) / 4.;
var latS1 = lat0 + interval, latS2 = lat0 - interval, latS;
if ((latS1 > 0. && latS2 > 0.) || (latS1 < 0. && latS2 < 0.)) {
latS = Math.max(Math.abs(latmax), Math.abs(latmin)) / 2.;
} else {
latS = 0.;
}
return d3.geoCylindricalEqualArea()
.parallel(latS)
.precision(.1)
.rotate([-lon0, 0]);
}
else if (projectionString == 'Transverse cylindrical equal area') {
return d3.geoTransverseCylindricalEqualArea()
.parallel(0)
.precision(.1)
.rotate([-lon0, 0, 90]);
}
else if (projectionString == 'Equidistant cylindrical') {
return d3.geoEquirectangular()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Cassini') {
return d3.geoEquirectangular()
.rotate([-lon0, 0, 90])
.angle(-90)
.precision(.1);
}
else if (projectionString == 'Equidistant conic') {
var interval = (latmax - latmin) / 6;
return d3.geoConicEquidistant()
.parallels([latmin + interval, latmax - interval])
.center([0, lat0])
.precision(.1)
.rotate([-lon0, 0]);
}
else if (projectionString == 'Albers equal area conic') {
var interval = (latmax - latmin) / 6;
return d3.geoAlbers()
.rotate([-lon0, 0])
.center([0, lat0])
.parallels([latmin + interval, latmax - interval])
.precision(.1);
}
else if (projectionString == 'Lambert conformal conic') {
var interval = (latmax - latmin) / 6;
return d3.geoConicConformal()
.rotate([-lon0, 0])
//.clipAngle(90)
.center([0, lat0])
.parallels([latmin + interval, latmax - interval])
.precision(.1);
}
else if (projectionString == 'Mollweide') {
return d3.geoMollweide()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Hammer (or Hammer-Aitoff)') {
return d3.geoHammer()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Equal Earth') {
return d3.geoEqualEarth()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Eckert IV') {
return d3.geoEckert4()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Wagner IV (or Putnins P2`)') {
return d3.geoWagner4()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Wagner VII (or Hammer-Wagner)') {
return d3.geoWagner7()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Robinson') {
return d3.geoRobinson()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Natural Earth') {
return d3.geoNaturalEarth()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Winkel Tripel') {
return d3.geoWinkel3()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Patterson') {
return d3.geoPatterson()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Plate Carrée') {
return d3.geoEquirectangular()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Miller cylindrical I') {
return d3.geoMiller()
.rotate([-lon0, 0])
.precision(.1);
}
else if (projectionString == 'Polar azimuthal equidistant') {
return d3.geoAzimuthalEquidistant()
.clipAngle(180 - 1e-3)
.precision(.1)
.rotate([-lngP_eq, -pole_eq]);
}
else if (projectionString == 'Oblique azimuthal equidistant') {
return d3.geoAzimuthalEquidistant()
.clipAngle(180 - 1e-3)
.precision(.1)
.rotate([-lon0, -lat0]);
}
else if (projectionString == 'Two-point equidistant') {
var pt1 = [lon0, lat0], // First point
pt2 = [lng2_eq, lat2_eq]; // Second point
// Clipping the area around the antipode
// https://github.com/d3/d3-geo-projection/issues/192#issuecomment-642398941
const eps = 1e-3, u = (d3.geoDistance(pt1, pt2) / 2) * (180 / Math.PI) + eps,
ellipse = {
type: "Polygon",
coordinates: [ [
[ 180 - u, eps],
[ 180 - u, -eps],
[-180 + u, -eps],
[-180 + u, eps],
[ 180 - u, eps]
]]
};
return d3.geoTwoPointEquidistant(pt1,pt2)
.preclip(d3.geoClipPolygon(ellipse));
}
else {
// projection error condition
var previewMapProjectionName = $("#previewMap #projectionName");
previewMapProjectionName.append("<p></p><p></p><p>Map preview not avaliable</p><p></p><p></p>");
return;
}
}
function clearCanvasMap() {
// helper function to clear the d3 preview map canvas because
// 1. the canvas context and the projection name display must be cleared before being updated
// 2. some other conditions do not show the d3 preview map at all
var previewMapCanvas = d3.select("#previewMap canvas").node();
previewMapCanvas
.getContext("2d")
.clearRect(0, 0, previewMapCanvas.width, previewMapCanvas.height);
// to be safe, also clear the projection name text below the d3 preview map
var previewMapProjectionName = $("#previewMap #projectionName");
previewMapProjectionName.empty();
}
/* Map drawing function (D3)*/
function addCanvasMap(lat0, lon0, projectionString, world, currentlyDragging) {
// first and only once, attempt to fetch both of the needed world geojson files
// otherwise, continue with the geojson files that were already fetched
if (!world110m && !world50m) {
Promise
.all([
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@1/world/110m.json"),
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@1/world/50m.json")
])
.then(function(allGeoJsonData) {
world110m = allGeoJsonData[0];
world50m = allGeoJsonData[1];
continueDrawingCanvasMap(world110m, world50m, lat0, lon0, projectionString, world, currentlyDragging);
});
} else {
continueDrawingCanvasMap(world110m, world50m, lat0, lon0, projectionString, world, currentlyDragging);
}
}
function continueDrawingCanvasMap(world110m, world50m, lat0, lon0, projectionString, world, currentlyDragging) {
// clear the canvas context and the projection name display must be cleared before being updated
clearCanvasMap();
//Setting graticule layer
var graticule = d3.geoGraticule(),
sphere = {type : "Sphere"};
grid = graticule();
//Setting rectangle layer
var lammax = lonmax,
lammin = lonmin,
phimax = latmax,
phimin = latmin;
var dlam = lammax - lammin,
dphi = phimax - phimin,
step = Math.min(dlam, dphi) / 15.0;
var pt, rec_pts = [], rec_poly;
//Create two ractangles for world equidistant cases
if (projectionString == 'Polar azimuthal equidistant' ||
projectionString == 'Oblique azimuthal equidistant' ||
projectionString == 'Two-point equidistant') {
var rec1 = [], rec2 = [];
//Building first rectangle
for (pt = lammin; pt < lon0; pt += step) {
rec1.push([pt, phimax]);
}
for (pt = phimax; pt > phimin; pt -= step) {
rec1.push([lon0, pt]);
}
for (pt = lon0; pt > lammin; pt -= step) {
rec1.push([pt, phimin]);
}
for (pt = phimin; pt < phimax; pt += step) {
rec1.push([lammin, pt]);
}
rec1.push([lammin, phimax]);
//Building second rectangle
for (pt = lon0; pt < lammax; pt += step) {
rec2.push([pt, phimax]);
}
for (pt = phimax; pt > phimin; pt -= step) {
rec2.push([lammax, pt]);
}
for (pt = lammax; pt > lon0; pt -= step) {
rec2.push([pt, phimin]);
}
for (pt = phimin; pt < phimax; pt += step) {
rec2.push([lon0, pt]);
}
rec2.push([lon0, phimax]);
rec_poly = {
type: "FeatureCollection", features: [
{type: "Feature", geometry: {type: "Polygon", coordinates: [rec1]}},
{type: "Feature", geometry: {type: "Polygon", coordinates: [rec2]}}
]
};
}
//Create only one ractangle for all other cases
else {
//Prevents the polygon from collapsing
if (dlam > 359.999) {
var eps = 1/3600.;
lammin += eps;
lammax -= eps;
}
if (dphi > 179.999) {
var eps = 1/3600.;
phimin += eps;
phimax -= eps;
}
//Building rectangle
for (pt = lammin; pt < lammax; pt += step) {
rec_pts.push([pt, phimax]);
}
for (pt = phimax; pt > phimin; pt -= step) {
rec_pts.push([lammax, pt]);
}
for (pt = lammax; pt > lammin; pt -= step) {
rec_pts.push([pt, phimin]);
}
for (pt = phimin; pt < phimax; pt += step) {
rec_pts.push([lammin, pt]);
}
rec_pts.push([lammin, phimax]);
rec_poly = {type: "Feature", geometry: {type: "Polygon", coordinates: [rec_pts]}};
}
//Defining D3 projection
var projection = pickProjection(lat0, lon0, projectionString);
if (projection == null) {
return;
}
//Set the display text of the projection name that appears below the d3 preview map
$("#previewMap #projectionName").append(projectionString + "<br>");
//Scaling projection on original coordinates
projection.translate([0,0])
.scale(1);
var max_width = document.getElementById('previewMap').offsetWidth,
height, width, X;
//Computing scale factor and translation for world maps
if (world) {
if (projectionString == 'Polar azimuthal equidistant' || projectionString == 'Oblique azimuthal equidistant') {
width = height = 0.7 * max_width;
projection.fitSize([width - 20, height - 20], grid)
.translate([width / 2, height / 2]);
}
else if (projectionString == 'Two-point equidistant') {
height = 0.5 * max_width;
width = max_width;
projection.fitSize([width, height - 10], grid)
.translate([width / 2, height / 2]);
}
else {
//Computing extent coordinates
projection.rotate([0, 0]);
var coord1 = projection([180, 0]),
coord2 = projection([-180, 0]),
coord3 = projection([180, 90]),
coord4 = projection([-180, -90]);
//Definding original width and height of the extent
width = Math.abs(coord1[0] - coord2[0]);
height = Math.abs(coord3[1] - coord4[1]);
//Final scaling factor and translation parameters
X = Math.min(max_width / width, max_width / height);
width *= X;
height *= X;
projection.rotate([-lon0, 0])
.fitSize([width, height], grid);
}
}
//Computing scale factor and translation for other maps
else {
var xmin = Number.MAX_VALUE,
xmax = -Number.MAX_VALUE,
ymin = Number.MAX_VALUE,
ymax = -Number.MAX_VALUE,
pt, fit_pts = rec_pts.slice();
//Making sure origin point(s) will also display
if ( document.getElementById("showCenter").checked ) {
fit_pts.push([lon0, lat0]);
}
//Project rectangle
for (var i = 0; i < fit_pts.length; i++)
{
pt = projection(fit_pts[i]);
xmin = Math.min(xmin, pt[0]);
xmax = Math.max(xmax, pt[0]);
ymin = Math.min(ymin, pt[1]);
ymax = Math.max(ymax, pt[1]);
}
//Calculate size of rectangle
width = Math.abs(xmax - xmin);
height = Math.abs(ymax - ymin);
//Final scaling factor and translation parameters
X = Math.min(max_width / width, 0.66 * max_width / height);
width *= X;
height *= X;
projection.translate([width / 2, height / 2])
.fitSize([width, height], {type: 'Feature', geometry: {type: 'MultiPoint', coordinates: fit_pts}});
}
//Setting data layer
var data;
var scale = 720. / (lonmax - lonmin) / (Math.sin(latmax * Math.PI / 180.) - Math.sin(latmin * Math.PI / 180.));
if (currentlyDragging || (scale < 6)) {
data = world110m;
} else {
data = world50m;
}
land = topojson.feature(data, data.objects.countries);
//Drawing map elements
var canvas = d3.select("#previewMap canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
var path = d3.geoPath(projection, context);
// Style sphere
context.beginPath();
path(sphere);
context.fillStyle = "#add8e6";
context.fill();
// Style land
context.beginPath();
path(land);
context.fillStyle = "#eee";
context.fill();
context.lineWidth = 0.3;
context.strokeStyle = "#999";
context.stroke();
// Style graticule
context.beginPath();
path(grid);
context.lineWidth = 0.5;
context.globalAlpha = 0.2;
context.strokeStyle = "#555";
context.stroke();
context.globalAlpha = 1;
// Style ractangle
if ( document.getElementById("showEextent").checked ) {
context.beginPath();
path(rec_poly);
context.lineWidth = 3;
context.globalAlpha = 0.25;
context.fillStyle = "#ff7b1a";
context.fill();
context.globalAlpha = 1;
}
// Style origin point
if ( document.getElementById("showCenter").checked ) {
var origin = [], pt;
if (projectionString == 'Polar azimuthal equidistant') {
origin.push([lngP_eq, pole_eq]);
}
else if (projectionString == 'Oblique azimuthal equidistant') {
origin.push([lngC_eq, latC_eq]);
}
else if (projectionString == 'Two-point equidistant') {
origin.push([lng1_eq, lat1_eq]);
origin.push([lng2_eq, lat2_eq]);
}
else {
origin.push([lon0, lat0]);
}
//Drawing points
for (var i = 0; i < origin.length; i++)
{
pt = projection(origin[i]);
context.beginPath();
context.arc(pt[0], pt[1], 4, 0, 2 * Math.PI);
context.fillStyle = "#3388ff";
context.fill();
context.closePath();
}
}
}
function addClippingParams() {
var lonmin = document.getElementById('lonmin').value;
var latmin = document.getElementById('latmin').value;
var lonmax = document.getElementById('lonmax').value;
var latmax = document.getElementById('latmax').value;
var coordinatesArray = `${lonmin}, ${latmin}, ${lonmax}, ${latmax}`;
var proj = document.getElementsByClassName("linkPROJ4")[0].getAttribute("onclick");
var match = proj.match(/"([^"]+)"/);
match = match ? match[1] : null;
var text = `PROJECTION: "${match}"\nRAWBBOX: ${JSON.stringify(coordinatesArray)}`;
var textAreaParams = document.getElementById("textAreaParams");
textAreaParams.innerHTML = text;
}