使用pyshp包进行shapefile文件修改的例子

yipeiwu_com6年前Python基础

由于最近在处理shp文件,想要跳出arcpy的限制,所以打算学习一下pyshp包的使用方法。在使用《Python地理空间分析指南(第2版)》的时候发现书中部分代码由于版本更新,无法运行。开贴记录踩过的雷。

这个问题是出现在5.5.4的shapefile文件修改中:

 # -*- coding:gb2312 -*-
import shapefile
import utm
 
file_path=r"C:\Users\skfzh\Documents\python地理空间分析指南\第五章\5.5.4\NYC_MUSEUMS_GEO\NYC_MUSEUMS_GEO.shp"
r=shapefile.Reader(file_path)
# print(list(r.fields))
# print(r.shapeTypeName)
#版本修改,路径和类型都要在writer里面定义
w=shapefile.Writer(r"C:\Users\skfzh\Documents\python地理空间分析指南\第五章\5.5.4\output\NYC_MUSEUMS_UTM",\
          shapeType=r.shapeType)
w.fields=list(r.fields[1:])
for rec in r.iterShapeRecords():#新版本已经删除了w.records 
  # print(*rec.record)
  w.record(*rec.record)
for sha in r.iterShapes():
  # print(sha.points[0])
  lon,lat=sha.points[0]
  y,x,zone,band=utm.from_latlon(lat,lon)
  w.point(x,y)
 
w.close()

以上这篇使用pyshp包进行shapefile文件修改的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现批量下载图片的方法

本文实例讲述了Python实现批量下载图片的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #-*-coding:utf-8-*-' #F...

浅谈django中的认证与登录

认证登录 django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1  authenticate(**credentials)  ...

Python使用matplotlib填充图形指定区域代码示例

Python使用matplotlib填充图形指定区域代码示例

本文代码重点在于演示Python扩展库matplotlib.pyplot中fill_between()函数的用法。 import numpy as np import matplot...

Python读取stdin方法实例

Python读取stdin方法实例

Python中常用到的两种标准化输入方式:分别sys.stdin和input,两者使用方式大致相同,但是总的来说sys.stdin使用方式更加多样化一些,下面就例子说明两者之间的使用差别...

Python发送http请求解析返回json的实例

python发起http请求,并解析返回的json字符串的小demo,方便以后用到。 #! /usr/bin/env python # -*- coding:gbk -*-...