I have a geojson file which consists of New York City covered with hexagon polygons, which I read into a geopandas dataframe. I wish to add the Stamen TonerLite basemap to the plot of the dataframe. However, I am unable to replicate the same results as the working example here - https://geopandas.org/gallery/plotting_basemap_background.html
I give a small example below of the code and crs of the associated dataframe:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import geopandas as gpd
import contextily as ctx
fp = "/data/hex_bins/nyc_hex_bins.geojson"
map_df = gpd.read_file(fp)
map_df = map_df.to_crs(epsg=3857)
ax = map_df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, zoom=12, source=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
I get a blank background with no map background, but just the hexagons in shape of NYC.
The crs of the dataframe that I plot is
<Projected CRS: EPSG:3857>
Name: WGS 84 / Pseudo-Mercator
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: World - 85°S to 85°N
- bounds: (-180.0, -85.06, 180.0, 85.06)
Coordinate Operation:
- name: Popular Visualisation Pseudo-Mercator
- method: Popular Visualisation Pseudo Mercator
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
The crs is exactly the same as one I get from the example from the above link (that example works for me.)
How do I figure out what is the issue? I have used this geojson file for many plots over the years using folium or ipyleaflet, and do not suspect anything is wrong with it. But here is a link to the file - https://drive.google.com/file/d/1HO854_YFTtRaL4e-nPrL43woYou-IY-a/view?usp=sharing
Your file is corrupted. The GeoJSON is loaded with CRS 3857, while the geometry itself is in 4326. Just assign the correct CRS before reprojecting to Web Mercator.
map_df = gpd.read_file(fp)
map_df.crs = 4326 # this line
map_df = map_df.to_crs(epsg=3857)
ax = map_df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, zoom=12, source=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
Note that your hexagons have not been generated in the correct projected CRS, so they are skewed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.