python Shapely使用指南詳解
Shapely是一個(gè)Python庫(kù),用于操作和分析笛卡爾坐標(biāo)系中的幾何對(duì)象。
引入包
from shapely.geometry import Point from shapely.geometry import LineString
共有的變量和方法
object.area
Returns the area (float) of the object.
object.bounds
返回對(duì)象的(minx,miny,maxx,maxy)元組(float類型)
object.length
返回對(duì)象的長(zhǎng)度
object.geom_type
返回對(duì)象類型
object.distance(other)
返回本對(duì)象和另一個(gè)對(duì)象的距離
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)
一個(gè)點(diǎn)對(duì)象有area和長(zhǎng)度都為0
>>> point.area 0.0 >>> point.length 0.0
坐標(biāo)可以通過(guò)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構(gòu)造函數(shù)傳入?yún)?shù)是2個(gè)或多個(gè)點(diǎn)序列
一個(gè)LineStrings對(duì)象area為0,長(zhǎng)度非0
>>> line = LineString([(0,0), (0,1), (1,2)]) >>> line.area 0.0 >>> line.length 2.414213562373095
獲得坐標(biāo)
>>> 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依然可以接受一個(gè)同類型對(duì)象
>>> line2 = LineString(line) >>> line2.coords[:] [(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
常見(jiàn)格式轉(zhuǎn)換
>>> 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'
兩者都有l(wèi)oads和dumps方法
對(duì)于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)]
對(duì)于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)]
更多關(guān)于python Shapely使用方法請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
python自動(dòng)化測(cè)試之破解圖文驗(yàn)證碼
這篇文章介紹了python自動(dòng)化測(cè)試之破解圖文驗(yàn)證碼的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
python 裝飾器功能以及函數(shù)參數(shù)使用介紹
之前學(xué)習(xí)編程語(yǔ)言大多也就是學(xué)的很淺很淺,基本上也是很少涉及到裝飾器這些的類似的內(nèi)容。總是覺(jué)得是一樣很神奇的東西,舍不得學(xué)(嘿嘿)。今天看了一下書籍。發(fā)現(xiàn)道理還是很簡(jiǎn)單的2012-01-01
Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示
這篇文章主要介紹了Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示,下面文章圍繞Python爬蟲(chóng)的相關(guān)資料展開(kāi)對(duì)爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)的詳細(xì)介紹,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-01-01
Django 限制用戶訪問(wèn)頻率的中間件的實(shí)現(xiàn)
這篇文章主要介紹了Django 限制用戶訪問(wèn)頻率的中間件的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
python中使用OpenCV進(jìn)行人臉檢測(cè)的例子
這篇文章主要介紹了python中使用OpenCV進(jìn)行人臉檢測(cè)的例子,需要的朋友可以參考下2014-04-04
matplotlib實(shí)現(xiàn)區(qū)域顏色填充
這篇文章主要為大家詳細(xì)介紹了matplotlib實(shí)現(xiàn)區(qū)域顏色填充,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python利用帶權(quán)重隨機(jī)數(shù)解決抽獎(jiǎng)和游戲爆裝備問(wèn)題
帶權(quán)重隨機(jī)數(shù)即是隨機(jī)數(shù)各個(gè)區(qū)間段被抽中的概率根據(jù)權(quán)重而不同,這里我們就來(lái)看一下Python利用帶權(quán)重隨機(jī)數(shù)解決抽獎(jiǎng)和游戲爆裝備問(wèn)題的方法,首先還是來(lái)進(jìn)一步解釋帶權(quán)隨機(jī)數(shù):2016-06-06
python的mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)操作示例
這篇文章主要介紹了python的mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了python操作mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09

