Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Jun 10, 2024
1 parent a655d1c commit 65c081f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
30 changes: 25 additions & 5 deletions src/cldfgeojson/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
from . import geojson

__all__ = [
'feature_collection', 'fixed_geometry', 'merged_geometry', 'aggregate', 'InvalidRingWarning']
'feature_collection',
'fixed_geometry',
'merged_geometry',
'aggregate',
'InvalidRingWarning',
'correct_longitude',
]

Languoid = typing.Union[pyglottologLanguoid, pycldfLanguage]

Expand All @@ -37,15 +43,28 @@ def feature_collection(features: typing.List[dict], **properties) -> dict:
return dict(type="FeatureCollection", features=features, properties=properties)


def correct_longitude(lon):
sign = -1 if lon < 0 else 1
if abs(lon) > 180:
lon = lon - sign * ((abs(lon) // 360) + 1) * 360
if lon == -180:
lon = 180
return lon


def fixed_geometry(feature: geojson.Feature,
fix_longitude=False,
fix_antimeridian=False) -> geojson.Feature:
fix_longitude: bool = False,
fix_antimeridian: bool = False) -> geojson.Feature:
"""
Fixes a feature's geometry in-place.
Note: This may cut off parts of the supposed polygon.
:param feature:
:param fix_longitude: Flag signaling whether to adapt longitudes such that they are between \
-180 and 180 deg. Longitudes outside of this interval are translated by multiples of 360 to \
fall inside.
:param fix_antimeridian:
:return:
"""
if feature['geometry']['type'] not in ['Polygon', 'MultiPolygon']:
Expand All @@ -59,9 +78,10 @@ def fixed_geometry(feature: geojson.Feature,
if fix_longitude:
coords = []
for lon, lat in ring:
if lon > 180:
lon = lon - 360
flon = correct_longitude(lon)
if flon != lon:
invalid = True
lon = flon
coords.append([lon, lat])
ring = coords
# Some linear rings are self-intersecting. We fix these by taking the 0-distance
Expand Down
57 changes: 55 additions & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from shapely.geometry import shape, Point

from cldfgeojson.create import *
Expand All @@ -7,6 +8,24 @@ def test_feature_collection():
assert 'type' in feature_collection([])


@pytest.mark.parametrize(
'in_,out_',
[
(0, 0),
(1, 1),
(-1, -1),
(180, 180),
(-180, -180),
(181, -179),
(540, 180),
(-900, 180),
(-181, 179),
]
)
def test_correct_longitude(in_, out_):
assert correct_longitude(in_) == out_


def test_fixed_geometry():
f = {
"type": "Feature",
Expand Down Expand Up @@ -72,7 +91,7 @@ def test_fixed_geometry():
[5, 0],
]],
[[
[10, 5],
[370, 5],
[15, 5],
[15, 10],
[10, 10],
Expand All @@ -81,9 +100,43 @@ def test_fixed_geometry():
]
}
}
res = fixed_geometry(f)
res = fixed_geometry(f, fix_longitude=True)
assert shape(res['geometry']).contains(Point(12, 7))

f = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-179.70358951407547,
52.750507455036264
],
[
179.96672360880183,
52.00163609753924
],
[
-177.89334479610974,
50.62805205289558
],
[
-179.9847165338706,
51.002602948712465
],
[
-179.70358951407547,
52.750507455036264
]
]
]
}
}
res = fixed_geometry(f, fix_antimeridian=True)
assert res['geometry']['type'] == 'MultiPolygon' and len(res['geometry']['coordinates']) == 2


def test_aggregate(glottolog_cldf):
def make_feature(latoffset=0, lonoffset=0):
Expand Down

0 comments on commit 65c081f

Please sign in to comment.