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 31 Python
Python编程实现使用线性回归预测数据
Dec 07 Python
python的Tqdm模块的使用
Jan 10 Python
python3.5 email实现发送邮件功能
May 22 Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
Jul 05 Python
python中dict()的高级用法实现
Nov 13 Python
对Tensorflow中Device实例的生成和管理详解
Feb 04 Python
pytorch进行上采样的种类实例
Feb 18 Python
Django 后台带有字典的列表数据与页面js交互实例
Apr 03 Python
python中读入二维csv格式的表格方法详解(以元组/列表形式表示)
Apr 24 Python
Python SQLAlchemy库的使用方法
Oct 13 Python
python3 os进行嵌套操作的实例讲解
Nov 19 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/02 无线电
古巴咖啡 Cubita琥爵咖啡 独特的加勒比海风味咖啡
2021/03/06 新手入门
php 在线打包_支持子目录
2008/06/28 PHP
PHP 危险函数全解析
2009/09/09 PHP
利用php做服务器和web前端的界面进行交互
2016/10/31 PHP
yii2 commands模式以及配置crontab定时任务的方法
2017/08/19 PHP
通过Unicode转义序列来加密,按你说的可以算是混淆吧
2007/05/06 Javascript
用javascript实现分割提取页面所需内容
2007/05/09 Javascript
用js实现判断当前网址的来路如果不是指定的来路就跳转到指定页面
2011/05/02 Javascript
jQuery插件实现文字无缝向上滚动效果代码
2016/02/25 Javascript
JavaScript驾驭网页-DOM
2016/03/24 Javascript
jQuery原理系列-常用Dom操作详解
2016/06/07 Javascript
很实用的js选项卡切换效果
2016/08/12 Javascript
JavaScript中关键字 in 的使用方法详解
2016/10/17 Javascript
读Javascript高性能编程重点笔记
2016/12/21 Javascript
node.js基于mongodb的搜索分页示例
2017/01/22 Javascript
js实现PC端根据IP定位当前城市地理位置
2017/02/22 Javascript
Javascript实现异步编程的过程
2018/06/18 Javascript
jQuery实现的点击图片居中放大缩小功能示例
2019/01/16 jQuery
微信小程序页面间传值与页面取值操作实例分析
2019/04/30 Javascript
微信小程序学习总结(二)样式、属性、模板操作分析
2020/06/04 Javascript
Node.js 深度调试方法解析
2020/07/28 Javascript
[02:09]DOTA2辉夜杯 EHOME夺冠举杯现场
2015/12/28 DOTA
详细介绍Python函数中的默认参数
2015/03/30 Python
Python聊天室实例程序分享
2016/01/05 Python
Python实现的下载网页源码功能示例
2017/06/13 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
Django用户身份验证完成示例代码
2020/04/03 Python
keras的siamese(孪生网络)实现案例
2020/06/12 Python
Keras 实现加载预训练模型并冻结网络的层
2020/06/15 Python
带你认识HTML5中的WebSocket
2015/05/22 HTML / CSS
怎么处理XML的中文问题
2015/03/26 面试题
介绍一下HTTP、HTTPS和SSL
2012/12/16 面试题
2014党的群众路线教育实践活动总结报告
2014/10/31 职场文书
纯html+css实现奥运五环的示例代码
2021/08/02 HTML / CSS
《废话连篇——致新手》——chinapizza
2022/04/05 无线电