python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python shapely

Python shapely库的具体使用

作者:jiang_huixin

本文主要介绍了Python shapely库的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

基本类型

基本几何类型包括: 点(Point), 线(LineString), 线环(LinearRing), 多边形(Polygon)

所有的基本几何对象均存在以下属性:

Point

Point 代表几何上的一个点

from shapely.geometry import Point
point_1 = Point(1, 1)
# 传递的参数为坐标元组
point_2 = Point((2, 2))
print(point_1, point_2)
# 具体坐标
print('x =', point_1.x, 'y =', point_1.y)
# 长度面积
print('length =', point_1.length, 'area =', point_1.area)
# 边界框
print('bounds of point:', point_1.bounds)
# 坐标值
print('coords of point:', list(point_1.coords))

在几何对象的创建中, 一个坐标元组 (x, y[, z]) 代表一个点, 多个点的组合嵌套可以创建更复杂的几何对象

打印结果如下:

POINT (1 1) POINT (2 2)
x = 1.0 y = 1.0
length = 0.0 area = 0.0
bounds of point: (1.0, 1.0, 1.0, 1.0)
coords of point: [(1.0, 1.0)]

计算两个点的欧式距离:

# 1.4142135623730951
point_1.distance(point_2)

可以扩展为计算任意两个几何对象的最短距离

LineString

LineString 由多个点依次连接而成

from shapely.geometry import LineString, Point
line_1 = LineString([(0, 0), (0, 1), (1, 1)])
line_2 = LineString([Point(0, 0), Point(0, 1), Point(1, 1)])
print(line_1)
print(line_2)
# 长度面积
print('length =', line_1.length, 'area =', line_1.area)
# 边界框
print('bounds:', line_1.bounds)
# 坐标值-切片
print('coords:', line_1.coords[:])

打印结果如下:

LINESTRING (0 0, 0 1, 1 1)
LINESTRING (0 0, 0 1, 1 1)
length = 2.0 area = 0.0
bounds: (0.0, 0.0, 1.0, 1.0)
coords: [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)]

LinearRing

LinearRing 是一个封闭的 LineString, 起始点与终止点的坐标相同, 创建方法与 LineString 一致

from shapely.geometry import LinearRing, Point
ring_1 = LinearRing([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
# 起始点与终止点没有显式封闭
ring_2 = LinearRing([Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)])
print(ring_1)
print(ring_2)
# 长度面积
print('length =', ring_2.length, 'area =', ring_2.area)
# 边界框
print('bounds:', ring_2.bounds)
# 坐标值
print('coords:', ring_2.coords[:])

在创建 LinearRing 时, 如果坐标点没有封闭, shapely 自动连接第一个点和最后一个点

打印结果如下:

LINEARRING (0 0, 0 1, 1 1, 1 0, 0 0)
LINEARRING (0 0, 0 1, 1 1, 1 0, 0 0)
length = 4.0 area = 0.0
bounds: (0.0, 0.0, 1.0, 1.0)
coords: [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]

Polygon

Polygon 相当于 LinearRing 所围的内容, Polygon 的边界(外环和内环)均为 LinearRing 类型不带"洞"的多边形

from shapely.geometry import LinearRing, Point, Polygon
# 1. 坐标元组
polygon_1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
# 2. Point
# 起始点与终止点没有显式封闭
polygon_2 = Polygon([Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)])
# 3. LinearRing
polygon_3 = Polygon(LinearRing([(0, 0), (0, 1), (1, 1), (1, 0)]))
print("1.", polygon_1)
print("2.", polygon_2)
print("3.", polygon_3)
# 长度面积
print('length =', polygon_1.length, 'area =', polygon_1.area)
# 边界框
print('bounds:', polygon_1.bounds)
# 外环
print('exterior:', polygon_1.exterior)
# 外环的坐标值
print('coords of exterior:', polygon_1.exterior.coords[:])
# 所有内环
# 多边形没有"洞"时, 没有内环
print('interiors:', polygon_1.interiors[:])

打印结果如下:

1. POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
2. POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
3. POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
length = 4.0 area = 1.0
bounds: (0.0, 0.0, 1.0, 1.0)
exterior: LINEARRING (0 0, 0 1, 1 1, 1 0, 0 0)
coords of exterior: [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]
interiors: []

带"洞"的多边形

from shapely.geometry import Polygon
ex = [(0, 0), (0, 3), (3, 3), (3, 0), (0, 0)]
hole = [(1, 1), (1, 2), (2, 2), (2, 1)]
polygon = Polygon(ex, holes=[hole])
# 长度面积
print('length =', polygon.length, 'area =', polygon.area)
# 边界框
print('bounds:', polygon.bounds)
# 外环
print('exterior:', polygon.exterior)
# 外环的坐标值
print('coords of exterior:', polygon.exterior.coords[:])
# 所有内环
print('interiors:', list(polygon.interiors))
# 第一个内环
print('first interior:', polygon.interiors[0])
# 第一个内环的坐标
print('coords of first interior:', polygon.interiors[0].coords[:])

