您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“python中如何使用pyshp讀寫shp文件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“python中如何使用pyshp讀寫shp文件”吧!
pip install pyshp
import shapefile
sf=shapefile.Reader("{路徑名}",encoding='utf-8') # 僅僅讀取
shapes=sf.shapes() 返回值是一個(gè)列表,包含該文件中所有的”幾何數(shù)據(jù)”對(duì)象
shape=sf.shape(0) Shape是第1個(gè)”幾何數(shù)據(jù)”對(duì)象
shapeType返回集合類型
返回第1個(gè)對(duì)象的數(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 返回?cái)?shù)據(jù)范圍
shape.bbox 返回第一個(gè)集合對(duì)象的數(shù)據(jù)范圍(左下角的x,y坐標(biāo)和右上角的x,y坐標(biāo))
points 所有坐標(biāo)點(diǎn)
shape.points 返回第一個(gè)集合對(duì)象的所有坐標(biāo)點(diǎn)
parts 返回’塊’的第一個(gè)點(diǎn)坐標(biāo)
shape.parts 返回第一個(gè)對(duì)象的每個(gè)”塊”的第一個(gè)點(diǎn)坐標(biāo)
獲取屬性列表
records
獲取屬性列表,是個(gè)函數(shù)
sf.records();
返回的值是個(gè)list
record
獲取一條數(shù)據(jù)
sf.record(0)
返回的值是class
同時(shí)獲取record和shape
# 同時(shí)讀取geometry and records sf.shapeRecords() 獲取所有 red=sf.shapeRecords()[0] #獲取第一條數(shù)據(jù) print(red.record) #獲取record print(red.shape) #獲取shape
獲取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)建點(diǎn) 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會(huì)自動(dòng)生成。 fileWrite.field('landid') fileWrite.field('landName') for i in range(len(landlist)): # 第一步:塞入形狀 ## 這個(gè)形狀指的就是那些點(diǎn)的集合 ## 由于源碼中要求的輸入是列表,因此就算只塞入一個(gè),也要套一個(gè)列表 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 寫入面,點(diǎn)線面使用不同函數(shù) fileWrite.poly([arr]) # 第二步:塞入屬性值 fileWrite.record(str(i), '地塊') # 保存結(jié)束 fileWrite.close()
到此,相信大家對(duì)“python中如何使用pyshp讀寫shp文件”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。