Skip to content

Commit

Permalink
update featurecollection crs if overridden by user
Browse files Browse the repository at this point in the history
  • Loading branch information
arbakker committed Nov 24, 2023
1 parent e61b5e5 commit f038066
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/geodense/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ def densify_file( # noqa: PLR0913
densify_in_projection,
)
densify_geojson_object(geojson_obj, config)
if src_crs is not None and isinstance(geojson_obj, CrsFeatureCollection):
geojson_obj.set_crs_auth_code(src_crs)
with open(
output_file_path, "w"
) if output_file_path != "-" else sys.stdout as out_f:
Expand Down
67 changes: 67 additions & 0 deletions tests/data/multipolygon_wrong_crs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "FeatureCollection",
"name": "polygonen",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::4326"
}
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
138871.518881731899455,
597389.993749326560646,
10
],
[
140036.122569288185332,
592678.040025010588579
],
[
145468.022542056889506,
593606.184292820515111
],
[
142646.432552648213459,
597849.345781013718806
],
[
138871.518881731899455,
597389.993749326560646,
10
]
]
],
[
[
[
266341.966320101171732,
624010.546634015277959
],
[
259248.56824272783706,
626424.482394860591739
],
[
261291.278331592533505,
618975.793064830591902
],
[
266341.966320101171732,
624010.546634015277959
]
]
]
]
}
}
]
}
31 changes: 31 additions & 0 deletions tests/test_densify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import os
import re
import tempfile
from contextlib import nullcontext as does_not_raise
from contextlib import suppress

import pyproj
import pytest
Expand Down Expand Up @@ -221,6 +223,35 @@ def test_densify_file(test_dir):
assert os.path.exists(out_file)


@pytest.mark.parametrize(
("input_file", "src_crs", "expectation"),
[
(
"multipolygon.json",
None,
"urn:ogc:def:crs:EPSG::28992",
),
("geometry-4326-no-crs.json", None, None),
("geometry-4326-no-crs.json", "OGC:CRS84", None),
(
"multipolygon_wrong_crs.json",
"EPSG:28992",
"urn:ogc:def:crs:EPSG::28992",
),
],
)
def test_densify_file_crs_matches_input(test_dir, input_file, src_crs, expectation):
output_file = os.path.join(tempfile.mkdtemp(), input_file)
input_file = os.path.join(test_dir, "data", input_file)
densify_file(input_file, output_file, src_crs=src_crs)
with open(output_file) as f:
geojson = json.load(f)
crs = None
with suppress(KeyError):
crs = geojson["crs"]["properties"]["name"]
assert crs == expectation


@pytest.mark.parametrize(
("input_file", "output_file", "expectation"),
[
Expand Down

0 comments on commit f038066

Please sign in to comment.