分享

在python中处理shapefile-实例,转载

 昵称QAb6ICvc 2022-03-16

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

1、Python Shapefile Library的下载与安装:

Python Shapefile Library下载地址:https://code.google.com/p/pyshp/

Python Shapefile Library使用时无需安装,只需在Python程序中导入该模块文件即可(import shapefile,具体导入方法参考Python教程中模块的导入部分)

2、Shapefile文件的读操作

2.1 Python Shapefile Library提供了Reader类,通过创建Reader类的对象(如下面的sf)进行shapefile文件的读操作:

                        sf = shapefile . Reader ('shapefile name’)

2.2 使用Python Shapefile Library读取shapefile文件的”几何数据”(Geometry)和”属性数据”(Attribute Record)

”几何数据”一般有多个几何对象组成,比如一个”点文件”,每个点就是一个对象;对于一个多边形文件,每个对象可能包含有多个多边形,每个多边形又称为”块(parts)“,每个 “块”由多个点组成。

每个几何对象包含有4个属性:数据类型(shapeType),代表该”几何数据”对象的数据类型(点,shapeType=1,线,shapeType=3,多边形,shapeType=5);数据范围(bbox),只针对多点数据,代表该”几何数据”对象的边界范围;数据块(parts),只针对线或者多边形,代表该”几何数据”对象各个块的第一个点的索引;点集(points),代表该”几何数据”对象的所有点坐标。

”属性数据”即每个”几何数据”对象在属性表中的对应项。

2.2.1“几何数据”的读取方法

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

              Shapes = sf.shapes( ),Shapes是一个列表(相当于一维数组),存放着该文件中所有的”几何数据”对象

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

通过shapeType,bbox,points,parts返回每个”几何数据”对象的属性信息:

Shapes(0).shapeType,返回第1个对象的数据类型属性(或者Shape.shapeType)

Shapes(2).bbox,返回第3个对象的数据范围(左下角的x,y坐标和右上角的x,y坐标)

Shape(3).points,返回第4个对象的所有点坐标

Shape(1).parts,返回第2个对象的每个”块”的第一个点坐标

2.2.2 “属性数据”的读取方法

”属性数据”通过Reader类的records( )record( )方法来读取,其区别和使用方法同shapes( )shape( )

”属性数据”的fields通过Reader类的fields方法来获取,其返回值为包括属性表每个字段的名称、数据类型、数据长度等的一个列表。

2.2.3 “几何数据”和”属性数据”同时读取的方法

通过Reader类的shapeRecords( )shapeRecord( )方法可以同时读取shapefile文件的”几何数据”和”属性数据”。

                         ShapeRecords = sf.shapeRecords( ).

                     ShapeRecords[0].shape.shapeType,返回第1个对象的”几何数据”的数据类型属性

                         ShapeRecords[0].record[1:3],返回第1个对象的”属性数据”的第2和第3个属性值

3 shapefile文件的写操作

3.1 Python Shapefile Library提供了Writer类,通过创建Writer类的对象(如下面的sf)进行shapefile文件的写操作:

                         sf = shapefile .Writer (shapeType = 1)

3.2 文件类型的确定

写shapefile文件时,首先要确定shapeType,可以通过以下两种方法确定:

1、在创建Writer类对象时直接确定shapeType,如上所示

2、通过为Writer类对象的shapeType属性赋值,如sf.shapeType = 1

3.3 “几何数据”与”属性数据”的自动平衡

shapefile文件要求”几何数据”与”属性数据”要有一一对应的关系,如果有”几何数据”而没有相应的属性值存在,那么在使用ArcGIS软件打开所创建的shapefile文件时会出错。为了避免这种情况的发生,可以设置 sf.autoBalance = 1,以确保每创建一个”几何数据”,该库会自动创建一个属性值(空的属性值)来进行对应。autoBalance默认为0。

3.4 shapefile文件的创建

shapefile文件的创建分为2步:

1、创建”几何数据”,通过Writer类的point(x,y,z,m)方法创建点数据,通过poly(parts = [ [ [1,5], [5,5], [5,1], [3,3], [1,1] ] ])方法创建线(parts有2个点)或多边形数据(parts > 2个点)。

2、创建”属性数据”,首先通过field()方法创建属性表字段,然后通过record()方法为每个几何数据添加相应的属性值。

(field的创建可以参见http://gis./questions/35593/using-the-python-shape-library-pyshp-how-to-convert-csv-file-to-shp)

例1:

>>>w = shapefile.Writer()

>>>w.autoBalance = 1

>>>w = shapefile.Writer(shapefile.POINT)

>>>w.point(1,1)

>>>w.point(3,1)

>>>w.point(4,3)

>>>w.point(2,2)

>>>w.field('FIRST_FLD’)

>>>w.field('SECOND_FLD’,’C’,’40’)     #’SECOND_FLD’为字段名称,C代表数据类型为字符串,长度为40

>>>w.record('First’,’Point’)

>>>w.record('Second’,’Point’)

>>>w.record('Third’,’Point’)

>>>w.record('Fourth’,’Point’)

>>>w.save('shapefiles/test/point’)

例2:

>>>w = shapefile.Writer()

>>>w.autoBalance = 1

>>>w = shapefile.Writer(shapefile.POLYGON)

>>>w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])

>>>w.field('FIRST_FLD’,’C’,’40’)

>>>w.field('SECOND_FLD’,’C’,’40’)

>>>w.record('First’,’Polygon’)

>>>w.save('shapefiles/test/polygon’)

例3:

>>>w = shapefile.Writer()

>>>w.autoBalance = 1

>>>w = shapefile.Writer(shapefile.POLYLINE)

>>>w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])

>>>w.poly(parts=[[[1,3],[5,3]]], shapeType=shapefile.POLYLINE)

>>>w.field('FIRST_FLD’,’C’,’40’)

>>>w.field('SECOND_FLD’,’C’,’40’)

>>>w.record('First’,’Line’)

>>>w.record('Second’,’Line’)

>>>w.save('shapefiles/test/line’)

4 shapefile文件的编辑

4.1 Python Shapefile Library提供了Editor类,通过创建Editor类的对象(如下面的sf)进行shapefile文件的编辑:

                         sf = shapefile .Editor ('shapefile name’)

4.2 编辑示例:

例1,增加一个点文件

>>>e = shapefile.Editor(shapefile=”shapefiles/test/point.shp”)

>>>e.point(0,0,10,2)            #注意,这里如果没有z值(10,即高度值)和M值(2,即测量值),编辑时可能会出现问题

>>>e.record(“Appended”,”Point”)

>>>e.save('shapefiles/test/point’)

例2,增加一条线

>>>e = shapefile.Editor(shapefile=”shapefiles/test/line.shp”)

>>>e.line(parts=[[[10,5],[15,5],[15,1],[13,3],[11,1]]])

>>>e.record('Appended’,’Line’)

>>>e.save('shapefiles/test/line’)

例3,增加一个多边形

>>>e = shapefile.Editor(shapefile=”shapefiles/test/polygon.shp”)

>>>e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]])

>>>e.record(“Appended”,”Polygon”)

>>>e.save('shapefiles/test/polygon’)

例4,删除第一个点

>>>e = shapefile.Editor(shapefile=”shapefiles/test/point.shp”)

>>>e.delete(0)

>>>e.save('shapefiles/test/point’)

例5,删除最后一个多边形

>>>e = shapefile.Editor(shapefile=”shapefiles/test/polygon.shp”)

>>>e.delete(-1)

>>>e.save('shapefiles/test/polygon’)

本文参照Python Shapefile Library官方文档,可到其网站下载英文原版https://code.google.com/p/pyshp/

转载出处:http://blog.sina.com.cn/s/blog_b3a4f3f80101cye7.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多