Fast methods to work with hydro- and topography data in pure Python.

Overview
https://codecov.io/gh/Deltares/pyflwdir/branch/main/graph/badge.svg?token=N4VMHJJAV3 Latest developers docs Latest PyPI version

PyFlwDir

Intro

PyFlwDir contains a series of methods to work with gridded DEM and flow direction datasets, which are key to many workflows in many earth siences. Pyflwdir supports several flow direction data conventions and can easily be extended to include more. The package contains some unique methods such as Iterative Hydrography Upscaling (IHU) method to upscale flow directions from high resolution data to coarser model resolution.

Pyflwdir is in pure python and powered by numba to keep it fast.

Featured methods

https://raw.githubusercontent.com/Deltares/pyflwdir/master/docs/_static/pyflwdir.png

  • flow directions from elevation data using a steepest gradient algorithm
  • strahler stream order
  • flow direction upscaling
  • (sub)basin delineation
  • pfafstetter subbasins delineation
  • classic stream order
  • height above nearest drainage (HAND)
  • geomorphic floodplain delineation
  • up- and downstream tracing and arithmetics
  • hydrologically adjusting elevation
  • upstream accumulation
  • vectorizing streams
  • many more!

Installation

We recommend installing PyFlwdir using conda or pip.

Install the package from conda using:

$ conda install pyflwdir -c conda-forge

Install the package from pip using:

$ pip install pyflwdir

In order to run the examples in the notebook folder some aditional packages to read and write raster and vector data, as well as to plot these data are required. A complete environment can be installed from the environment.yml file using:

$ conda env create -f environment.yml
$ pip install pyflwdir

Quickstart

The most common workflow to derive flow direction from digital elevation data and subsequent delineate basins or vectorize a stream network can be done in just a few lines of code.

To read elevation data from a geotiff raster file elevation.tif do:

import rasterio
with rasterio.open("elevation.tif", "r") as src:
    elevtn = src.read(1)
    nodata = src.nodata
    transform = src.transform
    crs = src.crs

Derive a FlwdirRaster object from this data:

import pyflwdir
flw = pyflwdir.from_dem(
    data=elevtn,
    nodata=src.nodata,
    transform=transform,
    latlon=crs.is_geographic,
)

Delineate basins and retrieve a raster with unique IDs per basin: Tip: This raster can directly be written to geotiff and/or vectorized to save as vector file with rasterio

basins = flw.basins()

Vectorize the stream network and save to a geojson file:

import geopandas as gpd
feat = flw.streams()
gdf = gpd.GeoDataFrame.from_features(feats, crs=crs)
gdf.to_file('streams.geojson', driver='GeoJSON')

Documentation

See docs for a many examples and a full reference API.

Development and Testing

Welcome to the pyflwdir project. All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome. See CONTRIBUTING.rst for how we work.

Changes

See CHANGELOG.rst

Authors

See AUTHORS.txt

License

This is free software: you can redistribute it and/or modify it under the terms of the MIT License. A copy of this license is provided in LICENSE

