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

whitebox catchment delineation improvements #267

Open
barneydobson opened this issue Aug 23, 2024 · 2 comments
Open

whitebox catchment delineation improvements #267

barneydobson opened this issue Aug 23, 2024 · 2 comments
Labels
feature Adding a new functionality, small or large refactor Refactoring existing code without significantly changing functionality

Comments

@barneydobson
Copy link
Collaborator

barneydobson commented Aug 23, 2024

With #266 we have whitebox which introduces a range of powerful tools for catchment delineation.

A summary of obvious tasks:

  • We should be able to remove all dependency on pyflwdir with this in the long run.
  • Enables flexible resampling.
  • Explore use of filters.
  • Can more devotedly handle burning/carving in.
@barneydobson barneydobson added feature Adding a new functionality, small or large refactor Refactoring existing code without significantly changing functionality labels Aug 23, 2024
@barneydobson barneydobson mentioned this issue Aug 23, 2024
1 task
@barneydobson
Copy link
Collaborator Author

Example code to do this with @cheginit 's new wrapper:

import shapely

bbox = shapely.box(*dem.rio.bounds())
u, x, y = zip(
    *[
        (u, float(p["x"]), float(p["y"]))
        for u, p in g.nodes(data=True)
        if shapely.Point(p["x"], p["y"]).within(bbox)
    ]
)
gpd.GeoSeries(gpd.points_from_xy(x, y), crs=g.graph["crs"]).to_file("pour_pts.shp")

whitebox_tools("BreachDepressionsLeastCost", ["-i=dem.tif", "-o=dem_corr.tif"])
whitebox_tools("D8Pointer", ["-i=dem_corr.tif", "-o=fdir.tif"])
whitebox_tools("D8FlowAccumulation", ["-i=fdir.tif", "--pntr", "-o=streams.tif"])
whitebox_tools("JensonSnapPourPoints", ["--pour_pts=pour_pts.shp", "--streams=streams.tif", "--snap_dist=15.0", "-o=pour_pts_snapped.shp"])
whitebox_tools("Watershed", ["--d8_pntr=fdir.tif", "--pour_pts=pour_pts_snapped.shp", "-o=watersheds.tif"])

with rasterio.open("watersheds.tif") as src:
    gdf_bas = vectorize(
        src.read(1).astype(np.int32), 0, src.transform, src.crs, name="basin"
    )
    gdf_bas = gdf_bas[gdf_bas.basin != src.nodata]
    gdf_bas["id"] = [u[x - 1] for x in gdf_bas["basin"]]

@cheginit
Copy link
Collaborator

A more elegant way of calling whitebox_tools is using a dict:

wbt_args = {
    "BreachDepressionsLeastCost": ["-i=dem.tif", "-o=dem_corr.tif"],
    "D8Pointer": ["-i=dem_corr.tif", "-o=fdir.tif"],
    "D8FlowAccumulation": ["-i=fdir.tif", "--pntr", "-o=streams.tif"],
    "JensonSnapPourPoints": ["--pour_pts=pour_pts.shp", "--streams=streams.tif", "--snap_dist=15.0", "-o=pour_pts_snapped.shp"],
    "Watershed": ["--d8_pntr=fdir.tif", "--pour_pts=pour_pts_snapped.shp", "-o=watersheds.tif"]
}
for tool, args in wbt_args.items():
    whitebox_tools(tool, args)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adding a new functionality, small or large refactor Refactoring existing code without significantly changing functionality
Projects
None yet
Development

No branches or pull requests

2 participants