rioxarray icon indicating copy to clipboard operation
rioxarray copied to clipboard

DOC: Example of converting 2D coordinates to regular grid

Open snowman2 opened this issue 3 years ago • 5 comments

Your data has unevenly spaced 2D lat/lon values.

That means that either:

  1. Your data is not geographic and was re-projected to lat/lon in the 2D space to preserve the coordinate locations.
  2. 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

snowman2 avatar Jan 19 '21 17:01 snowman2

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

snowman2 avatar Jan 19 '21 17:01 snowman2

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,
)

snowman2 avatar Jan 19 '21 17:01 snowman2

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

Zepy1 avatar Aug 26 '21 07:08 Zepy1

See: #202

snowman2 avatar Aug 26 '21 13:08 snowman2

Related #724

snowman2 avatar Dec 22 '23 15:12 snowman2