Comments
  • use pyflwdir package to parse nextxy data of CaMa-Flood!

    use pyflwdir package to parse nextxy data of CaMa-Flood!

    Dear @DirkEilander ,

    This is Jiangchao Qiu, a Ph.D. student from Sun Yat-sen University, China. I am major in simulating storm surge using physical model.

    I know you by reading your paper : The effect of surge on riverine flood hazard and impact in deltas globally. Great works!

    Recently, my collaborator provides me some global modeling result using CaMa-Flood in 15 arcmin resolution. I am trying to analyze these result and make some visualization, especially the river network and river mouths along the coastlines.

    Firstly, I used the function of cmf_maps_io.py (in your compound_hotpots repositories) to transform the nextxy.bin file to nextxy.tif file glb_15min.zip

    Secondly, I want to use the pydlwdir package to visualize the river network, but some errors occurred when I parse the nextxy.tif file to
    the actionable common format.

    the following is the screensnaps image

    image

    I try to use the function of pyflwdir.FlwdirRaster and pyflwdir.from_array, but both failed, I don't know how to fix it, could you please help me check and give some suggestion about how to solve this problem.

    By the way, the environment of my package seems no problem, since all of the example in the notebook folder can be carried out smothly.

    Many thanks to you and looking forward to your reply.

    Best regards, Jiangchao

    opened by Jiangchao3 8
  • error in plotting the river mouths using the nextxy.bin dataset

    error in plotting the river mouths using the nextxy.bin dataset

    Hi @DirkEilander ,

    I want to identify all of the river mouth and make a plot using the 3 arcmin river network dataset nextxy.bin.

    But it is odd that the figure I plotted here seems reversed in the latitude. Could you please have a look? Many thanks.

    Best regards, Jiangchao

    my scripts:

    import pyflwdir import numpy as np

    bbox=[-180, -90, 180, 90] data, transform = pyflwdir.read_nextxy( fn='glb_03min/nextxy.bin', nrow=3600, ncol=7200, bbox=bbox)

    import xarray as xr xr.DataArray(data[0]).plot() image

    x = data[0] lon = np.linspace(-180,180,7200) lat = np.linspace(-90,90,3600) lon[np.where(x==-9)[1]],lat[np.where(x==-9)[0]]

    import geopandas as gpd from shapely.geometry import Point gdf = gpd.GeoDataFrame([Point(x, y) for x,y in zip(lon[np.where(x==-9)[1]], lat[np.where(x==-9)[0]])]) gdf.columns = ['geometry']

    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) fig, ax = plt.subplots() gdf.plot(ax=ax,markersize=0.1, color='red') world.plot(ax=ax); image

    opened by Jiangchao3 7
  • Distance to river network

    Distance to river network

    My apologies for opening this issue as I don't know how else to do this, but I was wondering if the pyfldir toolkit can be used to calculate the distance to a river network? I've been desperately looking for an open-source tool to do this. So a method that uses a flow direction raster to calculate the flow distance to the nearest stream.

    opened by MauKruisheer 3
  • Helper function to get coordinates from linear indices

    Helper function to get coordinates from linear indices

    Feature request: This may be in the code but I haven't see it in the docs anywhere. The flow path trace function returns a list of pixel indices in the linear index format. A helper function to convert to the coordinate system of the raster would be helpful.

    opened by JasonDalton 3
  • User guide instructions for plotting import error

    User guide instructions for plotting import error

    Dear all,

    thanks for this package. I am running conda 4.11 on Win10 and have installed pyflwdir as instructed. When following the user guide instructions for plotting I stumble upon below error.

    Python 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:22:46) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from utils import quickplot, colors, cm
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'utils'
    

    Any hints on how to alleviate the situation are highly welcome. Same counts for install under Ubuntu WSL.

    Kind regards

    Sebastian

    opened by schoeller 2
  • Numba error creating flow direction raster

    Numba error creating flow direction raster

    I think I have the error narrowed down to how the JIT compiler views either the rasterio object or the parameters sent to pyflwdir.from_dem() function. I have a simple example based on the Quickstart from the documentation. The error occurs when i try to create a flow direction raster from elevation read through rasterio.
    https://colab.research.google.com/drive/1engswQ4uaKB0UMMM4zt8YzC8wEAnK1o1?usp=sharing

    opened by JasonDalton 2
  • Streams

    Streams

    • FlwdirRaster.streams method includes a (zero-length) line at pits and adds option for trace direction in combination with segment end cells.
    • Moved accuflux method from FlwdirRaster to parent Flwdir class.
    opened by DirkEilander 1
  • increase short river lengths

    increase short river lengths

    Subgrid river lengths can locally be very short which slows down the model. Potentially we can redistribute the subgrid river lengths within a single river branch to speed up calculations without making a large computational error.

    method in flwdir.py

    • inputs: river length, min threshold

    numba jitted method in streams.py

    • input: idxs_ds; idxs_seq (sorted from down- to upstream); confluences (based on number of upstream cells), river lengths, min threshold

    suggested algorithm

    from up- to downstream:
       while not at next confluence or less than n cells:
          get lengths & indices
       average lengths
    
    assert that the total river length is the same
    
    opened by DirkEilander 0
  • unexpected results adjust_dem

    unexpected results adjust_dem

    np.random.seed(11)
    dem = np.random.rand(15, 10)
    flwdir = pyflwdir.from_dem(dem, outlets='min')
    dem1 = flwdir.dem_adjust(dem)
    np.all((dem1 - flwdir.downstream(dem1))>=0)
    >> False
    
    invalid 
    opened by DirkEilander 0
  • subgrid floodplain schematization

    subgrid floodplain schematization

    a method that maps subgrid cells to river cell (at model resolution) outflow points based on D8, sorts the HAND values (in future also manning?) and returns the contributing area for fixed HAND intervals

    opened by DirkEilander 0
  • Question: How can I create shapefiles of the upstream area of >10,000 points?

    Question: How can I create shapefiles of the upstream area of >10,000 points?

    Hi,

    I would like to create polygons of the contributing area for > 10,000 points. I have all information that I need for this, including a 30m DEM, a river network (LineString geodataframe file) and derived flow direction raster. All compiled using pyflwdir.

    I'm currently using the pyflwdir tool, but this takes around 5 minutes per point.. Has anyone any clues on how to approach this easier and quicker? Also, to insert the streams from a LineString geodataframe (instead of this flow.stream_order() > 4 command). This is my current script:

    with rasterio.open(flwdir_fn, "r") as src: flwdir = src.read(1) crs = src.crs flw = pyflwdir.from_array( flwdir, ftype="d8", transform=src.transform, latlon=crs.is_geographic, cache=True, )

    for point in geo_df["geometry"].head(): x = point.x y = point.y print(x, y) subbasins = flw.basins(xy=(x, y), streams=flw.stream_order() >= 4)

    Many many thanks!

    opened by MauKruisheer 2
  • Subcatchment routing hierarchy by landuse

    Subcatchment routing hierarchy by landuse

    Dear all,

    I am trying to resemble a functionality similar to GisToSWMM using pyflwdir. In my understanding of this algorithm a subcatchment hierarchy is determined considering landuse. Every (sub-)subcatchment contains only one landuse and routes either to another (sub-)subcatchment or an outlet. Outlets are given in the form of enlarged nodes, thus rasterized sink information derived from outlet nodes and DEM sinks.

    Reading through pyflwdir documentation I figure that the Pfafstetter method allows for deriving hierarchical subcatchment information. If understood correctly it permits to figure out a form of subcatchment routing, thus which subcatchment drains to another subcatchment or outlet.

    I am seeking ways to mask this routing method using landuse, so that every subcatchment contains only one landuse. Any ideas if and/or how to achieve this goal are highly appreciated.

    Kind regards

    Sebastian

    opened by schoeller 8
  • prettify IHU upscaling code

    prettify IHU upscaling code

    • streams array can be simplified, no need to save cell indices
    • lots of duplicate code between upscale_check and upscale_error methods
    • remove unused code
    • improve comments. consistent use of pixel vs cell in terminology.
    opened by DirkEilander 0
