rioxarray
rioxarray copied to clipboard
DOC: Example of converting 2D coordinates to regular grid
Your data has unevenly spaced 2D lat/lon values.
That means that either:
- Your data is not geographic and was re-projected to lat/lon in the 2D space to preserve the coordinate locations.
- Your data is not represented in an evenly spaced grid.
If (1) is your problem, you will need to re-project your data from 2D latlon back to the projected coordinates in 1D, add the 1D x&y coordinates to your dataset. Then it should work.
If (2) is your problem, then you will need to decide on the resolution of your grid, the bounds of your area of interest, and resample your data to the new regularly spaced grid. Then it will work.
From: https://github.com/corteva/rioxarray/issues/47#issuecomment-532377611
There are several options for the case where the data is unevently spaced and you want to convert it to a regular grid;
-
pyresample
: https://pyresample.readthedocs.io/en/latest/swath.html (maybe @djhoese could assist here?) - The
rasterize_points_
functions powered by scipy: https://github.com/corteva/geocube/blob/master/geocube/rasterize.py -
gdal.Grid
: https://gdal.org/python/osgeo.gdal-module.html#Grid
geocube
:
Step 1: https://gis.stackexchange.com/questions/384581/raster-to-geopandas/384691#384691
rds = xarray.open_dataset("path_to_file.nc")
df = rds.squeeze().to_dataframe().reset_index()
geometry = gpd.points_from_xy(df.x, df.y)
gdf = gpd.GeoDataFrame(df, crs=rds.rio.crs, geometry=geometry)
Step 2: https://corteva.github.io/geocube/stable/examples/rasterize_point_data.html
from geocube.api.core import make_geocube
from geocube.rasterize import rasterize_points_griddata
geo_grid = make_geocube(
vector_data=gdf,
resolution=(-0.1, 0.1),
rasterize_function=rasterize_points_griddata,
)
There are several options for the case where the data is unevently spaced and you want to convert it to a regular grid;
* `pyresample`: https://pyresample.readthedocs.io/en/latest/swath.html (maybe @djhoese could assist here?) * The `rasterize_points_` functions powered by scipy: https://github.com/corteva/geocube/blob/master/geocube/rasterize.py * `gdal.Grid`: https://gdal.org/python/osgeo.gdal-module.html#Grid
Also the Xoak library seems promising to perform point-wise selection of irregularly spaced data : https://xoak.readthedocs.io/en/latest/examples/introduction.html
See: #202
Related #724