forked from unicef-polymer/etools-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-layer.html
539 lines (465 loc) · 12.5 KB
/
leaflet-layer.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
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
<link rel="import" href="leaflet-core.html">
<!--
Abstract element representing ILayer (<a href="http://leafletjs.com/reference.html#ilayer">Leaflet Reference</a>).
-->
<script>
'use strict';
window.leafletMap = window.leafletMap || {};
window.leafletMap.Mixins = window.leafletMap.Mixins || {};
leafletMap.Mixins.LeafletILayer = Polymer.dedupingMixin((baseClass) => class extends baseClass {
/**
* returns true.
*
* @method isLayer
*/
isLayer() {
return true;
}
});
leafletMap.Mixins.LeafletTileLayer = Polymer.dedupingMixin((baseClass) => class extends baseClass {
/**
* Fired when the tile layer starts loading tiles.
*
* @event loading
*/
/**
* Fired when the tile layer loaded all visible tiles.
*
* @event load
*/
/**
* Fired when a tile is requested and starts loading.
*
* @event tileloadstart
* @type TileEvent
* @param {HTMLElement} tile The tile element (image).
* @param {string} url The source URL of the tile.
*/
/**
* Fired when a tile loads.
*
* @event tileload
* @type TileEvent
* @param {HTMLElement} tile The tile element (image).
* @param {string} url The source URL of the tile.
*/
/**
* Fired when a tile is removed (e.g. when you have unloadInvisibleTiles on).
*
* @event tileunload
* @type TileEvent
* @param {HTMLElement} tile The tile element (image).
* @param {string} url The source URL of the tile.
*/
/**
* The `url` attribute sets the address template for tilesets.
*
* 'http://{s}.somedomain.com/blabla/{z}/{x}/{y}.png'
*
* {s} means one of the available subdomains (used sequentially to help with
* browser parallel requests per domain limitation; subdomain values are specified
* in options; a, b or c by default, can be omitted), {z} — zoom level, {x} and {y}
* — tile coordinates.
*
* @attribute url
* @type string
*/
static get properties() {
return {
/**
* The `min-zoom` attribute sets the minimum zoom number.
*
* @attribute min-zoom
* @type number
*/
minZoom: {
type: Number,
value: 0
},
/**
* The `max-zoom` attribute sets the maximum zoom number.
*
* @attribute max-zoom
* @type number
*/
maxZoom: {
type: Number,
value: 18
},
/**
* The `maxnativezoom` attribute sets the maximum zoom number the tiles source has available. If it is specified, the tiles on all zoom levels higher than maxNativeZoom will be loaded from maxZoom level and auto-scaled.
*
* @attribute maxnativezoom
* @type number
*/
maxNativeZoom: {
type: Number,
value: null
},
/**
* The `tile-size` attribute sets the tile size (width and height in pixels, assuming tiles are square).
*
* @attribute tile-size
* @type number
*/
tileSize: {
type: Number,
value: 256
},
/**
* The `subdomains` attribute sets the subdomains of the tile service. Can be passed in the form of one string (where each letter is a subdomain name) or an array of strings.
*
* @attribute subdomains
* @type string or string[]
*/
subdomains: {
type: String,
value: 'abc'
},
/**
* The `error-tile-url` attribute sets the URL to the tile image to show in place of the tile that failed to load.
*
* @attribute error-tile-url
* @type string
*/
errorTileUrl: {
type: String,
value: ''
},
/**
* The `attribution` attribute sets the attribute. As html code needs to be escaped here, it is preferable to define it as child element.
*
* @attribute attribution
* @type
*/
attribution: {
type: String,
value: ''
},
/**
* The `tms` attribute sets wether inverses Y axis numbering for tiles should be used (turn this on for TMS services).
*
* @attribute tms
* @type bool
*/
tms: {
type: Number,
value: false
},
/**
* The `continuous-world` attribute sets the wether tile coordinates won't be wrapped by world width (-180 to 180 longitude) or clamped to lie within world height (-90 to 90). Use this if you use Leaflet for maps that don't reflect the real world (e.g. game, indoor or photo maps).
*
* @attribute continuous-world
* @type bool
*/
continuousWorld: {
type: Boolean,
value: false
},
/**
* The `nowrap` attribute sets wether the tiles just won't load outside the world width (-180 to 180 longitude) instead of repeating.
*
* @attribute nowrap
* @type bool
*/
noWrap: {
type: Boolean,
value: false
},
/**
* The `zoom-offset` attribute sets the zoom number used in tile URLs will be offset with this value.
*
* @attribute zoom-offset
* @type number
*/
zoomOffset: {
type: Number,
value: 0
},
/**
* The `zoom-reverse` attribute sets whether the zoom number used in tile URLs will be reversed (maxZoom - zoom instead of zoom)
*
* @attribute zoom-reverse
* @type bool
*/
zoomReverse: {
type: Boolean,
value: false
},
/**
* The `opacity` attribute sets the opacity of the tile layer.
*
* @attribute opacity
* @type number
*/
opacity: {
type: Number,
value: 1.0,
observer: '_opacityChanged'
},
/**
* The `z-index` attribute sets the explicit zIndex of the tile layer. Not set by default.
*
* @attribute z-index
* @type number
*/
zIndex: {
type: Number,
value: null,
observer: '_zIndexChanged'
},
/**
* The `detect-retina` attribute sets whether if user is on a retina display, it will request four tiles of half the specified size and a bigger zoom level in place of one to utilize the high resolution.
*
* @attribute detect-retina
* @type bool
*/
detectRetina: {
type: Boolean,
value: false
},
/**
* The `reuse-tiles` attribute sets whether all the tiles that are not visible after panning are placed in a reuse queue from which they will be fetched when new tiles become visible (as opposed to dynamically creating new ones). This will in theory keep memory usage low and eliminate the need for reserving new memory whenever a new tile is needed.
*
* @attribute reuse-tiles
* @type bool
*/
reuseTiles: {
type: Boolean,
value: false
} };
}
// unloadInvisibleTiles updateWhenIdle bounds
_opacityChanged() {
if (this.layer) {
this.layer.setOpacity(this.opacity);
}
}
_zIndexChanged() {
if (this.layer) {
this.layer.setZIndex(this.zIndex);
}
}
disconnectedCallback() {
if (this.container && this.layer) {
this.container.removeLayer(this.layer);
}
}
});
</script>
<!--
element which defines a tile layer (<a href="http://leafletjs.com/reference.html#tilelayer">Leaflet Reference</a>).
##### Example
<leaflet-tilelayer
url="https://stendhalgame.org/map/3/{z}-{x}-{y}.png"
minzoom="2" maxzoom="6" nowrap>
Map source: <a href="https://stendhalgame.org">Stendhal MMORPG</a>
</leaflet-tilelayer>
@element leaflet-tilelayer
@blurb element which defines a tile layer. The content of the leaflet-tilelayer is used as attribution.
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
@homepage https://leaflet-extras.github.io/leaflet-map/
-->
<dom-module id="leaflet-tilelayer">
<style>
:host {display: none;}
</style>
<template>
</template>
<script>
'use strict';
const tileLayerMixin = leafletMap.Mixins.LeafletILayer(
leafletMap.Mixins.LeafletTileLayer(Polymer.Element)
);
class LeafletTilelayer extends tileLayerMixin {
static get is() {return 'leaflet-tilelayer';}
static get properties() {
return {
url: {
type: String,
value: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
observer: '_urlChanged'
},
container: {
type: Object,
observer: '_containerChanged'
} };
}
_containerChanged() {
if (this.container) {
var cleanedUrl = this.url.replace(new RegExp('%7B([sxyz])%7D', 'g'), '{$1}');
var layer = L.tileLayer(cleanedUrl, {
attribution: Polymer.dom(this).innerHTML + this.attribution,
minZoom: this.minZoom,
maxZoom: this.maxZoom,
maxNativeZoom: this.maxNativeZoom,
tileSize: this.tileSize,
subdomains: this.subdomains,
errorTileUrl: this.errorTileUrl,
tms: this.tms,
continuousWorld: this.continuousWorld,
noWrap: this.noWrap,
zoomOffset: this.zoomOffset,
zoomReverse: this.zoomReverse,
opacity: this.opacity,
zIndex: this.zIndex,
detectRetina: this.detectRetina,
reuseTiles: this.reuseTiles
});
this.layer = layer;
// forward events
layer.on('loading load tileloadstart tileload tileunload', function(e) {
this.dispatchEvent(new CustomEvent(e.type, e));
}, this);
this.layer.addTo(this.container);
}
}
_urlChanged() {
if (this.layer) {
var cleanedUrl = this.url.replace(new RegExp('%7B([sxyz])%7D', 'g'), '{$1}');
this.layer.setUrl(cleanedUrl);
}
}
}
window.customElements.define(LeafletTilelayer.is, LeafletTilelayer);
</script>
</dom-module>
<!--
element which defines a tile layer for wms (<a href="http://leafletjs.com/reference.html#tilelayer-wms">Leaflet Reference</a>).
##### Example
<leaflet-tilelayer-wms
url="https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi"
layers="nexrad-n0r-900913" format="image/png" transparent>
Weather data © 2012 IEM Nexrad
</leaflet-tilelayer-wms>
@element leaflet-tilelayer-wms
@blurb Element which defines a tile layer for wms. The content of the leaflet-tilelayer-wms is used as attribution. It inherits attributes and events from <leaflet-tilelayer>
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
@homepage https://leaflet-extras.github.io/leaflet-map/
@since 0.0.2
-->
<dom-module id="leaflet-tilelayer-wms">
<style>
:host {display: none;}
</style>
<template>
</template>
<script>
'use strict';
const tileWmsMixin = leafletMap.Mixins.LeafletILayer(
leafletMap.Mixins.LeafletTileLayer(Polymer.Element)
);
class LeafletTileLayerWms extends tileWmsMixin {
static get is() {return 'leaflet-tilelayer-wms';}
static get properties() {
return {
url: {
type: String,
observer: '_urlChanged'
},
/**
* The `layers` attribute sets the comma-separated list of WMS layers to show (required).
*
* @attribute layers
* @type string
*/
layers: {
type: String,
value: ''
},
/**
* The `styles` attribute sets the comma-separated list of WMS styles.
*
* @attribute styles
* @type string
*/
styles: {
type: String,
value: ''
},
/**
* The `format` attribute sets the WMS image format (use 'image/png' for layers with transparency).
*
* @attribute format
* @type string
*/
format: {
type: String,
value: 'image/jpeg'
},
/**
* The `transparent` attribute whether the WMS service will return images with transparency.
*
* @attribute transparent
* @type string
*/
transparent: {
type: Boolean,
value: false
},
/**
* The `version` attribute sets the version of the WMS service to use.
*
* @attribute version
* @type string
*/
version: {
type: String,
value: '1.1.1'
},
/**
* The `crs` attribute sets the coordinate Reference System to use for the WMS requests, defaults to map CRS. Don't change this if you're not sure what it means.
*
* @attribute crs
* @type string
*/
crs: Object,
container: {
type: Object,
observer: '_containerChanged'
} };
}
_containerChanged() {
if (this.container) {
var layer = L.tileLayer.wms(this.url, {
attribution: Polymer.dom(this).innerHTML + this.attribution,
minZoom: this.minZoom,
maxZoom: this.maxZoom,
maxNativeZoom: this.maxNativeZoom,
tileSize: this.tileSize,
subdomains: this.subdomains,
errorTileUrl: this.errorTileUrl,
tms: this.tms,
continuousWorld: this.continuousWorld,
noWrap: this.noWrap,
zoomOffset: this.zoomOffset,
zoomReverse: this.zoomReverse,
opacity: this.opacity,
zIndex: this.zIndex,
detectRetina: this.detectRetina,
reuseTiles: this.reuseTiles,
layers: this.layers,
styles: this.styles,
format: this.format,
transparent: this.transparent,
version: this.version,
crs: this.crs
});
this.layer = layer;
// forward events
layer.on('loading load tileloadstart tileload tileunload', function(e) {
this.dispatchEvent(new CustomEvent(e.type, e));
}, this);
this.layer.addTo(this.container);
}
}
_urlChanged() {
if (this.layer) {
this.layer.setUrl(this.url);
}
}
}
window.customElements.define(LeafletTileLayerWms.is, LeafletTileLayerWms);
</script>
</dom-module>
-