Shapely - Python Package - p1.intersection(p2)

Shapely- Python Package - p1.intersection(p2)

1. program

#
# Created by strong on 7/8/18
#

from shapely.geometry import Point, Polygon, MultiPolygon

poly1 = [(35.0041000000000011, -88.1954999999999956), (34.9917999999999978, -85.6068000000000069),
         (32.8404000000000025, -85.1756000000000029), (32.2593000000000032, -84.8927000000000049),
         (32.1535000000000011, -85.0341999999999985), (31.7946999999999989, -85.1358000000000033),
         (31.5199999999999996, -85.0438000000000045), (31.3384000000000000, -85.0836000000000041),
         (31.2092999999999989, -85.1069999999999993), (31.0023000000000017, -84.9943999999999988),
         (30.9953000000000003, -87.6008999999999958), (30.9422999999999995, -87.5926000000000045),
         (30.8538999999999994, -87.6256000000000057), (30.6744999999999983, -87.4072000000000031),
         (30.4404000000000003, -87.3687999999999931), (30.1463000000000001, -87.5240000000000009),
         (30.1545999999999985, -88.3863999999999947), (31.8938999999999986, -88.4742999999999995),
         (34.8937999999999988, -88.1020999999999930), (34.9478999999999971, -88.1721000000000004),
         (34.9106999999999985, -88.1461000000000041)]

poly2 = [(34.7998910000000024, -88.2202139999999986), (34.7998910000000024, -87.2202139999999986),
         (35.7998910000000024, -87.2202139999999986), (35.7998910000000024, -88.2202139999999986)]

p1 = Polygon(poly1)
p2 = Polygon(poly2)

print(p1.is_valid)
print(p2.is_valid)

print('P1 area:', p1.area)
print('P2 area:', p2.area)

print(p1.intersection(p2))

2. exception

/usr/bin/python2.7 /home/strong/PycharmProjects/shapely/shapely_1.py
False
True
('P1 area:', 13.125721954999968)
('P2 area:', 1.0)
No handlers could be found for logger "shapely.geos"
Traceback (most recent call last):
  File "/home/strong/PycharmProjects/shapely/shapely_1.py", line 31, in 
    print(p1.intersection(p2))
  File "/usr/local/lib/python2.7/dist-packages/shapely/geometry/base.py", line 620, in intersection
    return geom_factory(self.impl['intersection'](self, other))
  File "/usr/local/lib/python2.7/dist-packages/shapely/topology.py", line 70, in __call__
    self._check_topology(err, this, other)
  File "/usr/local/lib/python2.7/dist-packages/shapely/topology.py", line 38, in _check_topology
    self.fn.__name__, repr(geom)))
shapely.errors.TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry 

Process finished with exit code 1


>>> p1.is_valid

False

>>> p2.is_valid

True

 

A valid Polygon may not possess anyoverlapping exterior or interior rings.

 

 


你可能感兴趣的:(Python)