python使用pyshp读写shp文件的实现

本文主要介绍了python使用pyshp读写shp文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

安装

pip install pyshp

引入

import shapefile 

读取

sf=shapefile.Reader("{路径名}",encoding='utf-8') # 仅仅读取

shapes与shape

shapes=sf.shapes()  返回值是一个列表,包含该文件中所有的”几何数据”对象

shape=sf.shape(0) Shape是第1个”几何数据”对象

shapeType返回集合类型

返回第1个对象的数据类型属性

    几何类型 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 返回数据范围

shape.bbox    返回第一个集合对象的数据范围(左下角的x,y坐标和右上角的x,y坐标)

points 所有坐标点

shape.points   返回第一个集合对象的所有坐标点

parts 返回’块’的第一个点坐标

shape.parts 返回第一个对象的每个”块”的第一个点坐标

records与record

获取属性列表

records

获取属性列表,是个函数

sf.records();

返回的值是个list

record
获取一条数据

sf.record(0)
返回的值是class

shapeRecords

同时获取record和shape

# 同时读取geometry and records sf.shapeRecords() 获取所有 red=sf.shapeRecords()[0]  #获取第一条数据 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):         # 第一列,第二列作为经纬度(x,y)创建点         if i % 2 == 0:             arr.append([float(str[i]), float(str[i + 1])])     return arr fileWrite = shapefile.Writer("create/1.shp",encoding='utf-8')  # 新建数据存放位置 # 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 写入面,点线面使用不同函数     fileWrite.poly([arr])     # 第二步:塞入属性值     fileWrite.record(str(i), '地块') # 保存结束 fileWrite.close()

到此这篇关于python使用pyshp读写shp文件的实现的文章就介绍到这了,更多相关pyshp读写shp内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是python使用pyshp读写shp文件的实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python