python - ValueError : Input shapes do not overlap raster. Geopandas/Rasterio,屏蔽时可能出现 CRS 错误

标签 python gis mask geopandas rasterio

我正在使用这个数据集: https://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-density-rev11/data-download (世界网格人口密度)

有了这张 map : https://data.humdata.org/dataset/uganda-administrative-boundaries-as-of-17-08-2018 (乌干达行政边界 shapefile)

我已将乌干达 map 裁剪到我需要的区域,如下所示:

shape_records = uganda.shapeRecords()
desired_shapes = []

for s in shape_records:
    for x in s.record:
        if 'FORT PORTAL' in str(x):
            desired_shapes.append(s)

将它们加载到单个 geopandas 数据框中:

forgpd=[]
for x in desired_shapes:
    forgpd.append(x.__geo_interface__)

gdf = gpd.GeoDataFrame.from_features(forgpd, crs=4326)

然后我正在使用 rasterio 读取 .tif 世界人口文件。

gpw = rio.open('UgandaData/gpw_v4_population_density_rev11_2020_30_sec.tif')
gpw_region = gpw.read(1, window=gpw.window(*box))

我想裁剪它,使用这个:

from rasterio import mask as msk

region_mask, region_mask_tf = msk.mask(dataset=gpw, shapes=gdf.geometry, all_touched=True, filled=True, crop=True) #error here
region_mask = np.where(region_mask < 0, 0, region_mask).squeeze()

我收到以下错误:

WindowError: windows do not intersect
ValueError: Input shapes do not overlap raster.

这是我的crs:

Gridded population of world: CRS.from_epsg(4326)

Uganda(Fort Portal) : 
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

可能不同之处在于我没有为网格化的世界人口指定 WGS 84?如果是这样,这是如何指定的?

最佳答案

问题是 shapefile 是 UTM 坐标,而栅格是世界坐标系(纬度/经度)。即使您将 epsg:4326 crs 分配给 gdf,它的坐标仍在 UTM 中。您可以通过 this 之类的操作手动转换这些.

否则,您可以使用 QGIS 将世界栅格重新投影到 EPSG:21096(基于乌干达 shapefile 的 UTM 区域的估计),或者您可以使用 gdalwarp .

更改栅格上的投影后,您的其余代码就可以正常工作了。

关于python - ValueError : Input shapes do not overlap raster. Geopandas/Rasterio,屏蔽时可能出现 CRS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70022058/

相关文章:

python - hotshot可以在多线程中使用吗?

ios - 在iOS应用上将美国城市居中

draggable - kinetic.js 中具有可拖动图像的复杂剪切边界

2d - 如何在 webgpu 中创建 2d 剪贴蒙版?

CSS mask-image 不工作(试过 firefox 和 safari)

python - 一个组合对象如何引用它在内部组合的对象?

python - 没有数据库的 Django DRF

python - 为什么每个浏览器窗口的 session 不是唯一的

ios - 在 iOS 上带路线的离线 map

python - 当我通过 python 的 PIL 将 .tif 合并到 RGB 带时出错