Skip to content

Commit

Permalink
added wkt repr for chain/polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
parulsethi committed Feb 26, 2017
1 parent 2223c82 commit 7a6b0bf
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions pysal/cg/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from warnings import warn
from sphere import arcdist
import numpy as np
import copy

__all__ = ['Point', 'LineSegment', 'Line', 'Ray', 'Chain', 'Polygon',
'Rectangle', 'asShape']
Expand Down Expand Up @@ -1187,6 +1188,26 @@ def segments(self):
"""
return [[LineSegment(a, b) for (a, b) in zip(part[:-1], part[1:])] for part in self._vertices]

def __repr__(self):
"""
Returns the string representation of the Chain
__repr__() -> string
Parameters
----------
None
Attributes
----------
Examples
--------
>>> Chain([[Point((0, 0)), Point((1, 0)), Point((1, 1))],[Point((10,10)),Point((11,10)),Point((11,11))]])
[[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)], [(10.0, 10.0), (11.0, 10.0), (11.0, 11.0)]]
"""
return str(self.parts)


class Ring(Geometry):
"""
Expand Down Expand Up @@ -1392,8 +1413,6 @@ def contains_point(self, point):
return True




class Polygon(Geometry):
"""
Geometric representation of polygon objects.
Expand Down Expand Up @@ -1763,6 +1782,39 @@ def contains_point(self, point):

return False

def __repr__(self):
"""
Returns the string representation of the Polygon
__repr__() -> string
Parameters
----------
None
Attributes
----------
Examples
--------
>>> Polygon([[Point((0,0)), Point((1,3)), Point((3,3)), Point((3,1))],[Point((4,8)), Point((8,5)), Point((8,1)), Point((4,1))]], [Point((5, 2)), Point((5,3)), Point((6,3)), Point((6,2))])
[[((5.0, 2.0), (5.0, 3.0), (6.0, 3.0), (6.0, 2.0)), ((4.0, 8.0), (8.0, 5.0), (8.0, 1.0), (4.0, 1.0), (4.0, 8.0))], [((0.0, 0.0), (1.0, 3.0), (3.0, 3.0), (3.0, 1.0), (0.0, 0.0))]]
"""
if len(self._hole_rings) == 0:
return str(self.parts)
else:
rings = copy.deepcopy(self._part_rings)
out = []
# pair holes with exterior rings
for hole in self.holes:
for ring in rings:
if ring.contains_point(hole[0]):
out.append([tuple(hole), ring.vertices])
rings.remove(ring)
# append the remaining subpolygons(which didn't had holes)
for ring in rings:
out.append([ring.vertices])
return str(out)


class Rectangle(Geometry):
Expand Down

0 comments on commit 7a6b0bf

Please sign in to comment.