【发布时间】:2018-05-26 15:53:57
【问题描述】:
我有一些多边形(加拿大各省),使用GeoPandas 阅读,并希望使用这些来创建一个掩码,以应用于二维经纬网格上的网格数据(使用iris 从 netcdf 文件中读取)。最终目标是只保留给定省份的数据,其余数据被屏蔽。因此,对于省内的网格框,掩码将为 1,而对于省外的网格框,掩码将为 0 或 NaN。
可以从此处的 shapefile 获取多边形: https://www.dropbox.com/s/o5elu01fetwnobx/CAN_adm1.shp?dl=0
我正在使用的 netcdf 文件可以在这里下载: https://www.dropbox.com/s/kxb2v2rq17m7lp7/t2m.20090815.nc?dl=0
我想这里有两种方法,但我都在苦苦挣扎:
1) 使用多边形在经纬度网格上创建掩码,以便将其应用于 python 之外的大量数据文件(首选)
2) 使用多边形对已读入的数据进行屏蔽,仅提取感兴趣省份内的数据,进行交互处理。
到目前为止我的代码:
import iris
import geopandas as gpd
#read the shapefile and extract the polygon for a single province
#(province names stored as variable 'NAME_1')
Canada=gpd.read_file('CAN_adm1.shp')
BritishColumbia=Canada[Canada['NAME_1'] == 'British Columbia']
#get the latitude-longitude grid from netcdf file
cubelist=iris.load('t2m.20090815.nc')
cube=cubelist[0]
lats=cube.coord('latitude').points
lons=cube.coord('longitude').points
#create 2d grid from lats and lons (may not be necessary?)
[lon2d,lat2d]=np.meshgrid(lons,lats)
#HELP!
非常感谢您的任何帮助或建议。
更新:按照下面@DPeterK 的出色解决方案,我的原始数据可以被屏蔽,给出以下内容:
【问题讨论】:
-
您可能还对 gist.github.com/pelson/9785576 感兴趣,它演示了使用 shapely.vectorized 进行遮罩。它应该非常高效。如果这是您的解决方案,请考虑为该问题添加另一个答案。
标签: python gis geopandas cartopy python-iris