Skip to content

Commit

Permalink
Fixed mixed-case and mixed-space WKT variations
Browse files Browse the repository at this point in the history
  • Loading branch information
migurski committed Jun 16, 2020
1 parent 146e7f9 commit e68915f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/geodaisy/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,20 @@ def geo_interface_to_wkt(geo_interface):
return '{} {}'.format(geo_type, coords)


def split_wkt(wkt):
# type: (str) -> tuple
"""Splits a WKT string to a type and a set of coordinates."""
type_coords = re.match(r'^\s*([a-zA-Z]+)\s*(\(.+)\s*$', wkt.upper())
if type_coords:
return type_coords.group(1), type_coords.group(2)
return None, None


def wkt_to_geo_interface(wkt):
# type: (str) -> dict
"""Converts a WKT string to a geo_interface dictionary."""
try:
wkt_type, coords = re.split(r'(?<=[A-Z])\s', wkt)

wkt_type, coords = split_wkt(wkt)
geo_type = type_translations[wkt_type]

# Clean up the strings so they'll covert correctly
Expand Down
6 changes: 4 additions & 2 deletions src/geodaisy/geo_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
geo_interface_to_geojson,
geo_interface_to_wkt,
wkt_to_geo_interface,
wkt_types)
wkt_types,
split_wkt)


class GeoObject(object):
Expand Down Expand Up @@ -92,8 +93,9 @@ def _parse_string(self, geo_thing):
except ValueError:
raise ValueError(error_msg)
else:
wkt_type = geo_thing.split(' ')[0]
wkt_type, _ = split_wkt(geo_thing)
if wkt_type not in wkt_types:

raise ValueError(error_msg)
else:
return wkt_to_geo_interface(geo_thing)
6 changes: 3 additions & 3 deletions tests/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
geo_linestring = geojson.LineString([(8.919, 44.4074), (8.923, 44.4075)])
wkt_linestring = 'LINESTRING (30 10, 10 30, 40 40)'
wkt_linestring_ne = 'LINESTRING (58.612 34.642,58.613 34.641)'
wkt_linestring_sw = 'LINESTRING (-58.612 -34.642,-58.613 -34.641)'
wkt_linestring_se = 'LINESTRING (58.612 -34.642,58.613 -34.641)'
wkt_linestring_nw = 'LINESTRING (-58.612 34.642,-58.613 34.641)'
wkt_linestring_sw = 'linestring (-58.612 -34.642,-58.613 -34.641)'
wkt_linestring_se = 'LINESTRING(58.612 -34.642,58.613 -34.641)'
wkt_linestring_nw = 'linestring(-58.612 34.642,-58.613 34.641)'


# Polygons
Expand Down

0 comments on commit e68915f

Please sign in to comment.