python中pyshp_使用pyshp处理shapfile

在平时的数据处理中,经常会遇到将字符串或JSON格式数据转成空间数据的需求,使用ArcGIS Engine 或者shapelib单独写个应用程序比较麻烦,需要开发环境。编译,调试等等。那么使用python 直接操作shapefile 是一个比较好的选择。

pyshp 下载地址:

pyshp的使用比较简单

1.下载pyshp,将其放到python的根目录下(当然也可以通过设置path等方式来引用)

2.import shapefile

3.对shapefile进行操作

Reading Points in Shapes

>>> import shapefile

>>> sf = shapefile.Reader("shapefiles/blockgroups")

>>> shapes = sf.shapes()

>>> # Read the bounding box from the 4th shape

>>> shapes[3].bbox

[-122.485792, 37.786931000000003, -122.446285, 37.811019000000002]

>>># Read the 8th point in the 4th shape

>>> shapes[3].points[7]

[-122.471063, 37.787402999999998]

Reading Database Attributes

>>> # Read the field descriptors for the database file

>>> sf.fields

[("DeletionFlag", "C", 1, 0), ["AREA", "N", 18, 5],

... ["BKG_KEY", "C", 12, 0], ["POP1990", "N", 9, 0], ["POP90_SQMI", "N", 10, 1],

... ["HOUSEHOLDS", "N", 9, 0],

... ["MALES", "N", 9, 0], ["FEMALES", "N", 9, 0]]

>>> # Read the 2nd and 3rd field values of the 4th database record

>>> sf.records[3][1:3]

['060750601001', 4715]

Writing Shapefiles

>>> import shapefile

>>> # Make a point shapefile

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

>>> w.point(90.3, 30)

>>> w.point(92, 40)

>>> w.point(-122.4, 30)

>>> w.point(-90, 35.1)

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

>>> # Create a polygon shapefile

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值