感谢@AndreJ的这个想法,这里使用Django GEOS API是一个简单的解决方案,避免了重新投影任何事情:
1)创建一个在日期线上加上边框的MultiPolygon:
from django.contrib.gis.geos.collections import MultiPolygon, LinearRing, Polygon
box1 = ((180.0, 89), (179.95, 89), (179.95, -89), (180.0, -89), (180.0, 89))
box2 = ((-180.0, 89), (-179.95, 89), (-179.95, -89), (-180.0, -89), (-180.0, 89))
poly1 = Polygon(LinearRing(box1))
poly2 = Polygon(LinearRing(box2))
poly = MultiPolygon(poly1, poly2)
2)如果有问题的几何相交,则返回差值:
from django.contrib.gis.geos.geometry import GEOSGeometry
geometry = GEOSGeometry(WKT) # WKT is your polygon in WKT string format
if geometry.intersects(poly):
print("Geometry crosses dateline... splitting")
geometry = geometry.difference(poly) # clip with dateline polygon
结果如下: