[1025]python地理处理包shapely

[1025]python地理处理包shapely文章目录构建集合图形以及获取集合图形点信息多边形显示多边形分割Polygon被MultiLineString切割merge多个多边形从Python形状多边形中提取点/坐标检查地理点是否在Python中的多边形内或外Python中用shapely做(1)生成二个多边形区域,计算想交的面积(2)生成一个点与一多边通过点缓冲来构建一个缓冲圆一般对象和方法PointsLineStringLineRingsPolygonsCollectionsCollectionsofPointsCollections

大家好,欢迎来到IT知识分享网。

github:https://github.com/Toblerity/Shapely

构建集合图形以及获取集合图形点信息

得到点集合,适合后面的处理。

from shapely.geometry import Polygon,Point

poly_1 = Polygon([(0, 0), (0, 2), (2, 2), (2, 0)])
 
# 获取多边形外边坐标信息,最后是闭合的
# 类似列表,不过里面是 tuple,按照 xy 顺序显示
poly_1.exterior.coords[:]
 
# 输出结果如下:
# [(0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)]
 
pts = poly_1.exterior.coords
type(pts)
# 输出结果
# shapely.coords.CoordinateSequence

多边形显示

最后一句保证显示按照比例

from matplotlib import pyplot
from shapely.geometry import Point
from descartes import PolygonPatch
import numpy as np
 
fig = pyplot.figure(1, dpi=90)
ax = fig.add_subplot(121)
 
patch1 = PolygonPatch(poly_1, alpha=0.5, zorder=1)
ax.add_patch(patch1)
 
patch2 = PolygonPatch(poly_2, alpha=0.5, zorder=1)
ax.add_patch(patch2)
 
patchc = PolygonPatch(poly_1.intersection(poly_2) , alpha=0.5, zorder=2)
ax.add_patch(patchc)
 
pyplot.xlim((-1, 4))
pyplot.ylim((-1, 3))
 
ax.set_aspect('equal', adjustable='box') 

[1025]python地理处理包shapely

from matplotlib import pyplot
from shapely.geometry import Point
from descartes import PolygonPatch
import numpy as np
 
fig = pyplot.figure(1, dpi=90)
ax = fig.add_subplot(121)
 
ax.add_patch(PolygonPatch(poly_1.buffer(0.5), alpha=0.5, zorder=1))
 
patch1 = PolygonPatch(poly_1, alpha=0.5, zorder=1)
ax.add_patch(patch1)
 
pyplot.xlim((-1, 3))
pyplot.ylim((-1, 3))
 
ax.set_aspect('equal', adjustable='box')

[1025]python地理处理包shapely

from matplotlib import pyplot
from shapely.geometry import Point
from descartes import PolygonPatch
import numpy as np
 
fig = pyplot.figure(1, dpi=90)
 
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
 
# 1
ax = fig.add_subplot(121)
 
patch1 = PolygonPatch(a, alpha=0.5, zorder=1)
ax.add_patch(patch1)
patch2 = PolygonPatch(b, alpha=0.5, zorder=1)
ax.add_patch(patch2)
c = a.union(b)
patchc = PolygonPatch(c, alpha=0.5, zorder=2)
ax.add_patch(patchc)
 
#pyplot.xlim((-1, 4))
#pyplot.ylim((-1, 3))
 
pyplot.xticks(np.arange(-1, 5, 1))
pyplot.yticks(np.arange(-1, 4, 1))
 
ax.set_aspect('equal', adjustable='box')

[1025]python地理处理包shapely

多边形分割

可以将多边形通过折线来分割

参考:Cut a polygon with two lines in Shapely
参考:shapely官方文档——Splitting

切割后得到一个多边形集合,通过遍历可以获取每一个 geometry 的具体信息。

from shapely.geometry import Polygon, Point, LineString
poly_1 = Polygon([(0,0), (0,2), (2,2), (2,0)])
line_1 = LineString([(-1,0.5), (-1,1.5), (3,1.5), (3,0.5), (-1,0.5)])
 
