Skip to content

Commit

Permalink
fix: update Shapely and refactor object formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
hypsug0 committed Jan 25, 2023
1 parent e5ce771 commit 2da2096
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 55 deletions.
37 changes: 11 additions & 26 deletions pyopenair/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
altitude_formatter,
fields_formatter,
comment_formatter,
object_formatter,
)


Expand Down Expand Up @@ -73,33 +74,17 @@ def wkt2openair(
header.append(altitude_formatter("H", ah_alti, ah_unit, ah_mode))
if al_alti and al_mode:
header.append(altitude_formatter("L", al_alti, al_unit, al_mode))
geom = loads(wkt)
if geom.geom_type == "Polygon":
node_coords = []
for node in list(geom.exterior.coords):
node_coords.append(generate_coords(node))
node_coords = list(OrderedDict.fromkeys(node_coords))
desc = "\n".join(header).format(label=label)
for coord in node_coords:
desc += "\n{}".format(coord)
return desc
elif geom.geom_type == "MultiPolygon":
areas = []
lengeom = len(geom)
i = 1
for g in geom:
node_coords = []
for node in list(g.exterior.coords):
node_coords.append(generate_coords(node))
node_coords = list(OrderedDict.fromkeys(node_coords))
label_elem = "{} ({}/{})".format(label, i, lengeom)
i = i + 1
desc_tpl = "\n".join(header)
desc = desc_tpl.format(label=label_elem)
for coord in node_coords:
desc += "\n{}".format(coord)
obj = loads(wkt)
if obj.geom_type == "Polygon":
return object_formatter(obj, label, header)
elif obj.geom_type == "MultiPolygon":
areas, len_geom, i = [], len(obj.geoms), 0
for g in obj.geoms:
i += 1
label_item = "{} ({}/{})".format(label, i, len_geom)
desc = object_formatter(g, label_item, header)
desc += "\n\n"
areas.append(desc)
return "\n".join(areas)
return "\n".join(areas)
else:
return None
25 changes: 25 additions & 0 deletions pyopenair/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging
from math import ceil, floor
from collections import OrderedDict
from shapely.geometry.polygon import Polygon


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -255,3 +257,26 @@ def coalesce(var: any, default: any = "default") -> any:
:rtype: any
"""
return default if var is None else var


def object_formatter(g: Polygon, label: str, header: list) -> str:
"""Return a single OpenAir object
:param g: Geometry to convert
:type g: Polygon
:param label: Object label
:type label: str
:param header: Object headers list
:type header: list
:return: Openair object
:rtype: str
"""

node_coords = []
for node in list(g.exterior.coords):
node_coords.append(generate_coords(node))
node_coords = list(OrderedDict.fromkeys(node_coords))
desc = "\n".join(header).format(label=label)
for coord in node_coords:
desc += "\n{}".format(coord)
return desc
Empty file removed tests/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

from typing import NamedTuple

class BasicDataToTest(NamedTuple):
wkt: str
an: str = "label"
ac: str = "class"


class DataToTest(NamedTuple):
wkt: str
an: str = "label"
ac: str = "class"
al_alti: int = 100
al_unit: str = "m"
al_mode: str = "AGL"
ah_alti: int = 1000
ah_unit: str = "m"
ah_mode: str = "AGL"

headers = ['AC class', 'AH 3281FT AGL', 'AN {label}','AL 328FT AGL']
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))"
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)))"
58 changes: 29 additions & 29 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import logging
from typing import NamedTuple
from data import BasicDataToTest, DataToTest, wktpoly, wktmultipoly

from pyopenair.factory import wkt2openair

logger = logging.getLogger(__name__)


class BasicDataToTest(NamedTuple):
wkt: str
an: str = "label"
ac: str = "class"


class DataToTest(NamedTuple):
wkt: str
an: str = "label"
ac: str = "class"
al_alti: int = 100
al_unit: str = "m"
al_mode: str = "AGL"
ah_alti: int = 1000
ah_unit: str = "m"
ah_mode: str = "AGL"


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))"
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)))"
# an = "label"
# ac = "class"
# al_alti = 100
# al_unit = "m"
# al_mode = "AGL"
# ah_alti = 1000
# ah_unit = "m"
# ah_mode = "AGL"
# class BasicDataToTest(NamedTuple):
# wkt: str
# an: str = "label"
# ac: str = "class"


# class DataToTest(NamedTuple):
# wkt: str
# an: str = "label"
# ac: str = "class"
# al_alti: int = 100
# al_unit: str = "m"
# al_mode: str = "AGL"
# ah_alti: int = 1000
# ah_unit: str = "m"
# ah_mode: str = "AGL"


# 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))"
# 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)))"
# # an = "label"
# # ac = "class"
# # al_alti = 100
# # al_unit = "m"
# # al_mode = "AGL"
# # ah_alti = 1000
# # ah_unit = "m"
# # ah_mode = "AGL"


def test_wkt2openair_poly_only_required():
Expand Down
17 changes: 17 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
generate_coords,
generate_openair_coord,
decdeg2dms,
object_formatter,
)
from shapely.wkt import loads
from data import BasicDataToTest, DataToTest, wktpoly, wktmultipoly, headers


def test_stringify_coords_l2():
Expand Down Expand Up @@ -94,3 +97,17 @@ def test_comment_formatter():
"""
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**********************************************************"
assert comment_formatter(comment) == result


def test_object_formatter():
obj = object_formatter(loads(wktpoly), "labeldetest", headers)
result = """AC class
AH 3281FT AGL
AN labeldetest
AL 328FT AGL
DP 44:40:28 N 03:51:34 E
DP 44:34:43 N 03:49:25 E
DP 44:31:04 N 03:57:59 E
DP 44:34:57 N 04:00:57 E
DP 44:40:42 N 03:57:59 E"""
assert obj == result

0 comments on commit 2da2096

Please sign in to comment.