1

I took the proposed solution from this question (How to mask NetCDF time series data from a shapefile in Python?) and tried to implement it. However, whenever I apply this solution it seems to set all of my values to NaNs and doesn't mask. Anyone know what's going on here? All of the data details below.

#load in precipitation data
data=xr.open_dataset('/Volumes/Ext HDD 1/Python_data/ERA_precip/data/SA_last10yrs_oct1_nov11/adaptor.mars.internal-1605293227.6880476-7726-17-60b7b292-7985-46a5-ac6e-d3ad5469e87a.nc')

I perform some calculations and up with this percent of normal dataarray that only contains latitude and longitude. It looks like this (pon):

enter image description here

Now, I have a shapefile, which is a list of points in southern Brazil. It looks like this and contains about 2500 points (south_bra_shape):

0       POINT (-50.70833 -26.20833)
1       POINT (-50.79167 -26.20833)
2       POINT (-50.79167 -26.12500)
3       POINT (-54.20833 -25.20833)
4       POINT (-50.95833 -26.20833)

Then I try the solution offered by the previous post:

pon.rio.set_spatial_dims(x_dim="longitude", y_dim="latitude", inplace=True)
pon.rio.write_crs("epsg:32663", inplace=True)
south_bra_shape=gpd.read_file('/Users/eli.turaskyriskpulse.com/Documents/shapefiles/brazil/SC_RGDS_PAR_brazil_soybeans/SC_RGDS_PAR_brazil_soybeans.shp',crs="epsg:32663")
clipped=pon.rio.clip(south_bra_shape.geometry.apply(mapping), south_bra_shape.crs, drop=False)

However, here is the result of clipped:

I do not understand what is happening here or what I am doing wrong.

enter image description here

5
  • Hey, welcome to GIS SE! Why are you using a layer of points to mask the netcdf? Do you want to mask it by the extent? Nov 16, 2020 at 0:33
  • I was trying to make a shape from these points that is then the mask. Not sure if that answers your question or not. Nov 16, 2020 at 0:48
  • Can you explain what shape you are trying to make with the points? A single polygon that surrounds all of the points?
    – snowman2
    Nov 16, 2020 at 1:03
  • Yes, that’s exactly what I want. And then use that shape to mask out my original xarray dataarray. Trying to take a mean over space and time. Nov 16, 2020 at 1:05
  • @snowman2 I want to make a polygon out of these points and then mask my original dataarray to only include the points within that polygon. Not all of the points are directly next to each other though so it obviously won’t be 100% precise, but that’s fine. Nov 16, 2020 at 2:11

2 Answers 2

1

Convex Hull in GeoPandas

Here is what I think you want to do:

clipped = pon.rio.clip(
    [mapping(south_bra_shape.geometry.unary_union.convex_hull)],
    south_bra_shape.crs,
    drop=False,
)
3
  • Thanks for this. A couple of things. I run this code as is and get this error: MissingCRS: CRS not found. Please set the CRS with 'set_crs()' or 'write_crs()'. My data is in PlateCarree. I then tried to pon.rio.write_crs("epsg:32663", inplace=True). This works, but I get the same result as in my question. The latitude and longitude values are the same as the original dataarray and all values are NaNs. Nov 16, 2020 at 11:21
  • For reference, this question is basically what I want to do as well with my list of points. gis.stackexchange.com/questions/289775/… But I can't seem to get that to work either. Nov 16, 2020 at 11:57
  • Actually I found something that works. Updating code above. Nov 16, 2020 at 12:10
0

I found a solution that is similar to what I posted in my original question.

#load in the list of points into a GeoDataFrame
south_bra_shape=gpd.read_file('/file/to/path/shapefile.shp')

#set the CRS for the xarray data
pon.rio.write_crs("epsg:32663", inplace=True)

#clip the data
pon.rio.clip(south_bra_shape.geometry.apply(mapping), south_bra_shape.crs)

That's it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.