import shapely
# 通过线可以将多边形进行切割
result = shapely.ops.split(poly_1, line_1)
result.wkt
 
# output:
# 'GEOMETRYCOLLECTION (POLYGON ((0 0, 0 0.5, 2 0.5, 2 0, 0 0)), POLYGON ((0 0.5, 0 1.5, 2 1.5, 2 0.5, 0 0.5)), POLYGON ((0 1.5, 0 2, 2 2, 2 1.5, 0 1.5)))'
 
for g in result:
    print(g.exterior.coords[:])
 
# output:
# [(0.0, 0.0), (0.0, 0.5), (2.0, 0.5), (2.0, 0.0), (0.0, 0.0)]
# [(0.0, 0.5), (0.0, 1.5), (2.0, 1.5), (2.0, 0.5), (0.0, 0.5)]
# [(0.0, 1.5), (0.0, 2.0), (2.0, 2.0), (2.0, 1.5), (0.0, 1.5)]

Polygon 被 MultiLineString 切割

典型的应用场景就是路网切割,路网是一段段的折线(LineString)组成的,因此需要切割指定的多边形,从而生成多个多边形,不能通过上面的方法实现。

参考:Divide a polygon into multiple small polygons using a MultiLineString

具体实现的思路是通过密集的 LineString 集合然后分裂转成 Polygon 的思路

from shapely.geometry import Polygon, LineString
from shapely.ops import linemerge, unary_union, polygonize
 
poly = Polygon([(0,0), (4,0), (4,4), (0,4)])
lines = [LineString([(0,1), (1,1), (2,1), (5,1)]), LineString([(1,1), (2,1), (3,3), (5,3)])]
 
lines.append(poly.boundary)
lines = unary_union(lines)
lines = linemerge(lines)
polygons = polygonize(lines)

路网数据  
[1025]python地理处理包shapely

切割后的数据
[1025]python地理处理包shapely

根据路网,切割望京区域(选取边界区域的线路,然后按照上面的操作获取多个区域,选取面积最大的)  
[1025]python地理处理包shapely

[1025]python地理处理包shapely

然后根据包含关系,获取望京内部的AOI信息,需要做个buffer,不然有些边界的区域会出错

[1025]python地理处理包shapely

merge 多个多边形

使用下面的方法

  • 显示构建 list
  • 然后再用 cascaded_union
def get_merge_poly(poly_dict):
    """
    获取给定的所有 polys 的合并结果,避免面积重复计算
    输入:字典,名称+数组型poly
    输出:shapely poly
    """
    from shapely.ops import cascaded_union
    # 先获取 shapely 格式 list
    polys = []
    for hull in poly_dict.values():
        poly = Polygon([(pt[1], pt[0]) for pt in hull])
        polys.append(poly)
         
    return cascaded_union(polys)

Python形状多边形中提取点/坐标

形状多边形的示例

from shapely.geometry import Polygon

# Create polygon from lists of points
x = [list of x vals]
y = [list of y vals]

some_poly= Polygon(x,y)

方法:

------------------------------------方法1:-------------------------------------------
x, y = some_poly.exterior.coords.xy

------------------------------------方法2:-------------------------------------------
>>> from shapely.geometry import Polygon, mapping
>>> sh_polygon = Polygon(((0,0), (1,1), (0,1)))
>>> mapping(sh_polygon)
{'type': 'Polygon', 'coordinates': (((0.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)),)}

------------------------------------方法3:-------------------------------------------
import numpy as np
x = [1, 2, 3, 4]
y = [9, 8, 7, 6]
polygon = Polygon(x,y)
points = np.array(polygon)

# points is:
[[ 1 9]
 [ 2 8]
 [ 3 7]
 [ 4 6]]

------------------------------------方法4:-------------------------------------------
p = Polygon([(0,0),(1,1),(1,0),(0,0)])
list(zip(*p.exterior.coords.xy))
或者
for x,y in p.exterior.coords:
   print(x,y)

