Blue_Green
Blue_Green

Reputation: 29

How can I extract shp-points values from a 3D array - (based on matching lat, lon and time)?

I have the problem that I have multiple data arrays (or merged together --> one big xarray dataset) and a shapefile with points. These points have inter alia columns with "geometry (=lat & lon)" and "Date". I would like to extract the values of the array/dataset at the lat, lon and time value- points the shapefile has. My goal is to have an array (or a dataset - even better) where the extracted values of the lat, lon and time of the shapefile are stored.

Now the problem is: the shapefile is 2D and the arrays are 3D.

  1. I could make a 2D array or list out of my 3D array and then try to extract with matching dates and lon and lat? [this was unfortunately not working as I could not transform my array to a list]
  2. Or I try to change the shapefile to a 3D Raster and then clip the values from the existing arrays?

I would be very happy for some thoughts and help! Thanks in advance!

Upvotes: 0

Views: 354

Answers (1)

Jesse Anderson
Jesse Anderson

Reputation: 4603

Building off the top answer to this question and assuming your points and dataset are in the same projection, you could do something like

import geopandas

pts = geopandas.read_file(<your shp>)
lons = pts.geometry.x.to_list()
lats = pts.geometry.y.to_list()
values_at_pts = your_xarray_dataset.sel(lon=lons, lat=lats, method="nearest")

Upvotes: 1

Related Questions