corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
504 stars 80 forks source link

"At least one of the clipped raster x,y coordinates has only one point" Error when using rio.clip_box #708

Closed laonahongchen closed 4 days ago

laonahongchen commented 8 months ago

Hi,

I am a beginner using the package to read rasters in a lazy mode.

I ran into this error message that seems should only occur when doing the integration test instead of general usage: "At least one of the clipped raster x,y coordinates has only one point."

Is there any guideline that I can refer to solve this problem? Many thanks!

The function is here:

def raster_to_df_rioxarray(file_name, lb, rb, tb, bb):
    # read the raster file and convert to an xarray dataframe

    # try to open raster file with rioxarray, if nodata exception then return empty dataframe
    try: #print exception information if error happens and return empty dataframe
        raster  = rxr.open_rasterio(file_name).rio.clip_box( 
        minx = lb,
        miny = tb,
        maxx = rb,
        maxy = bb,
        ) 
    except Exception as e:
        # only print if e does not contain "No data", because these are the unexpected exceptions
        if 'No data' not in str(e):
            print('exception file name:', file_name, e)
        return None

    raster = raster.rename('intensity')

    df = raster.to_dask_dataframe()
    raster.close()
    df = df.where(df['intensity'] > 0)
    df = df.dropna()
    df = df.compute() # tranforming to pandas dataframe

    return df
snowman2 commented 8 months ago

This is due to your clipped raster resulting in a single point in either the x or y coordinate. That is an invalid raster and as such an error is raised.

laonahongchen commented 8 months ago

Got it, thanks! I thought it was due to one of the windows having only one coordinate, which then will belong to a bug since the window should be self-adapted and not exposed to us users.

In this case, is there any suggestion on what I should use instead of the clip_box if it cannot guarantee it has more than a single point but just wants to get it as a dataset?

Thanks ahead!

snowman2 commented 8 months ago

In this case, is there any suggestion on what I should use instead of the clip_box if it cannot guarantee it has more than a single point but just wants to get it as a dataset?

Without changing any code, you could try use xarray.sel to pull out the data using the coordinate ranges. If you want to use rio.clip_box, you could add an argument to allow returning a dataset that is an invalid raster.

laonahongchen commented 8 months ago

Got it, thank you very much!

snowman2 commented 4 days ago

Reopening as it seems you can have a single point raster now. Can re-project and write to file ...