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 enumerate遍历数组示例应用
Sep 06 Python
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
Apr 11 Python
在Python中使用SQLite的简单教程
Apr 29 Python
Python实现快速排序和插入排序算法及自定义排序的示例
Feb 16 Python
Python的Flask框架中的Jinja2模板引擎学习教程
Jun 30 Python
浅谈python中的实例方法、类方法和静态方法
Feb 17 Python
python将视频转换为全字符视频
Apr 26 Python
django的model操作汇整详解
Jul 26 Python
关于Python 常用获取元素 Driver 总结
Nov 24 Python
Python socket聊天脚本代码实例
Jan 02 Python
Python MySQLdb 执行sql语句时的参数传递方式
Mar 04 Python
Python 字符串池化的前提
Jul 03 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
php网页后退不再出现过期
2007/03/08 PHP
php 在文件指定行插入数据的代码
2010/05/08 PHP
CodeIgniter启用缓存和清除缓存的方法
2014/06/12 PHP
详解PHP的Yii框架中自带的前端资源包的使用
2016/03/31 PHP
PHP substr()函数参数解释及用法讲解
2017/11/23 PHP
Thinkphp整合阿里云OSS图片上传实例代码
2019/04/28 PHP
漂亮的widgets,支持换肤和后期开发新皮肤(2007-4-27已更新1.7alpha)
2007/04/27 Javascript
JS动画效果代码3
2008/04/03 Javascript
JavaScript函数、方法、对象代码
2008/10/29 Javascript
DIV外区域Click后关闭DIV的实现代码
2011/12/21 Javascript
14款经典网页图片和文字特效的jQuery插件-前端开发必备
2015/08/25 Javascript
JS实现页面载入时随机显示图片效果
2016/09/07 Javascript
js移动端事件基础及常用事件库详解
2017/08/15 Javascript
jQuery实现的表格前端排序功能示例
2017/09/18 jQuery
jquery ajax异步提交表单数据的方法
2017/10/27 jQuery
Vue-cli3项目配置Vue.config.js实战记录
2018/07/29 Javascript
layui中select,radio设置不生效的解决方法
2019/09/05 Javascript
Python的randrange()方法使用教程
2015/05/15 Python
python生成器generator用法实例分析
2015/06/04 Python
python与caffe改变通道顺序的方法
2018/08/04 Python
使用python的pandas库读取csv文件保存至mysql数据库
2018/08/20 Python
python命令行工具Click快速掌握
2019/07/04 Python
利用python-docx模块写批量生日邀请函
2019/08/26 Python
Django模板导入母版继承和自定义返回Html片段过程解析
2019/09/18 Python
windows下Python安装、使用教程和Notepad++的使用教程
2019/10/06 Python
django使用xadmin的全局配置详解
2019/11/15 Python
开放系统互连参考模型
2016/06/29 面试题
手机业务员岗位职责
2013/12/13 职场文书
爱心捐款感谢信
2015/01/20 职场文书
2015年安全生产月活动总结
2015/03/26 职场文书
团委工作总结2015
2015/04/02 职场文书
办公经费申请报告
2015/05/15 职场文书
小学体育教学随笔
2015/08/14 职场文书
nginx location优先级的深入讲解
2021/03/31 Servers
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
python使用opencv对图像添加噪声(高斯/椒盐/泊松/斑点)
2022/04/06 Python