I have a shapefile that I read as a geopandas dataframe
import geopandas as gpd
gdf = gpd.read_file('myfile.shp')
gdf.plot()
where gdf.crs
<Projected CRS: ESRI:54009>
Name: World_Mollweide
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Coordinate Operation:
- name: World_Mollweide
- method: Mollweide
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
and gdf.total_bounds
array([-17561329.90352868, -6732161.66088735, 17840887.22672861,
8750122.26961274])
I would like to use basemap
to plot the lat/lon grid on top of it. This is what I am doing
from mpl_toolkits.basemap import Basemap
# Create a Basemap instance with the same projection as the GeoDataFrame
map = Basemap(projection='moll', lon_0=-0, lat_0=-0, resolution='c')
# Create a figure and axis
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the basemap
map.drawcoastlines()
map.drawcountries()
map.drawparallels(range(-90, 91, 30), labels=[1,0,0,0], fontsize=10)
map.drawmeridians(range(-180, 181, 60), labels=[0,0,0,1], fontsize=10)
# Plot the GeoDataFrame on top of the basemap
gdf.plot(ax=ax, color='red', markersize=5)
but this is what I get
That's because the Mollweide projection's parameters (i.e, the proj-string) used by the Basemap
are different from the ones of your GeoDataFrame (that is ESRI:54009) :
>>> gdf.crs.srs
'esri:54009'
>>> map.srs
'+proj=moll +R=6370997.0 +units=m +lat_0=0.0 +lon_0=0.0 +x_0=18019900...'
A simple fix would be to call to_crs
(with the basemap's srs) before making the plot
:
gdf.plot(ax=ax, color='red', markersize=5)
gdf.to_crs(map.srs).plot(ax=ax, color='red')
SN: Using map
as a variable name isn't recommended since it's a built-in.