该代码将用于附加了栅格像元计数的面数据。例如,如果栅格是土地覆盖,则每个土地覆盖类型都会有一列,并且每个多边形内会有对应的像元计数。此输出来自地理空间建模环境,因为制表栅格一直使我正在使用的大型shapefile崩溃。最终,我想要一个面积值,而不是一个单元格数,这是下面的代码将实现的。
因此,代码将遍历shapefile属性,提取原始计数字段,然后根据用户输入创建新字段(使用用户输入名称+栅格值通过AddField进行循环以获取新的字段名称),然后遍历原始字段的值,并为新字段计算用户指定的面积值。本质上,我试图自动执行通常在模型开发器中批处理的“添加字段”,“计算字段”模型。
我在弄清楚用于计算新字段的正确逻辑时遇到了麻烦。就像现在写的那样,它使用for循环获取原始字段,但是最终仅使用了最后的原始字段值,并且仅填充了最后的新字段。我需要循环以获取第一个原始字段并将其放入相应的新字段中,例如:
如果origFields =('NLCDV1','NLCDV2'...)并且addedFields =('KM2_LC1','KM2_LC2'...),则addField计算将以适当的值结束:'KM2_LC1'= convert(where convert =!NLCDV1!*单元)'KM2_LC2'=转换(转换=!NLCDV2!*单元),依此类推,对于可能存在的所有值。
#User inputs the desired final units, loop through to find the desired units and calculates the new fields.
unit = arcpy.GetParameterAsText(7) #Must be: SqMeter, SqKm, Acres, Hectares, SqMi, or SqFt.
#User must know original units of raster, must be in Meters or Foot_US!!!
for field in origFields:
if rastunit == "Meter":
#To make square meters final area unit.
if unit == "SqMeter":
arcpy.CalculateField_management(inputPoly, addField, convert, "PYTHON_9.3")
#To convert square meters into Square Kilometers.
elif unit == "SqKm":
arcpy.CalculateField_management(inputPoly, addField, convertsqmsqkm, "PYTHON_9.3")
#To convert square meters into Acres.
elif unit == "Acres":
arcpy.CalculateField_management(inputPoly, addField, convertsqmac, "PYTHON_9.3")
#To convert square meters into Hectares.
elif unit == "Hectares":
arcpy.CalculateField_management(inputPoly, addField, convertsqmhec, "PYTHON_9.3")
#To convert square meters into Square Miles.
elif unit == "SqMi":
arcpy.CalculateField_management(inputPoly, addField, convertsqmsqmi, "PYTHON_9.3")
#To convert square meters into Square Feet.
elif unit == "SqFt":
arcpy.CalculateField_management(inputPoly, addField, convertsqmsqft, "PYTHON_9.3")
else:
print arcpy.AddWarning("Ineligible unit provided.")
elif rastunit == "Foot_US":
#To make square feet final area unit.
if unit == "SqFt":
arcpy.CalculateField_management(inputPoly, addField, convert, "PYTHON_9.3")
else:
print "This raster has the following units:" +rastunit+ ". If not in Foot_US or Meters, please reproject the raster."
我只需要弄清楚如何获取对应的原始字段以匹配新添加的字段。我正在考虑使用zip(origField,addedField,calcs)之类的东西。当我这样做时,它给了我以下输出:
(u'NLCDV1', 'KM2_LC1', '!NLCDV1! * 900.0')
(u'NLCDV2', 'KM2_LC2', '!NLCDV2! * 900.0')
(u'NLCDV3', 'KM2_LC3', '!NLCDV3! * 900.0')
(u'NLCDV4', 'KM2_LC4', '!NLCDV4! * 900.0')
(u'NLCDV5', 'KM2_LC5', '!NLCDV5! * 900.0')
(u'NLCDV7', 'KM2_LC7', '!NLCDV7! * 900.0')
(u'NLCDV8', 'KM2_LC8', '!NLCDV8! * 900.0')
(u'NLCDV9', 'KM2_LC9', '!NLCDV9! * 900.0')
这些行正是我所需要的,但是我不确定是否可以使用这样的输出来填充字段,即使可以使用它们,我对python还是zip还是陌生的,所以我不知道如何使用它们。
逻辑方面的任何帮助将不胜感激,欢迎您提出任何有关清理/重新排序的建议。就像我说的那样,当这一切都说完了的时候,我相当新手。一旦有了逻辑,我会很高兴!感谢您的查看,如果我的代码过于复杂(如果是这种情况,也不会感到惊讶),则对不起。
解决方案
我(仍然)不确定我是否理解您的要求,但这也许会有所帮助:
origFields = (u'NLCDV1', u'NLCDV2', u'NLCDV3')
addedFields = ('KM2_LC1', 'KM2_LC2', 'KM2_LC3')
calcs = ('!NLCDV1! * 900.0', '!NLCDV2! * 900.0', '!NLCDV3! * 900.0')
for orig, added, calc in zip(origFields, addedFields, calcs):
print '{!r}, {!r}, {!r}'.format(orig, added, calc)
输出:
u'NLCDV1', 'KM2_LC1', '!NLCDV1! * 900.0'
u'NLCDV2', 'KM2_LC2', '!NLCDV2! * 900.0'
u'NLCDV3', 'KM2_LC3', '!NLCDV3! * 900.0'