question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Losing data when add a raster to a geocube

See original GitHub issue

I don’t know if it is a bug or my misunderstanding, but I’m following this example without fortune.

vector_in = geopandas.read_file(input_vector)
raster_in = rioxarray.open_rasterio(input_raster)

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

When I use out_grid["process_value"] = raster_in and later out_grid.to_netcdf('mydata.nc') I see that the output has a lot of nodata. But neither vector_in or raster_in have this amount of nodata.

Here there is a QGIS project with my data.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
snowman2commented, Mar 25, 2021

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)
1reaction
snowman2commented, Mar 24, 2021

Are you sure that there isn’t a bug inside your code?

Will need to dig into it later to see what is going on. Thanks for the images, that is helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Example - Filling in missing data & dealing with timestamps
Example - Filling in missing data & dealing with timestamps · Load in geopackage data and add CRS ·...
Read more >
No data returned when rasterizing specific measurements #50
I'd like plot the data but the conventional .plot() fails i presume because all that is in my output raster is coordinates -...
Read more >
How to use geocube with option -at (all_touched=TRUE)
First, I want to rasterize roads separately to make them look better by using the option "all_touched"=True. And then I want to merge...
Read more >
Add Rasters To Mosaic Dataset (Data Management)—ArcMap
ArcGIS geoprocessing tool that adds raster datasets from a file, a folder, a raster catalog, or an image service to a mosaic dataset....
Read more >
geocube Changelog - PyUp.io
Add "all_touched" keyword argument to geocube.rasterize.rasterize_image ... Added filter_nan kwarg to filter out missing data when rasterizing (issue 9)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found