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写个小监控
Jan 27 Python
python处理按钮消息的实例详解
Jul 11 Python
python 删除大文件中的某一行(最有效率的方法)
Aug 19 Python
网红编程语言Python将纳入高考你怎么看?
Jun 07 Python
python 高效去重复 支持GB级别大文件的示例代码
Nov 08 Python
对python 命令的-u参数详解
Dec 03 Python
pandas去除重复列的实现方法
Jan 29 Python
将string类型的数据类型转换为spark rdd时报错的解决方法
Feb 18 Python
Python 基于wxpy库实现微信添加好友功能(简洁)
Nov 29 Python
TensorFlow获取加载模型中的全部张量名称代码
Feb 11 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
Feb 27 Python
10个python爬虫入门实例(小结)
Nov 01 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 session 检测和注销
2009/03/16 PHP
php 静态页面中显示动态内容
2009/08/14 PHP
Yii把CGridView文本框换成下拉框的方法
2014/12/03 PHP
smarty自定义函数用法示例
2016/05/20 PHP
php将print_r处理后的数据还原为原始数组的解决方法
2016/11/02 PHP
PHP判断是否是微信打开,浏览器打开的方法
2018/03/14 PHP
tp5.1 框架路由操作-URL生成实例分析
2020/05/26 PHP
js 处理URL实用技巧
2010/11/23 Javascript
JQuery之拖拽插件实现代码
2011/04/14 Javascript
Vue开发过程中遇到的疑惑知识点总结
2017/01/20 Javascript
用director.js实现前端路由使用实例
2017/01/27 Javascript
jQuery animate()实现背景色渐变效果的处理方法【使用jQuery.color.js插件】
2017/03/15 Javascript
详解通过源码解析Node.js中cluster模块的主要功能实现
2018/05/16 Javascript
微信小程序适配iphoneX的实现方法
2018/09/18 Javascript
怎样使你的 JavaScript 代码简单易读(推荐)
2019/04/16 Javascript
解决vue props传Array/Object类型值,子组件报错的情况
2020/11/07 Javascript
[47:43]Alliance vs KG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python 中文字符串的处理实现代码
2009/10/25 Python
Django框架中render_to_response()函数的使用方法
2015/07/16 Python
python使用KNN算法手写体识别
2018/02/01 Python
python使用rpc框架gRPC的方法
2018/08/24 Python
PyQt5的安装配置过程,将ui文件转为py文件后显示窗口的实例
2019/06/19 Python
对Tensorflow中Device实例的生成和管理详解
2020/02/04 Python
Python3标准库之dbm UNIX键-值数据库问题
2020/03/24 Python
Python用类实现扑克牌发牌的示例代码
2020/06/01 Python
使用python求斐波那契数列中第n个数的值示例代码
2020/07/26 Python
Pycharm安装Qt Design快捷工具的详细教程
2020/11/18 Python
纯CSS3实现圆角效果(含IE兼容解决方法)
2014/05/07 HTML / CSS
德国童装购物网站:NICKI´S.com
2018/04/20 全球购物
荷兰度假屋租赁网站:Aan Zee
2020/02/28 全球购物
餐厅楼面部长岗位职责范文
2014/02/16 职场文书
幼儿园中班下学期评语
2014/04/18 职场文书
小学生运动会通讯稿
2014/09/23 职场文书
党员群众路线剖析材料
2014/10/08 职场文书
2015年新农合工作总结
2015/03/30 职场文书
校园新闻稿范文
2015/07/18 职场文书