-
Notifications
You must be signed in to change notification settings - Fork 33
/
canvas.html
333 lines (307 loc) · 11.1 KB
/
canvas.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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@0.7.3/dist/leaflet.css"/>
<script src="https://unpkg.com/leaflet@0.7.3/dist/leaflet.js"></script>
<script src="./TileLayer.GeoJSON.js"></script>
<script src="./leaflet-hash.js"></script>
<style>
body {padding: 0; margin: 0}
html, body, #mapdiv {height: 100%; width: 100%;}
.leaflet-container {background: #fff;}
</style>
</head>
<body>
<div id="mapdiv">
<script>
function tilexy(tilePoint,zoom,dz,canvas_dx,canvas_dy,tlng,tlat){
var tileSize=256;
var tz = zoom;
if(dz>0){tileSize = 256*Math.pow(2, dz);tz = tz -dz;}
var initialResolution = 2 * Math.PI / tileSize;
var Resolution = initialResolution / (Math.pow(2, tz));
var originShift = 2 * Math.PI / 2.0;
var X = tlng * (Math.PI / 180) ;
var Y=Math.log(Math.tan(Math.PI/4 + tlat * (Math.PI / 180) / 2) );
var tx=(X + originShift)/(tileSize * Resolution);
var ty=(Y + originShift)/(tileSize * Resolution);
ty = Math.pow(2, tz) - ty;
tx=(tx-tilePoint.x)*tileSize+canvas_dx;
ty=(ty-tilePoint.y)*tileSize+canvas_dy;
return ([tx,ty]);
}
function cl_geturl(tilePoint,zoom,dz){
var url_x = "";
var url_y = "";
var url_a = [];
if(dz<0){
var num =Math.pow(2, Math.abs(dz));
for ( var i=0; i<num; i++ ){
url_x = i+Math.floor(tilePoint.x / Math.pow(2, dz));
for ( var ii=0; ii<num; ii++ ){
url_y = ii+Math.floor(tilePoint.y / Math.pow(2, dz));
url_a.push("/"+url_x+"/"+url_y+".geojson");
}
}
}else{
url_x = tilePoint.x;
url_y = tilePoint.y;
url_a.push("/"+url_x+"/"+url_y+".geojson");
}
return url_a;
}
function featsearch(layerdata,tilePoint,zoom,dz,canvas,canvas_dx,canvas_dy,urlid){
if ( !layerdata ) return;
var ctx = canvas.getContext('2d');
for ( var i=0; i<layerdata.length; i++ ){
var s;
switch (urlid){
case 'experimental_rdcl':
s = rdcl_style(layerdata[i]);
break;
case 'experimental_railcl':
s = railcl_style(layerdata[i]);
break;
case 'experimental_rvrcl':
s = rvrcl_style(layerdata[i]);
break;
}
for ( var a in s){
if(a=='setLineDash'){
if(ctx.setLineDash!==undefined){
ctx.setLineDash(s[a]);
}
continue;
}
ctx[a] = s[a];
}
ctx.beginPath();
for ( var ii=0; ii<layerdata[i].geometry.coordinates.length; ii++ ){
var test = tilexy(tilePoint,zoom,dz,canvas_dx,canvas_dy,layerdata[i].geometry.coordinates[ii][0],layerdata[i].geometry.coordinates[ii][1]);
if(ii==0){ctx.moveTo(test[0], test[1]);}else{ctx.lineTo(test[0], test[1]);}
}
ctx.stroke();
}
}
//道路中心線スタイル
function rdcl_style(feature){
if(feature.properties['rdCtg'] == "国道" || feature.properties['rdCtg'] == "都道府県道"){
return {setLineDash:[],strokeStyle:"#777777",lineWidth:5,globalAlpha:0.5,lineCap:"butt"};
}
else if(feature.properties['rdCtg'] == "高速自動車国道等"){
return {setLineDash:[],strokeStyle:"#ff1493",lineWidth:5,globalAlpha:0.5,lineCap:"butt"};
}
else{
if(feature.properties['type']=="通常部"){
return {setLineDash:[],strokeStyle:"#aaaaaa",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
}else{
return {setLineDash:[3,3],strokeStyle:"#aaaaaa",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
}
}
}
//道路中心線レイヤ
var rdcllayer = L.tileLayer.canvas({attribution:"道路中心線",minZoom:14,maxNativeZoom:16,maxZoom:18,async:true});
rdcllayer.drawTile = function(canvas, tilePoint, zoom) {
if(zoom>this.options.minZoom){
var urlid = "experimental_rdcl";
var dz = zoom - this.options.maxNativeZoom;
var canvas_dx = 20;
var canvas_dy = 20;
if(dz>0){
canvas.width=256*Math.pow(2, dz) + canvas_dx *2;
canvas.height=256*Math.pow(2, dz) + canvas_dy *2;
}else{
canvas.width=256 + canvas_dx *2;
canvas.height=256 + canvas_dy *2;
}
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, canvas.width, canvas.height);
this.tileDrawn(canvas);
var url_a = cl_geturl(tilePoint,zoom,dz);
var flag =0;
var that = this;
for ( var i=0; i<url_a.length; i++ ){
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(flag==0){
flag=1;
canvas.style.left = canvas.offsetLeft - canvas_dx + 'px';
canvas.style.top = canvas.offsetTop - canvas_dy + 'px';
}
var cl_json = JSON.parse(this.responseText);
featsearch(cl_json.features,tilePoint,zoom,dz,canvas,canvas_dx,canvas_dy,urlid);
that.tileDrawn(canvas);
};
xhr.open("get", 'https://cyberjapandata.gsi.go.jp/xyz/'+urlid+'/'+ this.options.maxNativeZoom +url_a[i], true);
xhr.send();
}
}else{
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, 256, 256);
this.tileDrawn(canvas);
}
}
//河川中心線スタイル
function rvrcl_style(feature){
switch (feature.properties['type']){
case '細河川(通常部)':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case '細河川(枯れ川部)':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case '河川中心線(通常部)':
return {setLineDash:[],strokeStyle:"#00f",lineWidth:4,globalAlpha:0.5,lineCap:"butt"};
case '河川中心線(枯れ川部)':
return {setLineDash:[],strokeStyle:"#00f",lineWidth:4,globalAlpha:0.5,lineCap:"butt"};
case '人工水路(空間)':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case '人工水路(地下)':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case '用水路':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case 'その他':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
case '不明':
return {setLineDash:[],strokeStyle:"#77f",lineWidth:2,globalAlpha:0.5,lineCap:"butt"};
}
}
//河川中心線レイヤ
var rvrcllayer = L.tileLayer.canvas({attribution:"河川中心線",minZoom:14,maxNativeZoom:16,maxZoom:18,async:true});
rvrcllayer.drawTile = function(canvas, tilePoint, zoom) {
if(zoom>this.options.minZoom){
var urlid = "experimental_rvrcl";
var dz = zoom - this.options.maxNativeZoom;
var canvas_dx = 20;
var canvas_dy = 20;
if(dz>0){
canvas.width=256*Math.pow(2, dz) + canvas_dx *2;
canvas.height=256*Math.pow(2, dz) + canvas_dy *2;
}else{
canvas.width=256 + canvas_dx *2;
canvas.height=256 + canvas_dy *2;
}
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, canvas.width, canvas.height);
this.tileDrawn(canvas);
var url_a = cl_geturl(tilePoint,zoom,dz);
var flag =0;
var that = this;
for ( var i=0; i<url_a.length; i++ ){
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(flag==0){
flag=1;
canvas.style.left = canvas.offsetLeft - canvas_dx + 'px';
canvas.style.top = canvas.offsetTop - canvas_dy + 'px';
}
var cl_json = JSON.parse(this.responseText);
featsearch(cl_json.features,tilePoint,zoom,dz,canvas,canvas_dx,canvas_dy,urlid);
that.tileDrawn(canvas);
};
xhr.open("get", 'https://cyberjapandata.gsi.go.jp/xyz/'+urlid+'/'+ this.options.maxNativeZoom +url_a[i], true);
xhr.send();
}
}else{
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, 256, 256);
this.tileDrawn(canvas);
}
}
//鉄道中心線スタイル
function railcl_style(feature){
if(feature.properties['snglDbl']=="駅部分"){
return {setLineDash:[],strokeStyle:"#ff0",lineWidth:10,globalAlpha:0.5,lineCap:"butt"};
}else{
return {setLineDash:[],strokeStyle:"#0f0",lineWidth:5,globalAlpha:0.5,lineCap:"butt"};
}
}
//鉄道中心線レイヤ
var railcllayer = L.tileLayer.canvas({attribution:"鉄道中心線",minZoom:14,maxNativeZoom:16,maxZoom:18,async:true});
railcllayer.drawTile = function(canvas, tilePoint, zoom) {
if(zoom>this.options.minZoom){
var urlid = "experimental_railcl";
var dz = zoom - this.options.maxNativeZoom;
var canvas_dx = 20;
var canvas_dy = 20;
if(dz>0){
canvas.width=256*Math.pow(2, dz) + canvas_dx *2;
canvas.height=256*Math.pow(2, dz) + canvas_dy *2;
}else{
canvas.width=256 + canvas_dx *2;
canvas.height=256 + canvas_dy *2;
}
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, canvas.width, canvas.height);
this.tileDrawn(canvas);
var url_a = cl_geturl(tilePoint,zoom,dz);
var flag =0;
var that = this;
for ( var i=0; i<url_a.length; i++ ){
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(flag==0){
flag=1;
canvas.style.left = canvas.offsetLeft - canvas_dx + 'px';
canvas.style.top = canvas.offsetTop - canvas_dy + 'px';
}
var cl_json = JSON.parse(this.responseText);
featsearch(cl_json.features,tilePoint,zoom,dz,canvas,canvas_dx,canvas_dy,urlid);
that.tileDrawn(canvas);
};
xhr.open("get", 'https://cyberjapandata.gsi.go.jp/xyz/'+urlid+'/'+ this.options.maxNativeZoom +url_a[i], true);
xhr.send();
}
}else{
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.globalAlpha=0;
ctx.fillRect(0, 0, 256, 256);
this.tileDrawn(canvas);
}
}
//注記
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://cyberjapandata.gsi.go.jp/xyz/experimental_anno/style.js', false);
xhr.send(null);
var stylejs = eval( "(" + xhr.responseText + ")" );
var annolayer = new L.TileLayer.GeoJSON(
'https://cyberjapandata.gsi.go.jp/xyz/experimental_anno/{z}/{x}/{y}.geojson',
stylejs.options,stylejs.geojsonOptions);
var std = L.tileLayer(
'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png',
{attribution: "地理院タイル(標準地図)",
maxNativeZoom: 18, maxZoom: 18, opacity:1});
var ort = L.tileLayer(
'https://cyberjapandata.gsi.go.jp/xyz/ort/{z}/{x}/{y}.jpg',
{attribution: "地理院タイル(オルソ)",
maxNativeZoom: 17, maxZoom: 18, opacity:0.7});
var blank = L.tileLayer(
'https://cyberjapandata.gsi.go.jp/xyz/blank/{z}/{x}/{y}.png',
{attribution: "地理院タイル(白地図)",
maxNativeZoom: 14, maxZoom: 18, opacity:1});
var GSI = {
GLOBALS : {}
};
GSI.GLOBALS.map = L.map('mapdiv', {
center: [35.705698,139.957527], zoom: 16,minZoom: 2, maxZoom: 18,
layers: [rvrcllayer,rdcllayer,railcllayer,annolayer]});
L.control.scale({imperial: false}).addTo(GSI.GLOBALS.map);
//var baseLayers = {"地理院タイル(標準地図)": std,"地理院タイル(オルソ)": ort,"地理院タイル(白地図)": blank};
var baseLayers ={};
var overlays = {"地理院タイル(標準地図)": std,"地理院タイル(オルソ)": ort,"地理院タイル(白地図)": blank,'地図情報(河川中心線)': rvrcllayer,'地図情報(道路中心線)': rdcllayer,'地図情報(鉄道中心線)': railcllayer,'地図情報(注記)': annolayer};
L.control.layers(baseLayers, overlays,{position:'topright',collapsed:false}).addTo(GSI.GLOBALS.map);
var hash = L.hash(GSI.GLOBALS.map);
</script>
</body>
</html>