Adding data to a map
Adding different data types to a map:
In [1]:
Copied!
#pip install maplab
#pip install maplab
In [2]:
Copied!
import maplab
import maplab
Shapefiles
In [3]:
Copied!
m = maplab.Map(center=(30, -60), zoom=3)
m.add_search_control()
m.add_draw_control()
m.add_layers_control()
m.add_fullscreen_control()
m.add_shp(data = "data/tl_2022_us_aiannh.shp")
m
m = maplab.Map(center=(30, -60), zoom=3)
m.add_search_control()
m.add_draw_control()
m.add_layers_control()
m.add_fullscreen_control()
m.add_shp(data = "data/tl_2022_us_aiannh.shp")
m
--------------------------------------------------------------------------- CPLE_OpenFailedError Traceback (most recent call last) File fiona/ogrext.pyx:136, in fiona.ogrext.gdal_open_vector() File fiona/_err.pyx:291, in fiona._err.exc_wrap_pointer() CPLE_OpenFailedError: Unable to open data/tl_2022_us_aiannh.shx or data/tl_2022_us_aiannh.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it. During handling of the above exception, another exception occurred: DriverError Traceback (most recent call last) Cell In[3], line 6 4 m.add_layers_control() 5 m.add_fullscreen_control() ----> 6 m.add_shp(data = "data/tl_2022_us_aiannh.shp") 7 m File ~/.local/lib/python3.9/site-packages/maplab/maplab.py:210, in Map.add_shp(self, data, name, **kwargs) 197 """Adds a shapefile to the map. 198 199 Args: (...) 206 gdf.__geo_interface__: The shapefile layer. 207 """ 208 import geopandas as gpd --> 210 gdf = gpd.read_file(data) 211 geojson = gdf.__geo_interface__ 212 self.add_geojson(geojson, name=name, **kwargs) File ~/.local/lib/python3.9/site-packages/geopandas/io/file.py:248, in _read_file(filename, bbox, mask, rows, engine, **kwargs) 245 else: 246 path_or_bytes = filename --> 248 return _read_file_fiona( 249 path_or_bytes, from_bytes, bbox=bbox, mask=mask, rows=rows, **kwargs 250 ) 252 else: 253 raise ValueError(f"unknown engine '{engine}'") File ~/.local/lib/python3.9/site-packages/geopandas/io/file.py:289, in _read_file_fiona(path_or_bytes, from_bytes, bbox, mask, rows, where, **kwargs) 286 reader = fiona.open 288 with fiona_env(): --> 289 with reader(path_or_bytes, **kwargs) as features: 290 crs = features.crs_wkt 291 # attempt to get EPSG code File ~/.local/lib/python3.9/site-packages/fiona/env.py:457, in ensure_env_with_credentials.<locals>.wrapper(*args, **kwds) 454 session = DummySession() 456 with env_ctor(session=session): --> 457 return f(*args, **kwds) File ~/.local/lib/python3.9/site-packages/fiona/__init__.py:336, in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, allow_unsupported_drivers, **kwargs) 333 path = parse_path(fp) 335 if mode in ("a", "r"): --> 336 colxn = Collection( 337 path, 338 mode, 339 driver=driver, 340 encoding=encoding, 341 layer=layer, 342 enabled_drivers=enabled_drivers, 343 allow_unsupported_drivers=allow_unsupported_drivers, 344 **kwargs 345 ) 346 elif mode == "w": 347 colxn = Collection( 348 path, 349 mode, (...) 358 **kwargs 359 ) File ~/.local/lib/python3.9/site-packages/fiona/collection.py:243, in Collection.__init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, ignore_fields, ignore_geometry, include_fields, wkt_version, allow_unsupported_drivers, **kwargs) 241 if self.mode == "r": 242 self.session = Session() --> 243 self.session.start(self, **kwargs) 244 elif self.mode in ("a", "w"): 245 self.session = WritingSession() File fiona/ogrext.pyx:588, in fiona.ogrext.Session.start() File fiona/ogrext.pyx:143, in fiona.ogrext.gdal_open_vector() DriverError: Unable to open data/tl_2022_us_aiannh.shx or data/tl_2022_us_aiannh.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
Basemaps
In [4]:
Copied!
m = maplab.Map(center=(30, -60), zoom=3)
m
m = maplab.Map(center=(30, -60), zoom=3)
m
Out[4]:
In [5]:
Copied!
m.add_basemap(basemap='terrain')
m.add_basemap(basemap='terrain')
GeoJSON
In [6]:
Copied!
m.add_geojson(data = "")
m.add_geojson(data = "")
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) Cell In[6], line 1 ----> 1 m.add_geojson(data = "") File ~/.local/lib/python3.9/site-packages/maplab/maplab.py:190, in Map.add_geojson(self, data, **kwargs) 187 import json 189 if isinstance(data, str): --> 190 with open(data, "r") as f: 191 data = json.load(f) 193 geojson = ipyleaflet.GeoJSON(data=data, **kwargs) FileNotFoundError: [Errno 2] No such file or directory: ''
Any Vector Data
In [7]:
Copied!
m.add_vector('data/tl_2022_us_aiannh.shp')
m.add_vector('data/tl_2022_us_aiannh.shp')
--------------------------------------------------------------------------- CPLE_OpenFailedError Traceback (most recent call last) File fiona/ogrext.pyx:136, in fiona.ogrext.gdal_open_vector() File fiona/_err.pyx:291, in fiona._err.exc_wrap_pointer() CPLE_OpenFailedError: Unable to open data/tl_2022_us_aiannh.shx or data/tl_2022_us_aiannh.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it. During handling of the above exception, another exception occurred: DriverError Traceback (most recent call last) Cell In[7], line 1 ----> 1 m.add_vector('data/tl_2022_us_aiannh.shp') File ~/.local/lib/python3.9/site-packages/maplab/maplab.py:238, in Map.add_vector(self, data, name, **kwargs) 236 import geopandas as gpd 237 if data.endswith(".shp"): --> 238 self.add_shp(data, name=name, **kwargs) 239 if data.endswith(".geojson"): 240 self.add_geojson(data, name=name, **kwargs) File ~/.local/lib/python3.9/site-packages/maplab/maplab.py:210, in Map.add_shp(self, data, name, **kwargs) 197 """Adds a shapefile to the map. 198 199 Args: (...) 206 gdf.__geo_interface__: The shapefile layer. 207 """ 208 import geopandas as gpd --> 210 gdf = gpd.read_file(data) 211 geojson = gdf.__geo_interface__ 212 self.add_geojson(geojson, name=name, **kwargs) File ~/.local/lib/python3.9/site-packages/geopandas/io/file.py:248, in _read_file(filename, bbox, mask, rows, engine, **kwargs) 245 else: 246 path_or_bytes = filename --> 248 return _read_file_fiona( 249 path_or_bytes, from_bytes, bbox=bbox, mask=mask, rows=rows, **kwargs 250 ) 252 else: 253 raise ValueError(f"unknown engine '{engine}'") File ~/.local/lib/python3.9/site-packages/geopandas/io/file.py:289, in _read_file_fiona(path_or_bytes, from_bytes, bbox, mask, rows, where, **kwargs) 286 reader = fiona.open 288 with fiona_env(): --> 289 with reader(path_or_bytes, **kwargs) as features: 290 crs = features.crs_wkt 291 # attempt to get EPSG code File ~/.local/lib/python3.9/site-packages/fiona/env.py:457, in ensure_env_with_credentials.<locals>.wrapper(*args, **kwds) 454 session = DummySession() 456 with env_ctor(session=session): --> 457 return f(*args, **kwds) File ~/.local/lib/python3.9/site-packages/fiona/__init__.py:336, in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, allow_unsupported_drivers, **kwargs) 333 path = parse_path(fp) 335 if mode in ("a", "r"): --> 336 colxn = Collection( 337 path, 338 mode, 339 driver=driver, 340 encoding=encoding, 341 layer=layer, 342 enabled_drivers=enabled_drivers, 343 allow_unsupported_drivers=allow_unsupported_drivers, 344 **kwargs 345 ) 346 elif mode == "w": 347 colxn = Collection( 348 path, 349 mode, (...) 358 **kwargs 359 ) File ~/.local/lib/python3.9/site-packages/fiona/collection.py:243, in Collection.__init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, ignore_fields, ignore_geometry, include_fields, wkt_version, allow_unsupported_drivers, **kwargs) 241 if self.mode == "r": 242 self.session = Session() --> 243 self.session.start(self, **kwargs) 244 elif self.mode in ("a", "w"): 245 self.session = WritingSession() File fiona/ogrext.pyx:588, in fiona.ogrext.Session.start() File fiona/ogrext.pyx:143, in fiona.ogrext.gdal_open_vector() DriverError: Unable to open data/tl_2022_us_aiannh.shx or data/tl_2022_us_aiannh.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
Raster Data
In [8]:
Copied!
m = maplab.Map(zoom=1, center=(-20.2, 57.5))
url = 'https://opendata.digitalglobe.com/events/mauritius-oil-spill/post-event/2020-08-12/105001001F1B5B00/105001001F1B5B00.tif'
m.add_raster(url, name='Oil Spill')
m
m = maplab.Map(zoom=1, center=(-20.2, 57.5))
url = 'https://opendata.digitalglobe.com/events/mauritius-oil-spill/post-event/2020-08-12/105001001F1B5B00/105001001F1B5B00.tif'
m.add_raster(url, name='Oil Spill')
m
Out[8]:
Last update:
2023-05-11