-
Notifications
You must be signed in to change notification settings - Fork 1
/
map-hd.html
177 lines (153 loc) · 5.69 KB
/
map-hd.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<link type="text/css" rel="stylesheet" href="styles/svgStyle.css" id="svgCss" />
<script src="scripts/common.js"></script>
<script src="scripts/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://unpkg.com/topojson@3"></script>
<script src="data/world-hd.js"></script>
<script>
const CLIP_ANGLE = 175;
const ASIMEQ_TRANSF = d3.geo
.azimuthalEquidistant()
.translate([1000, 1000])
.rotate([0,90,0])
.clipAngle(CLIP_ANGLE)
.precision(.1)
const GRAT_COUNT = (1000/6371) * TO_DEG;
let moving = 0;
const inpts = {};
let g_land = undefined;
let g_countries = undefined;
let g_graticule = undefined;
let g_grid = [];
const LAND = topojson.feature(worldHD, worldHD.objects.land);
const COUNTRIES = topojson.mesh(worldHD, worldHD.objects.countries);
$(document).ready(()=>{
// Get DOM elements
inpts.lat = $('#inptLat')[0];
inpts.lng = $('#inptLng')[0];
inpts.rot = $('#inptRot')[0];
inpts.mvTo = $('#inptMoveTo')[0];
inpts.grat = $('#inptGrat')[0];
inpts.grid = $('#inptGrid')[0];
inpts.dist = $('#inptDist')[0];
inpts.land = $('#inptLand')[0];
inpts.countries = $('#inptCtry')[0];
// Set default values or use query parameters (?lat=1.111&lng=2.222&rot=3.333)
const urlParams = new URLSearchParams(window.location.search);
inpts.lat.value = urlParams.has('lat') ? urlParams.get('lat') : 90;
inpts.lng.value = urlParams.has('lng') ? urlParams.get('lng') : 0;
inpts.rot.value = urlParams.has('rot') ? urlParams.get('rot') : 0;
// Set SVG
reset();
// Get SVG style and extract into the svg element (needed for exporting as file.svg)
$.ajax({
url: 'styles/svgStyle.css',
type: 'GET',
cache: true,
dataType: 'text',
contentType: 'text/plain',
success: (response) => {
$('#map > svg').prepend($('<style/>').text(response));
},
error: ()=>{
console.log('Ajax Error')
// Alternative (may not work in all browsers)
try{
const file = document.getElementById('svgCss');
const content = Array.prototype.map.call(file.sheet.cssRules, (x)=>x.cssText).join(' ');
$('#map > svg').prepend($('<style/>').text(content));
} catch(e) {
// Could not export SVG with style
$('#btnDownloadSVG').attr('disabled', true).html('Not supported');
}
}
});
// Update views to show SVG
setTimeout(updateGraphics);
setTimeout(updateView);
})
function reset() {
$('#map').empty(); // reset
const svg = d3.selectAll('#map').data([ASIMEQ_TRANSF]).append('svg');
svg.attr('xmlns', 'http://www.w3.org/2000/svg');
svg.each(project);
let dyn = svg.append('g').attr('id', 'dyn');
let stt = svg.append('g').attr('id', 'stt');
g_land = dyn.append('path').datum(LAND);
g_countries = dyn.append('path').datum(COUNTRIES);
g_graticule = dyn.append('path').datum(d3.geo.graticule()());
const grids = stt.selectAll('path.circle').data(d3.range(GRAT_COUNT, CLIP_ANGLE, GRAT_COUNT)).enter();
g_dist = grids.append('path').datum(d=>d3.geo.circle().origin([0,90,0]).angle(d)());
g_grid[0] = grids.append('path').datum(d=>d3.geo.circle().origin([0,0,0]).angle(d)());
g_grid[1] = grids.append('path').datum(d=>d3.geo.circle().origin([90,0,0]).angle(d)());
// Set resizable
svg.attr('viewBox', '500 500 1000 1000');
svg.attr('width', null).attr('height', null);
d3.select('#stt').each(redrawPath);
}
function moveTo(lat, lng, rot) {
ASIMEQ_TRANSF.rotate([-lng, -lat, rot]);
d3.select('#dyn').each(redrawPath);
inpts.lat.value = lat.toFixed(6);
inpts.lng.value = lng.toFixed(6);
}
function updateView() {
const lat = parseFloat(inpts.lat.value);
const lng = parseFloat(inpts.lng.value);
const rot = parseFloat(inpts.rot.value);
moveTo(lat, lng, rot)
}
function updateGraphics() {
g_land.attr('class', inpts.land.checked?'land':'unset');
g_countries.attr('class', inpts.countries.checked?'countries':'unset');
g_graticule.attr('class', inpts.grat.checked?'graticule':'unset');
g_dist.attr('class', inpts.dist.checked?'dist':'unset');
for(g of g_grid)
g.attr('class', inpts.grid.checked?'grid':'unset');
}
</script>
</head>
<body>
<div id="inputs">
<div>
<span>Lat:</span>
<input id="inptLat" type="number" min="-90" max="90" step="0.000001" value="90"/>
<span>Lng:</span>
<input id="inptLng" type="number" min="-180" max="180" step="0.000001" value="0"/>
<span>Rot:</span>
<input id="inptRot" type="number" min="-180" max="180" step="0.01" value="0"/>
<button id="inptMoveTo" onclick="updateView()">Go</button>
</div>
<div>Lat: Latitude (-90 to 90, 0=Equator)</div>
<div>Lng: Longitude (-180 to 180, 0=Greenwitch)</div>
<div>Rot: Rotation (0=North Up, -179 to -1=turn clockwise, 1 to 179=turn counterclockwise, 180 or -180=South up)</div>
<div>
<span>Land:</span>
<input id="inptLand" type="checkbox" onchange="updateGraphics()" checked="checked" />
<span>Countries:</span>
<input id="inptCtry" type="checkbox" onchange="updateGraphics()" />
<span>Graticule:</span>
<input id="inptGrat" type="checkbox" onchange="updateGraphics()" />
<span>Distances:</span>
<input id="inptDist" type="checkbox" onchange="updateGraphics()" />
<span>Grid:</span>
<input id="inptGrid" type="checkbox" onchange="updateGraphics()" />
</div>
<div>
<span>Download current view as SVG:</span>
<button id="btnDownloadSVG" onclick="saveSvg($('#map').html(), 'map.svg')">Download</button>
</div>
</div>
<div id="map"></div>
<span>
<a href="map-animated.html">Animated map</a>
<br/>
<a href="https://github.com/NatNgs/maps">https://github.com/NatNgs/maps</a>
</span>
</body>
</html>