1. 开发准备
    安装命令: pip install pyshp
    使用导入: import shapefile
  2. 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())  # 输出所有属性表

3.补充

4.shp类型信息

Python读取类的属性 python读取文件属性_Python读取类的属性

5.

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()

展示效果:

Python读取类的属性 python读取文件属性_机器学习_02

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()