Skip to content
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

contextily compatible to basemap? #256

Open
a-ma72 opened this issue Oct 17, 2024 · 1 comment
Open

contextily compatible to basemap? #256

a-ma72 opened this issue Oct 17, 2024 · 1 comment

Comments

@a-ma72
Copy link

a-ma72 commented Oct 17, 2024

I try to add a map to coastlines drawn with basemap, (both sources use the the same WGS84 Web Mercator projection):

import contextily as cx
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap


# Map source
source = cx.providers.OpenStreetMap.Mapnik

# Plot
fig, ax = plt.subplots(figsize=(10, 10))

m = Basemap(
    projection="merc",  # Mercator-Projektion
    epsg=3857,          # WGS84 Web Mercator
    llcrnrlat=44.82,
    urcrnrlat=55.30,
    llcrnrlon=5.89,
    urcrnrlon=15.56,
    resolution="i",
    ax=ax,
)
m.drawcoastlines()

# Add context-basemap
cx.add_basemap(ax, crs="EPSG:3857", source=source)

# Show
plt.show()

but the maps doesn't match:

image

Is contextily compatible to basemap at all?

@martinfleis
Copy link
Member

contextily assumes that coordinates used in the Matplotlib object match the coordinates expected by the set CRS. I believe that Basemap is transforming those in some way that makes it different. For example, in the case of the code above:

box = ax.get_window_extent()
box.corners()

array([[285.66987562, 110.        ],
       [285.66987562, 880.        ],
       [739.33012438, 110.        ],
       [739.33012438, 880.        ]])

While contextily expects something like

geopandas.GeoSeries([shapely.box(5.89, 44.82, 15.56, 55.30)], crs=4326).to_crs(3857).total_bounds

array([ 655671.80077238, 5593228.54285185, 1732131.27674334,
       7420309.04808307])

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