Skip to content

Commit

Permalink
check simple geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
Shalucik committed Jun 5, 2024
1 parent b82070a commit 27c21d3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The current checks are (see also the 'show-validations' command):
| RC20 | It is recommended that all (MULTI)POLYGON geometries have a counter-clockwise orientation for their exterior ring, and a clockwise direction for all interior rings. |
| RQ21 | All layer and column names shall not be longer than 57 characters. |
| RQ22 | Only the following EPSG spatial reference systems are allowed: 28992, 3034, 3035, 3040, 3041, 3042, 3043, 3044, 3045, 3046, 3047, 3048, 3049, 3857, 4258, 4326, 4936, 4937, 5730, 7409. |
| RQ23 | Geometry should be simple. |
| UNKNOWN_WARNINGS | It is recommended that the unexpected (GDAL) warnings are looked into. |

\* Legacy requirements are only executed with the validate command when explicitly requested in the validation set.
Expand Down
4 changes: 4 additions & 0 deletions geopackage_validator/validations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
GeometryTypeEqualsGpkgDefinitionValidator,
)
from geopackage_validator.validations.geometry_valid_check import ValidGeometryValidator
from geopackage_validator.validations.geometry_simple_check import (
SimpleGeometryValidator,
)
from geopackage_validator.validations.layerfeature_check import (
OGRIndexValidator,
NonEmptyLayerValidator,
Expand Down Expand Up @@ -45,6 +48,7 @@
"FeatureIdValidator",
"GeometryTypeValidator",
"ValidGeometryValidator",
"SimpleGeometryValidator",
"OGRIndexValidator",
"NonEmptyLayerValidator",
"LayerNameValidator",
Expand Down
45 changes: 45 additions & 0 deletions geopackage_validator/validations/geometry_simple_check.py
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_TEMPLATE = """SELECT
count(rowid) AS count,
cast(rowid AS INTEGER) AS row_id
FROM "{table_name}" WHERE ST_IsSimple("{column_name}") = 0"""


def query_geometry_simple(dataset) -> Iterable[Tuple[str, str, 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:
if count > 0:
yield table_name, column_name, count, row_id
dataset.ReleaseResultSet(validations)


class SimpleGeometryValidator(validator.Validator):
"""Geometries should be simple."""

code = 23
level = validator.ValidationLevel.ERROR
message = "Found not simple geometry in table: {table_name}, column {column_name}, {count} {count_label}, example id {row_id}"

def check(self) -> Iterable[str]:
result = query_geometry_simple(self.dataset)

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
]
Binary file added tests/data/test_geometry_simple.gpkg
Binary file not shown.
1 change: 1 addition & 0 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_determine_validations_to_use_none():
"RQ15",
"RQ21",
"RQ22",
"RQ23",
"RC17",
"RC18",
"RC19",
Expand Down
18 changes: 18 additions & 0 deletions tests/validations/test_geometry_simple_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from geopackage_validator.utils import open_dataset
from geopackage_validator.validations.geometry_simple_check import query_geometry_simple


def test_with_gpkg():
dataset = open_dataset("tests/data/test_geometry_simple.gpkg")
checks = list(query_geometry_simple(dataset))
assert len(checks) == 1
assert checks[0][0] == "test_geometry_simple"
assert checks[0][1] == "geometry"
assert checks[0][2] == 1
assert checks[0][3] == 1


def test_with_gpkg_allcorrect():
dataset = open_dataset("tests/data/test_allcorrect.gpkg")
checks = list(query_geometry_simple(dataset))
assert len(checks) == 0

0 comments on commit 27c21d3

Please sign in to comment.