Releases(v0.5.6)
  • v0.5.6(Nov 15, 2022)

    New

    • FlwdirRaster.smooth_rivlen method to smooth river length with a moving window operation over a river network.

    Changed

    • Move to flit and pyproject.toml for installation and publication
    • drop support for python 3.7
    • update docs to Sphinx pydata style
    Source code(tar.gz)
    Source code(zip)
  • v0.5.5(Feb 16, 2022)

    New

    • read_nextxy method to read binary nextxy data

    Bugfix

    • Support -9 (river outlet at ocean) and -10 (inland river pit) pit values for nextxy data
    • Fix 'argmin of an empty sequence' error in dem_dig_d4

    Improved

    • improve gvf and manning estimates in river_depth method
    Source code(tar.gz)
    Source code(zip)
  • v0.5.4(Jan 18, 2022)

    Improved

    • prioritize non-boundary cells with same elevation over boundary cells in dem.fill_depressions #17

    Bugfix

    • fix dem_adjust method #16
    Source code(tar.gz)
    Source code(zip)
  • v0.5.3(Nov 18, 2021)

    Improved

    • add new idxs_pit argument to dem.fill_despressions

    Bugfix

    • min_rivdph argument was not always applied in FlwdirRaster.river_depth
    Source code(tar.gz)
    Source code(zip)
  • v0.5.2(Nov 17, 2021)

    New

    • Flwdir.river_depth for gradually varying flow (gvf) and manning river depth estimation
    • Flwdir.path method to get the indices of flow paths for vector flow directions

    Improved

    • FlwdirRaster.streams method includes a (zero-length) line at pits and adds option for trace direction in combination with segment end cells.
    • Moved accuflux method from FlwdirRaster to parent Flwdir class.
    • Additional how argument in fillnodata to indicate how to combine values at confluences. Min, max, sum and mean are supported.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.1(Oct 3, 2021)

  • v0.5(Sep 28, 2021)

    New

    • General Flwdir object for 1D vector based (instead of raster based) flow directions
    • flwdir.from_dataframe methods to derive a Flwdir object from a (Geo)DataFrame based on the row index and a column with downstream row indices.
    • dem.fill_depressions and pyflwdir.from_dem methods to derive flow directions from DEMs based on Wang & Lui (2015)
    • gis_utils.get_edge method to get a boolean mask of valid cells at the interface with nodata cells or the array edge.
    • gis_utils.spread2d method to spread valid values on a 2D raster with optional friction and mask rasters
    • FlwdirRaster.dem_dig_d4 method to adjust a DEM such that each cell has a 4D neighbor with equal or lower elevation.
    • FlwdirRaster.fillnodata method fill nodata gaps by propagating valid values up or downstream.
    • region.region_outlets method; which is also wrapped in the new FlwdirRaster.basin_outlets method
    • region.region_dissolve method to dissovle regions into their nearest neighboring region
    • FlwdirRaster.subbasins_areas method to derive subbasins based on a minimal area threshold

    Improved

    • added type="classis" for bottum-up stream order to FlwdirRaster.stream_order, default is type="strahler"
    • return subbasin outlet indices for all FlwdirRaster.subbasin* methods
    • improved subgrid slope method with optional lstsq regression based slope
    • FlwdirRaster.streams takes an optional idxs_out argument to derive stream vectors for unit catchments
    • FlwdirRaster.streams takes an optional max_len argument to split large segments into multiple smaller ones.
    • Using the new Flwdir object as common base of FlwdirRaster to share methods and properties
    • gis_utils.IDENTITY transform has North -> South orientation (yres < 0) instead of S->N orientation which is in line with flow direction rasters.
    • new restrict_strord argument in FlwdirRaster.moving_average and FlwdirRaster.moving_median methods to restrict the moving window to cells with same or larger stream order.

    Bugfix

    • strahler stream_order method gave incorrect results
    • basins.subbasins_pfafstetter reimplementation to fix mall functioning when jitted
    • FlwdirRaster.streams fix when called with optional min_sto argument

    Deprecated

    • FlwdirRaster.main_tributaries method is deprecated due to mallfunctioning when jitted
    • FlwdirRaster.inflow_idxs and FlwdirRaster.outflow_idxs
    Source code(tar.gz)
    Source code(zip)
