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

Rws network #67

Merged
merged 53 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0571783
addes network-class
DanielTollenaar Nov 20, 2023
743b0c2
added snapping within tolerance
DanielTollenaar Nov 21, 2023
b40aa21
Merge branch 'main' into network-class
DanielTollenaar Nov 21, 2023
9e8e4ee
Update network.py
DanielTollenaar Nov 21, 2023
50a0701
fix tests
DanielTollenaar Nov 21, 2023
8cbfb55
updated network-class
DanielTollenaar Nov 22, 2023
433e1b3
Create kunstwerken.py
ngoorden Nov 23, 2023
4eaaefb
commit
ngoorden Nov 23, 2023
a96cae0
Update kunstwerken.py
ngoorden Nov 23, 2023
26c15dd
Update network.py
DanielTollenaar Nov 23, 2023
2b34308
Merge branch 'RWS-network' of https://github.com/Deltares/Ribasim-NL …
DanielTollenaar Nov 23, 2023
eab038e
major improvements to network class
DanielTollenaar Nov 23, 2023
7880124
Update kunstwerken.py
ngoorden Nov 24, 2023
f2772cb
Merge branch 'RWS-network' of https://github.com/Deltares/Ribasim-NL …
ngoorden Nov 24, 2023
eecfed2
netwerk
DanielTollenaar Nov 30, 2023
29a2b99
sample raster
DanielTollenaar Nov 30, 2023
c6d33d5
Update netwerk_2.py
ngoorden Nov 30, 2023
1375fbd
Merge branch 'RWS-network' of https://github.com/Deltares/Ribasim-NL …
ngoorden Nov 30, 2023
9ef9188
network2 and create raster
DanielTollenaar Dec 6, 2023
3e46fca
commit
ngoorden Dec 7, 2023
7f0162b
misc changes
DanielTollenaar Dec 8, 2023
a144ce8
Merge branch 'main' into RWS-network
DanielTollenaar Dec 8, 2023
423867e
driel
DanielTollenaar Dec 14, 2023
0968d9f
merge_models
DanielTollenaar Jan 8, 2024
33d4add
Update kunstwerk.py
ngoorden Jan 19, 2024
1678d1d
Update kunstwerk.py
DanielTollenaar Jan 24, 2024
aa3dce1
Merge branch 'main' into RWS-network
DanielTollenaar Jan 24, 2024
8d73fa8
Update kunstwerk.py
ngoorden Jan 25, 2024
57430bb
Update kunstwerk.py
DanielTollenaar Jan 25, 2024
468c727
Update netwerk.py
ngoorden Jan 25, 2024
e92e060
Merge branch 'RWS-network' of https://github.com/Deltares/Ribasim-NL …
ngoorden Jan 25, 2024
589a321
Update netwerk.py
DanielTollenaar Jan 25, 2024
4d1d2c0
updated Network & reset index
DanielTollenaar Jan 25, 2024
ccb7e6c
misc changes
DanielTollenaar Jan 29, 2024
4fd4843
Create concat.py
DanielTollenaar Jan 29, 2024
8a55e63
verdeelsleutels
DanielTollenaar Feb 2, 2024
96cf0f9
Update kunstwerk.py
ngoorden Feb 7, 2024
bbd643c
working concat and more
DanielTollenaar Feb 15, 2024
5ea3eac
Update samenvoegen_modellen.py
DanielTollenaar Feb 16, 2024
e6340fe
areas and hydroobjects
DanielTollenaar Feb 16, 2024
1262ebd
reogranized rws procssing-scripts
DanielTollenaar Feb 19, 2024
58fa64b
Update .vscode/settings.json
Feb 26, 2024
a450ebc
Update src/ribasim_nl/ribasim_nl/discrete_control.py
Feb 26, 2024
ab4e5ac
Update src/ribasim_nl/ribasim_nl/geodataframe.py
Feb 26, 2024
6f518e7
Update geodataframe.py
DanielTollenaar Feb 26, 2024
1206c7c
Update src/ribasim_nl/ribasim_nl/model.py
Feb 26, 2024
3ef9a16
Update src/ribasim_nl/ribasim_nl/verdeelsleutels.py
Feb 26, 2024
90ec9dd
Update src/ribasim_nl/ribasim_nl/network.py
Feb 26, 2024
a1d3257
Update src/ribasim_nl/ribasim_nl/network.py
Feb 26, 2024
361c74a
removed # %%
DanielTollenaar Feb 26, 2024
9553e47
Merge branch 'RWS-network' of https://github.com/Deltares/Ribasim-NL …
DanielTollenaar Feb 26, 2024
28b55c8
Update 1_create_bathymetrie.py
DanielTollenaar Feb 26, 2024
aef657c
Merge branch 'main' into RWS-network
DanielTollenaar Feb 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notebooks/nl-kunstwerken.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
236 changes: 236 additions & 0 deletions notebooks/rijkswaterstaat/1_create_bathymetrie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
# %%
import itertools
import math
from functools import partial

