带有if语句的Python for循环

时间:2013-07-26 20:11:39

标签: python logic arcgis calculated-columns arcpy

此代码将用于附加了栅格单元格数的多边形数据。例如,如果栅格是土地覆盖,则每个土地覆盖类型将有一列,每个多边形内有相应的细胞计数。此输出来自地理空间建​​模环境,因为Tabulate Raster在我正在使用的大型shapefile上不断崩溃。最终,我想要一个区域值,而不是细胞计数,这是下面的代码将实现的。

因此,代码将循环遍历shapefile属性,拉出原始计数字段,然后根据用户输入创建新字段(使用用户输入名称+栅格值循环通过AddField获取新字段名称),然后循环原始字段的值并计算新字段的用户指定区域值。基本上我正在尝试自动化添加字段,计算字段模型,我通常在模型构建器中批处理。

我无法找出计算新字段的正确逻辑。正如现在所写,它获取带有for循环的原始字段,但最后只使用最后的原始字段值,并且仅填充最后一个新字段。我需要循环来获取第一个原始字段并将其放入相应的新字段中,例如:

如果origFields =('NLCDV1','NLCDV2'......) 和addedFields =('KM2_LC1','KM2_LC2'...) 然后addField计算将以适当的值结束: 'KM2_LC1'=转换(其中convert =!NLCDV1!* cell) 对于可能存在的所有值,'KM2_LC2'=转换(convert =!NLCDV2!* cell)等等。

#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的新手,也是拉链的新手,所以我不喜欢知道如何使用它们。

我们非常感谢任何有关逻辑的帮助,欢迎任何关于清理/重新订购的建议。就像我说的那样,我很高兴这一切都说完了。一旦我掌握了逻辑,我就会好起来!感谢您的关注,如果我的代码过于复杂,那就太遗憾了(如果是这样的话,也不会感到惊讶)。

1 个答案:

答案 0 :(得分:1)

我(仍然)不确定我明白你在问什么,但也许这会有所帮助:

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'
相关问题