Skip to content

Commit

Permalink
Update point_in_polygon.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fiveham authored Dec 26, 2019
1 parent e51a9ab commit 0424e7d
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions point_in_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class BBox:
"""A class to model the bounding box of a collection of points in 2D
space."""

ADD_IDENT = None

def __init__(self, x, X, y, Y, illegal=False):
"""`x`: low x value
`X`: high x value
Expand Down Expand Up @@ -58,6 +60,7 @@ def __repr__(self):

def __iter__(self):
return iter([self.x, self.X, self.y, self.Y])
BBox.ADD_IDENT = BBox(_MAX, _MIN, _MAX, _MIN, illegal=True)

class _Ring(list):
"""A class to represent an inner or outer boundary of a Polygon and to
Expand All @@ -70,6 +73,7 @@ def __init__(self, points):
points[0][1],
points[-1][0],
points[-1][1]))

p0x, p0y = points[0][:2]
p1x, p1y = points[-1][:2]
if p0x != p1x or p0y != p1y:
Expand Down Expand Up @@ -272,6 +276,47 @@ def __eq__(self, other):
return self is other
#return self.outers == other.outers and self.inners == other.inners

def to_kml(self, soup=None):
import kml
if soup is None:
result = '<Placemark>%s</Placemark>'
if len(self.outers) > 1:
result %= '<MultiGeometry>%s</MultiGeometry>'
for i, outer in enumerate(self.outers):
inners = self._out_to_in[i]
polygon = '<Polygon><outerBoundaryIs><LinearRing><coordinates>'
polygon += kml.coords_to_text(outer)
polygon += '</coordinates></LinearRing></outerBoundaryIs>'
for inner in inners:
polygon += '<innerBoundaryIs><LinearRing><coordinates>'
polygon += kml.coords_to_text(inner)
polygon += '</coordinates></LinearRing></innerBoundaryIs>'
polygon += '</Polygon>%s'

result %= polygon
result %= ''
return result
else:
result = soup.new_tag('Placemark')
focus = result
if len(self.outers) > 1:
focus = kml.add(focus, 'MultiGeometry', soup=soup)
for i, outer in enumerate(self.outers):
inners = self._out_to_in[i]
polygon = kml.add(focus, 'Polygon', soup=soup)
kml.add(polygon,
['outerBoundaryIs',
'LinearRing',
'coordinates'],
soup=soup).string = kml.coords_to_text(outer)
for inner in inners:
kml.add(polygon,
['innerBoundaryIs',
'LinearRing',
'coordinates'],
soup=soup).string = kml.coords_to_text(inner)
return result

def spatial_index(self, scale):
import spindex

Expand Down Expand Up @@ -363,6 +408,17 @@ def from_shape(shape, info=None, edge_okay=False):

return Polygon(outers, inners, info=info, edge_okay=edge_okay)

@staticmethod
def from_boundaries(boundaries, info=None, edge_okay=False):
from shapefile import signed_area
outers, inners = [], []
for boundary in boundaries:
if signed_area(boundary) >= 0:
outers.append(boundary)
else:
inners.append(boundary)
return Polygon(outers, inners, info=info, edge_okay=edge_okay)

@staticmethod
def from_kml(placemark, info=None, edge_okay=False):
"""Convert a KML Placemark into a Polygon.
Expand All @@ -373,6 +429,8 @@ def from_kml(placemark, info=None, edge_okay=False):

import kml, itertools
geo = placemark.MultiGeometry or placemark.Polygon
if geo is None:
print(placemark)
outers = [kml.coords_from_tag(obi.coordinates)
for obi in geo('outerBoundaryIs')]

Expand Down

0 comments on commit 0424e7d

Please sign in to comment.