检查地理点是否在Python中的多边形内或外

  • 地理坐标必须正确存储。实施例np.array([[Lon_A, Lat_A], [Lon_B, Lat_B], [Lon_C, Lat_C]])
  • 创建多边形
  • 创建待测试
  • 使用polygon.contains(point)要测试的点,如果点是内部(True)或外(False)的多边形。

这里是代码的缺失部分:

from shapely.geometry import Point 
from shapely.geometry.polygon import Polygon 

lons_lats_vect = np.column_stack((lons_vect, lats_vect)) # Reshape coordinates 
polygon = Polygon(lons_lats_vect) # create polygon 
point = Point(y,x) # create point 
print(polygon.contains(point)) # check if polygon contains point 

注:多边形不考虑大周期明显,因此,有必要的边缘分割成许多段从而增加顶点的数量。

Python中用shapely做(1)生成二个多边形区域,计算想交的面积(2)生成一个点与一多边

from shapely.geometry import Polygon,Point

# 生成两个多边形区域
ploy_1 = Polygon([(1,2),(1,3),(2,4),(6,3)])
ploy_2 = Polygon([(2,1),(3,3),(4,4),(5,1)])

# 相交区域面积
in_s = ploy_1.intersection(ploy_2).area

# 生成一个点
p_1 = Point(0,0)

# 点和多边形ploy_1的最短距离
s = ploy_1.distance(p_1)

代码块如上,执行如下
[1025]python地理处理包shapely

通过点缓冲来构建一个缓冲圆

>>> from shapely.geometry import Point
>>> patch = Point(0.0, 0.0).buffer(10.0)
>>> patch
<shapely.geometry.polygon.Polygon object at 0x...>
>>> patch.area
313.65484905459385

一般对象和方法

  • object.area
    返回对象面积(float)

  • object.bounds
    返回对象边界(minx, miny, maxx, maxy) 的元组(float)

  • object.length
    返回对象长度(float)

  • object.minimum_clearance
    返回可以移动节点以生成无效几何体的最小距离。

这可以被认为是一个几何体鲁棒性的度量,其中最小间隙值越大,表示几何体越坚固。如果几何图形(例如点)不存在最小间隙,则将返回 math.infinity。该方法需要GEOS 3.6 以上版本。

>>> from shapely.geometry import Polygon
>>> Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]).minimum_clearance
1.0
  • object.geom_type
    返回对象的几何类型
>>> Point(0, 0).geom_type
'Point'
  • object.distance(other)
    返回到另一个几何对象的最小距离
>>> Point(0,0).distance(Point(1,1))
1.4142135623730951
  • object.hausdorff_distance(other) (1.6.0版本以上)
    返回到另一个几何对象的Hausdorff距离(float)。两个几何体之间的Hausdorff距离是任意一个几何体上的一个点与另一个几何体上的最近点之间的最远距离
>>> point = Point(1, 1)
>>> line = LineString([(2, 0), (2, 4), (3, 4)])
>>> point.hausdorff_distance(line)
3.605551275463989
>>> point.distance(Point(3, 4))
3.605551275463989
  • object.representative_point()
    返回一个可以保证在几何对象内的廉价计算点
    注意:这通常与质心不同。
>>> donut = Point(0, 0).buffer(2.0).difference(Point(0, 0).buffer(1.0))
>>> donut.centroid.wkt
'POINT (-0.0000000000000001 -0.0000000000000000)'
>>> donut.representative_point().wkt
'POINT (-1.5000000000000000 0.0000000000000000)'

Points

class Point(coordinates)

点构造函数采用位置坐标值或点元组参数。

>>> from shapely.geometry import Point
>>> point = Point(0.0, 0.0)
>>> q = Point((0.0, 0.0))

1、点的面积和长度属性为0

>>> point.area
0.0
>>> point.length
0.0

2、点的边界是均为0的元组

>>> point.bounds
(0.0, 0.0, 0.0, 0.0)

3、点的坐标值可以使用coords, x, y, 和z 获取

>>> list(point.coords)
[(0.0, 0.0)]
>>> point.x
0.0
>>> point.y
0.0

4、坐标可以被切片(1.2.14版本新特性)

>>> point.coords[:]
[(0.0, 0.0)]

5、Point构造函数还接受另一个Point实例,从而生成一个副本

>>> Point(point)
<shapely.geometry.point.Point object at 0x...>

LineString

class LineString(coordinates)

LineString构造函数采用2个或更多(x,y[,z])点元组的有序序列

构造的LineString对象表示点之间的一个或多个连接的线性样条曲线。允许按顺序重复点,但可能会导致性能损失,应避免。线串可以交叉(即复杂而不简单)

[1025]python地理处理包shapely

图1 左边是一个简单的LineString,右边是一个复杂的LineString。每个点的(MultiPoint)边界显示为黑色,描述这些线的其他点显示为灰色

1、LineString的面积为0,长度不为0

>>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1)])
>>> line.area
0.0
>>> line.length
1.4142135623730951

2、LineString的边界是 (minx, miny, maxx, maxy) 的元组

>>>> line.bounds
(0.0, 0.0, 1.0, 1.0)

3、LineString的坐标值可以使用coords获取

>>>> len(line.coords)
2
>>> list(line.coords)
[(0.0, 0.0), (1.0, 1.0)]

4、坐标可以被切片(1.2.14版本新特性)

>>>> point.coords[:]
[(0.0, 0.0), (1.0, 1.0)]
>>> point.coords[1:]
[(1.0, 1.0)]

5、LineString构造函数还接受另一个LineString实例,从而生成一个副本

>>>> LineString(line)
<shapely.geometry.linestring.LineString object at 0x...>

6、也可以使用一系列混合点实例或坐标元组来构造LineString。各个坐标将复制到新对象中

>>> LineString([Point(0.0, 1.0), (2.0, 3.0), Point(4.0, 5.0)])
<shapely.geometry.linestring.LineString object at 0x...>

LineRings

class LinearRing(coordinates)

LinearRing构造函数采用(x,y[,z])点元组的有序序列

通过在第一个和最后一个索引中传递相同的值,可以显式地闭合序列。否则,将通过将第一个元组复制到最后一个索引来隐式闭合序列。与LineString一样,允许有序序列中的重复点,但可能会导致性能损失,因此应该避免。线迹不能交叉,也不能单点接触

[1025]python地理处理包shapely

图2 左边是有效的LinearRing,右边是无效的自相交LinearRing。描述环的点以灰色显示。环的边界是空的

注意:Shapely不会阻止这种环的产生,但在对其进行操作时会引发异常。

1、LinearRing的面积为0,长度不为0

>>>> from shapely.geometry.polygon import LinearRing
>>> ring = LinearRing([(0, 0), (1, 1), (1, 0)])
>>> ring.area
0.0
>>> ring.length
3.4142135623730949

2、LinearRing的边界是 (minx, miny, maxx, maxy) 的元组

>>>> ring.bounds
(0.0, 0.0, 1.0, 1.0)

3、LinearRing的坐标值可以使用coords获取

>>>> len(ring.coords)
4
>>> list(ring.coords)
[(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]

4、坐标可以被切片(1.2.14版本新特性)

>>>> point.coords[:]
[(0.0, 0.0), (1.0, 1.0)]
>>> point.coords[1:]
[(1.0, 1.0)]

5、LinearRing构造函数还接受另一个LinearRing实例,从而生成一个副本

>>>> LinearRing(ring)
<shapely.geometry.polygon.LinearRing object at 0x...>

Polygons

class Polygon(shell[, holes=None])

Polygon构造函数接受两个位置参数。第一个是(x,y[,z])点元组的有序序列,其处理方式与LinearRing完全相同。第二个是一个可选的无序的环状序列,指定了特征的内部边界或“洞”

有效多边形的环不能相互交叉,且只能接触一个点。同样,Shapely不会阻止无效特性的创建,但是在操作它们时会引发异常

[1025]python地理处理包shapely

[1025]python地理处理包shapely

a)有效,其中一个内环在一个点上与外环接触;
b)无效,因为它的内环在多个点上与外环接触;
c)无效,因为它的外环和内环沿一条线接触;
d)无效,因为它的内环沿着一条线接触

