I have data for precipitation in netcdf file format and a shapely polygon geometry. I want to extract only the data that overlaps with the polygon. Any idea how we do that in python?
it's very simple. what you need to do is to take value point by point. and have to apply the mathematical formula of the polygon. now, if the point that you extracted from the file is satisfying that math formula then you can surely say, that this will overlap.
In the past, I've done this exact task by using geopandas and shapely. You can convert the point coordinates in your netcdf to Point objects using shapely, which then allows you to create a GeoDataFrame using the list of Point objects as the geometry. Keep in mind, you will need to flatten your netcdf data into a single column per variable (so 1 column for the geometry, one each for whatever other data you have on it).
Once you've converted your netcdf file to a Point GeoDataFrame, you can perform a spatial join (sjoin) to combine the polygon and points.