-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2.html
300 lines (248 loc) · 10.1 KB
/
demo2.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet-src.js"></script>
<script type="text/javascript" src="./proj4.js"></script>
<script type="text/javascript" src="./proj4leaflet.js"></script>
<script type="text/javascript" src="./L.Graticule.js"></script>
<style>
html,
body {
height: 95%;
}
#map {
height: 100%; width: 95vw;
background: #e5ffff;
}
</style>
</head>
<body>
<div style="display:flex; padding:4px 0px 10px 0px;">
<div>projection :</div>
<div style='min-width:8px;'></div>
<div class='select-style'>
<select id=projection onchange="map_init();">
<option value="EPSG:2154,+proj=lcc +lat_1=30 +lat_2=60 +lat_0=0 +lon_0=126 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs,10000,40,120">Lambert Conformal Conic</option>
<option value="EPSG:3031,+proj=Polar_Stereographic +lat_0=90 +lat_ts=90 +lon_0=120 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs,20000,40,120">Polar Stereographic</option>
<option value="EPSG:32662,+proj=eqc +lat_0=0 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs,25000,0,0">Equidistant Cylindrical</option>
</select>
</div>
<!--
<div style='min-width:10px;'></div>
<div id="lat" style='border:1px solid gray; border-radius:3px; width:50px; height:18px; font-size:12px; padding:0px 3px 0px 3px;'></div>
<div style='min-width:10px;'></div>
<div id="lon" style='border:1px solid gray; border-radius:3px; width:50px; height:18px; font-size:12px; padding:0px 3px 0px 3px;'></div>
-->
</div>
<div style="height: 95%; width: 95vw; border:1px solid black;">
<div id="map"></div>
</div>
<script>
var map;
var geojsonData;
var geojsonVectorTile;
map_init(map);
function map_init() {
var projection = document.getElementById("projection").value.toString().split(',');
var resolutions = [];
for (var z=0; z<8; z++) {
resolutions[z] = parseFloat(projection[2])/Math.pow(1.5,z);
}
var lat_0 = parseFloat(projection[3]);
var lon_0 = parseFloat(projection[4]);
var lcc_crs = new L.Proj.CRS(projection[0], projection[1],
{
resolutions: resolutions
}
);
if (map == undefined) {
map = L.map('map', {
maxZoom: 8,
minZoom: 0,
crs: lcc_crs
}).setView([40, 120], 0);
// pane 추가
var mapPaneName1 = "geojson";
var mapPaneName2 = "graticule";
// pane layer 생성
map.createPane(mapPaneName1);
map.createPane(mapPaneName2);
// pane layer z-inex set
map.getPane(mapPaneName1).style.zIndex = 0;
map.getPane(mapPaneName2).style.zIndex = 100;
// pane layer 마우스 및 클릭 이벤트
map.getPane(mapPaneName1).style.pointerEvents = 'none';
map.getPane(mapPaneName2).style.pointerEvents = 'none';
var canvas = L.canvas({pane: "graticule", padding: 1.5});
L.graticule({ interval:20, style:{dashArray:'4,4', color:'#333', weight:1}, renderer: canvas }).addTo(map);
//map.on('mousemove', function(e) {
// document.getElementById("lat").innerText = e.latlng.lat.toFixed(2);
// document.getElementById("lon").innerText = e.latlng.lng.toFixed(2);
//});
fnGeoJson(map, mapPaneName1);
}
else {
if (geojsonVectorTile != undefined && geojsonVectorTile._leaflet_id != undefined) {
map.removeLayer(geojsonVectorTile);
}
map.options.crs = lcc_crs;
map.fire('viewreset');
var resolution = 1/map.options.crs._scales[0];
fnGeojsonBounds(map, geojsonData);
geojsonVectorTile = fnGeoTile(map, "geojson", geojsonData, resolution, true, "#ffffee");
geojsonVectorTile.addTo(map);
map.setView([lat_0, lon_0], 0, {animate:false});
map.fire('viewreset');
}
}
function fnGeoJson(map, pane) {
var url = "https://raw.githubusercontent.com/hunter3789/geojson/main/custom.geo.50m.json";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.timeout = 60000;
xhr.overrideMimeType("application/json; charset=utf-8");
xhr.onreadystatechange = function () {
if (xhr.readyState != 4 || xhr.status != 200) return;
else {
geojsonData = JSON.parse(xhr.responseText);
var resolution = 1/map.options.crs._scales[0];
fnGeojsonBounds(map, geojsonData);
geojsonVectorTile = fnGeoTile(map, pane, geojsonData, resolution, true, "#ffffee");
geojsonVectorTile.addTo(map);
}
};
xhr.send();
}
function fnGeojsonBounds(map, data) {
for (var i=0; i<data.features.length; i++) {
var xmax = ymax = -999999.;
var xmin = ymin = 999999.;
if (data.features[i].geometry.type == "MultiPolygon") {
for (var j=0; j<data.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<data.features[i].geometry.coordinates[j].length; k++) {
for (var l=0; l<data.features[i].geometry.coordinates[j][k].length; l++) {
var point = map.project(L.latLng([data.features[i].geometry.coordinates[j][k][l][1], data.features[i].geometry.coordinates[j][k][l][0]]), 0);
if (xmax <= point.x) {
xmax = point.x;
}
if (xmin >= point.x) {
xmin = point.x;
}
if (ymax <= point.y) {
ymax = point.y;
}
if (ymin >= point.y) {
ymin = point.y;
}
}
}
}
data.features[i].properties.xmax = xmax;
data.features[i].properties.xmin = xmin;
data.features[i].properties.ymax = ymax;
data.features[i].properties.ymin = ymin;
//console.log(data.features[i].properties.admin, data.features[i].properties.bounds);
}
else if (data.features[i].geometry.type == "Polygon") {
for (var j=0; j<data.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<data.features[i].geometry.coordinates[j].length; k++) {
var point = map.project(L.latLng([data.features[i].geometry.coordinates[j][k][1], data.features[i].geometry.coordinates[j][k][0]]), 0);
if (xmax <= point.x) {
xmax = point.x;
}
if (xmin >= point.x) {
xmin = point.x;
}
if (ymax <= point.y) {
ymax = point.y;
}
if (ymin >= point.y) {
ymin = point.y;
}
}
}
data.features[i].properties.xmax = xmax;
data.features[i].properties.xmin = xmin;
data.features[i].properties.ymax = ymax;
data.features[i].properties.ymin = ymin;
//console.log(data.features[i].properties.admin, data.features[i].properties.bounds);
}
}
}
function fnGeoTile(map, pane, data, resolution, fill, color) {
var tiles = new L.GridLayer({tileSize:512, pane:pane});
tiles.createTile = function(coords) {
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
var ctx = tile.getContext('2d');
var size = this.getTileSize()
tile.width = size.x
tile.height = size.y
// calculate projection coordinates of top left tile pixel
var nwPoint = coords.scaleBy(size)
var ratio = resolution*map.options.crs._scales[coords.z];
for (var i=0; i<data.features.length; i++) {
var xmin = data.features[i].properties.xmin*ratio;
var ymin = data.features[i].properties.ymin*ratio;
var xmax = data.features[i].properties.xmax*ratio;
var ymax = data.features[i].properties.ymax*ratio;
if (xmax < nwPoint.x || xmin > nwPoint.x + tile.width || ymax < nwPoint.y || ymin > nwPoint.y + tile.height) {
continue;
}
drawTile(ctx, coords, i, nwPoint, fill, color);
}
ctx.lineWidth = 0.5;
ctx.strokeStyle = 'blue';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(size.x, 0);
ctx.lineTo(size.x, size.y);
ctx.lineTo(0, size.y);
ctx.closePath();
ctx.stroke();
return tile;
}
function drawTile(ctx, coords, i, nwPoint, fill, color) {
ctx.strokeStyle = "black";
ctx.fillStyle = color;
ctx.beginPath();
if (data.features[i].geometry.type == "MultiPolygon") {
for (var j=0; j<data.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<data.features[i].geometry.coordinates[j].length; k++) {
for (var l=0; l<data.features[i].geometry.coordinates[j][k].length; l++) {
var point = map.project(L.latLng([data.features[i].geometry.coordinates[j][k][l][1], data.features[i].geometry.coordinates[j][k][l][0]]), coords.z);
if (l==0) {
ctx.moveTo(point.x - nwPoint.x, point.y - nwPoint.y);
}
else {
ctx.lineTo(point.x - nwPoint.x, point.y - nwPoint.y);
}
}
}
}
}
else if (data.features[i].geometry.type == "Polygon") {
for (var j=0; j<data.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<data.features[i].geometry.coordinates[j].length; k++) {
var point = map.project(L.latLng([data.features[i].geometry.coordinates[j][k][1], data.features[i].geometry.coordinates[j][k][0]]), coords.z);
if (k==0) {
ctx.moveTo(point.x - nwPoint.x, point.y - nwPoint.y);
}
else {
ctx.lineTo(point.x - nwPoint.x, point.y - nwPoint.y);
}
}
}
}
ctx.closePath();
if (fill == true) {
ctx.fill();
}
ctx.stroke();
}
return tiles;
}
</script>
</body>
</html>