简体   繁体   English

使用添加字段和字段计算器的Python for ArcGIS for循环

[英]For loop with Python for ArcGIS using Add Field and Field Calculator

I'll try to give a brief background here. 我会在这里简要介绍一下背景。 I recently received a large amount of data that was all digitized from paper maps. 我最近收到了大量的数据,这些数据全部从纸质地图上进行了数字化处理。 Each map was saved as an individual file that contains a number of records (polygons mostly). 每个地图都保存为一个单独的文件,其中包含许多记录(大多数为多边形)。 My goal is to merge all of these files into one shapefile or geodatabase, which is an easy enough task. 我的目标是将所有这些文件合并到一个shapefile或地理数据库中,这很容易。 However, other than spatial information, the records in the file do not have any distinguishing information so I would like to add a field and populate it with the original file name to track its provenance. 但是,除了空间信息之外,文件中的记录没有任何区别性信息,因此我想添加一个字段,并使用原始文件名填充该字段以跟踪其出处。 For example, in the file "505_dmg.shp" I would like each record to have a "505_dmg" id in a column in the attribute table labeled "map_name". 例如,在文件“ 505_dmg.shp”中,我希望每个记录在属性表中标记为“ map_name”的列中都有一个“ 505_dmg” ID。 I am trying to automate this using Python and feel like I am very close. 我正在尝试使用Python自动执行此操作,并且感觉非常接近。 Here is the code I'm using: 这是我正在使用的代码:

# Import system module
import arcpy
from arcpy import env
from arcpy.sa import *

# Set overwrite on/off
arcpy.env.overwriteOutput = "TRUE"

# Define workspace
mywspace = "K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test"
print mywspace

# Set the workspace for the ListFeatureClass function
arcpy.env.workspace = mywspace

try:
    for shp in arcpy.ListFeatureClasses("","POLYGON",""):
        print shp
        map_name = shp[0:-4]
        print map_name
        arcpy.AddField_management(shp, "map_name", "TEXT","","","20")
        arcpy.CalculateField_management(shp, "map_name","map_name", "PYTHON")
except:
     print "Fubar, It's not working"
     print arcpy.GetMessages()
else:
     print "You're a genius Aaron"

The output I receive from running this script: 我从运行此脚本收到的输出:

>>> 

K:/Research/DATA/ADS_data/Historic/R2_ADS_Historical_Maps/Digitized Data/Arapahoe/test
505_dmg.shp
505_dmg
506_dmg.shp
506_dmg
You're a genius Aaron

Appears successful, right? 看来成功了吧? Well, it has been...almost: a field was added and populated for both files, and it is perfect for 505_dmg.shp file. 好吧,它几乎已经到了:为这两个文件添加并填充了一个字段,它对于505_dmg.shp文件而言是完美的。 Problem is, 506_dmg.shp has also been labeled "505_dmg" in the "map_name" column. 问题是,506_dmg.shp也已在“ map_name”列中标记为“ 505_dmg”。 Though the loop appears to be working partially, the map_name variable does not seem to be updating. 尽管循环似乎在部分起作用,但map_name变量似乎并未更新。 Any thoughts or suggestions much appreciated. 任何想法或建议,不胜感激。

Thanks, 谢谢,

Aaron 亚伦

I received a solution from the ESRI discussion board: https://geonet.esri.com/thread/114520 我从ESRI讨论区收到了一个解决方案: https : //geonet.esri.com/thread/114520

Basically, a small edit in the Calculate field function did the trick. 基本上,在“计算”字段功能中进行小的编辑即可达到目的。 Here is the new code that worked: 这是起作用的新代码:

arcpy.CalculateField_management(shp, "map_name","\"" + map_name + "\"", "PYTHON")  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM