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

266 support stopbanks down #267

Merged
merged 14 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "geofabrics"
version = "1.1.22"
version = "1.1.23"
description = "A package for creating geofabrics for flood modelling."
readme = "README.md"
authors = [{ name = "Rose pearson", email = "rose.pearson@niwa.co.nz" }]
Expand Down
108 changes: 83 additions & 25 deletions src/geofabrics/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import typing
import pathlib
import geopandas
import pandas
import shapely
import dask
import dask.array
Expand Down Expand Up @@ -42,11 +43,11 @@ def chunk_mask(mask, chunk_size):
return mask


def clip_mask(arr, geometry, chunk_size):
def clip_mask(arr, geometry, chunk_size, invert=False):
mask = (
xarray.ones_like(arr, dtype=numpy.float16)
.compute()
.rio.clip(geometry, drop=False)
.rio.clip(geometry, drop=False, invert=invert)
.notnull()
)
if chunk_size is not None:
Expand Down Expand Up @@ -298,6 +299,7 @@ class DemBase(abc.ABC):
"coarse DEM": 5,
"patch": 6,
"stopbanks": 7,
"masked feature": 8,
"interpolated": 0,
"no data": -1,
}
Expand Down Expand Up @@ -351,14 +353,28 @@ def save_dem(
), "all DataArray variables of a xarray.Dataset must have a CRS"

try:
for key in dem.data_vars:
dem[key] = dem[key].astype(geometry.RASTER_TYPE)
self._write_netcdf_conventions_in_place(dem, self.catchment_geometry.crs)
if filename.suffix.lower() == ".nc":
if compression is not None:
compression["grid_mapping"] = dem.encoding["grid_mapping"]
encoding_keys = (
"_FillValue",
"dtype",
"scale_factor",
"add_offset",
"grid_mapping",
)
encoding = {}
for key in dem.data_vars:
compression["dtype"] = dem[key].dtype
encoding[key] = compression
encoding[key] = {
encoding_key: value
for encoding_key, value in dem[key].encoding.items()
if encoding_key in encoding_keys
}
if "dtype" not in encoding[key]:
encoding[key]["dtype"] = dem[key].dtype
encoding[key] = {**encoding[key], **compression}
dem.to_netcdf(
filename, format="NETCDF4", engine="netcdf4", encoding=encoding
)
Expand Down Expand Up @@ -401,7 +417,7 @@ def save_and_load_dem(
)
self.save_dem(filename=filename, dem=self._dem)
del self._dem
gc.collect()
gc.collect()
self._dem = self._load_dem(filename=filename)

@staticmethod
Expand Down Expand Up @@ -437,7 +453,6 @@ def _write_netcdf_conventions_in_place(
crs_dict
A dict with horizontal and vertical CRS information.
"""


dem.rio.write_crs(crs_dict["horizontal"], inplace=True)
dem.rio.write_transform(inplace=True)
Expand Down Expand Up @@ -1051,6 +1066,46 @@ def interpolate_ocean_bathymetry(self, bathy_contours, method="linear"):
method="first",
)

def clip_within_polygon(self, polygon_paths: list, label: str):
"""Clip existing DEM to remove areas within the polygons"""
crs = self.catchment_geometry.crs
dem_bounds = geopandas.GeoDataFrame(
geometry=[shapely.geometry.box(*self._dem.rio.bounds())],
crs=crs["horizontal"],
)
clip_polygon = []
for path in polygon_paths:
clip_polygon.append(geopandas.read_file(path).to_crs(crs["horizontal"]))
clip_polygon = pandas.concat(clip_polygon).dissolve()
clip_polygon = clip_polygon.clip(dem_bounds)
if clip_polygon.area.sum() > self.catchment_geometry.resolution**2:
self.logger.info(
f"Clipping to remove all features in polygons {polygon_paths}"
)
mask = clip_mask(
arr=self._dem.z,
geometry=clip_polygon.geometry,
chunk_size=self.chunk_size,
)
self._dem["z"] = self._dem.z.where(
~mask,
numpy.nan,
)
self._dem["data_source"] = self._dem.data_source.where(
~mask,
self.SOURCE_CLASSIFICATION[label],
)
self._dem["lidar_source"] = self._dem.lidar_source.where(
~mask, self.SOURCE_CLASSIFICATION["no data"]
)
self._write_netcdf_conventions_in_place(
self._dem, self.catchment_geometry.crs
)
else:
self.logger.warning(
f"No clipping. Polygons {polygon_paths} do not overlap DEM."
)

def interpolate_elevations_within_polygon(
self,
elevations: geometry.EstimatedElevationPoints,
Expand Down Expand Up @@ -2716,27 +2771,30 @@ def add_lidar(
self._write_netcdf_conventions_in_place(self._dem, self.catchment_geometry.crs)

def add_roads(self, roads_polygon: dict):
"""Set roads to paved and unpaved roughness values.
"""Set roads to paved and unpaved roughness values.

Parameters
----------
Parameters
----------

roads_polygon
Dataframe with polygon and associated roughness values
"""
roads_polygon
Dataframe with polygon and associated roughness values
"""


# Set unpaved roads
mask = clip_mask(
self._dem.z, roads_polygon[roads_polygon["surface"]=="unpaved"].geometry, self.chunk_size
)
self._dem["zo"] = self._dem.zo.where(~mask, self.default_values["unpaved"])
# Then set paved roads
mask = clip_mask(
self._dem.z, roads_polygon[roads_polygon["surface"]=="paved"].geometry, self.chunk_size
)
self._dem["zo"] = self._dem.zo.where(~mask, self.default_values["paved"])
self._write_netcdf_conventions_in_place(self._dem, self.catchment_geometry.crs)
# Set unpaved roads
mask = clip_mask(
self._dem.z,
roads_polygon[roads_polygon["surface"] == "unpaved"].geometry,
self.chunk_size,
)
self._dem["zo"] = self._dem.zo.where(~mask, self.default_values["unpaved"])
# Then set paved roads
mask = clip_mask(
self._dem.z,
roads_polygon[roads_polygon["surface"] == "paved"].geometry,
self.chunk_size,
)
self._dem["zo"] = self._dem.zo.where(~mask, self.default_values["paved"])
self._write_netcdf_conventions_in_place(self._dem, self.catchment_geometry.crs)

def _add_tiled_lidar_chunked(
self,
Expand Down
Loading
Loading