新聞中心
本篇內(nèi)容主要講解“python中如何使用pyshp讀寫shp文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“python中如何使用pyshp讀寫shp文件”吧!

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了衡陽縣免費建站歡迎大家使用!
安裝
pip install pyshp
引入
import shapefile
讀取
sf=shapefile.Reader("{路徑名}",encoding='utf-8') # 僅僅讀取
shapes與shape
shapes=sf.shapes() 返回值是一個列表,包含該文件中所有的”幾何數(shù)據(jù)”對象
shape=sf.shape(0) Shape是第1個”幾何數(shù)據(jù)”對象
shapeType返回集合類型
返回第1個對象的數(shù)據(jù)類型屬性
幾何類型 NULL = 0 POINT = 1 POLYLINE = 3 POLYGON = 5 MULTIPOINT = 8 POINTZ = 11 POLYLINEZ = 13 POLYGONZ = 15 MULTIPOINTZ = 18 POINTM = 21 POLYLINEM = 23 POLYGONM = 25 MULTIPOINTM = 28 MULTIPATCH = 31 print(shape.shapeType)
bbox 返回數(shù)據(jù)范圍
shape.bbox 返回第一個集合對象的數(shù)據(jù)范圍(左下角的x,y坐標(biāo)和右上角的x,y坐標(biāo))
points 所有坐標(biāo)點
shape.points 返回第一個集合對象的所有坐標(biāo)點
parts 返回’塊’的第一個點坐標(biāo)
shape.parts 返回第一個對象的每個”塊”的第一個點坐標(biāo)
records與record
獲取屬性列表
records
獲取屬性列表,是個函數(shù)
sf.records();
返回的值是個list
record
獲取一條數(shù)據(jù)
sf.record(0)
返回的值是class
shapeRecords
同時獲取record和shape
# 同時讀取geometry and records sf.shapeRecords() 獲取所有 red=sf.shapeRecords()[0] #獲取第一條數(shù)據(jù) print(red.record) #獲取record print(red.shape) #獲取shape
fields
獲取shp文件屬性字段
print(sf.fields)
[('DeletionFlag', 'C', 1, 0), ['OBJECTID', 'N', 9, 0], ['BSM', 'C', 12, 0], ['PXZQDM', 'C', 2, 0], ['PXZQMC', 'C', 50, 0]]寫入
import shapefile
outshp = 'a.shp'
landlist=[ '84.60212,45.03658,84.60794,45.03938,84.61473,45.04151,84.62442,45.04375,84.62727,45.03632,84.63939,45.0367,84.64906,45.03277,84.63886,45.02233',
'84.58063,45.05523,84.57974,45.04717,84.59864,45.04792,84.60078,45.05523,84.58758,45.05473,84.58223,45.05523'
]
def tramform(lat_lng):
str =lat_lng
str = str.split(',')
arr = []
for i in range(len(str) - 1):
# 第一列,第二列作為經(jīng)緯度(x,y)創(chuàng)建點
if i % 2 == 0:
arr.append([float(str[i]), float(str[i + 1])])
return arr
fileWrite = shapefile.Writer("create/1.shp",encoding='utf-8') # 新建數(shù)據(jù)存放位置
# shp文件屬性字段 Fid,Shape會自動生成。
fileWrite.field('landid')
fileWrite.field('landName')
for i in range(len(landlist)):
# 第一步:塞入形狀
## 這個形狀指的就是那些點的集合
## 由于源碼中要求的輸入是列表,因此就算只塞入一個,也要套一個列表
arr=[]
arr=tramform(landlist[i])
#[[84.60212, 45.03658], [84.60794, 45.03938], [84.61473, 45.04151], [84.62442, 45.04375], [84.62727, 45.03632], [84.63939, 45.0367], [84.64906, 45.03277], [84.63886, 45.02233]]
#poly 寫入面,點線面使用不同函數(shù)
fileWrite.poly([arr])
# 第二步:塞入屬性值
fileWrite.record(str(i), '地塊')
# 保存結(jié)束
fileWrite.close()到此,相信大家對“python中如何使用pyshp讀寫shp文件”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站欄目:python中如何使用pyshp讀寫shp文件
本文來源:http://www.dlmjj.cn/article/gosshe.html


咨詢
建站咨詢
