天天看點

ArcPy資料處理應用之批量字段指派

該任務要求從文本檔案中讀取配置資訊,根據配置資訊中的圖層名和新舊字段值對應情況,批量将取值為舊值的要素相應字段賦以新值。

如,A圖層BM字段有唯一值11、12、13,其中13需要修改為15,配置檔案中儲存有相應的配置項即“A,13,15”,其他唯一值11、12的資料不修改。

ArcPy處理代碼如下:

... n = 0
... arcpy.env.workspace=r'D:/data.gdb'
... f = open(r'D:/T4_CodeMap.txt','r')
... for line in f.readlines():
...     n = n + 1
...     data = line.strip().split(',')
...     if len(data)==3 and arcpy.Exists(data[0]):
...         exp = u"{0} if !YSDM!=='{1}' else !YSDM!".format(data[2],data[1])
...         print u"{0} Cal: {1}, exp: {2}".format(n, data, exp)
...         arcpy.CalculateField_management(data[0],"YSDM",exp,"PYTHON_9.3")
...     else:
...         print u"{0}, Skip".format(n)
... f.close()
           

讀取到的配置檔案内容(截取部分)如下:

CSLDX_KDCSG,810304,810304102
CSLDX_KDCSG,820100,820100002
CSLDX_KDCSG,820200,820200002
DLJ_KDCSG,330101011,330101012
DLJ_KDCSG,380403003,380403002
DLJ_KDCSG,380506023,380506022
DLJ_KDCSG,810302013,810302012
DLJ_KDCSG,820200013,820200012
GDM_KDCSG,810100002,810100003
GDM_KDCSG,810300,810300003
GDM_KDCSG,810301,810301003
GDM_KDCSG,810302,810302003
GDM_KDCSG,810303,810303003
GDM_KDCSG,810304,810304003
GLM_KDCSG,40600013,406000133
GLM_KDCSG,420201034,420201033
GLM_KDCSG,420301014,420301013
GLM_KDCSG,420500014,420500003
GLM_KDCSG,420600014,420600013
           

繼續閱讀