Skip to content

Commit 2da2096

Browse files
committed
fix: update Shapely and refactor object formatter
1 parent e5ce771 commit 2da2096

File tree

6 files changed

+105
-55
lines changed

6 files changed

+105
-55
lines changed

pyopenair/factory.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
altitude_formatter,
99
fields_formatter,
1010
comment_formatter,
11+
object_formatter,
1112
)
1213

1314

@@ -73,33 +74,17 @@ def wkt2openair(
7374
header.append(altitude_formatter("H", ah_alti, ah_unit, ah_mode))
7475
if al_alti and al_mode:
7576
header.append(altitude_formatter("L", al_alti, al_unit, al_mode))
76-
geom = loads(wkt)
77-
if geom.geom_type == "Polygon":
78-
node_coords = []
79-
for node in list(geom.exterior.coords):
80-
node_coords.append(generate_coords(node))
81-
node_coords = list(OrderedDict.fromkeys(node_coords))
82-
desc = "\n".join(header).format(label=label)
83-
for coord in node_coords:
84-
desc += "\n{}".format(coord)
85-
return desc
86-
elif geom.geom_type == "MultiPolygon":
87-
areas = []
88-
lengeom = len(geom)
89-
i = 1
90-
for g in geom:
91-
node_coords = []
92-
for node in list(g.exterior.coords):
93-
node_coords.append(generate_coords(node))
94-
node_coords = list(OrderedDict.fromkeys(node_coords))
95-
label_elem = "{} ({}/{})".format(label, i, lengeom)
96-
i = i + 1
97-
desc_tpl = "\n".join(header)
98-
desc = desc_tpl.format(label=label_elem)
99-
for coord in node_coords:
100-
desc += "\n{}".format(coord)
77+
obj = loads(wkt)
78+
if obj.geom_type == "Polygon":
79+
return object_formatter(obj, label, header)
80+
elif obj.geom_type == "MultiPolygon":
81+
areas, len_geom, i = [], len(obj.geoms), 0
82+
for g in obj.geoms:
83+
i += 1
84+
label_item = "{} ({}/{})".format(label, i, len_geom)
85+
desc = object_formatter(g, label_item, header)
10186
desc += "\n\n"
10287
areas.append(desc)
103-
return "\n".join(areas)
88+
return "\n".join(areas)
10489
else:
10590
return None

pyopenair/helper.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import logging
55
from math import ceil, floor
6+
from collections import OrderedDict
7+
from shapely.geometry.polygon import Polygon
68

79

810
logger = logging.getLogger(__name__)
@@ -255,3 +257,26 @@ def coalesce(var: any, default: any = "default") -> any:
255257
:rtype: any
256258
"""
257259
return default if var is None else var
260+
261+
262+
def object_formatter(g: Polygon, label: str, header: list) -> str:
263+
"""Return a single OpenAir object
264+
265+
:param g: Geometry to convert
266+
:type g: Polygon
267+
:param label: Object label
268+
:type label: str
269+
:param header: Object headers list
270+
:type header: list
271+
:return: Openair object
272+
:rtype: str
273+
"""
274+
275+
node_coords = []
276+
for node in list(g.exterior.coords):
277+
node_coords.append(generate_coords(node))
278+
node_coords = list(OrderedDict.fromkeys(node_coords))
279+
desc = "\n".join(header).format(label=label)
280+
for coord in node_coords:
281+
desc += "\n{}".format(coord)
282+
return desc

tests/__init__.py

Whitespace-only changes.

tests/data.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
from typing import NamedTuple
3+
4+
class BasicDataToTest(NamedTuple):
5+
wkt: str
6+
an: str = "label"
7+
ac: str = "class"
8+
9+
10+
class DataToTest(NamedTuple):
11+
wkt: str
12+
an: str = "label"
13+
ac: str = "class"
14+
al_alti: int = 100
15+
al_unit: str = "m"
16+
al_mode: str = "AGL"
17+
ah_alti: int = 1000
18+
ah_unit: str = "m"
19+
ah_mode: str = "AGL"
20+
21+
headers = ['AC class', 'AH 3281FT AGL', 'AN {label}','AL 328FT AGL']
22+
wktpoly = "POLYGON((3.85949710384011 44.6745125533035,3.82379153743386 44.5787302437756,3.96661380305886 44.5180516500055,4.01605227962136 44.5826428195841,3.96661380305886 44.6784186781885,3.85949710384011 44.6745125533035))"
23+
wktmultipoly = "MULTIPOLYGON(((4.36423688809855 44.3711532565767,4.33496656347426 44.3243697013707,4.39636764710812 44.3034034517409,4.44767104348119 44.3660466322347,4.39032381481851 44.4011336006027,4.36423688809855 44.3711532565767)),((4.24706792369804 44.5079123913788,4.24619170613213 44.468186176919,4.35772605055038 44.4520521267275,4.29510144795417 44.5093795504602,4.24706792369804 44.5079123913788)))"

tests/test_factory.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import logging
2-
from typing import NamedTuple
2+
from data import BasicDataToTest, DataToTest, wktpoly, wktmultipoly
33

44
from pyopenair.factory import wkt2openair
55

66
logger = logging.getLogger(__name__)
77

88

9-
class BasicDataToTest(NamedTuple):
10-
wkt: str
11-
an: str = "label"
12-
ac: str = "class"
13-
14-
15-
class DataToTest(NamedTuple):
16-
wkt: str
17-
an: str = "label"
18-
ac: str = "class"
19-
al_alti: int = 100
20-
al_unit: str = "m"
21-
al_mode: str = "AGL"
22-
ah_alti: int = 1000
23-
ah_unit: str = "m"
24-
ah_mode: str = "AGL"
25-
26-
27-
wktpoly = "POLYGON((3.85949710384011 44.6745125533035,3.82379153743386 44.5787302437756,3.96661380305886 44.5180516500055,4.01605227962136 44.5826428195841,3.96661380305886 44.6784186781885,3.85949710384011 44.6745125533035))"
28-
wktmultipoly = "MULTIPOLYGON(((4.36423688809855 44.3711532565767,4.33496656347426 44.3243697013707,4.39636764710812 44.3034034517409,4.44767104348119 44.3660466322347,4.39032381481851 44.4011336006027,4.36423688809855 44.3711532565767)),((4.24706792369804 44.5079123913788,4.24619170613213 44.468186176919,4.35772605055038 44.4520521267275,4.29510144795417 44.5093795504602,4.24706792369804 44.5079123913788)))"
29-
# an = "label"
30-
# ac = "class"
31-
# al_alti = 100
32-
# al_unit = "m"
33-
# al_mode = "AGL"
34-
# ah_alti = 1000
35-
# ah_unit = "m"
36-
# ah_mode = "AGL"
9+
# class BasicDataToTest(NamedTuple):
10+
# wkt: str
11+
# an: str = "label"
12+
# ac: str = "class"
13+
14+
15+
# class DataToTest(NamedTuple):
16+
# wkt: str
17+
# an: str = "label"
18+
# ac: str = "class"
19+
# al_alti: int = 100
20+
# al_unit: str = "m"
21+
# al_mode: str = "AGL"
22+
# ah_alti: int = 1000
23+
# ah_unit: str = "m"
24+
# ah_mode: str = "AGL"
25+
26+
27+
# wktpoly = "POLYGON((3.85949710384011 44.6745125533035,3.82379153743386 44.5787302437756,3.96661380305886 44.5180516500055,4.01605227962136 44.5826428195841,3.96661380305886 44.6784186781885,3.85949710384011 44.6745125533035))"
28+
# wktmultipoly = "MULTIPOLYGON(((4.36423688809855 44.3711532565767,4.33496656347426 44.3243697013707,4.39636764710812 44.3034034517409,4.44767104348119 44.3660466322347,4.39032381481851 44.4011336006027,4.36423688809855 44.3711532565767)),((4.24706792369804 44.5079123913788,4.24619170613213 44.468186176919,4.35772605055038 44.4520521267275,4.29510144795417 44.5093795504602,4.24706792369804 44.5079123913788)))"
29+
# # an = "label"
30+
# # ac = "class"
31+
# # al_alti = 100
32+
# # al_unit = "m"
33+
# # al_mode = "AGL"
34+
# # ah_alti = 1000
35+
# # ah_unit = "m"
36+
# # ah_mode = "AGL"
3737

3838

3939
def test_wkt2openair_poly_only_required():

tests/test_helper.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
generate_coords,
77
generate_openair_coord,
88
decdeg2dms,
9+
object_formatter,
910
)
11+
from shapely.wkt import loads
12+
from data import BasicDataToTest, DataToTest, wktpoly, wktmultipoly, headers
1013

1114

1215
def test_stringify_coords_l2():
@@ -94,3 +97,17 @@ def test_comment_formatter():
9497
"""
9598
result = "**********************************************************\n* A pariatur dignissimos vel ipsum pariatur *\n* Veritatis eum aut aut dolorem doloremque reprehenderit *\n* Dolor et dicta ducimus in provident quod. *\n* *\n**********************************************************"
9699
assert comment_formatter(comment) == result
100+
101+
102+
def test_object_formatter():
103+
obj = object_formatter(loads(wktpoly), "labeldetest", headers)
104+
result = """AC class
105+
AH 3281FT AGL
106+
AN labeldetest
107+
AL 328FT AGL
108+
DP 44:40:28 N 03:51:34 E
109+
DP 44:34:43 N 03:49:25 E
110+
DP 44:31:04 N 03:57:59 E
111+
DP 44:34:57 N 04:00:57 E
112+
DP 44:40:42 N 03:57:59 E"""
113+
assert obj == result

0 commit comments

Comments
 (0)