Comments (8)
Hi Sebastian,
Nice application! I think that should be possible but not with the pfafstetter method. I would suggest to try the following:
- Create regions from the landuse map, using e.g. scipy.ndimage.label. This returns a map with unique IDs for connected regions with the same landuse.
- Find the most downstream "outflow pixel" of each region using pyflwdir.FlwdirRaster.inflow_idxs Note: should still be added to the API Reference in the docs. This returns a list with linear indices of outflow pixels.
- Derive subbasins using pyflwdir.FlwdirRaster.basins based on the indices of the outflow pixels from step 2, supplied using the
idxs_out
option. This returns a map with unique IDs for each basin. - Find the relations between the basins using pyflwdir.FlwdirRaster.streams, again based on the indices of the outflow pixels from step 2, supplied using the
idxs_out
option. This returns geofeatures with river segments between outflow pixels and per segment the index of the next downstream segment (and thus basin).
Hope this is helpful!
from pyflwdir.
@DirkEilander many thanks for the detailed response.
I am stumbling in step 1. I have read a geodataframe for landuse from a gpkg layer and converted to xarray + calling of label as follows:
lu_grid = make_geocube(
vector_data=gdf_lu,
output_crs=gdf_lu.crs.to_epsg(),
resolution=(-1, 1),
)
s = generate_binary_structure(2,2)
lu_labeled_array, lu_num_features = label(lu_grid.type, s)
lu_num_features contains one solutions only which is not desired taking below variable content:
print(lu_grid.type)
<xarray.DataArray 'type' (y: 231, x: 204)>
array([[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
...,
[nan, nan, nan, ..., 30., 61., nan],
[nan, nan, nan, ..., 61., 30., nan],
[nan, nan, nan, ..., 61., 61., 30.]])
Coordinates:
* y (y) float64 230.5 229.5 228.5 227.5 226.5 ... 3.5 2.5 1.5 0.5
* x (x) float64 17.5 18.5 19.5 20.5 ... 217.5 218.5 219.5 220.5
spatial_ref int32 0
Attributes:
name: type
long_name: type
_FillValue: nan
Hints on where to dig further are highly welcome.
from pyflwdir.
Note that this goes beyond pyflwdir, so I'm just guessing here. You might have to pass a numpy.ndarray instead of xarray.DataArray to the label method, so lu_labeled_array, lu_num_features = label(lu_grid.type.values, s)
. I'm also not sure how the label method deals with nan values.
from pyflwdir.
@DirkEilander looks as if this works to deliver a dataarray with reasonable results. Crawling along...
from xrspatial.zonal import regions
lu_regions = regions(raster=lu_grid.type,neighborhood=8)
from pyflwdir.
@schoeller Curious to hear how this is going.
from pyflwdir.
@DirkEilander Efforts have been laying low since 1 month now. Have documented scripting efforts under https://gist.github.com/schoeller/ed0cfa4c163edaa4cde19d8cf8c891ef
from pyflwdir.
@DirkEilander: I have started over my efforts. Being nooby I have stumbled over your first point. I have used the original raster files from the reference project. The file looks as below:
I have tried the following code:
import rasterio
import scipy
with rasterio.open('GIS/raster_landuse.tif', nodata=0) as src:
labeled_array, num_features = scipy.ndimage.label(src.read(1).astype(int))
print(num_features)
num_features throws 1 as a result, which I do not consider reasonable. Maybe you can spare time to pinpoint me into the right direction.
Best
Sebastian
from pyflwdir.
Hi Sebastian,
From the docs of scipy.ndimage.label method: "Any non-zero values in input are counted as features and zero values are considered the background." Seeing the picture if looks like most non-zero (greyscale) pixels are connected right? If you use want just want to label the black areas you could try adjusting the input the label method using e.g. "src.read(1).astype(int) > x".
Note that this is not pyflwdir functionality so for help on this method it's best to use Stack Overflow or the scipy issue trackers.
from pyflwdir.
Related Issues (20)
- Distance to river network HOT 3
- User guide instructions for plotting import error HOT 2
- Failed read MERIT flowdir HOT 4
- unexpected results adjust_dem
- prioritize non-boundary cells with same elevation over boundary cells in dem.fill_depressions
- fix version number in docs
- use pyflwdir package to parse nextxy data of CaMa-Flood! HOT 8
- error in plotting the river mouths using the nextxy.bin dataset HOT 7
- increase short river lengths
- Question: How can I create shapefiles of the upstream area of >10,000 points? HOT 2
- subgrid floodplain schematization
- river branch map
- check compatibility with latest numpy and py3.11
- No implementation of function Function(<built-in function heappush>) found for signature HOT 5
- method to get all upstream nodes for vector based flow directions
- working with circular grids HOT 1
- FlwdirRaster.stream crashed on a ~44000x20000 raster on a machine with 16 GB or less
- FlwdirRaster.accuflux() segmentation faults with a raster ~ (63000, 80000) on machine with >1Tb RAM HOT 9
- Helper function to get coordinates from linear indices HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyflwdir.