将底图添加到 geopandas plot

Adding basemap to geopandas plot

我有一个 geojson 文件,其中包含覆盖着六边形多边形的纽约市,我将其读入 geopandas 数据框。我希望将 Stamen TonerLite 底图添加到数据框的绘图中。但是,我无法复制与此处工作示例相同的结果 - https://geopandas.org/gallery/plotting_basemap_background.html

我在下面给出了相关数据帧的代码和crs的一个小例子:

%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()

我得到一个没有地图背景的空白背景,只有纽约市形状的六边形。

我画的dataframe的crs是

<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

crs 与我从上面的示例中得到的 crs 完全相同 link(该示例对我有用。)

如何找出问题所在?多年来,我使用 folium 或 ipyleaflet 将此 geojson 文件用于许多绘图,并且不怀疑它有任何问题。但是这里有一个 link 到文件 - https://drive.google.com/file/d/1HO854_YFTtRaL4e-nPrL43woYou-IY-a/view?usp=sharing

您的文件已损坏。 GeoJSON 加载了 CRS 3857,而几何本身是 4326。在重新投影到 Web Mercator 之前,只需分配正确的 CRS。

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()

请注意,您的六边形未在正确的投影 CRS 中生成,因此它们是倾斜的。