regionmask的wrap_lon参数在-180~180范围气候数据上失效及相关CRS问题求助
regionmask的wrap_lon参数在-180~180范围气候数据上失效及相关CRS问题求助
大家好,我最近在用regionmask 0.13.0处理气候NetCDF数据的掩膜操作时遇到了棘手的问题,折腾好一阵没找到解决办法,想请教一下社区的各位大佬:
核心问题
我手里的气候数据经度范围是-180到180,用regionmask做区域掩膜的时候,不管怎么设置wrap_lon参数都达不到预期效果:
- 不指定
wrap_lon时,直接抛出错误:ValueError: lon has data that is larger than 180 and smaller than 0. Set `wrap_lon=False` to skip this check. - 手动设置
wrap_lon=180后,掩膜结果全是NaN,完全起不到过滤目标区域的作用。
排查过程中发现的异常点
- 查看shapefile的
geometry字段时,发现坐标值是非常大的数值(明显不是常规的经纬度格式):0 MULTIPOLYGON (((-1832380.592 2237164.258, -182... Name: geometry, dtype: geometry - 打印
shp_file.crs后,确认这个shapefile用的是**EPSG:3857(WGS 84 / Pseudo-Mercator)**投影坐标系,单位是米:<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 between 85.06°S and 85.06°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 ensemble - Ellipsoid: WGS 84 - Prime Meridian: Greenwich - 尝试在读取shapefile时显式指定CRS为
EPSG:3857:
结果又收到警告:shp_file = gpd.read_file(f"datafiles/{filename}.shp", crs='EPSG:3857')geo_env/lib/python3.12/site-packages/pyogrio/raw.py:198: RuntimeWarning: driver ESRI Shapefile does not support open option CRS
最小复现代码
下面是能复现问题的简化代码,大家可以参考:
import xarray as xr import geopandas as gpd import regionmask # 打开并预处理气候数据集 t2m_file = xr.open_dataset("datafiles/temp.nc") # 调整经度到-180~180范围并排序 t2m_file.coords['longitude'] = (t2m_file.coords['longitude'] + 180) % 360 - 180 t2m_file = t2m_file.sortby(t2m_file.longitude) t2m = t2m_file['t2m'] # 读取shapefile并创建区域掩膜 filename = 'North_Africa' shp_file = gpd.read_file(f"datafiles/{filename}.shp") shp_region = regionmask.Regions(shp_file.geometry) shp_file.plot() # 绘图能正常显示区域,说明shapefile本身的地理信息是正确的 # 执行掩膜操作(此处出现问题) mask_region = shp_region.mask(t2m.longitude, t2m.latitude, wrap_lon=180) tem_masked_region = t2m.where(mask_region == 0)
补充说明
用到的shapefile和NetCDF测试文件体积都很小,若需要验证的话我可以进一步提供。
备注:内容来源于stack exchange,提问作者Kernel
相关产品推荐
相关产品推荐