1、Polygon的面积和长度不为0

>>>> from shapely.geometry import Polygon
>>> polygon = Polygon([(0, 0), (1, 1), (1, 0)])
>>> polygon.area
0.5
>>> polygon.length
3.4142135623730949

2、Polygon的边界是(minx, miny, maxx, maxy)数组

>>>> from shapely.geometry import Polygon
>>> polygon = Polygon([(0, 0), (1, 1), (1, 0)])
>>> polygon.area
0.5
>>> polygon.length
3.4142135623730949

3、Polygon的构成环通过exterior 和 interiors 属性获取

>>>> list(polygon.exterior.coords)
[(0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]
>>> list(polygon.interiors)
[]

4、Polygon的构造接受LineString 和LinearRing实例

>>> coords = [(0, 0), (1, 1), (1, 0)]
>>> r = LinearRing(coords)
>>> s = Polygon(r)
>>> s.area
0.5
>>> t = Polygon(s.buffer(1.0).exterior, [r])
>>> t.area
6.5507620529190334

5、矩形的构造可以使用shapely.geometry.box()函数(1.2.9新版本特性)

shapely.geometry.box(minx, miny, maxx, maxy, ccw=True) 根据提供的边界框值生成矩形,默认情况下按逆时针顺序

>>>> from shapely.geometry import box >>> b = box(0.0, 0.0, 1.0, 1.0) >>> b <shapely.geometry.polygon.Polygon object at 0x...>
>>> list(b.exterior.coords)
[(1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0), (1.0, 0.0)]

6、要获得具有已知方向的多边形,使用shapely.geometry.polygon.orient()函数(1.2.10新版本特性)

shapely.geometry.polygon.orient(polygon,sign=1.0)

返回给定多边形的正确定向副本。返回结果有符号区域将具有给定的符号。符号1.0表示其外环的坐标方向将为逆时针方向

Collections

几何对象的异构集合可能是由一些Shapely操作造成的。例如,两个LineStrings 可以沿一条线在一个点相交。为了表示这些结果,Shapely提供了类似冻结集的、不可变的几何对象集合。集合可以是同质(MultiPoint 等)或异构的

>>> a = LineString([(0, 0), (1, 1), (1,2), (2,2)])
>>> b = LineString([(0, 0), (1, 1), (2,1), (2,2)])
>>> x = a.intersection(b)
>>> x
<shapely.geometry.collection.GeometryCollection object at 0x...>
>>> from pprint import pprint
>>> pprint(list(x))
[<shapely.geometry.point.Point object at 0x...>,
 <shapely.geometry.linestring.LineString object at 0x...>]

[1025]python地理处理包shapely

a)一条绿色和黄色的线,沿着一条线以及一个点相交;
b)交点(蓝色)是一个包含一个LineString 和一个Point的集合

1、GeometryCollection的成员可以通过geoms属性或迭代器协议in或list()进行访问

>>> pprint(list(x.geoms))
[<shapely.geometry.point.Point object at 0x...>,
 <shapely.geometry.linestring.LineString object at 0x...>]
>>> pprint(list(x))
[<shapely.geometry.point.Point object at 0x...>,
 <shapely.geometry.linestring.LineString object at 0x...>]

2、集合也可以切片(1.2.14新版本特性)

