1.安装及导入(在安装shapely之前一定要先安装geos)

pip install geos
pip install shapely

shapely是专门做图形计算的包,基本上图形线段,点的判断包里都有,实现的几何对象的基本类型是点、曲线和曲面;
from shapely.geometry import Point
from shapely.geometry import LineString
from shapely.geometry.polygon import LinearRing
from shapely.geometry import Polygon

#集合
from shapely.geometry import MultiPoint
from shapely.geometry import MultiLineString
from shapely.geometry 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(list(point.coords))
print(point.x)
print(point.y)

#坐标可切片
print(point.coords[:])

构造函数可接受对象类型
Point(point)

(2) 线 LineString

line = LineString([(0, 0), (1, 1)])

#一条线的面积和长度
print(line.area)
print(line.length)

#边界 返回(minx, miny, maxx, maxy) 元组
print(line.bounds)

#坐标值
print(len(line.coords))
print(list(line.coords))

#坐标可切片
print(point.coords[:])

#构造函数可接受对象类型
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(len(ring.coords))
print(list(ring.coords))

LinearRing(ring)

(4) 多边形 Polygon

polygon = Polygon([(0, 0), (1, 1), (1, 0)])

#面积 ,长度
print(polygon.area)
print(polygon.length)

#边界 返回(minx, miny, maxx, maxy) 元组
print(polygon.bounds)

#坐标 外部与内部
print(list(polygon.exterior.coords))
print(list(polygon.interiors))

coords = [(0, 1), (1, 4), (3, 0), (3,1)]
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) object.contains(other)

other里没有点在object的exterior,且other的interior里至少有一个点在object的interior
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.contains(other))

object = Polygon([(0, 0), (0, 1), (1, 1)])
other = Polygon([(0, 1), (1, 2), (0, 0)])
print(object.contains(other))

(2) object.crosses(other)

object的interior与other的interior相交,且不包含他
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.crosses(other))

object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
other = LineString([(0.5, 0.5), (2, 2)])
print(object.crosses(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.intersects(other)

object的interior或者boundary和other的interior或者boundary相交 返回TRUE
object = LineString([(0, 0), (1, 1)])
other = LineString([(0, 0), (1, 1)])
print(object.intersects(other))

object = Point((0, 0))
other = Point((1, 1))
print(object.intersects(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) object.touches(other)

other和object至少有一个共同的点,且他们的interior不相交
object = LineString([(0, 0), (1, 1)])
other = LineString([(1, 1), (2, 2)])
print(object.touches(other))

object = Point((0, 1))
other = Point((0, 0))
print(object.touches(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

交并显示

intersection = line_1.intersection(b)
x1, y1 = line_1.xy
x2, y2 = b.boundary.xy
plt.figure()
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