rdcrawfo

Can???t get this python script to work from a layer with selection in ArcMap

Discussion created by rdcrawfo on Jan 31, 2012
Latest reply on Jan 31, 2012 by curtvprice
This script works as a Feature Class from ArcCatalog and ArcMap. I would like to get it to work as a Layer with a selection set from ArcMap. Would like to be able to run the loop in this script on a small set of selected records and not have to run it on the entire Feature Class every time. What the script does is calibrate the M values of the routed lines with two attribute fields ???BMP??? (beginning mile post) and ???EMP??? (ending mile post). There are two other fields getting updated to track the status and date of the calibration. When I run this script on a layer ???SelectLayerByAttribute_management??? gives an error ???list index out of range???. How can I get it to run on the selected features in ArcMap? I am running the script from ArcToolBox. The input perameter is set as "Feature Layer". It lets me browes to a Feature Class or select a Layer from ArcMap. I want to be able to use the Layer so I can also use the selection.

Rick Crawford
United States Forest Service, Southwest Region

# Calibrates routes for Foads or Trails from BMP and EMP attributes
import arcpy
import datetime
arcpy.env.overwriteOutput=1
# Edit next Line for input
inFC = arcpy.GetParameterAsText(0)
toField = "BMP" 
fromField = "EMP" 
actField = "RT_ACTION"
dateField = "ACTIONSTAMP"
desc = arcpy.Describe(inFC)
shapeField = desc.shapeFieldName
oidField = desc.OIDFieldName
oidQueryName = arcpy.AddFieldDelimiters(inFC, oidField)
##  print oidQueryName
g = arcpy.Geometry()
#Create update cursor on input feature class
rows = arcpy.UpdateCursor(inFC)
#Make feature layer from input feature class
arcpy.MakeFeatureLayer_management(inFC, "featureLayer")
#loop through rows in feature layer and select each OID
for row in rows:
  arcpy.SelectLayerByAttribute_management("featureLayer", "New_Selection", oidQueryName + " = " + str(row.getValue(oidField)))
  #create geometry object from each feature row
  geomList = arcpy.CreateRoutes_lr("featureLayer", toField, g, "TWO_FIELDS", fromField, toField)
  #update shape field for input feature class for each row (replace M values)
  row.setValue(shapeField, geomList[0])
  row.setValue (actField, "CALIBRATED")
  row.setValue(dateField, datetime.datetime.now())
  rows.updateRow(row)
arcpy.Delete_management("featureLayer")
del row, rows

Outcomes