Owner
Deltares
Deltares is an independent institute for applied research in the field of water and subsurface.
Deltares
TensorFlow-LiveLessons - "Deep Learning with TensorFlow" LiveLessons

TensorFlow-LiveLessons Note that the second edition of this video series is now available here. The second edition contains all of the content from th

Deep Learning Study Group 830 Jan 03, 2023
This is an official PyTorch implementation of Task-Adaptive Neural Network Search with Meta-Contrastive Learning (NeurIPS 2021, Spotlight).

NeurIPS 2021 (Spotlight): Task-Adaptive Neural Network Search with Meta-Contrastive Learning This is an official PyTorch implementation of Task-Adapti

Wonyong Jeong 15 Nov 21, 2022
Selene is a Python library and command line interface for training deep neural networks from biological sequence data such as genomes.

Selene is a Python library and command line interface for training deep neural networks from biological sequence data such as genomes.

Troyanskaya Laboratory 323 Jan 01, 2023
A PyTorch implementation of Multi-digit Number Recognition from Street View Imagery using Deep Convolutional Neural Networks

SVHNClassifier-PyTorch A PyTorch implementation of Multi-digit Number Recognition from Street View Imagery using Deep Convolutional Neural Networks If

Potter Hsu 182 Jan 03, 2023
Deep Latent Force Models

Deep Latent Force Models This repository contains a PyTorch implementation of the deep latent force model (DLFM), presented in the paper, Compositiona

Tom McDonald 5 Oct 26, 2022
SwinTrack: A Simple and Strong Baseline for Transformer Tracking

SwinTrack This is the official repo for SwinTrack. A Simple and Strong Baseline Prerequisites Environment conda (recommended) conda create -y -n SwinT

LitingLin 196 Jan 04, 2023
Hyperbolic Image Segmentation, CVPR 2022

