简体   繁体   English

将底图添加到 geopandas plot

[英]Adding basemap to geopandas plot

I have a geojson file which consists of New York City covered with hexagon polygons, which I read into a geopandas dataframe.我有一个 geojson 文件,其中包含覆盖有六边形多边形的纽约市,我将其读入 geopandas dataframe。 I wish to add the Stamen TonerLite basemap to the plot of the dataframe.我希望将 Stamen TonerLite 底图添加到 dataframe 的 plot 中。 However, I am unable to replicate the same results as the working example here - https://geopandas.org/gallery/plotting_basemap_background.html但是,我无法复制与此处的工作示例相同的结果 - https://geopandas.org/gallery/plotting_basemap_background.html

I give a small example below of the code and crs of the associated dataframe:我在下面给出一个小例子,相关的 dataframe 的代码和 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()

I get a blank background with no map background, but just the hexagons in shape of NYC.我得到一个没有 map 背景的空白背景,只有纽约市形状的六边形。

The crs of the dataframe that I plot is我 plot 的 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

The crs is exactly the same as one I get from the example from the above link (that example works for me.) crs 与我从上述链接的示例中获得的完全相同(该示例对我有用。)

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.多年来,我使用 folium 或 ipyleaflet 将这个 geojson 文件用于许多地块,并且不怀疑它有什么问题。 But here is a link to the file - https://drive.google.com/file/d/1HO854_YFTtRaL4e-nPrL43woYou-IY-a/view?usp=sharing但这里是文件的链接 - 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. GeoJSON 加载了 CRS 3857,而几何本身在 4326 中。只需在重新投影到 Web 墨卡托之前分配正确的 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()

Note that your hexagons have not been generated in the correct projected CRS, so they are skewed.请注意,您的六边形尚未在正确的投影 CRS 中生成,因此它们是倾斜的。

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM