2 Star 4 Fork 3

汉塞大叔/Geospatial_Analysis_By_Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2.4 为已有shp添加字段.py 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
汉塞大叔 提交于 2021-01-20 19:53 . 上传 py
import shapefile
r = shapefile.Reader("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_UTM")
w = shapefile.Writer("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_02")
geo = shapefile.Reader("shapefiles/NYC_MUSEUMS_GEO")
# 1.添加字段
w.fields = list(r.fields[1:])
w.field("GEO_LAT", "F", 5, 8)
w.field("GEO_LON", "F", 5, 8)
# [('DeletionFlag', 'C', 1, 0),
# ['NAME', 'C', 254, 0],
# ['TEL', 'C', 254, 0],
# ['URL', 'C', 254, 0],
# ['ADRESS1', 'C', 254, 0],
# ['ADDRESS2', 'C', 254, 0],
# ['CITY', 'C', 254, 0],
# ['ZIP', 'N', 19, 8]]
# 2.复制记录
for i in range(geo.numRecords):
lon, lat = geo.shape(i).points[0]
shapeRecord = r.record(i)
# 方法 1:
shapeRecord.extend([lat, lon])
w.record(*shapeRecord)
# 方法 2:
# w.record(NAME=shapeRecord[0], TEL=shapeRecord[1], URL=shapeRecord[2],
# ADRESS1=shapeRecord[3], ADDRESS2=shapeRecord[4], CITY=shapeRecord[5],
# ZIP=shapeRecord[6], GEO_LAT=lat, GEO_LON=lon)
# 3.复制图形
for item in r.iterShapes():
x, y = item.points[0]
w.point(x, y)
print(r.record(0))
w.close()
r.close()
# 验证添加的字段
r = shapefile.Reader("shapefiles/NYC_MUSEUMS_02")
print(r)
print(r.record(0))
r.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/fengfeng233/geospatial_-analysis_-by_-python.git
git@gitee.com:fengfeng233/geospatial_-analysis_-by_-python.git
fengfeng233
geospatial_-analysis_-by_-python
Geospatial_Analysis_By_Python
master

搜索帮助