Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support for negative numbers in coordinate sequences #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.7"
- "3.5"
- "3.6"
- "3.7-dev"
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
EMAIL = '[email protected]'
AUTHOR = 'Kevin Brochet-Nguyen'
REQUIRES_PYTHON = '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
VERSION = '0.1.1'
REQUIRED = ['typing'] # Required packages
VERSION = '0.1.2RC3'
REQUIRED = [] # Required packages

here = os.path.abspath(os.path.dirname(__file__))

Expand Down
18 changes: 13 additions & 5 deletions src/geodaisy/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,31 @@ 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
if geo_type in {'Polygon', 'MultiLineString', 'MultiPolygon'}:
coords = re.sub(r'(?<=\d)\), \((?=\d)', ')), ((', coords)
coords = re.sub(r'(?<=\d)\), ?\((?=-?\d)', ')), ((', coords)

# Pairs of coordinates must be enclosed in parentheses
coords = re.sub(r'(?<=\d), (?=\d)', '), (', coords)
coords = re.sub(r'(?<=\d), ?(?=-?\d)', '), (', coords)

# Coordinates within parentheses must be separated by commas
coords = re.sub(r'(?<=\d) (?=\d)', ', ', coords)
coords = re.sub(r'(?<=\d) +(?=-?\d)', ', ', coords)

# Now we can turn the string into a tuple or a tuple of tuples
coords = literal_eval(coords)
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)
7 changes: 6 additions & 1 deletion tests/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
# LineStrings
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)'


# Polygons
Expand Down Expand Up @@ -109,4 +113,5 @@

wkt_shapes = [wkt_point, wkt_linestring, wkt_polygon, wkt_polygon_with_hole,
wkt_multipoint, wkt_multilinestring, wkt_multipolygon,
wkt_multipolygon_with_hole]
wkt_multipolygon_with_hole, wkt_linestring_ne,
wkt_linestring_nw, wkt_linestring_se, wkt_linestring_sw]