mapping dom
mapping dom

Reputation: 1955

Dynamically set folium zoom and postion to geopandas dataframe extent

I have a geopandas dataframe containing many polygons.

I wish to plot these in a folium map and have that map set the zoom and extent dynamically to fit all polygons onto the map.

I've tried the following

bounds = df.total_bounds
m.fit_bounds(bounds)
m

But get TypeError: Object of type ndarray is not JSON serializable

Upvotes: 0

Views: 1423

Answers (2)

Herbert
Herbert

Reputation: 5645

Slightly simpler and more readable:

bounds = df.total_bounds.tolist()
m.fit_bounds([bounds[:2][::-1], bounds[2:][::-1]])
m

Upvotes: 0

swatchai
swatchai

Reputation: 18812

Try this syntax:-

m.fit_bounds([[bounds[0],bounds[1]], [bounds[2],bounds[3]]])

Upvotes: 0

Related Questions