Skip to content

Commit

Permalink
CI for geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Sep 25, 2023
1 parent a919a12 commit 552b065
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CI/SCRIPTS/script_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def vectors_path():
return get_ci_data_path().joinpath("vectors")


def geometry_path():
return get_ci_data_path().joinpath("geometry")


def files_path():
return get_ci_data_path().joinpath("files")

Expand Down
36 changes: 30 additions & 6 deletions CI/SCRIPTS/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@
# limitations under the License.
""" Script testing vector functions """

from CI.SCRIPTS.script_utils import s3_env, vectors_path
from CI.SCRIPTS.script_utils import geometry_path, s3_env, vectors_path
from sertit import ci, geometry, vectors
from sertit.geometry import fill_polygon_holes, get_wider_exterior

ci.reduce_verbosity()


@s3_env
def test_get_wider_exterior():
"""Test get_wider_exterior"""
footprint_raw_path = geometry_path().joinpath("footprint_raw.geojson")
footprint_path = geometry_path().joinpath("footprint.geojson")
ci.assert_geom_equal(
get_wider_exterior(vectors.read(footprint_raw_path)),
vectors.read(footprint_path),
)


@s3_env
def test_simplify_footprint():
"""Test simplify footprint"""
complicated_footprint_path = vectors_path().joinpath(
complicated_footprint_path = geometry_path().joinpath(
"complicated_footprint_spot6.geojson"
)
max_nof_vertices = 40
Expand All @@ -51,16 +63,28 @@ def test_geometry_fct():
assert env.bounds == from_env.bounds


@s3_env
def test_make_valid():
"""Test make valid"""
broken_geom_path = vectors_path().joinpath("broken_geom.shp")
broken_geom_path = geometry_path().joinpath("broken_geom.shp")
broken_geom = vectors.read(broken_geom_path)
assert len(broken_geom[~broken_geom.is_valid]) == 1
valid = geometry.make_valid(broken_geom, verbose=True)
assert len(valid[~valid.is_valid]) == 0
assert len(valid) == len(broken_geom)


# Missing:
# - get_wider_exterior
# - fill_polygon_holes
@s3_env
def test_fill_polygon_holes():
"""Test fill_polygon_holes"""
water_path = geometry_path().joinpath("water.geojson")
water_none_path = geometry_path().joinpath("water_filled_none.geojson")
water_0_path = geometry_path().joinpath("water_filled_0.geojson")
water_1000_path = geometry_path().joinpath("water_filled_1000.geojson")
water = vectors.read(water_path)

ci.assert_geom_equal(fill_polygon_holes(water), vectors.read(water_none_path))
ci.assert_geom_equal(fill_polygon_holes(water, 0), vectors.read(water_0_path))
ci.assert_geom_equal(
fill_polygon_holes(water, threshold=1000), vectors.read(water_1000_path)
)
4 changes: 2 additions & 2 deletions CI/SCRIPTS/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def test_vectors():
@s3_env
def test_kmz():
"""Test KMZ files"""
kmz_path = vectors_path().joinpath("AOI.kmz")
gj_path = vectors_path().joinpath("AOI.geojson")
kmz_path = vectors_path().joinpath("AOI_kmz.kmz")
gj_path = vectors_path().joinpath("AOI_kmz.geojson")

# Read vectors
kmz = vectors.read(kmz_path)
Expand Down
10 changes: 6 additions & 4 deletions sertit/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def get_wider_exterior(vector: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
Returns:
vector: gpd.GeoDataFrame: Wider exterior
"""
vector = vector.explode(index_parts=True)

# Get the footprint max (discard small holes stored in other polygons)
wider = vector[vector.area == np.max(vector.area)]
Expand Down Expand Up @@ -193,14 +194,15 @@ def fill_polygon_holes(
gpd_results: gpd.GeoDataFrame, threshold: float = None
) -> gpd.GeoDataFrame:
"""
Fill holes over a given threshold for all polygons of a GeoDataFrame
Fill holes over a given threshold on the hole area (in meters) for all polygons of a GeoDataFrame.
If the threshold is set to None, every hole is filled.
Args:
gpd_results (gpd.GeoDataFrame): Geodataframe filled whith drilled polygons
threshold (float): Holes threshold
threshold (float): Holes area threshold, in meters. If set to None, every hole is filled.
Returns:
gpd.GeoDataFrame: GeoDataFrame updated
gpd.GeoDataFrame: GeoDataFrame with filled holes
"""

def _fill_polygon(polygon: Polygon, threshold: float = None):
Expand All @@ -221,7 +223,7 @@ def _fill_polygon(polygon: Polygon, threshold: float = None):
return Polygon(polygon.exterior)

# Ensure the geometries are valid
gpd_results.geometry = gpd_results.geometry.apply(make_valid)
gpd_results = make_valid(gpd_results)

# Check if vector is projected, if not convert it
with vectors.utm_crs(gpd_results) as utm_results:
Expand Down

0 comments on commit 552b065

Please sign in to comment.