shapely 是一个专门进行二维平面图像计算包
shapely是一个BSD授权的Python包。是专门做图形计算,用于操作和分析笛卡尔坐标系中的几何对象 ,基本上图形线段,点的判断包里都有,shapely里主要由Point,LineString,Polygon这三类组成。
常用对象 | 说明 | |
---|---|---|
Point |
点 | |
LineString |
线段 | |
Polygon |
多边形 |
共有属性
共有属性 | 描述 | |
---|---|---|
object.area |
获得面积 | |
object.bounds |
获得边界 (minx,miny,maxx,maxy) | |
object.length |
长度 | |
object.geom_type |
获得类型 Point | |
object.distance(other) |
获取和其他对象距离,不限于本类型 | |
object.is_empty |
是否为空 | |
object.is_valid |
是否有效 |
from shapely.geometry import Point from shapely.geometry import LineString print (Point(0,0).distance(Point(0,1))) line = LineString([(0,0), (1,1), (1,2)]) print(line.area) print(bine.bounds) print(line.length) print(line.geom_type) # ---------------------------------------------------------- 1.0 0.0 (0.0, 0.0, 1.0, 2.0) 2.414213562373095 'LineString'
Point
点对象
点对象具有零面积和非零长度
from shapely.geometry import Point # 三种创建方式 point = Point(1,1) point_2 = Point((1,1)) point_3 = Point(point) # 复制已有的点对象 print(point.area) # 点的 area==0.0 print(ponit.length) # 点的 length==0.0 # 得到X,y # Point.x,Point.y,Point.z : 获取对应x,y,z坐标值 print(point.x) # 1.0 print(point.y) # 1.0 # (minx, miny, maxx, maxy) 元组 print(point.bounds) #(1) # Point.coords: 返回坐标值 # 通过 coords得到 x,y print(list(p.coords)) # [(1.0,1.0)] # coords可以被切片 print(p.coords[:]) # [(1.0,1.0)]
LineStrings
LineStrings
构造函数传入参数是2个或多个点序列
线段对象
线段对象具有零面积和非零长度
from shapely.geometry import LineString line = LinearRing([(0, 0), (1, 1), (1, 0)]) # 创建线段对象 print(line.area) #0 print(line.length) #3.4142135623730949 print(line.bounds) #边界 #(0.0, 0.0, 1.0, 1.0)
Polygon
多边形对象----为了结果的正确性,建议强制指定为凸多边形
Polygon
构造函数采用两个位置参数。 第一个是(x,y [,z])
点元组的有序序列,其处理方式与LinearRing
情况完全相同。 第二个是可选的无序环状序列,用于指定特征的内部边界或“孔”。
from shapely.geometry import Polygon polygon = Polygon([(0, 0), (1, 1), (1, 0)]) # 三角形 print(polygon.area) print(polygon.length) print(polygon.bounds) print(polygon.boundary) #0.5 #3.414213562373095 #(0.0, 0.0, 1.0, 1.0) #LINESTRING (0 0, 1 1, 1 0, 0 0)
object.bound
polygon = Polygon([(0, 0),(1, 1), (1, 0)]) print(polygon.bounds) #(0.0, 0.0, 1.0, 1.0) # xmin,ymin,xmax,ymax
object.boundary
polygon = Polygon([(0, 0), (2, 2), (2, 0)]) print(polygon.boundary) # 降一维度,由多边形变成线段 # 最小对象集合 #LINESTRING (0 0, 2 2, 2 0, 0 0) line = LinearRing([(0, 0), (1, 1), (1, 0)]) # 创建线段对象 print(line.boundary) #MULTIPOINT EMPTY
object.centroid
line = LinearRing([(0, 0), (1, 1), (1, 0)]) # 创建线段对象 polygon = Polygon([(0, 0), (0,1),(1, 1), (1, 0)]) print(polygon.centroid) print(line.centroid) #POINT (0.5 0.5) #POINT (0.6464466094067263 0.3535533905932737)
进阶属性
进阶属性 | 描述 | |
---|---|---|
object.contains(other) |
是否包含 | |
object.difference(other) |
差集 | |
object.intersection(other) |
交集 | |
object.symmetric_difference(other) |
对称差集 | |
object.union(other) |
并集 |
object.intersection(other)
交集
返回此对象与另一个几何对象的交集的表示形式
a,b相交的部分 即 a∩b
polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) polygon2 = Polygon([(0, 0), (0.5, 1), (-0.5, 1), (-1, 0)]) box=polygon1.intersection(polygon2) print(box) print(box.area) #POLYGON ((0 0, 0 1, 0.5 1, 0 0)) #0.25 # 如果两个图形没有交集 polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) polygon2 = Polygon([(0, 0), (-0.5, 1), (-1, 0)]) box=polygon2.intersection(polygon1) print(box) print(box.area) #POINT (0 0) #0.0
object.difference(other)
polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) polygon2 = Polygon([(0, 0), (0.5, 1), (-0.5, 1), (-1, 0)]) polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) polygon2 = Polygon([(0, 0), (0.5, 1), (-0.5, 1), (-1, 0)]) box=polygon2.difference(polygon1) print(box) print(box.area) #POLYGON ((0 0, -1 0, -0.5 1, 0 1, 0 0)) #0.75
object.symmetric_difference(other)
object.union(other)
并集
返回此对象和另一个几何对象的点并集的表示形式
a,b的并集 即 a∪b
更高效的方法: shapely.ops.unary_union()
polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) polygon2 = Polygon([(0, 0), (0.5, 1), (-0.5, 1), (-1, 0)]) box = polygon2.union(polygon1) print(box) print(box.area) #POLYGON ((0 1, 0.5 1, 1 1, 1 0, 0 0, -1 0, -0.5 1, 0 1)) #1.75
还有很多有意思的用法:
参考:
https://blog.csdn.net/linzi1994/article/details/106484538