GIS算法:7_python拓扑套件shapely

标签: GIS算法

可用于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

 

一元谓词关系判断

 

 

object.has_z

Returns True if the feature has not only x and y, but also z coordinates for 3D (or so-called, 2.5D) geometries.

是否有高程

 

 

object.is_ccw

Returns True if coordinates are in counter-clockwise order (bounding a region with positive signed area). This method applies to LinearRingobjects only

坐标序列是否是逆时针

 

 

object.is_empty

Returns True if the feature’s interior and boundary(in point set terms) coincide with the empty set.

空集判断

 

 

object.is_ring

Returns True if the feature is closed. A closed feature’s boundary coincides with the empty set.

闭环判断

 

 

object.is_simple

Returns True if the feature does not cross itself.

自相交判断

 

 

object.is_valid

Returns True if a feature is “valid”

合法性判断

 

 

 

 

 

 

 

Binary Predicates

 

二元谓词

 

 

object.almost_equals(other[, decimal=6])

Returns True if the object is approximately equal to the other at all points to specified decimal place precision.

相似性判断

 

 
   

object.contains(other)

Returns True if the object’s interior contains the boundary and interior of the other object and their boundaries do not touch at all.

包含

   

object.crosses(other)

Returns True if the interior of the object intersects the interior of the other but does not contain it, and the dimension of the intersection is less than the dimension of the one or the other.

内部交叉

   

object.disjoint(other)

Returns True if the boundary and interior of the object do not intersect at all with those of the other.

分离

   

object.equals(other)

Returns True if the set-theoretic boundary, interior, and exterior of the object coincide with those of the other.

相等

   

object.intersects(other)

Returns True if the boundary and interior of the object intersect in any way with those of the other.

相交

   

object.touches(other)

Returns True if the objects have at least one point in common and their interiors do not intersect with any part of the other.

接触

   

object.within(other)

Returns True if the object’s boundary and interiorintersect only with the interior of the other (not its boundary or exterior).

在内部

   

 

 

 

   

DE-9IM Relationships

 

九维关系谓词模型

   

object.relate(other)

Returns a string representation of the DE-9IM matrix of relationships between an object’s interior, boundary, exterior and those of another geometric object.

关系

   

 

 

 

   

Set-theoretic Methods

 

集论方法

   

object.boundary

Returns a lower dimensional object representing the object’s set-theoretic boundary.

边界

   

object.centroid

Returns a representation of the object’s geometric centroid (point).

图心

   

object.difference(other)

Returns a representation of the points making up this geometric object that do not make up the other object.

空间取反

   

object.intersection(other)

Returns a representation of the intersection of this object with the other geometric object.

取交集

   

object.symmetric_difference(other)

Returns a representation of the points in this object not in the other geometric object, and the points in the other not in this geometric object.

并集-交集

   

object.union(other)

Returns a representation of the union of points from this object and the other geometric object.

联合

   

 

 

 

   

Constructive Methods

 

建设方法

   

object.buffer

Returns an approximate representation of all points within a given distance of the this geometric object.

缓冲区

   

(distance, resolution=16,

   

cap_style=1, join_style=1,

   

mitre_limit=1.0)

   

object.convex_hull

Returns a representation of the smallest convex Polygon containing all the points in the object unless the number of points in the object is less than three.

凸包

   

object.envelope

Returns a representation of the point or smallest rectangular polygon (with sides parallel to the coordinate axes) that contains the object.

最小外包矩形

   

object.parallel_offset

Returns a LineString or MultiLineString geometry at a distance from the object on its right or its left side.

平移

   

(distancesideresolution=16,

   

join_style=1mitre_limit=1.0)

   

object.simplify(tolerance,

Returns a simplified representation of the geometric object.

简化

   

preserve_topology=True)

   

 

 

 

   

Affine Transformations

 

仿射变化

   

shapely.affinity.affine_transform

Returns a transformed geometry using an affine transformation matrix.

 

   

(geommatrix)

   

shapely.affinity.rotate

Returns a rotated geometry on a 2D plane.

 

   

(geomangleorigin='center',

   

use_radians=False)

   

shapely.affinity.scale

Returns a scaled geometry, scaled by factors along each dimension.

 

   

(geomxfact=1.0yfact=1.0,

   

zfact=1.0origin='center')

   

shapely.affinity.skew

Returns a skewed geometry, sheared by angles along x and y dimensions.

 

   

(geomxs=0.0ys=0.0,

   

origin='center'use_radians=False)

   

shapely.affinity.translate

Returns a translated geometry shifted by offsets along each dimension.

 

   

(geomxoff=0.0yoff=0.0zoff=0.0)

   

shapely.ops.transform(funcgeom)

Applies func to all coordinates of geom and returns a new geometry of the same type from the transformed coordinates.

 

   

 

 

 

   

Merging Linear Features

 

线性合并

   

shapely.ops.polygonize(lines)

Returns an iterator over polygons constructed from the input lines.

构面

   

shapely.ops.polygonize_full(lines)

Creates polygons from a source of lines, returning the polygons and leftover geometries.

 

   

shapely.ops.linemerge(lines)

Returns a LineString or MultiLineStringrepresenting the merger of all contiguous elements of lines.

 

   

 

 

 

   

Cascading Unions

 

级联合并

   

shapely.ops.cascaded_union(geoms)

Returns a representation of the union of the given geometric objects.

 

   

shapely.ops.unary_union(geoms)

Returns a representation of the union of the given geometric objects.

 

   

 

 

 

   

Prepared Geometry Operations

 

几何预操作

   

prepared.prep(ob)

Creates and returns a prepared geometric object.

 

   

 

 

 

   

