Select to view content in your preferred language

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

5098
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

Hi Felipe, can you describe the error that you get?  Are you using any error handling?
0 Kudos
Andres_FelipeRamirez_Mejia
Deactivated User
Hi....

I have a try - Except sentence starting before this line with arcpy.da.UpdateCursor(territory, fields) as cursor:

and the error I get is this
0 Kudos
by Anonymous User
Not applicable
Original User: AdamCox

hmm, if you import the sys and traceback modules, you can add this code in the except statement:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     "\
                + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
print pymsg
print msgs

Hopefully you can get more detailed information about what's going on.
0 Kudos
Andres_FelipeRamirez_Mejia
Deactivated User
Hi Adam...

I added the code you gave me and the new nessages didn't show new info about what's going on...

look at the messages...

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

Ok, I don't know the earlier parts of your code, but perhaps there is an issue with how you are creating the layers and/or getting them from the tool parameters... assuming you are running it through a tool dialogue box.  It may also have to do with the parameter types you are using in the dialogue box.  Can you pass that information along?
0 Kudos
Andres_FelipeRamirez_Mejia
Deactivated User
Hi Adam...

Here is an example of the code...with some modifications

import arcpy
import datetime
import os
import sys
import traceback
arcpy.env.overwriteOutput = True

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,  fields) as cursor:
        for row in cursor:
            currentID = row[0]
            where = "{0}={1}".format(arcpy.AddFieldDelimiters(territory, fldFID), currentID)
            print "where: "+where
            arcpy.SelectLayerByAttribute_management("territory_lyr", "NEW_SELECTION", where)
            print "Selection ok"
except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     "\
                    + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    print pymsg
    print msgs

and the error is....[ATTACH=CONFIG]31637[/ATTACH]
0 Kudos
by Anonymous User
Not applicable
Original User: af.ramirez

Hi Adam...

Here is an example of the code...with some modifications

import arcpy
import datetime
import os
import sys
import traceback
arcpy.env.overwriteOutput = True

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,  fields) as cursor:
        for row in cursor:
            currentID = row[0]
            where = "{0}={1}".format(arcpy.AddFieldDelimiters(territory, fldFID), currentID)
            print "where: "+where
            arcpy.SelectLayerByAttribute_management("territory_lyr", "NEW_SELECTION", where)
            print "Selection ok"
except Exception as e:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     "\
                    + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    print pymsg
    print msgs

and the error is....[ATTACH=CONFIG]31637[/ATTACH]


and the parameters are...
0 Kudos
by Anonymous User
Not applicable
Original User: AdamCox

I may have found the error: in the code you posted, you need to have "territory_lyr" as the first parameter in AddFieldDelimiters, right now you have the territory string, so that is probably causing an issue with the query.  It would be good to use "territory_lyr" in the Describe method as well.
0 Kudos
Andres_FelipeRamirez_Mejia
Deactivated User
Hi Adam...

I tried the things you suggested but the didn't work...

The same error message comes out...

I don't know what else I can do....
0 Kudos