打印结果如下:

length = 16.0 area = 8.0
bounds: (0.0, 0.0, 3.0, 3.0)
exterior: LINEARRING (0 0, 0 3, 3 3, 3 0, 0 0)
coords of exterior: [(0.0, 0.0), (0.0, 3.0), (3.0, 3.0), (3.0, 0.0), (0.0, 0.0)]
interiors: [<shapely.geometry.polygon.LinearRing object at 0x7fdf46c4d7c0>]
first interior: LINEARRING (1 1, 1 2, 2 2, 2 1, 1 1)
coords of first interior: [(1.0, 1.0), (1.0, 2.0), (2.0, 2.0), (2.0, 1.0), (1.0, 1.0)]

集合类型

Point, LineString, Polygon 对应的集合类型分别为 MultiPoint, MultiLineString, MultiPolygon

在创建时, 既可以使用坐标元组作为参数, 也可以使用对应的基本几何对象作为参数

from shapely.geometry import (
    LineString,
    MultiLineString,
    MultiPoint,
    MultiPolygon,
    Point,
    Polygon
)
# 坐标元组参数
mul_point_1 = MultiPoint([(1, 1), (2, 2)])
# 几何对象参数
mul_point_2 = MultiPoint([Point(1, 1), Point(2, 2)])
# 混合参数
mul_point_3 = MultiPoint([(1, 1), Point(2, 2)])
line_1 = LineString([(0, 0), (0, 1)])
line_2 = LineString([Point(1, 0), Point(1, 1)])
multi_line_1 = MultiLineString([[(0, 0), (0, 1)], [(1, 0), (1, 1)]])
multi_line_2 = MultiLineString([line_1, line_2])
polygon_1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
polygon_2 = Polygon([Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1)])
# None 表示 holes=None
multi_polygon_1 = MultiPolygon([
    ([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)], None),
    ([(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)], None)
])
multi_polygon_2 = MultiPolygon([polygon_1, polygon_2])

不同类型的基本几何对象也可以组成一个集合对象

from shapely.geometry import GeometryCollection, LineString, Point, Polygon
line = LineString([Point(0, 1), Point(0, 2)])
polygon = Polygon([Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1)])
c = GeometryCollection([Point(0, 0), line, polygon])
print(c)
# 长度面积
print('length =', c.length, 'area =', c.area)
# 边界框
print('bounds:', c.bounds)

集合对象也具有长度, 面积, 边界框等属性

打印结果:

GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (0 1, 0 2), POLYGON ((1 1, 1 2, 2 2, 2 1, 1 1)))
length = 5.0 area = 1.0
bounds: (0.0, 0.0, 2.0, 2.0)

导出

常用的数据格式包括 wkt(well-know text) 和 wkb(well-know binary)

import shapely
from shapely import Point, wkb, wkt
p = Point(1, 1)
print("initial wkb:", wkb.dumps(p))
print("initial wkt:", wkt.dumps(p))

打印结果:

initial wkb: b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
initial wkt: POINT (1.0000000000000000 1.0000000000000000)

可以稍微优化一下输出格式:

# 十六进制表示
print("wkb with hex:", wkb.dumps(p, hex=True))
# 移除多余的零
print("wkt trim zero:", wkt.dumps(p, trim=True))

wkb with hex: 0101000000000000000000F03F000000000000F03F
wkt trim zero: POINT (1 1)

与此类似的 wkb.dump() 和 wkt.dump() 方法将数据导出到文件中

其他导出方法:

print("to_wkb:", shapely.to_wkb(p))  # 等价于 p.wkb
print("to_wkt:", shapely.to_wkt(p))  # 等价于 p.wkt

还可以输出 svg 字符串:

print(p._repr_svg_())

在 jupyter notebook 环境下输出几何对象时自动调用 x._repr_svg_() 方法, 输出 svg 图形

导入

import shapely
from shapely import wkb, wkt
wkb_i = '0101000000000000000000F03F000000000000F03F'
wkt_i = 'POINT (1 1)'
print(shapely.from_wkb(wkb_i))
print(shapely.from_wkt(wkt_i))
print(wkb.loads(wkb_i))
print(wkt.loads(wkt_i))

打印结果:

POINT (1 1)
POINT (1 1)
POINT (1 1)
POINT (1 1)

到此这篇关于Python shapely库的具体使用的文章就介绍到这了,更多相关Python shapely内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文