python 您所在的位置:网站首页 shapes怎么读啊 python

python

2023-12-16 17:10| 来源: 网络整理| 查看: 265

python-shapefile库读取shapefile文件信息

shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Class),主要包括点(point)、线(polyline)和多边形(polygon)。Python脚本是ArcGIS官方推荐的脚本语言,通过Python脚本能够很方便的调用ArcGIS中的各种工具和函数批量完成所需操作。本文所介绍的这个库(Python Shapefile Library)是一个Python库,用于在Python脚本中对ArcGIS中的Shapefile文件(.shp,.shx,.dbf等格式)进行读写操作。

开发准备 安装命令: pip install pyshp 使用导入: import shapefile

Shapefile文件的读操作 通过创建Reader类的对象进行shapefile文件的读操作。

file = shapefile.Reader('shapefile name')

“几何数据”通过Reader类的shapes( )和shape( )方法来读取,二者的区别在于:shapes()方法不需要指定参数,其返回值是一个列表,包含该文件中所有的"几何数据"对象,而shape( )方法则需要通过指定参数返回所需要的"几何数据"对象。

shape = file.shape(i)#读取第i+1个要素,索引序列从0开始(参数是整数类型) shapes = file.shapes()#读取全部要素 # -*- coding: utf-8 -*- import shapefile# 使用pyshp file = shapefile.Reader("E://arcgisData//行政区划数据//省界_region.shp")#读取 #读取元数据 print(str(file.shapeType)) # 输出shp类型 print(file.encoding)# 输出shp文件编码 print(file.bbox) # 输出shp的文件范围(外包矩形) print(file.numRecords) # 输出shp文件的要素数据 print(file.fields)# 输出所有字段信息 # print(file.records()) # 输出所有属性表 补充 shp类型信息 shp属性表字段信息 字段索引字段类型C字符,文字N数字,带或不带小数F浮动(与“N”相同)L逻辑,表示布尔值True / False值D日期M备忘录,在GIS中没有意义,而是xbase规范的一部分 展示单个要素 实现代码: import shapefile # 使用pyshp from matplotlib import pyplot as plt file = shapefile.Reader("E://arcgisData//行政区划数据//省界_region.shp") border_shape = file # 通过创建reader类的对象进行shapefile文件的读取 # border_points border = border_shape.shapes() # .shapes()读取几何数据信息,存放着该文件中所有对象的 几何数据 # border是一个列表 border_points = border[0].points print(border_points)# 返回第1个对象的所有点坐标 # border_points = [(x1,y1),(x2,y2),(x3,y3),…] x, y = zip(*border_points) # x=(x1,x2,x3,…) # y=(y1,y2,y3,…) fig, ax = plt.subplots() # 生成一张图和一张子图 # plt.plot(x,y,'k-') # x横坐标 y纵坐标 ‘k-’线性为黑色 plt.plot(x, y, color='#6666ff', label='fungis') # x横坐标 y纵坐标 ‘k-’线性为黑色 ax.grid() # 添加网格线 ax.axis('equal') plt.show()

展示效果:

3. shapefile文件的写操作 创建点状要素类 import shapefile # 使用pyshp file = shapefile.Writer("E://arcgisData//test//站点_2")#新建数据存放位置 #创建两个字段 file.field('FIRST_FLD') file.field('SECOND_FLD','C','40') #'SECOND_FLD'为字段名称,C代表数据类型为字符串,长度为40 file.point(1,1) file.record('First','Point') file.point(300,10) file.record('Second','Point') file.point(400,30) file.record('Third','Point') file.point(200,200) file.record('Fourth','Point') #写入数据 file.close()

创建线状要素类

import shapefile # 使用pyshp file = shapefile.Writer("E://arcgisData//test//polyline")#新建数据存放位置 #创建两个字段 file.field('FIRST_FLD') file.field('SECOND_FLD','C','40') #'SECOND_FLD'为字段名称,C代表数据类型为字符串,长度为40 file.line([[[1,5],[5,5],[5,1],[3,3],[1,1]]]) file.record('First','polyline') file.line([[[1,500],[300,30],[1,16]]]) file.record('Second','polyline') #写入数据 file.close() 创建面状要素类 import shapefile # 使用pyshp file = shapefile.Writer("E://arcgisData//test//polygon")#新建数据存放位置 #创建两个字段 file.field('FIRST_FLD') file.field('SECOND_FLD','C','40') #'SECOND_FLD'为字段名称,C代表数据类型为字符串,长度为40 file.poly([[[1,5],[5,5],[5,1],[3,3],[1,1]]]) file.record('First','polygon') file.poly([[[1,500],[300,30],[1,16],[1,500]]]) file.record('Second','polygon') #写入数据 file.close()

4 shapefile文件的编辑(后续补充~)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有