Diagnostics

 

诊断

   

validation.explain_validity(ob)

Returns a string explaining the validity or invalidity of the object.

 

   

 

 

 

   

Interoperation--Well-Known Formats

 

wkt框架互操作

   

object.wkb.encode('hex')

A Well Known Text (WKT) or Well Known Binary(WKB) representation of any geometric object can be had via its wkt or wkb attribute.

wkb转wkt

   

object.wkt

 

 

   

shapely.wkb.dumps(ob)

Returns a WKB representation of ob.

 

   

shapely.wkb.loads(wkb)

Returns a geometric object from a WKB representation wkb.

 

   

shapely.wkt.dumps(ob)

Returns a WKT representation of ob.

 

   

shapely.wkt.loads(wkt)

Returns a geometric object from a WKT representation wkt.

 

   

 

 

 

   

Interoperation-- Numpy and Python Arrays

 

numpy和arrays互操作

   

shapely.geometry.asShape()

family of functions can be used to wrap Numpy coordinate arrays so that they can then be analyzed using Shapely while maintaining their original storage

 

   

 

 

 

   
   

Interoperation-- Python Geo Interface

 

geo接口互操作

   

shapely.geometry.asShape(context)

Adapts the context to a geometry interface. The coordinates remain stored in the context.

 

   

shapely.geometry.shape(context)

Returns a new, independent geometry with coordinates copied from the context.

 

   

shapely.geometry.mapping(ob)

Returns a new, independent geometry with coordinates copied from the context.

 

   

 

 

版权声明:本文为sinat_41310868原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_41310868/article/details/106699553

智能推荐

Python shapely 线缓冲区分析与显示

Python shapely 线缓冲区分析与显示 1.引入shapely 2.线的缓冲区 3.显示: 4.显示结果...

CV笔记03:自监督GAN(ss-gan)

无需标注数据,利用辅助性旋转损失的自监督GANs,-- 对抗+自监督的无监督方式 《通过辅助旋转损失进行的自监督GAN》CVPR 2019 论文速看 0.摘要 目前自然图像合成主要是条件GAN,但是其缺点是需要标注数据。 我们利用两种流行的无监督学习技术,对抗训练和自我监督,并朝着缩小有条件GAN和无条件GAN之间的差距迈出了一步。 我们允许网络在代表学习的任务上进行协作,同时相对于经典GAN博弈...

题目练习

题目: 解决的代码: 注意:链表指针在操作以后记得移动...

Retrofit(三)上传文件

想了想,觉得还是把自定义的东西放到最后再讲,所以讲下用Retrofit上传文件,就拿上传图片来说,因为上传图片我是想写一个专题的,包括以下: 1.上传图片操作 2.展示图片操作 3.选择图片操作 上传图片这篇讲,用Retrofit,之后我还想写一篇是用httpurlconnection的,因为用它会有个拼接的操作,只有经历过拼接才会更深刻的了解使用Http上传文件的过程。展示图片我其实已经写完了,...

Linux安装SQL2019

官方文档 导入公共存储库 GPG **: 为 SQL Server 2019 注册 Microsoft SQL Server Ubuntu 存储库: 使用以下命令进行安装 SQL2019: 包安装完成后,运行 mssql-conf setup,按照提示设置 SA 密码并选择版本,并执行以下命令: 完成配置后,验证服务是否正在运行:...

猜你喜欢

vue-cli使用vscode编辑器如何自动eslint检测

VSCode 保存时自动ESlint格式化 Eslint 自动格式化 首先安装ESLint插件 2.安装完成后,每次修改完代码都需要执行 "npm run lint"之后才能格式化 打开设置选项 编辑代码settings.json 新建.eslintrc.js文件 根据eslint官网规则(eslint官网),编写eslintrc.js文件 保存之后,今后在保存时就能自动ESL...

搭建VUE脚手架(二)---修改完善cli3

光创建完cli3脚手架只是成功了一半,现在还无法直接使用(确切地说还不好用),还需要继续改造 这是本文修改后项目的目录结构,创建文件时可以参考。 1.创建vue.config.js 2.创建常量配置文件(.env.development和.env.production),用于存放常量 .env.development .env.production 3.ESLink配置(别问为什么,问就是我严格语...

hadoop-zookeeper-hive-flume-kafka-hbase集群,解决登录报错,Cli.sh启动失败,Error: java.lang.UnsupportedClassVersion

1.克隆虚拟机之前清理垃圾 2.选中“克隆虚拟机”:“右键→管理→克隆” 弹窗 操作 1 下一步 2 克隆自:虚拟机中的当前状态 3 创建完整克隆 4 虚拟机命名,选择存储位置 5 完成 3.配置IP 3.1获取地址:vi /etc/udev/rules.d/70-persistent-net.rules 3.2修改本机IP:vi...

访问日志

1.访问日志不记录静态文件        一个网站会有很多元素,比如图片、js、css等静态文件比较多,用户每请求一个页面就会访问很多的静态元素,这些元素的请求都会被记录到日志中去。如果一个网站访问量比较大,那么访问日志就会增加的非常快,甚至一天就可以达到几个GN。这不仅会对服务器的磁盘空间造成影响,更重要的是会影响磁盘的读写速度。    ...

苹果2016年或失去“世界最大公司”头衔

× loading.. 企业网D1Net 登入注册 当前位置:新闻中心 → 行业动态 → 正文 苹果2016年或失去“世界最大公司”头衔 责任编辑:jackye 作者:风帆 | 2016-01-15 09:03:57 本文摘自:腾讯科技 苹果2016年或失去“世界最大公司”头衔 1月15日,据国外媒体报道,随着谷歌在智...