Select to view content in your preferred language

Script works in python win but does not in al toolbox within ArcMAP

5088
10
02-20-2014 05:35 AM
by Anonymous User
Not applicable
Original User: af.ramirez

Hi everybody...

I've been developing a quite long script, and I tested it in PyScripter an IDLE when it was finished.... well it works perfectly, so I added it to a toolbox in ArcMAP and when I ran it it did not work....

I have spent many days trying to figure out what the problem is but until now it has been imposible...

This is a piece of the code where the error comes out...

print "Startin iterations"
    print datetime.datetime.now()
    fields= ("FID", "avg_mindis", "I_conc", "Max_lenght","TamanoTerr")
    ##create UpdateCursor and iterate trough each register
    with arcpy.da.UpdateCursor(territory,  fields) as cursor:
        for row in cursor:
            currentID= row[0]
            #print currentID
            ## select current cell with current FID
           where= " FID = " + str(currentID)
            arcpy.SelectLayerByAttribute_management(territory_lyr, "NEW_SELECTION", where )

            arcpy.SelectLayerByLocation_management(eventos_lyr, "INTERSECT", territory_lyr, "" , "NEW_SELECTION")

I don't know if maybe there is a problem with the selection by attributes...

Appreciate any help..

Felipe Ramirez
GIS Analyst

[ATTACH=CONFIG]31617[/ATTACH]
0 Kudos
10 Replies
by Anonymous User
Not applicable
Original User: AdamCox

Well, you could rework the code so you don't have to use SelectByAttribute.  This could work provided that the where clause is not the problem:
territory= arcpy.GetParameterAsText(0)
path= arcpy.GetParameterAsText(1)

arcpy.MakeFeatureLayer_management(territory,"territory_lyr")
fldFID = arcpy.Describe(territory).OIDFieldName
print "ID: "+fldFID
fields= (fldFID, "avg_mindis", "I_conc", "Max_lenght","TamanoTerr")

try:
    with arcpy.da.UpdateCursor("territory_lyr", fields) as cursor:
        for row in cursor:
            currentID = row[0]
            where = "{0}={1}".format(arcpy.AddFieldDelimiters("territory_lyr", fldFID), currentID)
            print "where: "+where

            #instead of making selection, just create a new feature layer using the where clause
            arcpy.MakeFeatureLayer_management("territory_lyr","territory_lyrSELECT",where)

            #arcpy.SelectLayerByAttribute_management("territory_lyr", "NEW_SELECTION", where)
            print "Selection ok"

You could try casting the currentID as a string, though it seems redundant at this point.
0 Kudos