python 读取、修改shp文件属性

  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类型信息

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

    展示效果:

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

 

 

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值