13 geopandas
Adding a GeoPandas GeoDataFrame to the map with a single line of code
Uncomment the following line to install leafmap if needed. You can also use conda to create a new environment to install geopandas and leafmap.
conda create geo -n python=3.8
conda activate geo
conda install geopandas
conda install mamba -c conda-forge
mamba install leafmap -c conda-forge
# !pip install geopandas
# !pip install leafmap
import leafmap
import geopandas as gpd
# leafmap.update_package()
Use GeoPandas to read a GeoJSON from an HTTP URL and return it as a GeoDataFrame.
Sample data: https://github.com/giswqs/leafmap/raw/master/examples/data/cable-geo.geojson
gdf = gpd.read_file(
"https://github.com/giswqs/leafmap/raw/master/examples/data/cable-geo.geojson"
)
--------------------------------------------------------------------------- HTTPError Traceback (most recent call last) Input In [4], in <cell line: 1>() ----> 1 gdf = gpd.read_file( 2 "https://github.com/giswqs/leafmap/raw/master/examples/data/cable-geo.geojson" 3 ) File ~/.local/lib/python3.9/site-packages/geopandas/io/file.py:170, in _read_file(filename, bbox, mask, rows, **kwargs) 167 filename = _expand_user(filename) 169 if _is_url(filename): --> 170 req = _urlopen(filename) 171 path_or_bytes = req.read() 172 reader = fiona.BytesCollection File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:214, in urlopen(url, data, timeout, cafile, capath, cadefault, context) 212 else: 213 opener = _opener --> 214 return opener.open(url, data, timeout) File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:523, in OpenerDirector.open(self, fullurl, data, timeout) 521 for processor in self.process_response.get(protocol, []): 522 meth = getattr(processor, meth_name) --> 523 response = meth(req, response) 525 return response File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:632, in HTTPErrorProcessor.http_response(self, request, response) 629 # According to RFC 2616, "2xx" code indicates that the client's 630 # request was successfully received, understood, and accepted. 631 if not (200 <= code < 300): --> 632 response = self.parent.error( 633 'http', request, response, code, msg, hdrs) 635 return response File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:561, in OpenerDirector.error(self, proto, *args) 559 if http_err: 560 args = (dict, 'default', 'http_error_default') + orig_args --> 561 return self._call_chain(*args) File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:494, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args) 492 for handler in handlers: 493 func = getattr(handler, meth_name) --> 494 result = func(*args) 495 if result is not None: 496 return result File /opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/urllib/request.py:641, in HTTPDefaultErrorHandler.http_error_default(self, req, fp, code, msg, hdrs) 640 def http_error_default(self, req, fp, code, msg, hdrs): --> 641 raise HTTPError(req.full_url, code, msg, hdrs, fp) HTTPError: HTTP Error 404: Not Found
Add a GeoDataFrame to the map.
m = leafmap.Map()
m.add_gdf(gdf, layer_name="Cable lines")
m
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [5], in <cell line: 2>() 1 m = leafmap.Map() ----> 2 m.add_gdf(gdf, layer_name="Cable lines") 3 m NameError: name 'gdf' is not defined
The following example requires the ipyleaflet plotting backend.
import leafmap.leafmap as leafmap # for ipyleaflet only
Read the GeoPandas sample dataset as a GeoDataFrame.
path_to_data = gpd.datasets.get_path("nybb")
gdf = gpd.read_file(path_to_data)
gdf
BoroCode | BoroName | Shape_Leng | Shape_Area | geometry | |
---|---|---|---|---|---|
0 | 5 | Staten Island | 330470.010332 | 1.623820e+09 | MULTIPOLYGON (((970217.022 145643.332, 970227.... |
1 | 4 | Queens | 896344.047763 | 3.045213e+09 | MULTIPOLYGON (((1029606.077 156073.814, 102957... |
2 | 3 | Brooklyn | 741080.523166 | 1.937479e+09 | MULTIPOLYGON (((1021176.479 151374.797, 102100... |
3 | 1 | Manhattan | 359299.096471 | 6.364715e+08 | MULTIPOLYGON (((981219.056 188655.316, 980940.... |
4 | 2 | Bronx | 464392.991824 | 1.186925e+09 | MULTIPOLYGON (((1012821.806 229228.265, 101278... |
Convert the GeoDataFrame to GeoJSON. Users can then use add_geojson()
to add the GeoJSON to the map. Alternatively, users can directly use the add_gdf()
function to add a GeoDataFrame to the map. Note you when hovering the mouse over the layer, an information box is shown at the lower right corner of the map. This feature is only available for the ipyleaflet plotting backend.
geojson = leafmap.gdf_to_geojson(gdf, epsg="4326")
# geojson
One can provide a list of colors (fill_colors
) to randomly fill the polygons.
m = leafmap.Map()
m.add_gdf(gdf, layer_name="New York boroughs", fill_colors=["red", "green", "blue"])
m