python之shapely库的使⽤
1.安装及导⼊(在安装shapely之前⼀定要先安装geos)knowledge is power
pip install geos
pip install shapely
shapely是专门做图形计算的包,基本上图形线段,点的判断包⾥都有,实现的⼏何对象的基本类型是点、曲线和曲⾯; ry import Point
ry import LineString
ry.polygon import LinearRing
ry import Polygon
#集合
ry import MultiPoint
ry import MultiLineString
ry import MultiPolygon
from shapely.ops import unary_union
import matplotlib.pyplot as plt
2. 常⽤⼏何对象的属性及⽅法
(1) 点 Point
point = Point((0.0, 1.0))
#⼀个点的⾯积和长度均为0
print(point.area)
print(point.length)
得意洋洋#边界 ,返回(minx, miny, maxx, maxy) 元组
print(point.bounds)
#坐标值
print(ds))
print(point.x)
print(point.y)
#坐标可切⽚
ds[:])
构造函数可接受对象类型
日语幸运Point(point)
(2) 线 LineString
line = LineString([(0, 0), (1, 1)])
#⼀条线的⾯积和长度
print(line.area)
print(line.length)
#边界 返回(minx, miny, maxx, maxy) 元组
print(line.bounds)
#坐标值
print(ds))
print(ds))
#坐标可切⽚
ds[:])
#构造函数可接受对象类型
LineString(line)
LineString([Point(0.0, 1.0), (2.0, 3.0), Point(4.0, 5.0)])
(3)线环 LinearRing
ring = LinearRing([(0, 0), (1, 1), (1, 0)])
#⾯积,长度
print(ring.area)
print(ring.length)
#边界 返回(minx, miny, maxx, maxy) 元组
print(ring.bounds)
#坐标
print(ds))
print(ds))
go over
LinearRing(ring)tem4
(4) 多边形 Polygon
polygon = Polygon([(0, 0), (1, 1), (1, 0)])
#⾯积 ,长度
print(polygon.area)
print(polygon.length)
#边界 返回(minx, miny, maxx, maxy) 元组
print(polygon.bounds)
#坐标 外部与内部
print(ds))
print(list(polygon.interiors))
coords = [(0, 1), (1, 4), (3, 0), (3,1)]ddn
Polygon(coords)
(5) 集合 collections
⼏何对象的异构集合可能由⼀些 Shapely 操作产⽣,集合可以是同类的(MultiPoint, MultiLineString, MultiPolygon 等)或异类的。MultiPoint([Point(0, 0), Point(1, 1)])
coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0)),((4, 5), (3, 6))]
MultiLineString(coords)
p1 = Polygon([(0, 1), (1, 4), (3, 0), (3,1)])
p2 = Polygon([(1, 2), (4, 3), (3, 2), (3,4)])
p3 = Polygon([(1, 1), (2, 4), (3, 4), (3,7)])
MultiPolygon([p1, p2, p3])
3.常⽤的⼀些⽅法
(1) ains(other)
other⾥没有点在object的exterior,且other的interior⾥⾄少有⼀个点在object的interior object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
ains(other))
object = Polygon([(0, 0), (0, 1), (1, 1)])
other = Polygon([(0, 1), (1, 2), (0, 0)])
ains(other))
(2) ss(other)
object的interior与other的interior相交,且不包含他
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
好听的英文舞曲
ss(other))
object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
other = LineString([(0.5, 0.5), (2, 2)])
ss(other))
(3) object.disjoint(other)
求职信格式
object的interior和boundary 和other的interior和boundary都不想交 返回True
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.disjoint(other))
object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
other = LineString([(2, 2), (3, 3)])
print(object.disjoint(other))
(4) object.intercts(other)
object的interior或者boundary和other的interior或者boundary相交 返回TRUE
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.intercts(other))
object = Point((0, 0))
other = Point((1, 1))
print(object.intercts(other))
(5) object.overlaps(other)
object的interior或者boundary和other的interior或者boundary相交,且不包含他, 返回TRUE object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.overlaps(other))
为您效劳object = Point((0, 0))
other = Point((1, 1))
print(object.overlaps(other))
(6) uches(other)
other和object⾄少有⼀个共同的点,且他们的interior不相交
object = LineString([(0, 0), (1, 1)])
other = LineString([(1, 1), (2, 2)])
uches(other))
object = Point((0, 1))
old women
other = Point((0, 0))
uches(other))
(7) shapely.ops.unary_union(geoms) ⼏何合并
返回多个⼏何对象合并以后的结果
import matplotlib.pyplot as plt # matplotlib 数据可视化
import geopandas as gpd
poly_union = gpd.GeoSeries([Polygon([(0,0), (0,2), (1,2), (1,3),
(2,3), (2,4), (3, 4), (3, 5), (5, 5), (5, 3), (4, 3), (4, 2),
(3,2), (3,1), (2, 1), (2, 0), (0, 0)])])
poly_union.plot(color = ‘blue’)
plt.show()
(8) buffer() 缓冲区分析
根缓冲区分析⽅法据指定的距离,在点、线、⾯⼏何对象周围建⽴⼀定宽度的区域的分析⽅法
点缓冲区:围绕给定点以指定缓冲距离为半径⽣成圆形区域
p1 = Point(1, 1)
a = p1.buffer(2)
a
线缓冲区:沿线对象的法线⽅向,分别向线对象的两侧平移⼀定的距离⽽得到两条线,并与在线端点处形成的光滑曲线(或平头)接合形成的封闭区域
line_1 = LineString([(0.1, 0.1), (2, 3)])
b = line_1.buffer(0.5)
b
交并显⽰
interction = line_1.interction(b)
x1, y1 =
x2, y2 =
plt.figure()
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()