GIS算法:7_python拓扑套件shapely

可用于GIS数据处理与空间计算的Python包有GDAL和shapely。

相对来说,GDAL功能更全面,还可以用于数据转换、瓦片地图发布、栅格影像分析等,shapely只能进行基本的数据处理和空间计算。

但大多数情况下shapely就完全够用了。

GDAL的官网:https://gdal.org/

Shapely的中文文档:https://www.osgeo.cn/shapely/

 

本例只讲shapely的用法。

可以通过cmd安装shapely,打开cmd,输入python,回车,查看python的版本号。

https://img-blog.csdnimg.cn/20200308225713157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzQxMzEwODY4,size_16,color_FFFFFF,t_70

去下载shapely安装程序,https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely

根据python版本和电脑位数,选了Shapely-1.6.4.post2-cp37-cp37m-win32.whl下载。

安装的时候,打开cmd,运行语句:pip install D:\gismap\软件\Shapely-1.6.4.post2-cp37-cp37m-win32.whl,输入,回车。其中D:\gismap\软件\是放安装包的路径。

如果提示,

https://img-blog.csdnimg.cn/20200308225713159.png

提示要升级pip,写啥就敲啥。python -m pip install --upgrade pip

升级完了,接着运行上一个语句,把shapely安装了。

 

 

构建RTree:生成50×50个点,根据这些点生成缓冲1.0的圆,用这些圆构建RTree,在RTree中查点[1,1]落在哪些圆里。
 

from shapely.geometry import Point
from shapely.strtree import STRtree
polygons=[]
for i in range(0,50):
    for j in range(0,50):
        polygon=Point([i,j]).buffer(1.0)
        polygon.__setattr__('id',str(i)+str(j))
        polygons.append(polygon)
tree=STRtree(polygons)
result=tree.query(Point(1,1))
for r in result:
    print(r.__getattribute__('id'))

 

 

 

线裁切面:如图所示,用红色线去裁切蓝色面,结果会生成3个面。

 

 

from shapely import wkt
from shapely.ops import polygonize
polygon=wkt.loads('POLYGON ((220 350, 400 440, 635 249, 380 80, 174 164, 179 265, 220 350))')
polyline=wkt.loads('LINESTRING (570 400, 392 315, 299 215, 430 140, 530 240, 450 360, 460 480)')
boundary=polygon.boundary
polyline=polyline.union(boundary)
lineList = []
for i in range(0,len(polyline.geoms)):
    lineList.append(polyline.geoms[i])
clipPolygon=polygonize(lineList)
buffer=polygon.buffer(1)
for c in clipPolygon:
    if buffer.contains(c):
        print(c)

 

 

将裁切线换成LINESTRING (320 330, 500 280, 400 150, 290 200, 400 360),结果如下:

 

 

 

结果可以在JTS TestBuilder中查验。

在控制台input的文本框中输入wkt文本,点击load geometrys,就可以在面板中展示图形。

 

Shapely常用函数列表:

Geometric Objects

 

 

 

object.area

Returns the area (float) of the object.

 

 

 

object.bounds

Returns a (minx, miny, maxx, maxy) tuple (float values) that bounds the object.

 

 

 

object.length

Returns the length (float) of the object.

 

 

 

object.geom_type

Returns a string specifying the Geometry Type

类型

 

 

object.distance(other)

Returns the minimum distance (float) to the othergeometric object.

 

 

 

object.representative_point()

Returns a cheaply computed point that is guaranteed to be within the geometric object.

返回几何图形内的标志点,不是图心或质心,因为凹多边形的图心或质心可能在几何图形外

 

 

 

 

 

 

 

Unary Predicates

 

一元谓词关系判断</

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值