>>> from shapely.geometry import MultiPoint
>>> m = MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])
>>> m[:1].wkt
'MULTIPOINT (0.0000000000000000 0.0000000000000000)'
>>> m[3:].wkt
'MULTIPOINT (2.0000000000000000 2.0000000000000000)'
>>> m[4:].wkt
'GEOMETRYCOLLECTION EMPTY'

注意:如果可能的话,最好使用下面描述的同类集合类型之一。

Collections of Points

class MultiPoint(points)

MultiPoint 构造函数接受一系列(x,y[,z])点元组

1、MultiPoint 面积和长度为0

>>>> from shapely.geometry import MultiPoint
>>> points = MultiPoint([(0.0, 0.0), (1.0, 1.0)])
>>> points.area
0.0
>>> points.length
0.0

2、其 x-y 边界范围是(minx, miny, maxx, maxy) 元组

>>>> points.bounds
(0.0, 0.0, 1.0, 1.0)

3、GeometryCollection的成员可以通过geoms属性或迭代器协议in或list()进行访问

>>> import pprint
>>> pprint.pprint(list(points.geoms))
[<shapely.geometry.point.Point object at 0x...>,
 <shapely.geometry.point.Point object at 0x...>]
>>> pprint.pprint(list(points))
[<shapely.geometry.point.Point object at 0x...>,
 <shapely.geometry.point.Point object at 0x...>]

4、构造函数还接受另一个MultiPoint 实例或无序的点实例序列,从而生成副本

>>>> MultiPoint([Point(0, 0), Point(1, 1)]) 
<shapely.geometry.multipoint.MultiPoint object at 0x...>

Collections of Points

class MultiLineString(lines)
MultiLineString构造函数接受一系列类似于直线的序列或对象

[1025]python地理处理包shapely

a)不连接的MultiLineString;
b)复杂的MultiLineString

1、MultiLineString 的面积和长度为0

>>> from shapely.geometry import MultiLineString
>>> coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
>>> lines = MultiLineString(coords)
>>> lines.area
0.0
>>> lines.length
3.4142135623730949

2、其 x-y 边界范围是(minx, miny, maxx, maxy) 元组

>>>> lines.bounds
(-1.0, 0.0, 1.0, 1.0)

3、它的成员是LineString的实例,可以通过geoms属性或迭代器协议in或list()进行访问

>>> len(lines.geoms)
2
>>> pprint.pprint(list(lines.geoms))
[<shapely.geometry.linestring.LineString object at 0x...>,
 <shapely.geometry.linestring.LineString object at 0x...>]
>>> pprint.pprint(list(lines))
[<shapely.geometry.linestring.LineString object at 0x...>,
 <shapely.geometry.linestring.LineString object at 0x...>]

4、构造函数还接受多行字符串的另一个实例或无序的LineString实例序列,从而生成副本

>>>> MultiLineString(lines) <shapely.geometry.multilinestring.MultiLineString object at 0x...>
>>> MultiLineString(lines.geoms) <shapely.geometry.multilinestring.MultiLineString object at 0x...>

Collections of Polygons

class MultiPolygon(polygons)
MultiPolygon构造函数采用一系列外部环和孔列表元组:[((a1,…,aM),[(b1,…,bN),…]),…]

[1025]python地理处理包shapely

a)有效的有两个成员的MultiPolygon;
b)无效MultiPolygon,因为它的成员接触到无限多个点(沿着一条线)

1、其 x-y 边界范围是(minx, miny, maxx, maxy) 元组

>>>> polygons.bounds
(-1.0, -1.0, 2.0, 2.0)

2、它的成员是Polygon的实例,可以通过geoms属性或迭代器协议in或list()进行访问

>>>> len(polygons.geoms) 3
>>> len(polygons) 3

Empty features

1、它是一个点集与空集重合;不是没有,而是类似于集([])。可以通过不带参数地调用各种构造函数来创建空要素。空要素几乎不支持任何操作。

>>> line = LineString()
>>> line.is_empty
True
>>> line.length
0.0
>>> line.bounds
()
>>> line.coords
[]

