Skip to content

Commit

Permalink
- Test and fix observation_points.clear function
Browse files Browse the repository at this point in the history
  • Loading branch information
Leynse committed Feb 20, 2025
1 parent b26fe77 commit ec24ab5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
14 changes: 8 additions & 6 deletions hydromt_sfincs/observation_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ def set(self, gdf: gpd.GeoDataFrame, merge: bool = True):
raise ValueError("Observation points must be of type Point.")

# Clip points outside of model region:
within = gdf.within(
self.model.region.unary_union
) # same as 'inpolygon' function
within = gdf.within(self.model.region.unary_union)
# within = gdf.within(self.model.region.union_all)
# > FIXME - tried to overcome deprecation warning of unary_union, but suggested alternative does not work
# NOTE - .within does same as 'inpolygon' function

if within.any() == True:
if within.all() == False:
# keep points that fall within region
Expand All @@ -128,7 +130,7 @@ def set(self, gdf: gpd.GeoDataFrame, merge: bool = True):
if merge and self.data is not None:
gdf0 = self.data
gdf = gpd.GeoDataFrame(pd.concat([gdf, gdf0], ignore_index=True))
self.logger.info("Adding new observation points to existing ones.")
logger.info("Adding new observation points to existing ones.")

self._data = gdf # set gdf in self.data

Expand Down Expand Up @@ -194,11 +196,11 @@ def delete(
raise ValueError("One of the indices exceeds length of index range!")

self.data.drop(index).reset_index(drop=True)
self.logger.info("Dropping point(s) from observations")
logger.info("Dropping point(s) from observations")

def clear(self):
"""Clean GeoDataFrame with observation points."""
self.data = gpd.GeoDataFrame()
self._data = gpd.GeoDataFrame()

# %% DDB GUI focused additional functions:
# add_point
Expand Down
16 changes: 13 additions & 3 deletions tests/test_observation_points.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import geopandas as gpd
from pyproj import CRS
import os
from os.path import join
Expand Down Expand Up @@ -30,7 +31,7 @@ def test_observation_points_io(model_config, tmp_path):
assert obs0.equals(obs1)


def test_observation_points_create(model_config, tmp_path):
def test_observation_points_create(model_config):
# goal: test if obsfile can be made from an existing geojson
# goal: compare to similar values from existing ascii sfincs.obs file
# goal: check behaviour merge = False and True
Expand Down Expand Up @@ -60,9 +61,18 @@ def test_observation_points_create(model_config, tmp_path):
assert obs2.size == 12 # (6,2) > now 6 points


# model_config.observation_points.clear() #> does not work?
def test_observation_points_clear(model_config):
# load including data
obs0 = model_config.observation_points.data

# call clear
model_config.observation_points.clear()

# check if actually cleared
assert model_config.observation_points.data.empty


# def test_observation_points_set(model):
# def test_observation_points_add_delete(model):
# goal: check if point can be added (one in and one outside of region)
# goal: check if points outside of region are actually clipped

Expand Down

0 comments on commit ec24ab5

Please sign in to comment.