Skip to content

Commit

Permalink
Merge pull request #102 from jwass/linestring_constructor
Browse files Browse the repository at this point in the history
Allow LineStrings to take arrays of Points
  • Loading branch information
sgillies committed Apr 8, 2014
2 parents 2bc492a + bb65311 commit f6e4e53
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 10 additions & 2 deletions shapely/geometry/linestring.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from shapely.geos import lgeos, TopologicalError
from shapely.geometry.base import BaseGeometry, geom_factory, JOIN_STYLE
from shapely.geometry.proxy import CachingGeometryProxy
from shapely.geometry.point import Point

__all__ = ['LineString', 'asLineString']

Expand Down Expand Up @@ -237,8 +238,15 @@ def geos_linestring_from_py(ob, update_geom=None, update_ndim=0):
if m < 2:
raise ValueError(
"LineStrings must have at least 2 coordinate tuples")

def _coords(o):
if isinstance(o, Point):
return o.coords[0]
else:
return o

try:
n = len(ob[0])
n = len(_coords(ob[0]))
except TypeError:
raise ValueError(
"Input %s is the wrong shape for a LineString" % str(ob))
Expand All @@ -256,7 +264,7 @@ def geos_linestring_from_py(ob, update_geom=None, update_ndim=0):

# add to coordinate sequence
for i in range(m):
coords = ob[i]
coords = _coords(ob[i])
# Because of a bug in the GEOS C API,
# always set X before Y
lgeos.GEOSCoordSeq_setX(cs, i, coords[0])
Expand Down
12 changes: 11 additions & 1 deletion shapely/tests/test_linestring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import unittest, numpy
from shapely.geometry import LineString, asLineString
from shapely.geometry import LineString, asLineString, Point


class LineStringTestCase(unittest.TestCase):
Expand All @@ -11,6 +11,16 @@ def test_linestring(self):
self.assertEqual(len(line.coords), 2)
self.assertEqual(line.coords[:], [(1.0, 2.0), (3.0, 4.0)])

# From Points
line2 = LineString((Point(1.0, 2.0), Point(3.0, 4.0)))
self.assertEqual(len(line2.coords), 2)
self.assertEqual(line2.coords[:], [(1.0, 2.0), (3.0, 4.0)])

# From mix of tuples and Points
line3 = LineString((Point(1.0, 2.0), (2.0, 3.0), Point(3.0, 4.0)))
self.assertEqual(len(line3.coords), 3)
self.assertEqual(line3.coords[:], [(1.0, 2.0), (2.0, 3.0), (3.0, 4.0)])

# From lines
copy = LineString(line)
self.assertEqual(len(copy.coords), 2)
Expand Down

0 comments on commit f6e4e53

Please sign in to comment.