2、可以设置空要素的坐标,之后几何图形不再为空

>>> line.coords = [(0, 0), (1, 1)]
>>> line.is_empty
False
>>> line.length
1.4142135623730951
>>> line.bounds
(0.0, 0.0, 1.0, 1.0)

Coordinate sequences

1、描述geometry的坐标序列表示为CoordinateSequence对象,这些序列不应该直接序列化,但可以从现有的geometry的Geometry.coords属性访问

>>>> line = LineString([(0, 1), (2, 3), (4, 5)]) 
>>> line.coords 
<shapely.coords.CoordinateSequence object at 0x00000276EED1C7F0>

2、坐标序列可以被索引、切片和迭代,就像它们是一个坐标元组的列表一样。

>>> line.coords[0]
(0.0, 1.0)
>>> line.coords[1:]
[(2.0, 3.0), (4.0, 5.0)]
>>> for x, y in line.coords:
...     print("x={}, y={}".format(x, y))
...
x=0.0, y=1.0
x=2.0, y=3.0
x=4.0, y=5.0

3、多边形的外部和每个内环都有一个坐标序列

>>>> poly = Polygon([(0, 0), (0, 1), (1, 1), (0, 0)]) >>> poly.exterior.coords <shapely.coords.CoordinateSequence object at 0x00000276EED1C048>

4、多部分几何图形没有坐标序列。其坐标序列存储在组成的几何图形上

>>> p = MultiPoint([(0, 0), (1, 1), (2, 2)])
>>> p[2].coords
<shapely.coords.CoordinateSequence object at 0x00000276EFB9B320>

线性参考方法

使用一维参照系统指定沿线性要素(例如LineStrings 和MultiLineStrings )的位置非常有用。Shapely支持基于长度或距离的线性参考,计算沿几何对象到给定点的投影的距离,或沿对象给定距离的点的距离(需GEOS3.2.0及以上版本支持)

  • object.interpolate(distance[, normalized=False])
    返回沿线性几何对象指定距离的点
    如果normalized 参数为True,距离将被解释为几何对象长度的比例部分
>>> ip = LineString([(0, 0), (0, 1), (1, 1)]).interpolate(1.5)
>>> ip
<shapely.geometry.point.Point object at 0x740570>
>>> ip.wkt
'POINT (0.5000000000000000 1.0000000000000000)'
>>> LineString([(0, 0), (0, 1), (1, 1)]).interpolate(0.75, normalized=True).wkt
'POINT (0.5000000000000000 1.0000000000000000)'
  • object.project(other[, normalized=False])

返回沿此几何对象到另一个对象最近的点的距离。

如果normalized参数为True,则返回对象长度的标准化距离,project()方法是interpolate()的逆方法。

 >>> LineString([(0, 0), (0, 1), (1, 1)]).project(ip)
1.5
>>> LineString([(0, 0), (0, 1), (1, 1)]).project(ip, normalized=True)
0.75

例如,可以使用线性参照方法在指定距离处剪切直线

def cut(line, distance):
    # Cuts a line in two at a distance from its starting point
    if distance <= 0.0 or distance >= line.length:
        return [LineString(line)]
    coords = list(line.coords)
    for i, p in enumerate(coords):
        pd = line.project(Point(p))
        if pd == distance:
            return [
                LineString(coords[:i+1]),
                LineString(coords[i:])]
        if pd > distance:
            cp = line.interpolate(distance)
            return [
                LineString(coords[:i] + [(cp.x, cp.y)]),
                LineString([(cp.x, cp.y)] + coords[i:])]
>>> line = LineString([(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)])
>>> pprint([list(x.coords) for x in cut(line, 1.0)])
[[(0.0, 0.0), (1.0, 0.0)],
 [(1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0), (5.0, 0.0)]]
