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á)式操作文本數(shù)據(jù)的方法
這篇文章主要介紹了使用Python正則表達(dá)式操作文本數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05python實(shí)現(xiàn)PID算法及測(cè)試的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)PID算法及測(cè)試的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python圖片和二進(jìn)制轉(zhuǎn)換的三種實(shí)現(xiàn)方式
本文介紹了將PIL格式、數(shù)組和圖片轉(zhuǎn)換為二進(jìn)制的不同方法,包括使用PIL庫(kù)、OpenCV和直接讀取二進(jìn)制,此外,還提到了數(shù)據(jù)傳輸中base64格式的應(yīng)用,這些信息對(duì)需要進(jìn)行圖片數(shù)據(jù)處理和轉(zhuǎn)換的開(kāi)發(fā)者非常有用2024-09-09Python基于select實(shí)現(xiàn)的socket服務(wù)器
這篇文章主要介紹了Python基于select實(shí)現(xiàn)的socket服務(wù)器,實(shí)例分析了Python基于select與socket模塊實(shí)現(xiàn)socket通信的相關(guān)技巧,需要的朋友可以參考下2016-04-04Python使用FastApi發(fā)送Post請(qǐng)求的基本步驟
FastAPI 是一個(gè)現(xiàn)代、快速(高性能)的 Web 框架,用于構(gòu)建 API,它基于 Python 3.6 及以上版本,在 FastAPI 中發(fā)送 POST 請(qǐng)求,通常是指創(chuàng)建一個(gè)接口來(lái)接收客戶端發(fā)送的 POST 請(qǐng)求,以下是使用 FastAPI 處理 POST 請(qǐng)求的基本步驟,需要的朋友可以參考下2024-09-09在tensorflow以及keras安裝目錄查詢操作(windows下)
這篇文章主要介紹了在tensorflow以及keras安裝目錄查詢操作(windows下),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06