-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet_tile.html
412 lines (338 loc) · 13.9 KB
/
leaflet_tile.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
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
<!DOCTYPE html>
<html>
<head>
<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>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
<style>
html,
body {
height: 95%;
}
#map {
width: 1000px;
height: 800px;
background: #e5ffff;
}
</style>
</head>
<body>
<div style="display:flex; padding:4px 0px 10px 0px;">
<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="width:1000px; border:1px solid black;">
<div id="map"></div>
</div>
<script>
var json;
var lcc_attrs = {
proj4string: "+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",
resolutions: [ 10000, 6666, 4444,
2962, 1975, 1103.8683,
735.9122, 490.6081, 327.0721, 218.0481],
zoom: {
min: 0,
max: 8
}
};
var lcc_crs = new L.Proj.CRS('EPSG:2154', lcc_attrs.proj4string,
{
resolutions: lcc_attrs.resolutions //rss
}
);
//console.log(lcc_crs.projection._proj.forward([126, 0]));
var map = L.map('map', {
maxZoom: lcc_attrs.zoom.max, //6,
minZoom: lcc_attrs.zoom.min, //0,
crs: lcc_crs,
continuousWorld: true,
attributionControl: false
}).setView([41, 115], 0);
// pane 추가
var mapPaneName1 = "geojson";
var mapPaneName2 = "image";
var mapPaneName3 = "graticule";
// pane layer 생성
map.createPane(mapPaneName1);
map.createPane(mapPaneName2);
map.createPane(mapPaneName3);
// pane layer z-inex set
map.getPane(mapPaneName1).style.zIndex = 0;
map.getPane(mapPaneName2).style.zIndex = 100;
map.getPane(mapPaneName3).style.zIndex = 200;
// pane layer 마우스 및 클릭 이벤트
map.getPane(mapPaneName1).style.pointerEvents = 'none';
map.getPane(mapPaneName2).style.pointerEvents = 'none';
map.getPane(mapPaneName3).style.pointerEvents = 'none';
var canvas = L.canvas({pane: "geojson", padding: 1.5});
var canvas2 = L.canvas({pane: "graticule", padding: 1.5});
L.graticule({ interval:20, style:{dashArray:'4,4', color:'#333', weight:1}, renderer: canvas2 }).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);
function fnGeoJson(map) {
var webworker;
if(typeof(webworker) == "undefined") {
webworker = new Worker("https://hunter3789.github.io/geojson/MapWorker.js");
}
webworker.onmessage = function(message, map) { // web worker 객체와의 연결
console.log(message, map);
fnGetBounds(message.data, map);
webworker.terminate(); // web worker 객체의 실행 종료
webworker = undefined; // web worker 객체의 재사용
};
//setTimeout(fnGetBounds, 1);
//fnGetBounds();
//L.geoJson(json).addTo(map);
//fnGeoTile(json);
}
function fnGetBounds(json) {
for (var i=0; i<json.features.length; i++) {
var xmax = ymax = -999999.;
var xmin = ymin = 999999.;
if (json.features[i].geometry.type == "MultiPolygon") {
for (var j=0; j<json.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<json.features[i].geometry.coordinates[j].length; k++) {
for (var l=0; l<json.features[i].geometry.coordinates[j][k].length; l++) {
var point = map.project(L.latLng([json.features[i].geometry.coordinates[j][k][l][1], json.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;
}
}
}
}
json.features[i].properties.xmax = xmax;
json.features[i].properties.xmin = xmin;
json.features[i].properties.ymax = ymax;
json.features[i].properties.ymin = ymin;
//console.log(json.features[i].properties.admin, json.features[i].properties.bounds);
}
else if (json.features[i].geometry.type == "Polygon") {
for (var j=0; j<json.features[i].geometry.coordinates.length; j++) {
for (var k=0; k<json.features[i].geometry.coordinates[j].length; k++) {
var point = map.project(L.latLng([json.features[i].geometry.coordinates[j][k][1], json.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;
}
}
}
json.features[i].properties.xmax = xmax;
json.features[i].properties.xmin = xmin;
json.features[i].properties.ymax = ymax;
json.features[i].properties.ymin = ymin;
//console.log(json.features[i].properties.admin, json.features[i].properties.bounds);
}
}
fnGeoTile(json);
}
function fnGeoTile(data) {
var tiles = new L.GridLayer({tileSize:512});
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)
ctx.fillStyle = 'white';
//ctx.fillRect(0, 0, size.x, 50);
ctx.fillStyle = 'black';
//ctx.fillText('x: ' + coords.x + ', y: ' + coords.y + ', zoom: ' + coords.z, 20, 20);
for (var i=0; i<data.features.length; i++) {
/*
var point = [];
point[0] = map.project(L.latLng([data.features[i].properties.ymin, data.features[i].properties.xmin]), coords.z);
point[1] = map.project(L.latLng([data.features[i].properties.ymin, data.features[i].properties.xmax]), coords.z);
point[2] = map.project(L.latLng([data.features[i].properties.ymax, data.features[i].properties.xmin]), coords.z);
point[3] = map.project(L.latLng([data.features[i].properties.ymax, data.features[i].properties.xmax]), coords.z);
var xmin = Math.min(point[0].x, point[1].x, point[2].x, point[3].x);
var xmax = Math.max(point[0].x, point[1].x, point[2].x, point[3].x);
var ymin = Math.min(point[0].y, point[1].y, point[2].y, point[3].y);
var ymax = Math.max(point[0].y, point[1].y, point[2].y, point[3].y);
*/
var point = map.unproject(L.point([data.features[i].properties.xmin, data.features[i].properties.ymin]), 0);
var point = map.project(L.latLng([point.lat, point.lng]), coords.z);
var xmin = point.x;
var ymin = point.y;
var point = map.unproject(L.point([data.features[i].properties.xmax, data.features[i].properties.ymax]), 0);
var point = map.project(L.latLng([point.lat, point.lng]), coords.z);
var xmax = point.x;
var ymax = point.y;
if (xmax < nwPoint.x || xmin > nwPoint.x + tile.width || ymax < nwPoint.y || ymin > nwPoint.y + tile.height) {
//if (xmax < nwPoint.x - tile.width || xmin > nwPoint.x + tile.width*2 || ymax < nwPoint.y - tile.height || ymin > nwPoint.y + tile.height*2) {
continue;
}
//else {
//console.log(data.features[i].properties.admin, [point, point2], nwPoint);
drawTile(tile, ctx, coords, i, nwPoint);
//}
/*
if (data.features[i].properties.bounds[0][1] < xmin || data.features[i].properties.bounds[0][0] > xmax || data.features[i].properties.bounds[1][1] < ymin || data.features[i].properties.bounds[1][0] > ymax) {
continue;
}
else {
console.log(bounds, data.features[i].properties.admin, data.features[i].properties.bounds);
}
*/
}
ctx.lineWidth = 0.2;
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;
}
tiles.addTo(map);
function drawTile(tile, ctx, coords, i, nwPoint) {
ctx.strokeStyle = 'red';
ctx.fillStyle = 'white';
ctx.beginPath();
var tolerance = 10.0;
if (data.features[i].geometry.type == "MultiPolygon") {
for (var j=0; j<data.features[i].geometry.coordinates.length; j++) {
var polygon = [];
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);
polygon.push(point.x);
polygon.push(point.y);
polygon.push(0);
//if (l==0) {
// ctx.moveTo(point.x - nwPoint.x, point.y - nwPoint.y);
//}
//else {
// ctx.lineTo(point.x - nwPoint.x, point.y - nwPoint.y);
//}
}
}
var last = polygon.length - 3;
polygon[2] = 1;
simplify(polygon, 0, last, tolerance);
polygon[last + 2] = 1;
for (var n = 0; n < polygon.length; n += 3) {
if (n==0) {
ctx.moveTo(polygon[n] - nwPoint.x, polygon[n+1] - nwPoint.y);
}
else {
ctx.lineTo(polygon[n] - nwPoint.x, polygon[n+1] - nwPoint.y);
}
}
}
}
else if (data.features[i].geometry.type == "Polygon") {
var 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);
polygon.push(point.x);
polygon.push(point.y);
polygon.push(0);
//if (k==0) {
// ctx.moveTo(point.x - nwPoint.x, point.y - nwPoint.y);
//}
//else {
// ctx.lineTo(point.x - nwPoint.x, point.y - nwPoint.y);
//}
}
var last = polygon.length - 3;
polygon[2] = 1;
simplify(polygon, 0, last, tolerance);
polygon[last + 2] = 1;
for (var n = 0; n < polygon.length; n += 3) {
if (n==0) {
ctx.moveTo(polygon[n] - nwPoint.x, polygon[n+1] - nwPoint.y);
}
else {
ctx.lineTo(polygon[n] - nwPoint.x, polygon[n+1] - nwPoint.y);
}
}
}
}
ctx.closePath();
ctx.stroke();
ctx.fill();
}
function simplify(coords, first, last, sqTolerance) {
var maxSqDist = sqTolerance;
var mid = (last - first) >> 1;
var minPosToMid = last - first;
var index;
var ax = coords[first];
var ay = coords[first + 1];
var bx = coords[last];
var by = coords[last + 1];
for (var i = first + 3; i < last; i += 3) {
var d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
if (d > maxSqDist) {
index = i;
maxSqDist = d;
} else if (d === maxSqDist) {
// a workaround to ensure we choose a pivot close to the middle of the list,
// reducing recursion depth, for certain degenerate inputs
// https://github.com/mapbox/geojson-vt/issues/104
var posToMid = Math.abs(i - mid);
if (posToMid < minPosToMid) {
index = i;
minPosToMid = posToMid;
}
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 3) simplify(coords, first, index, sqTolerance);
coords[index + 2] = maxSqDist;
if (last - index > 3) simplify(coords, index, last, sqTolerance);
}
}
// square distance from a point to a segment
function getSqSegDist(px, py, x, y, bx, by) {
var dx = bx - x;
var dy = by - y;
if (dx !== 0 || dy !== 0) {
var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = bx;
y = by;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = px - x;
dy = py - y;
return dx * dx + dy * dy;
}
}
</script>
</body>
</html>