《Python地理空间分析指南 第2版》学习笔记-5.5Shapefile文件编辑-中

《Python地理空间分析指南 第2版》学习笔记-5.5Shapefile文件编辑-中,第1张

第5章 Python与地理信息系 5.5 Shapefile文件编辑 5.5.1-5.5.3章节见《Python地理空间分析指南 第2版》学习笔记-5.5Shapefile文件编辑-上 5.5.4 Shapefile文件修改

书中部分代码由于版本更新,无法运行,鼓捣了半天,终于好了。


(1)复制矢量文件,并加上投影
import shapefile
import utm
import urllib.request

r = shapefile.Reader(r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_GEO.shp")
print(list(r.fields))
print(r.shapeTypeName)
# w = shapefile.Writer(r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_UTM2.shp", shapeType=r.shapeType)
with shapefile.Writer(r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_UTM.shp", shapeType=r.shapeType) as w:

    # print(r.fields[1:])
    w.fields = list(r.fields[1:])
    # w.records.extend(r.records())
    for rec in r.iterShapeRecords():
        # print(rec.record[0])
        # 逐条添加数
        # 注意*list(rec.record)和list(rec.record)的区别:
        w.record(*list(rec.record))
# 投影转换
    for shape in r.iterShapes():
        # print(sha.points[0])
        lon, lat = shape.points[0]
        # 投影转换,设置坐标
        y, x, zone, band = utm.from_latlon(lat, lon)
        w.point(x, y)
print(12)

# 此外,不需要像5-4重投影示例中那样写入一个PRJ投影文件。


# 有一种简便的方法可以**创建一个PRJ文件**, # 那就是通过EPSG代码访问SpatialReference.org网站来实现。


# 从前面的示例中的时区变量可以知道,之前使用的UTM18区的EPSG代码是26918,相关代码如下: #生成投影文件 prj = urllib.request.urlopen("http://spatialreference.org/ref/epsg/26918/esriwkt/") with open(r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_UTM.prj", "w") as f: f.write(str(prj.read()))


*list(rec.record)和list(rec.record)的区别 好大一坑啊
*list(rec.record):

list(rec.record):

(2)读取Shapefile文件,然后将其拷贝,并添加一个多边形保存。


import shapefile

file_name = r"ep202009.026_5day_pgn\ep202009.026_5day_pgn.shp"
r = shapefile.Reader(file_name)

# 拷贝shapefile文件到 写入对象 中
with shapefile.Writer(r"ep202009.026_5day_pgn\test2", r.shapeType) as w:
    # 复制现有的字段
    w.fields = list(r.fields)
    # 复制现有的记录
    for rec in r.records():
        w.record(list(rec))
    # 复制现有的矩形
    for s in r.shapes():
        w._shapeparts(parts=[s.points], shapeType=s.shapeType)
    # 添加一个矩形
    w.poly([[[-104, 24], [-104, 25], [-103, 25], [-103, 24], [-104, 24]]])
    # 添加一个新的数据
    w.record("STANLEY", "TD", "091022/1500", "27", "21", "48", "ep")

添加一个矩形并增加一条记录:

(3)添加投影字段,并把投影坐标写入到记录中
import shapefile

filename1 = r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_UTM444.shp"
r = shapefile.Reader(filename1)

filename2 = r"NYC_MUSEUMS_GEO\NYC_MUSEUMS_UTM555.shp"
with shapefile.Writer(filename2,r.shapeType) as w:
    # 复制原有的字段
    # w.fields = list(r.fields)
    w.fields = list(r.fields[1:])
    # 添加新字段,添加一个最大长度为8、小数精度为5位的浮点型字段
    w.field("LAT","F",8,5)
    w.field("LON","F",8,5)

    for i in range(r.numRecords):
        lon, lat = r.shape(i).points[0]
        #设置点的投影坐标
        w.point(lon, lat)
        #逐条记录添加,并添加投影坐标
        w.record(*list(r.record(i)), lat, lon)

NYC_MUSEUMS_UTM444.shp 的属性表:

NYC_MUSEUMS_UTM555.shp 的属性表:


结束语

《Python地理空间分析指南 第2版》学习笔记,仅供学习,如有侵权请联系删除。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/570388.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-09
下一篇2022-04-09

发表评论

登录后才能评论

评论列表(0条)

    保存