forked from opengeos/leafmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasemaps.py
634 lines (552 loc) · 20.1 KB
/
basemaps.py
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"""Module for basemaps.
More WMS basemaps can be found at the following websites:
1. USGS National Map: https://viewer.nationalmap.gov/services
2. MRLC NLCD Land Cover data: https://www.mrlc.gov/data-services-page
3. FWS NWI Wetlands data: https://www.fws.gov/wetlands/Data/Web-Map-Services.html
"""
import collections
import os
import requests
import folium
import ipyleaflet
import xyzservices.providers as xyz
from .common import check_package, planet_tiles
# from box import Box
# Custom XYZ tile services.
xyz_tiles = {
"OpenStreetMap": {
"url": "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
"attribution": "OpenStreetMap",
"name": "OpenStreetMap",
},
"ROADMAP": {
"url": "https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
"attribution": "Google",
"name": "Google Maps",
},
"SATELLITE": {
"url": "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
"attribution": "Google",
"name": "Google Satellite",
},
"TERRAIN": {
"url": "https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}",
"attribution": "Google",
"name": "Google Terrain",
},
"HYBRID": {
"url": "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
"attribution": "Google",
"name": "Google Satellite",
},
}
# Custom WMS tile services.
wms_tiles = {
"FWS NWI Wetlands": {
"url": "https://www.fws.gov/wetlands/arcgis/services/Wetlands/MapServer/WMSServer?",
"layers": "1",
"name": "FWS NWI Wetlands",
"attribution": "FWS",
"format": "image/png",
"transparent": True,
},
"FWS NWI Wetlands Raster": {
"url": "https://www.fws.gov/wetlands/arcgis/services/Wetlands_Raster/ImageServer/WMSServer?",
"layers": "0",
"name": "FWS NWI Wetlands Raster",
"attribution": "FWS",
"format": "image/png",
"transparent": True,
},
"NLCD 2019 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?",
"layers": "NLCD_2019_Land_Cover_L48",
"name": "NLCD 2019 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2016 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?",
"layers": "NLCD_2016_Land_Cover_L48",
"name": "NLCD 2016 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2013 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2013_Land_Cover_L48/wms?",
"layers": "NLCD_2013_Land_Cover_L48",
"name": "NLCD 2013 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2011 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2011_Land_Cover_L48/wms?",
"layers": "NLCD_2011_Land_Cover_L48",
"name": "NLCD 2011 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2008 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2008_Land_Cover_L48/wms?",
"layers": "NLCD_2008_Land_Cover_L48",
"name": "NLCD 2008 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2006 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2006_Land_Cover_L48/wms?",
"layers": "NLCD_2006_Land_Cover_L48",
"name": "NLCD 2006 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2004 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2004_Land_Cover_L48/wms?",
"layers": "NLCD_2004_Land_Cover_L48",
"name": "NLCD 2004 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"NLCD 2001 CONUS Land Cover": {
"url": "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2001_Land_Cover_L48/wms?",
"layers": "NLCD_2001_Land_Cover_L48",
"name": "NLCD 2001 CONUS Land Cover",
"attribution": "MRLC",
"format": "image/png",
"transparent": True,
},
"USGS NAIP Imagery": {
"url": "https://imagery.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?",
"layers": "USGSNAIPImagery:NaturalColor",
"name": "USGS NAIP Imagery",
"attribution": "USGS",
"format": "image/png",
"transparent": True,
},
"USGS NAIP Imagery False Color": {
"url": "https://imagery.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?",
"layers": "USGSNAIPImagery:FalseColorComposite",
"name": "USGS NAIP Imagery False Color",
"attribution": "USGS",
"format": "image/png",
"transparent": True,
},
"USGS NAIP Imagery NDVI": {
"url": "https://imagery.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?",
"layers": "USGSNAIPImagery:NDVI_Color",
"name": "USGS NAIP Imagery NDVI",
"attribution": "USGS",
"format": "image/png",
"transparent": True,
},
"USGS Hydrography": {
"url": "https://basemap.nationalmap.gov/arcgis/services/USGSHydroCached/MapServer/WMSServer?",
"layers": "0",
"name": "USGS Hydrography",
"attribution": "USGS",
"format": "image/png",
"transparent": True,
},
"USGS 3DEP Elevation": {
"url": "https://elevation.nationalmap.gov/arcgis/services/3DEPElevation/ImageServer/WMSServer?",
"layers": "33DEPElevation:Hillshade Elevation Tinted",
"name": "USGS 3DEP Elevation",
"attribution": "USGS",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2020": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2020_MAP",
"name": "ESA Worldcover 2020",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2020 S2 FCC": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2020_S2_FCC",
"name": "ESA Worldcover 2020 S2 FCC",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2020 S2 TCC": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2020_S2_TCC",
"name": "ESA Worldcover 2020 S2 TCC",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2021": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2021_MAP",
"name": "ESA Worldcover 2021",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2021 S2 FCC": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2021_S2_FCC",
"name": "ESA Worldcover 2021 S2 FCC",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
"ESA WorldCover 2021 S2 TCC": {
"url": "https://services.terrascope.be/wms/v2",
"layers": "WORLDCOVER_2021_S2_TCC",
"name": "ESA Worldcover 2021 S2 TCC",
"attribution": "ESA",
"format": "image/png",
"transparent": True,
},
}
def _unpack_sub_parameters(var, param):
temp = var
for sub_param in param.split("."):
temp = getattr(temp, sub_param)
return temp
def get_xyz_dict(free_only=True):
"""Returns a dictionary of xyz services.
Args:
free_only (bool, optional): Whether to return only free xyz tile services that do not require an access token. Defaults to True.
Returns:
dict: A dictionary of xyz services.
"""
xyz_dict = {}
for item in xyz.values():
try:
name = item["name"]
tile = _unpack_sub_parameters(xyz, name)
if _unpack_sub_parameters(xyz, name).requires_token():
if free_only:
pass
else:
xyz_dict[name] = tile
else:
xyz_dict[name] = tile
except Exception:
for sub_item in item:
name = item[sub_item]["name"]
tile = _unpack_sub_parameters(xyz, name)
if _unpack_sub_parameters(xyz, name).requires_token():
if free_only:
pass
else:
xyz_dict[name] = tile
else:
xyz_dict[name] = tile
xyz_dict = collections.OrderedDict(sorted(xyz_dict.items()))
return xyz_dict
def xyz_to_leaflet():
"""Convert xyz tile services to ipyleaflet tile layers.
Returns:
dict: A dictionary of ipyleaflet tile layers.
"""
leaflet_dict = {}
for key in xyz_tiles:
name = xyz_tiles[key]["name"]
url = xyz_tiles[key]["url"]
attribution = xyz_tiles[key]["attribution"]
leaflet_dict[key] = ipyleaflet.TileLayer(
url=url, name=name, attribution=attribution, max_zoom=22
)
for key in wms_tiles:
name = wms_tiles[key]["name"]
url = wms_tiles[key]["url"]
layers = wms_tiles[key]["layers"]
fmt = wms_tiles[key]["format"]
transparent = wms_tiles[key]["transparent"]
attribution = wms_tiles[key]["attribution"]
leaflet_dict[key] = ipyleaflet.WMSLayer(
url=url,
layers=layers,
name=name,
attribution=attribution,
format=fmt,
transparent=transparent,
)
xyz_dict = get_xyz_dict()
for item in xyz_dict:
name = xyz_dict[item].name
url = xyz_dict[item].build_url()
attribution = xyz_dict[item].attribution
if "max_zoom" in xyz_dict[item].keys():
max_zoom = xyz_dict[item]["max_zoom"]
else:
max_zoom = 22
leaflet_dict[name] = ipyleaflet.TileLayer(
url=url, name=name, max_zoom=max_zoom, attribution=attribution
)
if os.environ.get("PLANET_API_KEY") is not None:
planet_dict = planet_tiles(tile_format="ipyleaflet")
leaflet_dict.update(planet_dict)
return leaflet_dict
def xyz_to_pydeck():
"""Convert xyz tile services to pydeck custom tile layers.
Returns:
dict: A dictionary of pydeck tile layers.
"""
check_package("pydeck", "https://deckgl.readthedocs.io/en/latest/installation.html")
import pydeck as pdk
pydeck_dict = {}
for key in xyz_tiles:
url = xyz_tiles[key]["url"]
pydeck_dict[key] = url
xyz_dict = get_xyz_dict()
for item in xyz_dict:
url = xyz_dict[item].build_url()
pydeck_dict[item] = url
if os.environ.get("PLANET_API_KEY") is not None:
planet_dict = planet_tiles(tile_format="ipyleaflet")
for tile in planet_dict:
pydeck_dict[tile] = planet_dict[tile].url
pdk.settings.custom_libraries = [
{
"libraryName": "MyTileLayerLibrary",
"resourceUri": "https://cdn.jsdelivr.net/gh/giswqs/pydeck_myTileLayer@master/dist/bundle.js",
}
]
for key in pydeck_dict:
pydeck_dict[key] = pdk.Layer("MyTileLayer", pydeck_dict[key], key)
return pydeck_dict
def xyz_to_folium():
"""Convert xyz tile services to folium tile layers.
Returns:
dict: A dictionary of folium tile layers.
"""
folium_dict = {}
for key in xyz_tiles:
name = xyz_tiles[key]["name"]
url = xyz_tiles[key]["url"]
attribution = xyz_tiles[key]["attribution"]
folium_dict[key] = folium.TileLayer(
tiles=url,
attr=attribution,
name=name,
overlay=True,
control=True,
max_zoom=22,
)
for key in wms_tiles:
name = wms_tiles[key]["name"]
url = wms_tiles[key]["url"]
layers = wms_tiles[key]["layers"]
fmt = wms_tiles[key]["format"]
transparent = wms_tiles[key]["transparent"]
attribution = wms_tiles[key]["attribution"]
folium_dict[key] = folium.WmsTileLayer(
url=url,
layers=layers,
name=name,
attr=attribution,
fmt=fmt,
transparent=transparent,
overlay=True,
control=True,
)
xyz_dict = get_xyz_dict()
for item in xyz_dict:
name = xyz_dict[item].name
url = xyz_dict[item].build_url()
attribution = xyz_dict[item].attribution
if "max_zoom" in xyz_dict[item].keys():
max_zoom = xyz_dict[item]["max_zoom"]
else:
max_zoom = 22
folium_dict[name] = folium.TileLayer(
tiles=url,
attr=attribution,
name=name,
max_zoom=max_zoom,
overlay=True,
control=True,
)
if os.environ.get("PLANET_API_KEY") is not None:
planet_dict = planet_tiles(tile_format="folium")
folium_dict.update(planet_dict)
return folium_dict
def xyz_to_heremap():
"""Convert xyz tile services to hermap tile layers.
Returns:
dict: A dictionary of heremap tile layers.
"""
try:
import here_map_widget
except ImportError:
raise ImportError(
'This module requires the hermap package. Please install it using "pip install here-map-widget-for-jupyter".'
)
# Built-in heremap tile services.
here_tiles = {
"HERE_RASTER_NORMAL_MAP": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.map
),
"HERE_RASTER_NORMAL_BASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.base
),
"HERE_RASTER_NORMAL_BASE_NIGHT": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.basenight
),
"HERE_RASTER_NORMAL_LABELS": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.labels
),
"HERE_RASTER_NORMAL_TRANSIT": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.transit
),
"HERE_RASTER_NORMAL_XBASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.xbase
),
"HERE_RASTER_NORMAL_XBASE_NIGHT": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.normal.xbasenight
),
"HERE_RASTER_SATELLITE_MAP": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.satellite.map
),
"HERE_RASTER_SATELLITE_LABELS": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.satellite.labels
),
"HERE_RASTER_SATELLITE_BASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.satellite.base
),
"HERE_RASTER_SATELLITE_XBASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.satellite.xbase
),
"HERE_RASTER_TERRAIN_MAP": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.terrain.map
),
"HERE_RASTER_TERRAIN_LABELS": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.terrain.labels
),
"HERE_RASTER_TERRAIN_BASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.terrain.base
),
"HERE_RASTER_TERRAIN_XBASE": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.raster.terrain.xbase
),
"HERE_VECTOR_NORMAL_MAP": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.vector.normal.map
),
"HERE_VECTOR_NORMAL_TRUCK": here_map_widget.DefaultLayers(
layer_name=here_map_widget.DefaultLayerNames.vector.normal.truck
),
}
heremap_dict = {}
for key in xyz_tiles:
name = xyz_tiles[key]["name"]
url = xyz_tiles[key]["url"]
attribution = xyz_tiles[key]["attribution"]
heremap_dict[key] = here_map_widget.TileLayer(
provider=here_map_widget.ImageTileProvider(
url=url, attribution=attribution, name=name
)
)
xyz_dict = get_xyz_dict()
for item in xyz_dict:
name = xyz_dict[item].name
url = xyz_dict[item].build_url()
attribution = xyz_dict[item].attribution
if "max_zoom" in xyz_dict[item].keys():
max_zoom = xyz_dict[item]["max_zoom"]
else:
max_zoom = 22
heremap_dict[name] = here_map_widget.TileLayer(
provider=here_map_widget.ImageTileProvider(
url=url, attribution=attribution, name=name, max_zoom=max_zoom
)
)
heremap_dict.update(here_tiles)
return heremap_dict
def xyz_to_plotly():
"""Convert xyz tile services to plotly tile layers.
Returns:
dict: A dictionary of plotly tile layers.
"""
plotly_dict = {}
for key in xyz_tiles:
url = xyz_tiles[key]["url"]
attribution = xyz_tiles[key]["attribution"]
plotly_dict[key] = {
"below": "traces",
"sourcetype": "raster",
"sourceattribution": attribution,
"source": [url],
"name": key,
}
xyz_dict = get_xyz_dict()
for item in xyz_dict:
name = xyz_dict[item].name
url = xyz_dict[item].build_url()
attribution = xyz_dict[item].attribution
plotly_dict[name] = {
"below": "traces",
"sourcetype": "raster",
"sourceattribution": attribution,
"source": [url],
"name": name,
}
return plotly_dict
def xyz_to_bokeh():
"""Convert xyz tile services to bokeh tile layers.
Returns:
dict: A dictionary of bokeh tile layers.
"""
from bokeh.models import WMTSTileSource
bokeh_dict = {}
for key in xyz_tiles:
url = xyz_tiles[key]["url"]
attribution = xyz_tiles[key]["attribution"]
tile_options = {
"url": url,
"attribution": attribution,
}
bokeh_dict[key] = WMTSTileSource(**tile_options)
xyz_dict = get_xyz_dict()
for item in xyz_dict:
url = xyz_dict[item].build_url()
attribution = xyz_dict[item].attribution
key = xyz_dict[item].name
tile_options = {
"url": url,
"attribution": attribution,
}
bokeh_dict[key] = WMTSTileSource(**tile_options)
return bokeh_dict
def search_qms(keywords, limit=10):
"""Search qms files for keywords. Reference: https://github.com/geopandas/xyzservices/issues/65
Args:
keywords (str): Keywords to search for.
limit (int): Number of results to return.
"""
QMS_API = "https://qms.nextgis.com/api/v1/geoservices"
services = requests.get(
f"{QMS_API}/?search={keywords}&type=tms&epsg=3857&limit={str(limit)}"
)
services = services.json()
if services["count"] == 0:
return None
elif services["count"] <= limit:
return services["results"]
else:
return services["results"][:limit]
def get_qms(service_id):
QMS_API = "https://qms.nextgis.com/api/v1/geoservices"
service_details = requests.get(f"{QMS_API}/{service_id}")
return service_details.json()
def qms_to_leafmap(service_id):
service_details = get_qms(service_id)
name = service_details["name"]
url = service_details["url"]
attribution = service_details["copyright_text"]
layer = ipyleaflet.TileLayer(url=url, name=name, attribution=attribution)
return layer