-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
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 |
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! |
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. |
I checked it, but indeed it is not possible to specify coordinates in another coordinate system. It has to be a |
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 |
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!
The text was updated successfully, but these errors were encountered: