16 heremap
Using HERE Map Widget for Jupyter as a plotting backend
Uncomment the following line to install leafmap if needed.
# !pip install leafmap
Prerequisites¶
Before you run the below cells make sure you have:
- A HERE developer account, free and available under HERE Developer Portal
- An API key from the HERE Developer Portal
Export API key into environment variable
HEREMAPS_API_KEY
export HEREMAPS_API_KEY=YOUR-ACTUAL-API-KEY
import os
import leafmap.heremap as leafmap
# Read api_key from environment
api_key = os.environ["HEREMAPS_API_KEY"]
HERE default basemap¶
Create an interactive map.
m = leafmap.Map(api_key=api_key)
m
Specify the default map center and zoom level.
m = leafmap.Map(api_key=api_key, center=[50, 19], zoom=4) # center=[lat, lon]
m
Set the visibility of map controls.
m = leafmap.Map(api_key=api_key, fullscreen_control=False)
m
Change the map width and height.
m = leafmap.Map(api_key=api_key, height="450px")
m
Basemaps¶
Use built-in basemaps.
m = leafmap.Map(api_key=api_key, basemap="HERE_RASTER_TERRAIN_MAP")
m
zoom to bounds¶
Zoom to map to a bounding box [South, West, North, East].
m.zoom_to_bounds((-9.0882278, -55.3228175, 168.2249543, 72.2460938)) #
m.add_basemap(basemap="Esri.WorldTopoMap")
Add a custom XYZ tile layer.
m = leafmap.Map(api_key=api_key, layers_control=True)
m.add_tile_layer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
name="Google Satellite",
attribution="Google",
)
m
Add vector data¶
How to add GeoJSON to the map
Add a GeoJSON from an HTTP URL to the map.
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2, layers_control=True)
in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")
m
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/requests/models.py:910, in Response.json(self, **kwargs) 909 try: --> 910 return complexjson.loads(self.text, **kwargs) 911 except JSONDecodeError as e: 912 # Catch JSON-related errors and raise as requests.JSONDecodeError 913 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File ~/.local/lib/python3.9/site-packages/simplejson/__init__.py:525, in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw) 521 if (cls is None and encoding is None and object_hook is None and 522 parse_int is None and parse_float is None and 523 parse_constant is None and object_pairs_hook is None 524 and not use_decimal and not kw): --> 525 return _default_decoder.decode(s) 526 if cls is None: File ~/.local/lib/python3.9/site-packages/simplejson/decoder.py:373, in JSONDecoder.decode(self, s, _w, _PY3) 372 if end != len(s): --> 373 raise JSONDecodeError("Extra data", s, end, len(s)) 374 return obj JSONDecodeError: Extra data: line 1 column 4 - line 1 column 15 (char 3 - 14) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/leafmap/heremap.py:214, in Map.add_geojson(self, in_geojson, layer_name, style, hover_style, style_callback, fill_colors, info_mode, point_style, default_popup) 213 if in_geojson.startswith("http"): --> 214 data = requests.get(in_geojson).json() 215 else: File ~/.local/lib/python3.9/site-packages/requests/models.py:917, in Response.json(self, **kwargs) 916 else: --> 917 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: [Errno Extra data] 404: Not Found: 3 During handling of the above exception, another exception occurred: Exception Traceback (most recent call last) Input In [12], in <cell line: 4>() 1 m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2, layers_control=True) 3 in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson' ----> 4 m.add_geojson(in_geojson, layer_name="Cable lines") 6 m File ~/.local/lib/python3.9/site-packages/leafmap/heremap.py:229, in Map.add_geojson(self, in_geojson, layer_name, style, hover_style, style_callback, fill_colors, info_mode, point_style, default_popup) 227 raise TypeError("The input geojson must be a type of str or dict.") 228 except Exception as e: --> 229 raise Exception(e) 231 if not style: 232 style = { 233 "strokeColor": "black", 234 "lineWidth": 1, 235 } Exception: [Errno Extra data] 404: Not Found: 3
Add a local GeoJSON file to the map.
import json
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2)
with open("../data/countries.geojson") as fh:
geo = json.load(fh)
m.add_geojson(geo, layer_name="Countries")
m
Customize style for the GeoJSON layer.
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
style = {
"fillColor": "rgba(0, 0, 255, 0.2)",
"strokeColor": "blue",
}
hover_style = {"fillColor": "rgba(0, 0, 255, 0.7)"}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
in_shp = '../data/countries.shp'
in_geojson = '../data/us-states.json'
in_kml = '../data/us-states.kml'
Add a shapefile to the map.
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2)
m.add_shp(in_shp, layer_name="Shapefile")
m
Add a KML file to the map.
m = leafmap.Map(api_key=api_key, center=[40.273502, -86.126976], zoom=4)
m.add_kml(in_kml, layer_name="KML")
m
The add_vector function supports any vector data format supported by GeoPandas.
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_vector(url, layer_name="Countries")
m
Point style for GeoJSON¶
Customize the style of point layers.
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=2)
url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson"
point_style = {
"strokeColor": 'white',
"lineWidth": 1,
"fillColor": "red",
"fillOpacity": 0.7,
"radius": 5,
}
m.add_geojson(url, layer_name="Countries", point_style=point_style, default_popup=True)
m
import geopandas
import json
import os
countries = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
point_style = {
"strokeColor": 'white',
"lineWidth": 1,
"fillColor": "blue",
"fillOpacity": 0.7,
"radius": 5,
}
m = leafmap.Map(api_key=api_key, center=[0, 0], zoom=3)
m.add_gdf(countries, zoom_to_layer=False, point_style=point_style, default_popup=True)
m