Answers:
您可以为此使用GDAL / OGR Python绑定。
from osgeo import ogr
wkt1 = "POLYGON ((1208064.271243039 624154.6783778917, 1208064.271243039 601260.9785661874, 1231345.9998651114 601260.9785661874, 1231345.9998651114 624154.6783778917, 1208064.271243039 624154.6783778917))"
wkt2 = "POLYGON ((1199915.6662253144 633079.3410163528, 1199915.6662253144 614453.958118695, 1219317.1067437078 614453.958118695, 1219317.1067437078 633079.3410163528, 1199915.6662253144 633079.3410163528)))"
poly1 = ogr.CreateGeometryFromWkt(wkt1)
poly2 = ogr.CreateGeometryFromWkt(wkt2)
intersection = poly1.Intersection(poly2)
print intersection.ExportToWkt()
None
如果它们不相交,则返回。如果它们相交,则返回几何都相交。
您也可以在GDAL / OGR Cookbook中找到更多信息。
如果您知道或对学习R感兴趣,它具有一些有用的空间包。http://cran.r-project.org/web/views/Spatial.html 有Python模块可与R(RPy *)交互
我知道这是一个老问题,但是我已经编写了一个python库来处理凹凸多边形和圆之间的碰撞。
它非常简单易用,您可以使用!
例:
from collision import *
from collision import Vector as v
p0 = Concave_Poly(v(0,0), [v(-80,0), v(-20,20), v(0,80), v(20,20), v(80,0), v(20,-20), v(0,-80), v(-20,-20)])
p1 = Concave_Poly(v(20,20), [v(-80,0), v(-20,20), v(0,80), v(20,20), v(80,0), v(20,-20), v(0,-80), v(-20,-20)])
print(collide(p0,p1))
您还可以让它生成响应,包括:
overlap (how much they overlap)
overlap vector (when subtracted from second shapes position, the shapes will no longer be colliding)
overlap vector normalized (vector direction of collision)
a in b (whether the first shape is fully inside the second)
b in a (whether the second shape is fully inside the first)
如果您想知道级别,可以使用它。作为参数,您可以给出多边形列表。作为返回值,您可以获得级别列表。在级别列表中,有多边形。
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
def isPolygonInPolygon(poly1,poly2):
poly2 = Polygon(poly2)
for poi in poly1:
poi = Point(poi)
if(poly2.contains(poi)):
return True
def polygonTransformHierarchy(polygon_list):
polygon_list_hierarchy = []
for polygon1 in polygon_list:
level = 0
for polygon2 in polygon_list:
if(isPolygonInPolygon(polygon1, polygon2)):
level += 1
if(level > len(polygon_list_hierarchy)-1):
dif = (level+1)- len(polygon_list_hierarchy)
for _ in range(dif):
polygon_list_hierarchy.append([])
polygon_list_hierarchy[level].append(polygon1)
return polygon_list_hierarchy