Hyperbolic Image Segmentation, CVPR 2022 This is the implementation of paper Hyperbolic Image Segmentation (CVPR 2022). Repository structure assets :

Mina Ghadimi Atigh 46 Dec 29, 2022
PyTorch implementation of EfficientNetV2

[NEW!] Check out our latest work involution accepted to CVPR'21 that introduces a new neural operator, other than convolution and self-attention. PyTo

Duo Li 375 Jan 03, 2023
πŸ“š Papermill is a tool for parameterizing, executing, and analyzing Jupyter Notebooks.

papermill is a tool for parameterizing, executing, and analyzing Jupyter Notebooks. Papermill lets you: parameterize notebooks execute notebooks This

nteract 5.1k Jan 03, 2023
Modeling Temporal Concept Receptive Field Dynamically for Untrimmed Video Analysis

Modeling Temporal Concept Receptive Field Dynamically for Untrimmed Video Analysis This is a PyTorch implementation of the model described in our pape

qzhb 6 Jul 08, 2021
Stereo Hybrid Event-Frame (SHEF) Cameras for 3D Perception, IROS 2021

For academic use only. Stereo Hybrid Event-Frame (SHEF) Cameras for 3D Perception Ziwei Wang, Liyuan Pan, Yonhon Ng, Zheyu Zhuang and Robert Mahony Th

Ziwei Wang 11 Jan 04, 2023
A compendium of useful, interesting, inspirational usage of pandas functions, each example will be an ipynb file

Pandas_by_examples A compendium of useful/interesting/inspirational usage of pandas functions, each example will be an ipynb file What is this reposit

Guangyuan(Frank) Li 32 Nov 20, 2022
A vanilla 3D face modeling on pose-invariant and multi-lightning image data

3D-Face-Modeling A vanilla 3D face modeling on pose-invariant and multi-lightning image data Table of Contents Background Install Usage Contributing B

Haochen Zhang 1 Mar 12, 2022
Attendance Monitoring with Face Recognition using Python

Attendance Monitoring with Face Recognition using Python A python GUI integrated attendance system using face recognition to take attendance. In this

Vaibhav Rajput 2 Jun 21, 2022
A large dataset of 100k Google Satellite and matching Map images, resembling pix2pix's Google Maps dataset.

Larger Google Sat2Map dataset This dataset extends the aerial ⟷ Maps dataset used in pix2pix (Isola et al., CVPR17). The provide script download_sat2m

34 Dec 28, 2022
MixRNet(Using mixup as regularization and tuning hyper-parameters for ResNets)

MixRNet(Using mixup as regularization and tuning hyper-parameters for ResNets) Using mixup data augmentation as reguliraztion and tuning the hyper par

Bhanu 2 Jan 16, 2022
[제 13회 νˆ¬λΉ…μŠ€ 컨퍼런슀] OK Mugle! - μž₯λ₯΄λΆ€ν„° λ©œλ‘œλ””κΉŒμ§€, Content-based Music Recommendation

Ok Mugle! 🎡 μž₯λ₯΄λΆ€ν„° λ©œλ‘œλ””κΉŒμ§€, Content-based Music Recommendation 'Ok Mugle!'은 제13회 νˆ¬λΉ…μŠ€ 컨퍼런슀(2022.01.15)μ—μ„œ μ§„ν–‰ν•œ μŒμ•… μΆ”μ²œ ν”„λ‘œμ νŠΈμž…λ‹ˆλ‹€. Description πŸ“– λ³Έ ν”„λ‘œμ νŠΈμ—μ„œλŠ” Kakao

SeongBeomLEE 5 Oct 09, 2022
ICCV2021 Papers with Code

ICCV2021 Papers with Code

Amusi 1.4k Jan 02, 2023
a generic C++ library for image analysis

VIGRA Computer Vision Library Copyright 1998-2013 by Ullrich Koethe This file is part of the VIGRA computer vision library. You may use,

Ullrich Koethe 378 Dec 30, 2022
This repository contains codes of ICCV2021 paper: SO-Pose: Exploiting Self-Occlusion for Direct 6D Pose Estimation

SO-Pose This repository contains codes of ICCV2021 paper: SO-Pose: Exploiting Self-Occlusion for Direct 6D Pose Estimation This paper is basically an

shangbuhuan 52 Nov 25, 2022