Skip to content

Make fit_bounds work with projected coordinate systems #2120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DrewScatterday opened this issue Mar 22, 2025 · 5 comments
Closed

Make fit_bounds work with projected coordinate systems #2120

DrewScatterday opened this issue Mar 22, 2025 · 5 comments

Comments

@DrewScatterday
Copy link

Is your feature request related to a problem? Please describe.
I was using folium and did not realize that the fit_bounds function requires 4326 points even when your data is in 3857 or other crs

Describe the solution you'd like
Allowing other wkids for the fit_bounds would be awesome

Describe alternatives you've considered
The workaround I'm using is just projecting my data to 4326 or projecting the bounds to 4326

Thanks for your hard work on Folium!

@hansthen
Copy link
Collaborator

Hi Drew,

Do you have some example code that works differently than you expect? Folium is a thin wrapper around Leaflet. From Folium, we just pass the fitBounds instruction to the leaflet map. I don't know very much about these coordinate reference systems, but you can initialize the map (and WMS tile layers on it) with a coordinate system. The default is 3857, however, so maybe something else is wrong.

@DrewScatterday
Copy link
Author

Hey @hansthen thanks for taking the time!

Here is a sample code that I used in a Jupyter notebook. Hopefully this paints a clearer picture:

import folium
import mapclassify
import matplotlib
import pandas as pd
import geopandas

data = {'city': ['A', 'B', 'C'], 
        'wkt_geometry': ['POINT (1 2)', 'LINESTRING (4 5, 6 7)', 'POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))']}
df = pd.DataFrame(data)
geometry = geopandas.GeoSeries.from_wkt(df['wkt_geometry'])
gdf = geopandas.GeoDataFrame(df, geometry=geometry, crs="EPSG:4326")

m = gdf.explore()
# custom bounds in 4326
xmin, ymin, xmax, ymax = -15.205078, -8.851576, 11.035296, 6.315299
m.fit_bounds([[ymin, xmin], [ymax, xmax]])
m

gdf2 = gdf.to_crs("EPSG:3857")
m2 = gdf2.explore()
# custom bounds in 3857 - would be nice for this to exist
xmin, ymin, xmax, ymax = -1692621.554347, -989296.091792, 1506726.701557, 830516.677622
m2.fit_bounds([[ymin, xmin], [ymax, xmax]])
m2

Ultimately I'm just nitpicking on how the fit_bounds function works. Reading the docs for it, it seems as it only allows points in 4326.

But if you have a scenario like this where you want a custom extent on the map of geometries, it forces you to use 4326 for the extent instead of the wkid that the dataframe is in. An easy workaround though is just projecting your extent points into 4326 using something pyproj, shapely, or other geometry libraries. Let me know if I'm missing something though!

@hansthen
Copy link
Collaborator

I will have a look at it in a few days. At first glance your code seems like it should work. Not sure if we can help you for the reasons I stated earlier. But we can always refile this issue with the Leaflet team.

@hansthen
Copy link
Collaborator

I checked it, but indeed it is not possible to specify coordinates in another coordinate system. It has to be a LatLng object. This is not something that we can fix, as it is a Leaflet limitation.

@DrewScatterday
Copy link
Author

DrewScatterday commented Mar 29, 2025

I checked it, but indeed it is not possible to specify coordinates in another coordinate system. It has to be a LatLng object. This is not something that we can fix, as it is a Leaflet limitation.

no worries! Thank you for looking into it. It might be worth adding a notes in the docs that calls this out. Something like "be sure to project your points into 4326 when using the fit_bounds function". But again I'm just nitpicking 😉 not a huge deal as you can easily work around it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants