Skip to content

Commit

Permalink
extract the WKT parsing code and converts it into a dedicated reading…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
mdvandamme committed Dec 2, 2024
1 parent 2b27541 commit 79a82b3
Showing 1 changed file with 59 additions and 29 deletions.
88 changes: 59 additions & 29 deletions tracklib/io/track_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,40 +436,14 @@ def __readFromWkt(path:str, fmt:TrackFormat, verbose=False) -> TrackCollection:
if len(fields) <= 0:
continue

track = Track()
wkt = fields[fmt.id_wkt]
track = TrackReader.parseWkt(wkt)

if fmt.id_user >= 0:
track.uid = fields[fmt.id_user]
if fmt.id_track >= 0:
track.tid = fields[fmt.id_track]

wkt = fields[fmt.id_wkt]
if wkt[0:4] == "POLY":
wkt = fields[fmt.id_wkt].split("((")[1].split("))")[0]
wkt = wkt.split(",")
elif wkt[0:4] == "LINE":
wkt = fields[fmt.id_wkt].split("(")[1].split(")")[0]
wkt = wkt.split(",")
elif wkt[0:7] == "MULTIPO":
wkt = fields[fmt.id_wkt].split("((")[1].split("))")[0]
wkt = wkt.split(",")
if wkt[0] == "(":
wkt = wkt[1:]
wkt = wkt.split("),(")[0] # Multipolygon not handled yet
else:
raise WrongArgumentError("This type of wkt is not yet implemented.")

for s in wkt:
sl = s.strip().split(" ")
x = float(sl[0])
y = float(sl[1])
if len(sl) == 3:
z = float(sl[2])
else:
z = 0.0

point = Obs(makeCoords(x, y, z, fmt.srid.upper()), ObsTime())
track.addObs(point)

if not fmt.selector is None:
if not fmt.selector.contains(track):
continue
Expand All @@ -481,6 +455,62 @@ def __readFromWkt(path:str, fmt:TrackFormat, verbose=False) -> TrackCollection:
return TRACES


@staticmethod
def parseWkt(wkt:str) -> Track:
"""
Read track from a str, with geometry provided in wkt.
Only LineString and Polygon are handled yet.
Parameters
----------
wkt : TYPE
DESCRIPTION.
Raises
------
WrongArgumentError
DESCRIPTION.
Returns
-------
track : TYPE
DESCRIPTION.
"""

track = Track()

wkt = wkt.upper()
if wkt[0:4] == "POLY":
wkt = wkt.split("((")[1].split("))")[0]
wkt = wkt.split(",")
elif wkt[0:4] == "LINE":
wkt = wkt.split("(")[1].split(")")[0]
wkt = wkt.split(",")
elif wkt[0:7] == "MULTIPO":
wkt = wkt.split("((")[1].split("))")[0]
wkt = wkt.split(",")
if wkt[0] == "(":
wkt = wkt[1:]
wkt = wkt.split("),(")[0] # Multipolygon not handled yet
else:
raise WrongArgumentError("This type of wkt is not yet implemented.")

for s in wkt:
sl = s.strip().split(" ")
x = float(sl[0])
y = float(sl[1])
if len(sl) == 3:
z = float(sl[2])
else:
z = 0.0

point = Obs(makeCoords(x, y, z, 'ENU'), ObsTime())
track.addObs(point)

return track


# =========================================================================
# GPX
#
Expand Down

0 comments on commit 79a82b3

Please sign in to comment.