import fiona
import geopandas as gpd
import numpy as np
import rasterio
from geocube.api.core import make_geocube
from geocube.rasterize import rasterize_points_griddata
from rasterio import features, merge # noqa: F401
from rasterio.enums import Resampling
from rasterio.transform import from_origin
from rasterio.windows import from_bounds
from ribasim_nl import CloudStorage
from shapely.geometry import MultiPolygon, box

cloud = CloudStorage()


out_dir = cloud.joinpath("Rijkswaterstaat", "verwerkt", "bathymetrie")
out_dir.mkdir(exist_ok=True)
baseline_file = cloud.joinpath(
"baseline-nl_land-j23_6-v1", "baseline.gdb"
) # dit bestand is read-only voor D2HYDRO ivm verwerkersovereenkomst
layer = "bedlevel_points"

krw_poly_gpkg = cloud.joinpath(
"Basisgegevens", "KRW", "krw_oppervlaktewaterlichamen_nederland_vlakken.gpkg"
)

bathymetrie_nl = cloud.joinpath("Rijkswaterstaat", "aangeleverd", "bathymetrie")

res = 5
tile_size = 10000
bounds = (0, 300000, 320000, 660000)
nodata = -9999

# %%

datasets = [rasterio.open(i) for i in bathymetrie_nl.glob("*NAP.tif")]

data, transform = rasterio.merge.merge(
datasets, bounds=bounds, res=(res, res), nodata=nodata
)

data = np.where(data != nodata, data * 100, nodata).astype("int16")

profile = {
"driver": "GTiff",
"dtype": "int16",
"nodata": nodata,
"width": data.shape[2],
"height": data.shape[1],
"count": 1,
"crs": 28992,
"transform": transform,
"blockxsize": 256,
"blockysize": 256,
"compress": "deflate",
}

with rasterio.open(out_dir / "bathymetrie-nl.tif", mode="w", **profile) as dst:
dst.write(data)
dst.build_overviews([5, 20], Resampling.average)
dst.update_tags(ns="rio_overview", resampling="average")
dst.scales = (0.01,)


# %%
print("read mask")
water_geometries = gpd.read_file(out_dir / "water-mask.gpkg", engine="pyogrio")
with fiona.open(baseline_file, layer=layer) as src:
xmin, ymin, xmax, ymax = src.bounds
xmin = math.floor(xmin / tile_size) * tile_size
ymin = math.floor(ymin / tile_size) * tile_size
xmax = math.ceil(xmax / tile_size) * tile_size
ymax = math.ceil(ymax / tile_size) * tile_size
xmins = list(range(xmin, xmax, tile_size))
ymins = list(range(ymin, ymax, tile_size))
xymins = itertools.product(xmins, ymins)
transform = from_origin(xmin, ymax, res, res)

# %%
with rasterio.open(
out_dir / f"{layer}_{xmin}_{ymin}_{xmax}_{ymax}.tif",
mode="w",
driver="GTiff",
dtype="int16",
nodata=-32768.0,
count=1,
crs=28992,
compress="lzw",
tiled=True,
width=int((xmax - xmin) / res),
height=int((ymax - ymin) / res),
transform=transform,
) as dst:
profile = dst.profile
dst_bounds = dst.bounds
for xmin, ymin in xymins:
xmax = xmin + tile_size
ymax = ymin + tile_size

