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 相关文章推荐
python爬虫常用的模块分析
Aug 29 Python
Python类的专用方法实例分析
Jan 09 Python
Python模块搜索概念介绍及模块安装方法介绍
Jun 03 Python
深入浅析Python字符编码
Nov 12 Python
python 内置函数filter
Jun 01 Python
使用Django Form解决表单数据无法动态刷新的两种方法
Jul 14 Python
python使用标准库根据进程名如何获取进程的pid详解
Oct 31 Python
Django安装配置mysql的方法步骤
Oct 15 Python
python调用staf自动化框架的方法
Dec 26 Python
python3+selenium自动化测试框架详解
Mar 17 Python
python+logging+yaml实现日志分割
Jul 22 Python
Python爬虫运用正则表达式的方法和优缺点
Aug 25 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
SONY ICF-SW55的电路分析
2021/03/02 无线电
PHP下用rmdir实现删除目录的三种方法小结
2008/04/20 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
2013/04/15 PHP
PHP正则匹配反斜杠'\'和美元'$'的方法
2017/02/08 PHP
PC端微信扫码支付成功之后自动跳转php版代码
2017/07/07 PHP
再谈Yii Framework框架中的事件event原理与应用
2020/04/07 PHP
PHP函数用法详解【初始化、嵌套、内置函数等】
2020/06/02 PHP
[原创]后缀就扩展名为js的文件是什么文件
2007/12/06 Javascript
javascript中数组array及string的方法总结
2014/11/28 Javascript
jQuery时间日期三级联动(推荐)
2016/11/27 Javascript
基于javascript的异步编程实例详解
2017/04/10 Javascript
微信小程序实现移动端滑动分页效果(ajax)
2017/06/13 Javascript
es6 symbol的实现方法示例
2019/04/02 Javascript
微信网页登录逻辑与实现方法
2019/04/29 Javascript
JS中的算法与数据结构之栈(Stack)实例详解
2019/08/20 Javascript
vue基于v-charts封装双向条形图的实现代码
2019/12/09 Javascript
es6数组includes()用法实例分析
2020/04/18 Javascript
原生JavaScript实现随机点名表
2021/01/14 Javascript
Python跳出循环语句continue与break的区别
2014/08/25 Python
python实现查找excel里某一列重复数据并且剔除后打印的方法
2015/05/26 Python
从零开始学Python第八周:详解网络编程基础(socket)
2016/12/14 Python
详解Python3中字符串中的数字提取方法
2017/01/14 Python
对numpy Array [: ,] 的取值方法详解
2018/07/02 Python
Django使用Channels实现WebSocket的方法
2019/07/28 Python
python异常触发及自定义异常类解析
2019/08/06 Python
python 非线性规划方式(scipy.optimize.minimize)
2020/02/11 Python
浅谈python处理json和redis hash的坑
2020/07/16 Python
SmartBuyGlasses中国:唯视良品(销售名牌太阳镜、墨镜和眼镜框)
2017/07/03 全球购物
牦牛毛户外探险服装:Kora
2019/02/08 全球购物
介绍一下Java的事务处理
2012/12/07 面试题
大学生万能检讨书范例
2014/10/04 职场文书
幼儿园万圣节活动总结
2015/05/05 职场文书
离婚代理词范文
2015/05/23 职场文书
教师师德工作总结2015
2015/07/22 职场文书
新教师2015年度工作总结
2015/07/22 职场文书
大学宣传委员竞选稿
2015/11/19 职场文书