-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from PDOK/PDOK/validate_empty_geoms
Check for empty geometries
- Loading branch information
Showing
7 changed files
with
105 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Iterable, Tuple | ||
from geopackage_validator.validations import validator | ||
from geopackage_validator import utils | ||
|
||
SQL_EMPTY_TEMPLATE = """SELECT count(row_id) AS count, row_id | ||
FROM( | ||
SELECT | ||
cast(rowid AS INTEGER) AS row_id | ||
FROM "{table_name}" WHERE ST_IsEmpty("{column_name}") = 1 | ||
);""" | ||
|
||
|
||
def query_geometry_empty(dataset, sql_template) -> Iterable[Tuple[str, str, int, int]]: | ||
columns = utils.dataset_geometry_tables(dataset) | ||
|
||
for table_name, column_name, _ in columns: | ||
validations = dataset.ExecuteSQL( | ||
sql_template.format(table_name=table_name, column_name=column_name) | ||
) | ||
for count, row_id in validations: | ||
yield table_name, column_name, count, row_id | ||
dataset.ReleaseResultSet(validations) | ||
|
||
|
||
class EmptyGeometryValidator(validator.Validator): | ||
"""Geometries should not be empty.""" | ||
|
||
code = 24 | ||
level = validator.ValidationLevel.ERROR | ||
message = "Found empty geometry in table: {table_name}, column {column_name}, {count} {count_label}, example id {row_id}" | ||
|
||
def check(self) -> Iterable[str]: | ||
result = query_geometry_empty(self.dataset, SQL_EMPTY_TEMPLATE) | ||
|
||
return [ | ||
self.message.format( | ||
table_name=table_name, | ||
column_name=column_name, | ||
count=count, | ||
count_label=("time" if count == 1 else "times"), | ||
row_id=row_id, | ||
) | ||
for table_name, column_name, count, row_id in result | ||
if count > 0 | ||
] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from geopackage_validator.utils import open_dataset | ||
from geopackage_validator.validations.geometry_empty_check import ( | ||
EmptyGeometryValidator, | ||
) | ||
|
||
|
||
def test_with_gpkg_empty(): | ||
dataset = open_dataset("tests/data/test_geometry_empty.gpkg") | ||
result = list(EmptyGeometryValidator(dataset).check()) | ||
assert len(result) == 1 | ||
assert ( | ||
result[0] | ||
== "Found empty geometry in table: stations, column geom, 45 times, example id 129" | ||
) | ||
|
||
|
||
def test_with_gpkg_allcorrect(): | ||
dataset = open_dataset("tests/data/test_allcorrect.gpkg") | ||
result = list(EmptyGeometryValidator(dataset).check()) | ||
assert len(result) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters