PYTHON-arcgis的开发:批量操作字段

#添加arcpy包,这个步骤必不可少
import arcpy
… from arcpy import env
… env.workspace = “D:/资料集/123.gdb”
##########################################################################
… fcList = arcpy.ListFeatureClasses()
… for sf in fcList:
##########################################################################
…     arcpy.AddField_management(sf, “date”, “TEXT”, 9, “”, “”, “发震日期”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “time”, “TEXT”, 9, “”, “”, “发震时刻”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “name”, “TEXT”, 18, “”, “”, “地震名称”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “major_axis”, “LONG”, 9, “”, “”, “长轴长度”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “minor_axis”, “LONG”, 9, “”, “”, “短轴长度”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “direction”, “TEXT”, 9, “”, “”, “长轴方向”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “epicenter”, “TEXT”, 9, “”, “”, “烈度”, “NULLABLE”, “NON_REQUIRED”)
…     arcpy.AddField_management(sf, “area”, “TEXT”, 9, “”, “”, “面积”, “NULLABLE”, “NON_REQUIRED”)
##########################################################################
#python的强制类型转换比较容易即str(a)将a转换为string型变量
#这里可以着重注意if语句的格式,以及elif的特殊写法
…     fields = [‘方位角’]
…     with arcpy.da.UpdateCursor(sf,fields) as cursor:
…         for row in cursor:
…             if len(row[0]) == 4:l = long(row[0][1:3])
…             elif len(row[0]) == 3:l = long(row[0][1:2])
…             elif len(row[0]) == 1:l = -2
…             elif len(row[0]) == 2:l = -1
…             if l > 45:
…                 if l < 90:x = 2
…             elif l < 45:
…                 if l > 0:x = 3
…             elif l == 45:l = -1
…             cursor.updateRow(row)
…     if l == -1:arcpy.CalculateField_management(sf,”direction”,”!方位角![:1]+!方位角![-1:]”,”PYTHON_9.3″)
…     elif x == 2:arcpy.CalculateField_management(sf,”direction”,”!方位角![:1]+!方位角![-1:]+!方位角![-1:]”,”PYTHON_9.3″)
…     elif x == 3:arcpy.CalculateField_management(sf,”direction”,”!方位角![:1]+!方位角![:1]+!方位角![-1:]”,”PYTHON_9.3″)
…     elif l == -2:arcpy.CalculateField_management(sf,”direction”,”!方位角![:1]”,”PYTHON_9.3″)
##############################
#根据l值选择不同arcpy.CalculateField_management()操作方式的局限性太大,系统无法根据row[0]中的每个值进行判断及操作,而是len(row[0])出了不同值之后,l的值
是依据最后一个len值产生的,导致计算字段时的方法选择总是根据字段中最后一个字段值的len操作的出数值进行,而出现大面积错误,故我直接在row[0]操作时候更新字段
值,再将更新过的方位角字段通过计算字段赋值给direction字段。方法如下:
…     fields = [‘方位角’]
…     with arcpy.da.UpdateCursor(sf,fields) as cursor:
…         for row in cursor:
…             if len(row[0]) == 4:l = long(row[0][1:3])
…             elif len(row[0]) == 3:l = long(row[0][1:2])
…             elif len(row[0]) == 1:row[0] = row[0]
…             elif len(row[0]) == 2:row[0] = row[0]
…             if l > 45:
…                 if l < 90:
…                     if len(row[0]) == 3:row[0] = row[0][0]+row[0][2]+row[0][2]
…                     elif len(row[0]) == 4:row[0] = row[0][0]+row[0][3]+row[0][3]
…             elif l < 45:
…                 if l > 0:
…                     if len(row[0]) == 3:row[0] = row[0][0]+row[0][0]+row[0][2]
…                     elif len(row[0]) == 4:row[0] = row[0][0]+row[0][0]+row[0][3]
…             elif l == 45:row[0] = row[0][0] + row[0][3] 
…             cursor.updateRow(row)
…     arcpy.CalculateField_management(sf,”direction”,”!方位角!”,”PYTHON_9.3″)
############################################################################################################################################################以下部分是对字段中的每个值进行操作,而不是整个字段(取值,而不是笼统的!字段名!对整个字段进行操作)
#对字段值进行操作:举个例子
#fields = [‘字段0′,’字段1’]
#row[0] 即为字段0的值进行操作,而row[1]即为对字段1的值进行操作,以此类推
#cursor.updateRow(row)更新字段值
…     fields = [‘direction’]
…     with arcpy.da.UpdateCursor(sf,fields) as cursor:
…         for row in cursor:
#这里我用了字符串的置换命令,row[0].replace(‘a’,u’是’)表示将row[0]字符串中的’a’转换为单个中文只占用一个字节的中文字符’是’
…             row[0] = row[0].replace(‘N’,u’北’)
…             row[0] = row[0].replace(‘S’,u’南’)
…             row[0] = row[0].replace(‘W’,u’西’)
…             row[0] = row[0].replace(‘E’,u’东’)
…             cursor.updateRow(row)
###########################################################################
#arcpy的底层语言可能不是python语言
#a = ‘a’ a = “a” a = “‘a'”python语言都可以识别,他对符号的判断不是很严格
#arcpy中每个参数都要严格使用””
…     arcpy.CalculateField_management(sf,”date”,”!时间!”,”PYTHON_9.3″)
…     arcpy.CalculateField_management(sf,”name”,”!地点!+str(‘!震级!’)+u’级地震'”,”PYTHON_9.3″)
…     arcpy.CalculateField_management(sf,”major_axis”,”!长轴!”,”PYTHON_9.3″)
…     arcpy.CalculateField_management(sf,”minor_axis”,”!短轴!”,”PYTHON_9.3″)
…     arcpy.CalculateField_management(sf,”epicenter”,”!烈度!”,”PYTHON_9.3″)
…     arcpy.CalculateField_management(sf,”area”,”!面积!”,”PYTHON_9.3″)
…     arcpy.DeleteField_management(sf,[“烈度”, “时间”, “地点”, “震级”, “深度”, “死亡”, “受伤”, “经济损失”, “长轴”, “短轴”, “面积”, “方位角”])

###########################################################################################
#python语言对格式的要求非常严格
#不同层次的语句要严格按照各自层次进行行缩进
#如一层次与其子母层次各有4个空格的缩进差
#字符串中,u+”中文”每个中文字符占1个字节,”中文”每个中文字符占3个字节
############################################################################################
#row[0]对字段值进行更新的操作在优先级上明显优先于计算字段的多步赋值操作

转载自:https://blog.csdn.net/qq_33409778/article/details/52143088

You may also like...