python Shapely使用指南详解


Posted in Python onFebruary 18, 2020

Shapely是一个Python库,用于操作和分析笛卡尔坐标系中的几何对象。

引入包

from shapely.geometry import Point

from shapely.geometry import LineString

共有的变量和方法

object.area

Returns the area (float) of the object.

object.bounds

返回对象的(minx,miny,maxx,maxy)元组(float类型)

object.length

返回对象的长度

object.geom_type

返回对象类型

object.distance(other)

返回本对象和另一个对象的距离

object.representative_point()

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

>>> from shapely.geometry import Point
>>> print Point(0,0).distance(Point(0,1))
1.0
>>> from shapely.geometry import LineString
>>> line = LineString([(0,0), (1,1), (1,2)])
>>> line.area
0.0
>>> line.bounds
(0.0, 0.0, 1.0, 2.0)
>>> line.length
2.414213562373095
>>> line.geom_type
'LineString'

Point

class Point(coordinates)

三种赋值方式

>>> point = Point(0,0)
>>> point_2 = Point((0,0))
>>> point_3 = Point(point)

一个点对象有area和长度都为0

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

坐标可以通过coords或x、y、z得到

>>> p = Point(2,3)
>>> p.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d60dd0>

>>> list(p.coords)
[(2.0, 3.0)]
>>> p.x
2.0
>>> p.y
3.0

coords可以被切片

>>> p.coords[:]
[(2.0, 3.0)]

LineStrings

LineStrings构造函数传入参数是2个或多个点序列

一个LineStrings对象area为0,长度非0

>>> line = LineString([(0,0), (0,1), (1,2)])
>>> line.area
0.0
>>> line.length
2.414213562373095

获得坐标

>>> line.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
 >>> list(line.coords)
 [(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]

LineString依然可以接受一个同类型对象

>>> line2 = LineString(line)
>>> line2.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]

常见格式转换

>>> Point(1,1).wkt
'POINT (1 1)'
>>> Point(1,1).wkb
'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'
>>> 
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'

两者都有loads和dumps方法

对于wkt

>>> from shapely.wkt import dumps, loads
>>> s = dumps(Point(1,2))
>>> s
'POINT (1.0000000000000000 2.0000000000000000)'
>>> ss = loads(s)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]

对于wkb

>>> from shapely.wkb import dumps, loads
>>> s = dumps(Point(1,2), hex=True)
>>> s
'0101000000000000000000F03F0000000000000040'
>>> ss = loads(s, hex=True)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d78790>
>>> ss.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]

更多关于python Shapely使用方法请查看下面的相关链接

Python 相关文章推荐
两个命令把 Vim 打造成 Python IDE的方法
Mar 20 Python
Python 基础知识之字符串处理
Jan 06 Python
Windows环境下python环境安装使用图文教程
Mar 13 Python
Python一句代码实现找出所有水仙花数的方法
Nov 13 Python
python抖音表白程序源代码
Apr 07 Python
Python3利用print输出带颜色的彩色字体示例代码
Apr 08 Python
如何使用pyinstaller打包32位的exe程序
May 26 Python
pandas计算最大连续间隔的方法
Jul 04 Python
TensorFlow设置日志级别的几种方式小结
Feb 04 Python
Jmeter调用Python脚本实现参数互相传递的实现
Jan 22 Python
python3.9和pycharm的安装教程并创建简单项目的步骤
Feb 03 Python
Python开发简易五子棋小游戏
May 02 Python
Python模拟FTP文件服务器的操作方法
Feb 18 #Python
git查看、创建、删除、本地、远程分支方法详解
Feb 18 #Python
Python使用urllib模块对URL网址中的中文编码与解码实例详解
Feb 18 #Python
python实现根据给定坐标点生成多边形mask的例子
Feb 18 #Python
python有序查找算法 二分法实例解析
Feb 18 #Python
Python连接SQLite数据库并进行增册改查操作方法详解
Feb 18 #Python
Python 解析pymysql模块操作数据库的方法
Feb 18 #Python
You might like
第一个无线电台是由谁发明的
2021/03/01 无线电
php 什么是PEAR?(第二篇)
2009/03/19 PHP
Windows Apache2.2.11及Php5.2.9-1的安装与配置方法
2009/06/08 PHP
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
2013/07/01 PHP
php读取excel文件示例分享(更新修改excel)
2014/02/27 PHP
PHP使用GIFEncoder类生成的GIF动态图片验证码
2014/07/01 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
基于jQuery图片平滑连续滚动插件
2009/04/27 Javascript
js 异步处理进度条
2010/04/01 Javascript
javascript实现跳转菜单的具体方法
2013/07/05 Javascript
jQuery表格插件ParamQuery简单使用方法示例
2013/12/05 Javascript
JavaScript函数详解
2014/11/17 Javascript
简介AngularJS中使用factory和service的方法
2015/06/17 Javascript
javascript实现显示和隐藏div方法汇总
2015/08/14 Javascript
JavaScript实现上下浮动的窗口效果代码
2015/10/12 Javascript
jQuery使用$.each遍历json数组的简单实现方法
2016/04/18 Javascript
详解Vue.js 2.0 如何使用axios
2017/04/21 Javascript
JavaScript事件处理程序详解
2017/09/19 Javascript
JS实现可视化文件上传
2018/09/08 Javascript
详解搭建es6+devServer简单开发环境
2018/09/25 Javascript
js如何获取图片url的Blob值并预览示例代码
2019/03/07 Javascript
微信小程序实现带参数的分享功能(两种方法)
2019/05/17 Javascript
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
2019/05/30 NodeJs
如何在vue 中使用柱状图 并自修改配置
2021/01/21 Vue.js
Python中对元组和列表按条件进行排序的方法示例
2015/11/10 Python
Python实现MySQL操作的方法小结【安装,连接,增删改查等】
2017/07/12 Python
Python中的is和==比较两个对象的两种方法
2017/09/06 Python
详解Django的model查询操作与查询性能优化
2018/10/16 Python
pytorch 准备、训练和测试自己的图片数据的方法
2020/01/10 Python
用python批量移动文件
2021/01/14 Python
amazeui页面校验功能的实现代码
2020/08/24 HTML / CSS
澳大利亚制造的羊皮靴:Original UGG Boots
2017/11/13 全球购物
前台文员岗位职责及工作流程
2013/11/19 职场文书
干部考核评语
2014/04/29 职场文书
升职演讲稿范文
2014/05/23 职场文书
vue动态绑定style样式
2022/04/20 Vue.js