From 59685699986b113a65de0b55a4d4b14de4814a2c Mon Sep 17 00:00:00 2001 From: Anton Bakker Date: Fri, 11 Aug 2023 17:58:58 +0200 Subject: [PATCH] add pytest unit tests --- .githooks/pre-commit | 2 +- .vscode/launch.json | 1 - .vscode/settings.json | 7 +- example/EXAMPLES.md => EXAMPLE_REQUESTS.md | 7 +- README.md | 6 + example/polygon.json | 388 ------------------ pyproject.toml | 14 +- pytest.ini | 2 + .../__init__.py | 0 .../assets/ __init__.py | 0 .../assets/logging.conf | 0 .../assets/openapi.yaml | 0 .../assets/static/__init__.py | 0 .../assets/static/favicon.ico | Bin .../fastapi_rfc7807/README.md | 0 .../fastapi_rfc7807/__init__.py | 0 .../fastapi_rfc7807/middleware.py | 0 .../fastapi_rfc7807/schema.py | 0 .../limit_middleware/__init__.py | 0 .../limit_middleware/middleware.py | 0 .../coordinates_transformation_api}/main.py | 1 - .../coordinates_transformation_api}/models.py | 0 .../settings.py | 0 .../coordinates_transformation_api}/util.py | 0 tests/__init__.py | 0 ...eature-collection-geometry-collection.json | 55 +++ tests/data/feature-geometry-collection.json | 44 ++ tests/data/feature.json | 11 + tests/data/geometry-collection.json | 39 ++ tests/data/geometry.json | 7 + tests/data/linestrings-multi.json | 17 + tests/data/linestrings-punten.json | 18 + tests/data/linestrings.json | 17 + tests/data/points.json | 133 ++++++ tests/data/polygons-multi.json | 18 + tests/data/polygons.json | 18 + tests/test_geojson_transformation.py | 120 ++++++ 37 files changed, 524 insertions(+), 401 deletions(-) rename example/EXAMPLES.md => EXAMPLE_REQUESTS.md (96%) delete mode 100644 example/polygon.json create mode 100644 pytest.ini rename {coordinates_transformation_api => src/coordinates_transformation_api}/__init__.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/assets/ __init__.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/assets/logging.conf (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/assets/openapi.yaml (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/assets/static/__init__.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/assets/static/favicon.ico (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/fastapi_rfc7807/README.md (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/fastapi_rfc7807/__init__.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/fastapi_rfc7807/middleware.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/fastapi_rfc7807/schema.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/limit_middleware/__init__.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/limit_middleware/middleware.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/main.py (99%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/models.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/settings.py (100%) rename {coordinates_transformation_api => src/coordinates_transformation_api}/util.py (100%) create mode 100644 tests/__init__.py create mode 100644 tests/data/feature-collection-geometry-collection.json create mode 100644 tests/data/feature-geometry-collection.json create mode 100644 tests/data/feature.json create mode 100644 tests/data/geometry-collection.json create mode 100644 tests/data/geometry.json create mode 100644 tests/data/linestrings-multi.json create mode 100644 tests/data/linestrings-punten.json create mode 100644 tests/data/linestrings.json create mode 100644 tests/data/points.json create mode 100644 tests/data/polygons-multi.json create mode 100644 tests/data/polygons.json create mode 100644 tests/test_geojson_transformation.py diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 3dbaaab..f87c6bf 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -34,7 +34,7 @@ command_available() { } command_available "mypy" print_message "pre-commit - running mypy static type checking" 1 "" true -if ! mypy coordinates_transformation_api;then +if ! mypy src;then print_message "mypy static type check failed - commit aborted" 1 "ERROR" true exit 1 fi diff --git a/.vscode/launch.json b/.vscode/launch.json index aca7b43..46c8e23 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,6 @@ "--reload", "--port", "8000", "coordinates_transformation_api.main:app", - ], "env": { "LOG_LEVEL": "DEBUG", diff --git a/.vscode/settings.json b/.vscode/settings.json index cee7b74..bf07da3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,10 @@ "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" }, - "python.formatting.provider": "none" + "python.formatting.provider": "none", + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true } diff --git a/example/EXAMPLES.md b/EXAMPLE_REQUESTS.md similarity index 96% rename from example/EXAMPLES.md rename to EXAMPLE_REQUESTS.md index 65e05e4..10f6eaa 100644 --- a/example/EXAMPLES.md +++ b/EXAMPLE_REQUESTS.md @@ -1,4 +1,9 @@ -# EXAMPLES +# EXAMPLE REQUESTS + +```sh +curl -X 'POST' 'http://localhost:8000/transform?source-crs=EPSG:28992&target-crs=EPSG:4326' -H 'Content-Type: application/json' -d @feature-geometry-collection.json +``` + ```sh curl -X 'POST' 'http://localhost:8000/transform?source-crs=EPSG%3A28992&target-crs=EPSG%3A28992' \ diff --git a/README.md b/README.md index a9e97e7..33c849d 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,12 @@ Install enable precommit hook with: git config --local core.hooksPath .githooks ``` +To run debug session in VS Code install the package with `pip` with the `--editable` flag: + +```sh +pip install --editable . +``` + ## Install ```bash diff --git a/example/polygon.json b/example/polygon.json deleted file mode 100644 index 75740fd..0000000 --- a/example/polygon.json +++ /dev/null @@ -1,388 +0,0 @@ -{ - "type": "FeatureCollection", - "crs": { - "type": "name", - "properties": { - "name": "urn:ogc:def:crs:OGC:1.3:CRS84" - } - }, - "features": [ - { - "type": "Feature", - "properties": { - "name": "Parc de la Colline" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -72.357206347890767, - 47.72858763003908 - ], - [ - -71.86027854004486, - 47.527648291638172 - ], - [ - -72.37075892446839, - 47.539848426151735 - ], - [ - -72.357206347890767, - 47.72858763003908 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Centre Paul-Étienne Simard" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -72.357206347890767, - 48.013440900213297 - ], - [ - -72.239750684218109, - 48.013440900213297 - ], - [ - -72.253303260795718, - 47.856056000888501 - ], - [ - -72.027426984502114, - 47.856056000888501 - ], - [ - -72.036462035553868, - 48.013440900213297 - ], - [ - -71.905453795303586, - 48.01646283861713 - ], - [ - -71.891901218725963, - 47.801464984333364 - ], - [ - -72.361723873416651, - 47.810567474765456 - ], - [ - -72.357206347890767, - 48.013440900213297 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Loisirs Rivière du Moulin" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -72.194575428959382, - 48.33278115872843 - ], - [ - -72.018391933450374, - 48.33278115872843 - ], - [ - -71.846725963467236, - 48.251628525276693 - ], - [ - -71.950629050562299, - 48.107038644740094 - ], - [ - -72.203610480011122, - 48.107038644740094 - ], - [ - -72.397864077623623, - 48.221539261269051 - ], - [ - -72.194575428959382, - 48.33278115872843 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "L'Étoile-du-Nord" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -71.589227008492543, - 47.649521925935176 - ], - [ - -71.525981651130337, - 47.734664642855655 - ], - [ - -71.48532392139748, - 47.649521925935169 - ], - [ - -71.295587849310877, - 47.637347332276697 - ], - [ - -71.462736293768117, - 47.585573652777313 - ], - [ - -71.390455885354172, - 47.475766052599219 - ], - [ - -71.535016702182091, - 47.552045722357242 - ], - [ - -71.702165146639345, - 47.491030857179695 - ], - [ - -71.616332161647762, - 47.591667334264848 - ], - [ - -71.787998131630914, - 47.655608158761908 - ], - [ - -71.589227008492543, - 47.649521925935176 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Loisirs Lavoie et St-Jean-Baptiste" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -71.729270299794578, - 48.010418784700107 - ], - [ - -71.291070323784993, - 48.004374022337799 - ], - [ - -71.291070323784993, - 47.777183877693901 - ], - [ - -71.729270299794578, - 47.786290622064854 - ], - [ - -71.729270299794578, - 48.010418784700107 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Loisirs Diamant" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -71.693130095587605, - 48.341790157179155 - ], - [ - -71.286552798259123, - 48.344792802893032 - ], - [ - -71.449183717190522, - 48.224548983994914 - ], - [ - -71.277517747207369, - 48.070827446446337 - ], - [ - -71.751857927423927, - 48.085918544287573 - ], - [ - -71.507911549026844, - 48.21551928490868 - ], - [ - -71.693130095587605, - 48.341790157179155 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Sydenham" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -71.051641470913779, - 47.710352336655504 - ], - [ - -70.911598179611758, - 47.710352336655504 - ], - [ - -70.925150756189367, - 47.619080121567436 - ], - [ - -70.712827056473373, - 47.616034965734443 - ], - [ - -70.721862107525112, - 47.448278226184989 - ], - [ - -70.857387873301292, - 47.448278226184989 - ], - [ - -70.852870347775408, - 47.552045722357249 - ], - [ - -71.056158996439635, - 47.552045722357249 - ], - [ - -71.051641470913779, - 47.710352336655504 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Saint-Luc" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -71.110369302750115, - 47.798430466372736 - ], - [ - -70.902563128560018, - 47.983211774835986 - ], - [ - -70.699274479895777, - 47.789325849015306 - ], - [ - -71.110369302750115, - 47.798430466372736 - ] - ] - ] - } - }, - { - "type": "Feature", - "properties": { - "name": "Loisirs du Fjord du Saguenay" - }, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - -70.988396113551573, - 48.32977780546792 - ], - [ - -70.812212618042579, - 48.32977780546792 - ], - [ - -70.807695092516681, - 48.209498600656133 - ], - [ - -70.631511597007702, - 48.209498600656147 - ], - [ - -70.636029122533571, - 48.079882636349602 - ], - [ - -71.146509506957088, - 48.082900678850329 - ], - [ - -71.151027032482972, - 48.212509031269981 - ], - [ - -70.983878588025689, - 48.209498600656133 - ], - [ - -70.988396113551573, - 48.32977780546792 - ] - ] - ] - } - } - ] -} diff --git a/pyproject.toml b/pyproject.toml index 55a9c14..21d06cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "coordinates-transformation-api" description = "Coordinates transformation API of the Nederlandse Samenwerking Geodetische Infrastructuur (NSGI)" keywords = ["NSGI", "pyproj", "fastapi", "coordinates", "geo"] -license = {text = "MIT"} +license = { text = "MIT" } classifiers = [ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", @@ -23,13 +23,10 @@ dynamic = ["version"] [project.optional-dependencies] dev = ["black", "autoflake", "isort", "mypy"] + [build-system] +requires = ["setuptools>=61.0", "setuptools-git-versioning<2"] build-backend = "setuptools.build_meta" -requires = [ - "setuptools>=45", - "setuptools-git-versioning<2", - "wheel==0.38.4" -] [project.scripts] ct-api = "coordinates_transformation_api.main:main" @@ -37,11 +34,12 @@ ct-api = "coordinates_transformation_api.main:main" [tool.setuptools-git-versioning] enabled = true + [tool.setuptools.packages.find] +where = ["src"] # list of folders that contain the packages (["."] by default) include = ["coordinates_transformation_api*"] # package names should match these glob patterns (["*"] by default) -exclude = [] # exclude packages matching these glob patterns (empty by default) +namespaces = false # to disable scanning PEP 420 namespaces (true by default) [tool.setuptools.package-data] "coordinates_transformation_api.assets" = ["*"] "coordinates_transformation_api.assets.static" = ["*"] - diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..fcccae1 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = src diff --git a/coordinates_transformation_api/__init__.py b/src/coordinates_transformation_api/__init__.py similarity index 100% rename from coordinates_transformation_api/__init__.py rename to src/coordinates_transformation_api/__init__.py diff --git a/coordinates_transformation_api/assets/ __init__.py b/src/coordinates_transformation_api/assets/ __init__.py similarity index 100% rename from coordinates_transformation_api/assets/ __init__.py rename to src/coordinates_transformation_api/assets/ __init__.py diff --git a/coordinates_transformation_api/assets/logging.conf b/src/coordinates_transformation_api/assets/logging.conf similarity index 100% rename from coordinates_transformation_api/assets/logging.conf rename to src/coordinates_transformation_api/assets/logging.conf diff --git a/coordinates_transformation_api/assets/openapi.yaml b/src/coordinates_transformation_api/assets/openapi.yaml similarity index 100% rename from coordinates_transformation_api/assets/openapi.yaml rename to src/coordinates_transformation_api/assets/openapi.yaml diff --git a/coordinates_transformation_api/assets/static/__init__.py b/src/coordinates_transformation_api/assets/static/__init__.py similarity index 100% rename from coordinates_transformation_api/assets/static/__init__.py rename to src/coordinates_transformation_api/assets/static/__init__.py diff --git a/coordinates_transformation_api/assets/static/favicon.ico b/src/coordinates_transformation_api/assets/static/favicon.ico similarity index 100% rename from coordinates_transformation_api/assets/static/favicon.ico rename to src/coordinates_transformation_api/assets/static/favicon.ico diff --git a/coordinates_transformation_api/fastapi_rfc7807/README.md b/src/coordinates_transformation_api/fastapi_rfc7807/README.md similarity index 100% rename from coordinates_transformation_api/fastapi_rfc7807/README.md rename to src/coordinates_transformation_api/fastapi_rfc7807/README.md diff --git a/coordinates_transformation_api/fastapi_rfc7807/__init__.py b/src/coordinates_transformation_api/fastapi_rfc7807/__init__.py similarity index 100% rename from coordinates_transformation_api/fastapi_rfc7807/__init__.py rename to src/coordinates_transformation_api/fastapi_rfc7807/__init__.py diff --git a/coordinates_transformation_api/fastapi_rfc7807/middleware.py b/src/coordinates_transformation_api/fastapi_rfc7807/middleware.py similarity index 100% rename from coordinates_transformation_api/fastapi_rfc7807/middleware.py rename to src/coordinates_transformation_api/fastapi_rfc7807/middleware.py diff --git a/coordinates_transformation_api/fastapi_rfc7807/schema.py b/src/coordinates_transformation_api/fastapi_rfc7807/schema.py similarity index 100% rename from coordinates_transformation_api/fastapi_rfc7807/schema.py rename to src/coordinates_transformation_api/fastapi_rfc7807/schema.py diff --git a/coordinates_transformation_api/limit_middleware/__init__.py b/src/coordinates_transformation_api/limit_middleware/__init__.py similarity index 100% rename from coordinates_transformation_api/limit_middleware/__init__.py rename to src/coordinates_transformation_api/limit_middleware/__init__.py diff --git a/coordinates_transformation_api/limit_middleware/middleware.py b/src/coordinates_transformation_api/limit_middleware/middleware.py similarity index 100% rename from coordinates_transformation_api/limit_middleware/middleware.py rename to src/coordinates_transformation_api/limit_middleware/middleware.py diff --git a/coordinates_transformation_api/main.py b/src/coordinates_transformation_api/main.py similarity index 99% rename from coordinates_transformation_api/main.py rename to src/coordinates_transformation_api/main.py index ea63a1c..431f2ed 100644 --- a/coordinates_transformation_api/main.py +++ b/src/coordinates_transformation_api/main.py @@ -164,7 +164,6 @@ async def transform( target_crs: str = Query(alias="target-crs"), ): validate_crss(source_crs, target_crs, PROJS_AXIS_INFO) - transformer = get_transformer(source_crs, target_crs) transform_request_body(body, transformer) diff --git a/coordinates_transformation_api/models.py b/src/coordinates_transformation_api/models.py similarity index 100% rename from coordinates_transformation_api/models.py rename to src/coordinates_transformation_api/models.py diff --git a/coordinates_transformation_api/settings.py b/src/coordinates_transformation_api/settings.py similarity index 100% rename from coordinates_transformation_api/settings.py rename to src/coordinates_transformation_api/settings.py diff --git a/coordinates_transformation_api/util.py b/src/coordinates_transformation_api/util.py similarity index 100% rename from coordinates_transformation_api/util.py rename to src/coordinates_transformation_api/util.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/data/feature-collection-geometry-collection.json b/tests/data/feature-collection-geometry-collection.json new file mode 100644 index 0000000..41def51 --- /dev/null +++ b/tests/data/feature-collection-geometry-collection.json @@ -0,0 +1,55 @@ +{ + "type": "FeatureCollection", + "name": "polygonen", + "crs": { + "type": "name", + "properties": { + "name": "urn:ogc:def:crs:EPSG::28992" + } + }, + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "GeometryCollection", + "geometries": [ + { + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] + }, + { + "type": "Polygon", + "coordinates": [ + [ + [ + 138871.518881731899455, + 597389.993749326560646 + ], + [ + 140036.122569288185332, + 592678.040025010588579 + ], + [ + 145468.022542056889506, + 593606.184292820515111 + ], + [ + 142646.432552648213459, + 597849.345781013718806 + ], + [ + 138871.518881731899455, + 597389.993749326560646 + ] + ] + ] + } + ] + } + } + ] +} diff --git a/tests/data/feature-geometry-collection.json b/tests/data/feature-geometry-collection.json new file mode 100644 index 0000000..9d7786a --- /dev/null +++ b/tests/data/feature-geometry-collection.json @@ -0,0 +1,44 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "GeometryCollection", + "geometries": [ + { + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] + }, + { + "type": "Polygon", + "coordinates": [ + [ + [ + 138871.518881731899455, + 597389.993749326560646 + ], + [ + 140036.122569288185332, + 592678.040025010588579 + ], + [ + 145468.022542056889506, + 593606.184292820515111 + ], + [ + 142646.432552648213459, + 597849.345781013718806 + ], + [ + 138871.518881731899455, + 597389.993749326560646 + ] + ] + ] + } + ] + } + +} diff --git a/tests/data/feature.json b/tests/data/feature.json new file mode 100644 index 0000000..704c0c2 --- /dev/null +++ b/tests/data/feature.json @@ -0,0 +1,11 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] + } +} diff --git a/tests/data/geometry-collection.json b/tests/data/geometry-collection.json new file mode 100644 index 0000000..7a622da --- /dev/null +++ b/tests/data/geometry-collection.json @@ -0,0 +1,39 @@ +{ + "type": "GeometryCollection", + "geometries": [ + { + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] + }, + { + "type": "Polygon", + "coordinates": [ + [ + [ + 138871.518881731899455, + 597389.993749326560646 + ], + [ + 140036.122569288185332, + 592678.040025010588579 + ], + [ + 145468.022542056889506, + 593606.184292820515111 + ], + [ + 142646.432552648213459, + 597849.345781013718806 + ], + [ + 138871.518881731899455, + 597389.993749326560646 + ] + ] + ] + } + ] +} diff --git a/tests/data/geometry.json b/tests/data/geometry.json new file mode 100644 index 0000000..3f3f1aa --- /dev/null +++ b/tests/data/geometry.json @@ -0,0 +1,7 @@ +{ + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] +} diff --git a/tests/data/linestrings-multi.json b/tests/data/linestrings-multi.json new file mode 100644 index 0000000..87f041a --- /dev/null +++ b/tests/data/linestrings-multi.json @@ -0,0 +1,17 @@ +{ +"type": "FeatureCollection", +"name": "lijnen", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::28992" } }, +"features": [ +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 156264.906359842570964, 601302.588919493253343 ], [ 165681.964475793502061, 605544.313164469087496 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 197469.61676805181196, 578867.209674741141498 ], [ 208765.828605588612845, 585118.04955188173335 ], [ 214978.579108641220955, 579998.342453736579046 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 233912.686535700748209, 551369.281992391683161 ], [ 245564.067328312172322, 539677.288861267617904 ], [ 257328.60820127511397, 548003.141586222918704 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 210918.597344624489779, 478768.259641678829212 ], [ 232798.115296837408096, 470859.279475290386472 ], [ 236902.296422307554167, 456897.405543544969987 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 272182.597337315732148, 464381.992139570764266 ], [ 272438.267499944369774, 474055.442993962555192 ], [ 288261.877387178654317, 479765.751194861950353 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 93411.298435807053465, 474976.165362189291045 ], [ 98650.058117257722188, 467669.814339613658376 ], [ 113264.902673724602209, 477181.217063896707259 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 38816.458703284064541, 394689.020564639766235 ], [ 63378.898438817224815, 393203.124137901235372 ], [ 65707.721984194562538, 385797.269111435685772 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ -17084.258525104261935, 371192.313211922126357 ], [ -8383.290356248937314, 364984.209468487999402 ], [ -817.400946293841116, 353379.834339173045009 ], [ -14379.179841294622747, 348365.349435280484613 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 193638.184259118163027, 334806.62195219960995 ], [ 203576.012500613258453, 335883.131387825240381 ], [ 216054.644449666957371, 332046.926132246037014 ], [ 217133.905943340039812, 325105.827307404426392 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 266300.890377873321995, 625765.27717069722712 ], [ 272285.73768137593288, 625675.086520252167247 ], [ 278210.402989279828034, 627931.178850010619499 ], [ 275738.835845820023678, 632895.496214323444292 ] ] ] } } +] +} diff --git a/tests/data/linestrings-punten.json b/tests/data/linestrings-punten.json new file mode 100644 index 0000000..aa77a8c --- /dev/null +++ b/tests/data/linestrings-punten.json @@ -0,0 +1,18 @@ +{ +"type": "FeatureCollection", +"name": "punten", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::28992" } }, +"features": [ +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 146835.981928102381062, 599898.553942722966895 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 245676.616505969053833, 559172.935081139556132 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 279224.202361581847072, 473738.118095523212105 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 200834.991655219433596, 471426.359875430876855 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 229043.133943368622568, 396838.534053214243613 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 168596.048860643350054, 399253.948272640816867 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 200614.47486226959154, 333375.450340887065977 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 119900.970904283720301, 458283.601809155778028 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 46503.247291849751491, 386172.107844812097028 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -16345.099658865306992, 363764.308723671594635 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 267385.615052639564965, 627019.403345670783892 ] ] } } +] +} diff --git a/tests/data/linestrings.json b/tests/data/linestrings.json new file mode 100644 index 0000000..d43e8f4 --- /dev/null +++ b/tests/data/linestrings.json @@ -0,0 +1,17 @@ +{ +"type": "FeatureCollection", +"name": "lijnen", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::28992" } }, +"features": [ +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 156264.906359842570964, 601302.588919493253343 ], [ 165681.964475793502061, 605544.313164469087496 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 197469.61676805181196, 578867.209674741141498 ], [ 208765.828605588612845, 585118.04955188173335 ], [ 214978.579108641220955, 579998.342453736579046 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 233912.686535700748209, 551369.281992391683161 ], [ 245564.067328312172322, 539677.288861267617904 ], [ 257328.60820127511397, 548003.141586222918704 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 210918.597344624489779, 478768.259641678829212 ], [ 232798.115296837408096, 470859.279475290386472 ], [ 236902.296422307554167, 456897.405543544969987 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 272182.597337315732148, 464381.992139570764266 ], [ 272438.267499944369774, 474055.442993962555192 ], [ 288261.877387178654317, 479765.751194861950353 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 93411.298435807053465, 474976.165362189291045 ], [ 98650.058117257722188, 467669.814339613658376 ], [ 113264.902673724602209, 477181.217063896707259 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 38816.458703284064541, 394689.020564639766235 ], [ 63378.898438817224815, 393203.124137901235372 ], [ 65707.721984194562538, 385797.269111435685772 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ -17084.258525104261935, 371192.313211922126357 ], [ -8383.290356248937314, 364984.209468487999402 ], [ -817.400946293841116, 353379.834339173045009 ], [ -14379.179841294622747, 348365.349435280484613 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 193638.184259118163027, 334806.62195219960995 ], [ 203576.012500613258453, 335883.131387825240381 ], [ 216054.644449666957371, 332046.926132246037014 ], [ 217133.905943340039812, 325105.827307404426392 ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 266300.890377873321995, 625765.27717069722712 ], [ 272285.73768137593288, 625675.086520252167247 ], [ 278210.402989279828034, 627931.178850010619499 ], [ 275738.835845820023678, 632895.496214323444292 ] ] } } +] +} diff --git a/tests/data/points.json b/tests/data/points.json new file mode 100644 index 0000000..aafaf71 --- /dev/null +++ b/tests/data/points.json @@ -0,0 +1,133 @@ +{ + "type": "FeatureCollection", + "name": "punten", + "crs": { + "type": "name", + "properties": { + "name": "urn:ogc:def:crs:EPSG::28992" + } + }, + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 146835.981928102381062, + 599898.553942722966895 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 245676.616505969053833, + 559172.935081139556132 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 279224.202361581847072, + 473738.118095523212105 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 200834.991655219433596, + 471426.359875430876855 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 229043.133943368622568, + 396838.534053214243613 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 168596.048860643350054, + 399253.948272640816867 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 200614.47486226959154, + 333375.450340887065977 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 119900.970904283720301, + 458283.601809155778028 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 46503.247291849751491, + 386172.107844812097028 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + -16345.099658865306992, + 363764.308723671594635 + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": [ + 267385.615052639564965, + 627019.403345670783892 + ] + } + } + ] +} diff --git a/tests/data/polygons-multi.json b/tests/data/polygons-multi.json new file mode 100644 index 0000000..f5b91d2 --- /dev/null +++ b/tests/data/polygons-multi.json @@ -0,0 +1,18 @@ +{ +"type": "FeatureCollection", +"name": "polygonen", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::28992" } }, +"features": [ +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 138871.518881731899455, 597389.993749326560646 ], [ 140036.122569288185332, 592678.040025010588579 ], [ 145468.022542056889506, 593606.184292820515111 ], [ 142646.432552648213459, 597849.345781013718806 ], [ 138871.518881731899455, 597389.993749326560646 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 266341.966320101171732, 624010.546634015277959 ], [ 259248.56824272783706, 626424.482394860591739 ], [ 261291.278331592533505, 618975.793064830591902 ], [ 266341.966320101171732, 624010.546634015277959 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 251412.405977289337898, 560766.434022761066444 ], [ 252006.49793930555461, 554845.013168400502764 ], [ 260842.058783969318029, 553604.81155656662304 ], [ 251412.405977289337898, 560766.434022761066444 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 199874.273164226906374, 477269.185637327027507 ], [ 209089.737760954682017, 474707.833378498908132 ], [ 216253.349485622544307, 482266.052718433085829 ], [ 208514.571962466026889, 483139.787954131606966 ], [ 199874.273164226906374, 477269.185637327027507 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 121707.821315302426228, 465346.790826416690834 ], [ 125560.778979408118175, 461211.370293963293079 ], [ 129195.01494723855285, 460465.33905275905272 ], [ 129454.999910008249572, 463850.306700997985899 ], [ 126784.436833128449507, 463139.314015890355222 ], [ 121707.821315302426228, 465346.790826416690834 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 281894.111119233944919, 466374.494963860954158 ], [ 275895.331379124370869, 463805.699570343538653 ], [ 271919.600633895897772, 467821.940698907652404 ], [ 278005.237549021316227, 466760.210041912621818 ], [ 281894.111119233944919, 466374.494963860954158 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 225111.599715556658339, 401253.310843224753626 ], [ 222476.965624673699494, 396317.632036394556053 ], [ 227180.264142444590107, 393930.954146906151436 ], [ 228316.815325312200002, 400319.277518280316144 ], [ 225111.599715556658339, 401253.310843224753626 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 169635.911367430206155, 400542.505113535618875 ], [ 172124.009730785386637, 390746.700027158716694 ], [ 178746.620647073927103, 394697.216538632870652 ], [ 177742.546057417959673, 399348.116272700950503 ], [ 169635.911367430206155, 400542.505113535618875 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 204111.094124073133571, 338429.517250144679565 ], [ 206424.644976491370471, 330762.463511311565526 ], [ 215126.656913997314405, 331601.627037334372289 ], [ 216557.195253394864267, 336828.397569217369892 ], [ 204111.094124073133571, 338429.517250144679565 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 52529.655347931722645, 388809.643655742285773 ], [ 53095.177609896491049, 380197.328617827966809 ], [ 59490.169389823320671, 379335.062083175173029 ], [ 65783.330528796679573, 386594.239897803810891 ], [ 52529.655347931722645, 388809.643655742285773 ] ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -4081.052586732403142, 362070.243211577413604 ], [ -2047.100055279704975, 347662.399633223656565 ], [ 13722.130033615307184, 343239.014617707929574 ], [ 16419.729809323413065, 351089.644134238711558 ], [ -4081.052586732403142, 362070.243211577413604 ] ] ] ] } } +] +} diff --git a/tests/data/polygons.json b/tests/data/polygons.json new file mode 100644 index 0000000..a816623 --- /dev/null +++ b/tests/data/polygons.json @@ -0,0 +1,18 @@ +{ +"type": "FeatureCollection", +"name": "polygonen", +"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::28992" } }, +"features": [ +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 138871.518881731899455, 597389.993749326560646 ], [ 140036.122569288185332, 592678.040025010588579 ], [ 145468.022542056889506, 593606.184292820515111 ], [ 142646.432552648213459, 597849.345781013718806 ], [ 138871.518881731899455, 597389.993749326560646 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 266341.966320101171732, 624010.546634015277959 ], [ 259248.56824272783706, 626424.482394860591739 ], [ 261291.278331592533505, 618975.793064830591902 ], [ 266341.966320101171732, 624010.546634015277959 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 251412.405977289337898, 560766.434022761066444 ], [ 252006.49793930555461, 554845.013168400502764 ], [ 260842.058783969318029, 553604.81155656662304 ], [ 251412.405977289337898, 560766.434022761066444 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 199874.273164226906374, 477269.185637327027507 ], [ 209089.737760954682017, 474707.833378498908132 ], [ 216253.349485622544307, 482266.052718433085829 ], [ 208514.571962466026889, 483139.787954131606966 ], [ 199874.273164226906374, 477269.185637327027507 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 121707.821315302426228, 465346.790826416690834 ], [ 125560.778979408118175, 461211.370293963293079 ], [ 129195.01494723855285, 460465.33905275905272 ], [ 129454.999910008249572, 463850.306700997985899 ], [ 126784.436833128449507, 463139.314015890355222 ], [ 121707.821315302426228, 465346.790826416690834 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 281894.111119233944919, 466374.494963860954158 ], [ 275895.331379124370869, 463805.699570343538653 ], [ 271919.600633895897772, 467821.940698907652404 ], [ 278005.237549021316227, 466760.210041912621818 ], [ 281894.111119233944919, 466374.494963860954158 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 225111.599715556658339, 401253.310843224753626 ], [ 222476.965624673699494, 396317.632036394556053 ], [ 227180.264142444590107, 393930.954146906151436 ], [ 228316.815325312200002, 400319.277518280316144 ], [ 225111.599715556658339, 401253.310843224753626 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 169635.911367430206155, 400542.505113535618875 ], [ 172124.009730785386637, 390746.700027158716694 ], [ 178746.620647073927103, 394697.216538632870652 ], [ 177742.546057417959673, 399348.116272700950503 ], [ 169635.911367430206155, 400542.505113535618875 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 204111.094124073133571, 338429.517250144679565 ], [ 206424.644976491370471, 330762.463511311565526 ], [ 215126.656913997314405, 331601.627037334372289 ], [ 216557.195253394864267, 336828.397569217369892 ], [ 204111.094124073133571, 338429.517250144679565 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 52529.655347931722645, 388809.643655742285773 ], [ 53095.177609896491049, 380197.328617827966809 ], [ 59490.169389823320671, 379335.062083175173029 ], [ 65783.330528796679573, 386594.239897803810891 ], [ 52529.655347931722645, 388809.643655742285773 ] ] ] } }, +{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -4081.052586732403142, 362070.243211577413604 ], [ -2047.100055279704975, 347662.399633223656565 ], [ 13722.130033615307184, 343239.014617707929574 ], [ 16419.729809323413065, 351089.644134238711558 ], [ -4081.052586732403142, 362070.243211577413604 ] ] ] } } +] +} diff --git a/tests/test_geojson_transformation.py b/tests/test_geojson_transformation.py new file mode 100644 index 0000000..75ff299 --- /dev/null +++ b/tests/test_geojson_transformation.py @@ -0,0 +1,120 @@ +import json + +from geojson_pydantic import Feature, FeatureCollection +from geojson_pydantic.geometries import GeometryCollection, parse_geometry_obj +from pydantic_core import ValidationError +from pyproj import Transformer + +from coordinates_transformation_api.util import transform_request_body + + +def test_transform_geometry(): + with open("tests/data/geometry.json") as f: + data = json.load(f) + geometry = parse_geometry_obj(data) + geometry_original = parse_geometry_obj(data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(geometry, transformer) + + geometry_dict = json.loads(geometry.model_dump_json()) + # check if input is actually transformed + assert geometry != geometry_original + try: + parse_geometry_obj(geometry_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type Geometry: {exc}" + + +def test_transform_feature_geometrycollection(): + with open("tests/data/feature-geometry-collection.json") as f: + data = json.load(f) + feature = Feature(**data) + feature_original = Feature(**data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(feature, transformer) + feature_dict = json.loads(feature.model_dump_json()) + # check if input is actually transformed + assert feature != feature_original + try: + Feature(**feature_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type Feature: {exc}" + + +def test_transform_feature(): + with open("tests/data/feature.json") as f: + data = json.load(f) + feature = Feature(**data) + feature_original = Feature(**data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(feature, transformer) + feature_dict = json.loads(feature.model_dump_json()) + # check if input is actually transformed + assert feature != feature_original + try: + Feature(**feature_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type Feature: {exc}" + + +def test_transform_featurecollection(): + with open("tests/data/polygons.json") as f: + data = json.load(f) + fc = FeatureCollection(**data) + fc_original = FeatureCollection(**data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(fc, transformer) + + fc_dict = json.loads(fc.model_dump_json()) + # check if input is actually transformed + assert fc != fc_original + try: + FeatureCollection(**fc_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type FeatureCollection: {exc}" + + +def test_transform_featurecollection_geometrycollection(): + with open("tests/data/feature-collection-geometry-collection.json") as f: + data = json.load(f) + fc = FeatureCollection(**data) + fc_original = FeatureCollection(**data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(fc, transformer) + + fc_dict = json.loads(fc.model_dump_json()) + # check if input is actually transformed + assert fc != fc_original + try: + FeatureCollection(**fc_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type FeatureCollection: {exc}" + + +def test_transform_geometrycollection(): + with open("tests/data/geometry-collection.json") as f: + data = json.load(f) + gc = GeometryCollection(**data) + gc_original = GeometryCollection(**data) + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + transform_request_body(gc, transformer) + + gc_dict = json.loads(gc.model_dump_json()) + # check if input is actually transformed + assert gc != gc_original + try: # check if output type of transform_request_body equals input type + GeometryCollection(**gc_dict) + except ValidationError as exc: + assert ( + False + ), f"could not convert output of transform_request_body to type GeometryCollection: {exc}"