>>> pprint([list(x.coords) for x in cut(line, 2.5)])
[[(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (2.5, 0.0)],
 [(2.5, 0.0), (3.0, 0.0), (4.0, 0.0), (5.0, 0.0)]]

谓词和关系

几何对象中解释的类型的对象提供标准谓词作为属性(对于一元谓词)和方法(对于二元谓词)。无论是一元还是二元谓词,都返回True或False

object.interpolate

一元谓词

标准的一元谓词被实现为只读属性属性

  • object.has_z
    如果特征不仅具有的x和y坐标,还具有三维(或所谓的2.5D)几何图形的z坐标,则返回True
>>>> Point(0, 0).has_z
False
>>> Point(0, 0, 0).has_z
True
  • object.is_ccw (1.2.10版本)

如果坐标按逆时针顺序(以正向符号包围区域)返回True。此方法仅适用于线性化对象

>>>> LinearRing([(1,0), (1,1), (0,0)]).is_ccw
True

环可以通过以下方式实现反转:

>>>> ring = LinearRing([(0,0), (1,1), (1,0)]) 
>>> ring.is_ccw
False 
>>> ring.coords = list(ring.coords)[::-1] 
>>> ring.is_ccw
True
  • object.is_empty

如果要素的内部和边界(在点集术语中)与空集重合,则返回True

>>>> Point().is_empty
True 
>>> Point(0, 0).is_empty
False

注意:在操作符模块的attrgetter()函数的帮助下,诸如is_empty之类的一元谓词可以很容易地用作内置filter()或itertools.ifilter()

>>> from operator import attrgetter
>>> empties = filter(attrgetter('is_empty'), [Point(), Point(0, 0)])
>>> len(empties)
1
  • object.is_ring

如果要素是闭合的简单LineString,则返回True。一个封闭要素的边界与空集合重合。

>>> LineString([(0, 0), (1, 1), (1, -1)]).is_ring
False
>>> LinearRing([(0, 0), (1, 1), (1, -1)]).is_ring
True

此属性适用于LineString和LinearRing实例,但对其他实例没有意义

  • object.is_simple

如果要素不自相交,则返回True (只对LineStrings 和LinearRings有意义)

>>> LineString([(0, 0), (1, 1), (1, -1), (0, 1)]).is_simple
False
  • object.is_valid

如果要素是“有效的”,则返回True

一个有效的LinearRing 不能在一个点上与自己交叉或相接。一个有效的Polygon 不能有任何重叠的外环或内环。有效的MultiPolygon 不能集合任何重叠的多边形。对无效要素的操作可能会失败

>>> MultiPolygon([Point(0, 0).buffer(2.0), Point(1, 1).buffer(2.0)]).is_valid
False

上面的两个点非常接近,缓冲区操作产生的多边形(在下一节中解释)会重叠

注意:is_valid谓词可用于编写验证装饰器,该装饰器可确保从构造函数函数只返回有效对象

from functools import wraps
def validate(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        ob = func(*args, **kwargs)
        if not ob.is_valid:
            raise TopologicalError(
                "Given arguments do not determine a valid geometric object")
        return ob
    return wrapper
>>> @validate
... def ring(coordinates):
...     return LinearRing(coordinates)
...
>>> coords = [(0, 0), (1, 1), (1, -1), (0, 1)]
>>> ring(coords)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in wrapper
shapely.geos.TopologicalError: Given arguments do not determine a valid geometric object

参考:https://www.zhihu.com/question/382930026/answer/1211129777
https://www.cnblogs.com/alex-bn-lee/p/14605549.html
https://www.itdaan.com/tw/8b69be4d33e217e44647471905b7bc2f
http://cn.voidcc.com/question/p-qcdwozoo-boh.html
https://zhuanlan.zhihu.com/p/284625245
https://www.cnblogs.com/giserliu/p/4993697.html

其它:
https://blog.csdn.net/u012433049/article/details/82909484

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11996.html

(0)
上一篇 2024-03-17 21:26
下一篇 2024-03-18 17:15

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信