pythonpython-3.xgeopandas

Wrap_lon of the regionmask does not work with data span from -180 to 180


All,

I use regionmask package 0.13.0 to mask climate NetCDF data. I found that if my data extends from -180 to 180, the mask function returns all NAN even after I set wrap_lon=180 and I did not set wrap_lon I got the following error

ValueError: lon has data that is larger than 180 and smaller than 0. Set `wrap_lon=False` to skip this check.

I found that shp_file['geometry'] yields a very large number, which may explain this error, yet not sure why the mulipolygon number is so large.

0 MULTIPOLYGON (((-1832380.592 2237164.258, -182 Name: geometry, dtype: geometry

update : I printed the shp_file.crs and I found that the CRS is EPSG:3857,

<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

yet when I tried to open the shape file using the CRS

hp_file =gpd.read_file("datafiles/"+filename+'.shp',\
                        crs='EPSG:3857')

I got the following error.

geo_env/lib/python3.12/site-packages/pyogrio/raw.py:198: RuntimeWarning: driver ESRI Shapefile does not support open option CRS

Here is the minimal example

import xarray as xr
import geopandas as gpd
import regionmask

#%% opening the dataset
t2m_file    = xr.open_dataset("datafiles/"+"temp.nc")
# adjusting longitude.
t2m_file.coords['longitude'] = (t2m_file.coords['longitude'] + 180) % 360 - 180
t2m_file = t2m_file.sortby(t2m_file.longitude)

t2m = t2m_file['t2m']
#%%
filename='North_Africa'
shp_file =gpd.read_file("datafiles/"+filename+'.shp')
shp_region=regionmask.Regions(shp_file.geometry)
shp_file.plot()

#%%
mask_region=shp_region.mask(t2m.longitude,t2m.latitude,wrap_lon=180)
# masked temperture of the raw data
tem_masked_region=t2m.where(mask_region == 0)

The shape files and the netcdf are very small and could be downloaded from the box https://app.box.com/s/nyauxuuscbk0ws5firpmyjt3y51nrlr2

Thanks


Solution

  • As suggested by @Patrick, changing the shape coordinate from meters to degress solved this problem.

    shp_file=shp_file.to_crs('EPSG:4326’)
    

    the mask function of the regionmask package treats the meter coordinate as a degree coordinate; I think it will be good if the function tests first the type of Coordinate reference system CRS. Since the error that I got (below) makes me feel that the problem is the netcdf data file ValueError: lon has data that is larger than 180 and smaller than 0. Set `wrap_lon=False` to skip this check.