Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
laonahongchen opened this issue Nov 6, 2023 · 5 comments · Fixed by #796
Labels
question Further information is requested

Comments

@laonahongchen
Copy link

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
@laonahongchen laonahongchen added the bug Something isn't working label Nov 6, 2023
@snowman2
Copy link
Member

snowman2 commented Nov 8, 2023

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
Copy link
Author

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
Copy link
Member

snowman2 commented Nov 9, 2023

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.

@snowman2 snowman2 added question Further information is requested and removed bug Something isn't working labels Nov 9, 2023
@laonahongchen
Copy link
Author

Got it, thank you very much!

@snowman2
Copy link
Member

snowman2 commented Jul 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants