1

folium.GeoJson displays all the counties of New York State:

import geopandas as gpd
gdf = gpd.read_file("Counties_Shoreline.shp")
m = folium.Map(location=[43.062776, -75.420884], tiles="cartodbpositron", zoom_start=7)
folium.GeoJson(data=gdf["geometry"]).add_to(m)
m

However, it fails to display a single county, outputting a map without borders when only the first row data, gdf["geometry"][0], is used:

m = folium.Map(location=[43.062776, -75.420884], tiles='cartodbpositron', zoom_start=7)
folium.GeoJson(data=gdf["geometry"][0]).add_to(m)
m

This despite the fact that the polygon appears fine, rendering inside Jupyter Notebook.

Data here: http://gis.ny.gov/gisdata/fileserver/?DSID=927&file=NYS_Civil_Boundaries.shp.zip Shapefile: Counties_Shoreline.shp

NYS counties One county shapefile, displayed in Jupyter Notebook

4

Because gdf["geometry"]) is a GeoSerie and gdf["geometry"][0] is a shapely geometry (the geometry column is a list of shapely geometries).

The type of data

data=gdf["geometry"]
print(type(data)) 
<class 'geopandas.geoseries.GeoSeries'>
data= gdf["geometry"][0]
print(data)
<class 'shapely.geometry.polygon.Polygon'>

The GeoDataFrame has a projection

print(gdf.crs)
epsg:26918

but the shapely geometry not and Impossible to draw GeoJSON that has crs other than 4326

A solution is to change de projection of the GeoDataFrame to EPSG:4326 (WSG84)

gdf = gdf.to_crs("EPSG:4326") 
print(gdf.crs)
EPSG:4326
m = folium.Map(location=[43.062776, -75.420884],tiles="cartodbpositron", zoom_start=7)
folium.GeoJson(data=gdf["geometry"]).add_to(m) 
m

enter image description here

And

m = folium.Map(location=[43.062776, -75.420884], tiles='cartodbpositron', zoom_start=7)
folium.GeoJson(data=gdf["geometry"][0]).add_to(m)
m

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.