print(f"doing {xmin}, {ymin}, {xmax}, {ymax}")

bounds = (xmin, ymin, xmax, ymax)
area_poly = box(*bounds)
print("select water-mask geometries")
water_geometries_select = water_geometries[
water_geometries.intersects(area_poly)
]
if not water_geometries_select.empty:
area_poly_buffer = area_poly.buffer(res * 4)
print("read points")
gdf = gpd.read_file(
baseline_file,
layer=layer,
engine="pyogrio",
bbox=area_poly_buffer.bounds,
)
gdf = gdf.loc[(gdf.ELEVATION > -60) & (gdf.ELEVATION < 50)]

if not gdf.empty:
# we drop duplicated points within the same meter
print("drop duplicates")
gdf["x"] = gdf.geometry.x.astype(int)
gdf["y"] = gdf.geometry.y.astype(int)
gdf.drop_duplicates(["x", "y"], inplace=True)

print("make cube")
cube = make_geocube(
gdf,
measurements=["ELEVATION"],
resolution=(res, -res),
rasterize_function=partial(
rasterize_points_griddata, method="linear"
),
interpolate_na_method="nearest",
)

print("scale cube")
cube["ELEVATION"] = (cube.ELEVATION * 100).astype("int16")
cube.ELEVATION.attrs["_FillValue"] = -32768
cube.ELEVATION.attrs["scale_factor"] = 0.01

print("clip cube")
mask = water_geometries_select.geometry.unary_union.intersection(
area_poly
)
convex_hull = gdf.unary_union.convex_hull
if isinstance(mask, MultiPolygon):
mask = [
i.intersection(convex_hull)
for i in mask.geoms
if i.intersects(convex_hull)
]

else: # is one polygon
mask = [mask.intersection(convex_hull)]

cube = cube.rio.clip(mask)

if cube.ELEVATION.size > 0:
print("add to tiff")
window = from_bounds(*cube.rio.bounds(), transform)
dst.write(
np.fliplr(np.flipud(cube.ELEVATION)), window=window, indexes=1
)
else:
print("no cube.ELEVATION points within water-mask")
else:
print("no samples within boundary")
else:
print("no water-mask within bounds")

dst.build_overviews([5, 20], Resampling.average)
dst.update_tags(ns="rio_overview", resampling="average")
dst.scales = (0.01,)

# %%

# read bathymetrie-nl and its spatial characteristics
with rasterio.open(out_dir / "bathymetrie-nl.tif") as src:
bathymetry_nl_data = src.read(1)
bathymetry_nl_data = np.where(
bathymetry_nl_data == src.nodata, nodata, bathymetry_nl_data
)
bounds = src.bounds
transform = src.transform
profile = src.profile
scales = src.scales


with rasterio.open(out_dir / "bedlevel_points_-20000_300000_320000_660000.tif") as src:
window = from_bounds(*bounds, src.transform)
baseline_data = src.read(1, window=window)
baseline_data = np.where(baseline_data == src.nodata, nodata, baseline_data)


# data = baseline_data

shapes = (i.geometry for i in water_geometries.itertuples() if i.baseline)

baseline_mask = rasterio.features.rasterize(
shapes,
out_shape=bathymetry_nl_data.shape,
transform=transform,
fill=0,
default_value=1,
all_touched=True,
dtype="int8",
).astype(bool)

data = np.where(baseline_mask, baseline_data, bathymetry_nl_data)

profile = {
"driver": "GTiff",
"dtype": "int16",
"nodata": nodata,
"width": data.shape[1],
"height": data.shape[0],
"count": 1,
"crs": 28992,
"transform": transform,
"blockxsize": 256,
"blockysize": 256,
"compress": "deflate",
}

with rasterio.open(out_dir / "bathymetrie-merged.tif", mode="w", **profile) as dst:
dst.write(data, 1)
dst.build_overviews([5, 20], Resampling.average)
dst.update_tags(ns="rio_overview", resampling="average")
dst.scales = (0.01,)
Loading
Loading