OGR几何关系与操作
关系
本文脚本需要导入ogr模块
from osgeo import ogr
Equals
两个几何的边界、内部和外部重合
a1=ogr.CreateGeometryFromWkt("POINT(1 1)")
a2=ogr.CreateGeometryFromWkt("POINT(1 1)")
print(a1.Equals(a2)) # True
Contains
几何包含另一个几何的内部和边界,并且边界不接触,适用于所有几何类型,并且a.Contains(b)==b.Within(a)
p=ogr.CreateGeometryFromWkt("POLYGON((0 0,0 2,2 2,2 0,0 0))")
l=ogr.CreateGeometryFromWkt("LINESTRING(0.5 0.5,1.5 1.5)")
a=ogr.CreateGeometryFromWkt("POINT(0.5 0.5)")
b=ogr.CreateGeometryFromWkt("POINT(1 1)")
c=ogr.CreateGeometryFromWkt("POINT(1.5 1.5)")
print(p.Contains(l)) # True
print(p.Contains(a)) # b,c True
print()
print(l.Contains(a)) # False
print(l.Contains(b)) # True
print(l.Contains(c)) # False
print()
print(a.Within(l)) # False
print(b.Within(l)) # True
print(c.Within(l)) # False
点的边界为空,线的边界就是线的两个端点,因此线不包含本身的两个端点,多边形的边界是组成边界的线。
Crosses
几何与另一个几何内部相交,但不包含它,且相交的维度小于其中一个几何的维度,适用于线–>线,线–>面。
p=ogr.CreateGeometryFromWkt("POLYGON((0 0,0 2,2 2,2 0,0 0))")
l1=ogr.CreateGeometryFromWkt("LINESTRING(1 1,3 2)")
l2=ogr.CreateGeometryFromWkt("LINESTRING(2 3,3 1)")
print(l1.Crosses(p)) # True
print(l1.Crosses(l2)) # True
Disjoint
两个几何边界、内部都不相交
a1=ogr.CreateGeometryFromWkt("POINT(0 0)")
a2=ogr.CreateGeometryFromWkt("POINT(1 1)")
print(a1.Disjoint(a2)) # True
Touches
两个几何边界相交,内部不相交
l1=ogr.CreateGeometryFromWkt("LINESTRING(4 2,3 1)")
l2=ogr.CreateGeometryFromWkt("LINESTRING(3 1,2 2)")
p1=ogr.CreateGeometryFromWkt("POLYGON((0 0,0 1,1 1,2 0,0 0))")
p2=ogr.CreateGeometryFromWkt("POLYGON((1 1,1 2,2 2,2 0,1 1))")
print(l1.Touches(l2)) # True
print(p1.Touches(p2)) # True
print(l2.Touches(p2)) # True
Intersects
两个几何的Contains、Crosses、Equals、Touches、Within其中一个为True,则为True
Overlaps
两个多边形相交,但相互不包含
p1=ogr.CreateGeometryFromWkt("POLYGON((0 0,0 3,3 3,3 0,0 0))")
p2=ogr.CreateGeometryFromWkt("POLYGON((1 1,1 2,2 2,2 1,1 1))")
p3=ogr.CreateGeometryFromWkt("POLYGON((2 1,2 2,4 2,4 1,2 1))")
print(p2.Overlaps(p1)) # False
print(p3.Overlaps(p1)) # True
操作
Buffer
缓冲区
l=ogr.CreateGeometryFromWkt("LINESTRING(0 0,1 1)")
print(l.ExportToJson())
buffer=l.Buffer(0.5)
Boundary
提取边界
p=ogr.CreateGeometryFromWkt("POLYGON((0 0,1 2,3 1,2 0,0 0))")
print(p.ExportToJson())
boundary=p.Boundary()
print(boundary.ExportToJson())
# { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 1.0, 2.0 ], [ 3.0, 1.0 ], [ 2.0, 0.0 ], [ 0.0, 0.0 ] ] ] }
# { "type": "LineString", "coordinates": [ [ 0.0, 0.0 ], [ 1.0, 2.0 ], [ 3.0, 1.0 ], [ 2.0, 0.0 ], [ 0.0, 0.0 ] ] }
Intersection
相交
p1=ogr.CreateGeometryFromWkt("POLYGON((0 0,1 2,3 1,2 0,0 0))")
p2=ogr.CreateGeometryFromWkt("POLYGON((0 0,1 2,3 1,2 0,0 0))")
p1.Intersection(p2)
SymDifference
对称差
p1.SymDifference(p2)
Difference
相减
p1.Difference(p2)
p2.Difference(p1)
Union
p1.Union(p2)
Envelope
最小外包矩形
p=ogr.CreateGeometryFromWkt("POLYGON((0 0,1 2,3 1,2 0,0 0))")
envelope=p.GetEnvelope()
print("envelope:",envelope)
# envelope: (0.0, 3.0, 0.0, 2.0)
来源:CSDN
作者:llc的足迹
链接:https://blog.csdn.net/this_is_id/article/details/104169842