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

Compatibility with HydroSheds Flow Direction #58

Open
mn5hk opened this issue Jan 8, 2025 · 0 comments
Open

Compatibility with HydroSheds Flow Direction #58

mn5hk opened this issue Jan 8, 2025 · 0 comments

Comments

@mn5hk
Copy link

mn5hk commented Jan 8, 2025

Kind of request

Adding new functionality

Enhancement Description

Dear team,

I am currently trying to use pyflwdir to obtain subbasins for any arbitrary points using the Hydrosheds FlowDirection GeoTiff file. I wanted to check if it may be possible with the package. I have tried a few ways, but I end up in errors so far.

Sharing my (rough) code below:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import rasterio
from rasterio.mask import mask
from rasterio.plot import show
import shapely
import xarray as xr
import rioxarray as rxr
import geopandas as gpd
import fiona
import pyflwdir
from pyflwdir import FlwdirRaster

# Read Vector geometry
basins_fn = "basins.gpkg" #read Rhine basin vector from Geopackage
flowdir_fn = "eu_dir_3s.tif" #flowdirection file from Hydrosheds

layer_name = layers[0] #select Rhine basin layer
gdf = gpd.read_file(basins_fn, layer=layer_name)

# Read flow direction raster and clip it
rhine_shapes = gdf.geometry.values

with rasterio.open(flowdir_fn) as src:
    #Clip flowdir
    out_image, out_transform = mask(src, rhine_shapes, crop=True)
    out_meta = src.meta.copy()
    
    # Update metadata for the output file
    out_meta.update({
        "driver": "GTiff",
        "height": out_image.shape[1],
        "width": out_image.shape[2],
        "transform": out_transform
    })
    

# Use PyFlwDir workflow to get sub-basin
src = rasterio.open(output_path) # Rhine flowdir file from Hydrosheds

flwdir = src.read(1)
transform = src.transform
crs = src.crs
latlon = crs.to_epsg() == 4326

flw = pyflwdir.from_array(flwdir, ftype="d8", transform=transform, latlon=latlon, cache=True)

x, y = np.array([6, 7, 8, 10]), np.array([52, 49, 50, 50]) #random points for basin outlet
subbasins = flw.basins(xy=(x, y))

feats = flw.stream_order(type="strahler", mask=stream_mask)

gdf = gpd.GeoDataFrame.from_features(feats, crs = src.crs)

Currently, the last line shows the following error, while the earlier lines give some result. Not sure if my current approach is relevant..

Sharing the error message below:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In [58], line 1
----> 1 gdf = gpd.GeoDataFrame.from_features(feats, crs = src.crs)

File /usr/local/lib/python3.8/dist-packages/geopandas/geodataframe.py:648, in GeoDataFrame.from_features(cls, features, crs, columns)
    645 if hasattr(feature, "__geo_interface__"):
    646     feature = feature.__geo_interface__
    647 row = {
--> 648     "geometry": shape(feature["geometry"]) if feature["geometry"] else None
    649 }
    650 # load properties
    651 properties = feature["properties"]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Use case

No response

Additional Context

No response

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

No branches or pull requests

1 participant