pyshp读写shapefile
来源:互联网 发布:软件行业利润率 编辑:程序博客网 时间:2024/06/16 06:18
shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point)、线(polyline)和多边形(polygon)。Python脚本是ArcGIS官方推荐的脚本语言,通过Python脚本能够很方便的调用ArcGIS中的各种工具和函数批量完成所需操作。本文所介绍的这个库(Python ShapefileLibrary)是一个Python库,用于在Python脚本中对ArcGIS中的Shapefile文件(.shp,.shx,.dbf等格式)进行读写操作。
1、Python ShapefileLibrary的下载与安装:
Python ShapefileLibrary下载地址:https://code.google.com/p/pyshp/
Python ShapefileLibrary使用时无需安装,只需在Python程序中导入该模块文件即可(import shapefile,具体导入方法参考Python教程中模块的导入部分)
2、Shapefile文件的读操作
2.1 Python ShapefileLibrary提供了Reader类,通过创建Reader类的对象(如下面的sf)进行shapefile文件的读操作:
2.2 使用Python ShapefileLibrary读取shapefile文件的”几何数据”(Geometry)和”属性数据”(Attribute Record)
”几何数据”一般有多个几何对象组成,比如一个”点文件”,每个点就是一个对象;对于一个多边形文件,每个对象可能包含有多个多边形,每个多边形又称为”块(parts)“,每个“块”由多个点组成。
每个几何对象包含有4个属性:数据类型(shapeType),代表该”几何数据”对象的数据类型(点,shapeType=1,线,shapeType=3,多边形,shapeType=5);数据范围(bbox),只针对多点数据,代表该”几何数据”对象的边界范围;数据块(parts),只针对线或者多边形,代表该”几何数据”对象各个块的第一个点的索引;点集(points),代表该”几何数据”对象的所有点坐标。
”属性数据”即每个”几何数据”对象在属性表中的对应项。
2.2.1“几何数据”的读取方法
”几何数据”通过Reader类的shapes()和shape()方法来读取,二者的区别在于:shapes()方法不需要指定参数,其返回值是一个列表,包含该文件中所有的”几何数据”对象,而shape()方法则需要通过指定参数返回所需要的”几何数据”对象。
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文件的”几何数据”和”属性数据”。
3shapefile文件的写操作
3.1 Python ShapefileLibrary提供了Writer类,通过创建Writer类的对象(如下面的sf)进行shapefile文件的写操作:
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.stackexchange.com/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’)
>>>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’)
4shapefile文件的编辑
4.1 Python ShapefileLibrary提供了Editor类,通过创建Editor类的对象(如下面的sf)进行shapefile文件的编辑:
4.2 编辑示例:
例1,增加一个点文件
>>>e=shapefile.Editor(shapefile=”shapefiles/test/point.shp”)
>>>e.point(0,0,10,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 ShapefileLibrary官方文档,可到其网站下载英文原版https://code.google.com/p/pyshp/
转载出处:http://blog.sina.com.cn/s/blog_b3a4f3f80101cye7.html
- pyshp读写shapefile
- shapefile格式说明及读写代码示例
- 读写ESRI Shapefile格式的C#代码
- shapefile格式说明及读写代码示例
- shapefile格式说明及读写代码示例
- shapefile格式说明及读写代码示例
- shapefile格式说明及读写代码示例
- geotools读写shapefile文件汉字乱码问题的解决方法
- shapefile格式说明及读写代码示例(VB)(转)
- C++实现shapefile文件的读写和基本功能的操作
- shapefile格式的说明及读写代码示例
- pyshp创建shp点文件
- shapefile格式说明及读写代码示例(C++)(转)
- shapefile格式说明及读写代码示例(C++)(转)
- 谈谈Shapefile
- Shapefile简述
- 关于shapefile
- 剖析Shapefile
- 排序算法
- Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件
- httpclient namevaluepair 模拟form表单
- idea javax.servlet.http.HttpServletRequest 不存在
- 作业9
- pyshp读写shapefile
- spring boot实战(第十篇)Spring boot Bean加载源码分析
- SAP-MM-移动类型解析之发货
- [待整理]
- java判断语句(instanceof)
- RelativeLayout和LinearLayout性能比较 相对布局和线性布局的性能比较
- Git冲突:commit your changes or stash them before you can merge
- spring4.0之泛型依赖注入
- #if、#ifdef、#if defined之间的区别