pydata / pydata/xarray

Losing data when add a raster to a dataset

Open
#5,075 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

As I mentioned here, when I add a raster inside a geocube are produced a lot of nodata.

After a test @snowman2 has indicates that this is a possible bug of xarray libraries.

I was able to reproduce the issue you demonstrated. I think it has to do with decimal precision of the coordinates when adding the DataArray to the Dataset. That being said, I checked that the x coordinate and y coordinates were the exact same for raster_in and out_grid. This is likely an issue with xarray and not geocube.

For a workaround, this got it working without the gaps for me:

import geopandas
import rioxarray
from geocube.api.core import make_geocube

vector_in = geopandas.read_file("vector_data.shp")
raster_in = rioxarray.open_rasterio("raster_data.tif", masked=True).sel(band=1).drop("band")

out_grid = make_geocube(
    vector_data=vector_in,
    measurements=["id"],
    like=raster_in,
)

out_grid["process_value"] = (raster_in.dims, raster_in.values, raster_in.attrs, raster_in.encoding)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the linked geocube issue #62 and the reproduced example using geopandas, rioxarray, make_geocube, and raster_in/out_grid. Investigate the coordinate-precision behavior when adding raster_in to out_grid; done means the assignment no longer introduces nodata gaps without relying on the shown workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.