Select to view content in your preferred language

problem with SelectLayerByAttribute management in script

882
3
Jump to solution
02-19-2014 09:24 AM
rafaelvargas
Emerging Contributor
hello my problem is that this tool works perfectly in IDLE or PyScripter programs, but if I use in the script in ArcGIS desktop this problem occurs: arcgisscripting.ExecuteError:ERROR 000582...

why not executing in Arcgis script and the other progran yes?

the tool SelectLayerByAttribute management is inside the arcpy.da.UpdateCursor

this is my code:

import arcpy import datetime arcpy.env.overwriteOutput = True  territory= arcpy.GetParameterAsText(0) path= arcpy.GetParameterAsText(1) territory_lyr=  path+ "territorios_lyr"  arcpy.MakeFeatureLayer_management(territory,territory_lyr) fields= ("FID", "avg_mindis", "I_conc", "Max_lenght","TamanoTerr") with arcpy.da.UpdateCursor(territory,  fields) as cursor:     for row in cursor:         currentID= row[0]         where= "FID="+str(currentID)         arcpy.SelectLayerByAttribute_management(territory_lyr, "NEW_SELECTION",where)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor
Hi Rafael,

Is the error message pointing to a specific line in the code? What is the exact content of the parameters?

If I understand this correctly you create a feature layer and for each row you create a selection of that row, using the FID. I suppose there is more code which is not included in your post. What do you want to achieve with your code (what happens after the selection is created)?

A few pointers:


  • In your code you concatenate the path with a name "territorios_lyr". If your path is a true path,  the result will be missing the trailing slash at the end of the path. It is better to use os.path.join(path, "territorios_lyr") , this will require import os

  • However, when you create a feature layer, it resides in memory. You only specify a name (not a path) when creating the feature layer, so change the code to: territory_lyr = "territorios_lyr"

  • It is better to use "format" when combining text and numbers (for building the where clause): where = "FID={0}".format(currentID)

  • Since the where clause depends on the type of data (with respect to field delimiters) and since the name of the FID column can be derived using arcpy.Describe, you could change it to:

fldFID = arcpy.Describe(territory).OIDFieldName fields= (fldFID, "avg_mindis", "I_conc", "Max_lenght","TamanoTerr") with arcpy.da.UpdateCursor(territory,  fields) as cursor:     for row in cursor:         currentID = row[0]         where = "{0}={1}".format(arcpy.AddFieldDelimiters(territory, fldFID), currentID)         arcpy.SelectLayerByAttribute_management(territory_lyr, "NEW_SELECTION", where)         # do something with the selection...


Kind regards,

Xander

View solution in original post

0 Kudos
3 Replies
XanderBakker
Esri Esteemed Contributor
Hi Rafael,

Is the error message pointing to a specific line in the code? What is the exact content of the parameters?

If I understand this correctly you create a feature layer and for each row you create a selection of that row, using the FID. I suppose there is more code which is not included in your post. What do you want to achieve with your code (what happens after the selection is created)?

A few pointers:


  • In your code you concatenate the path with a name "territorios_lyr". If your path is a true path,  the result will be missing the trailing slash at the end of the path. It is better to use os.path.join(path, "territorios_lyr") , this will require import os

  • However, when you create a feature layer, it resides in memory. You only specify a name (not a path) when creating the feature layer, so change the code to: territory_lyr = "territorios_lyr"

  • It is better to use "format" when combining text and numbers (for building the where clause): where = "FID={0}".format(currentID)

  • Since the where clause depends on the type of data (with respect to field delimiters) and since the name of the FID column can be derived using arcpy.Describe, you could change it to:

fldFID = arcpy.Describe(territory).OIDFieldName fields= (fldFID, "avg_mindis", "I_conc", "Max_lenght","TamanoTerr") with arcpy.da.UpdateCursor(territory,  fields) as cursor:     for row in cursor:         currentID = row[0]         where = "{0}={1}".format(arcpy.AddFieldDelimiters(territory, fldFID), currentID)         arcpy.SelectLayerByAttribute_management(territory_lyr, "NEW_SELECTION", where)         # do something with the selection...


Kind regards,

Xander
0 Kudos
rafaelvargas
Emerging Contributor
Xander Bakker hello, thanks for your response
but look, I made your sugerences and the code works in programs Python (PyScripter) but if I run in ArcToolbox scrpit of Arcgis Desktop not

Attached a iamgenes of results from the study in the two cases:

1) with ArcToolbox scrpit of Arcgis Desktop  and I am print the error in the line select by atribute [ATTACH=CONFIG]31616[/ATTACH]
2) with program PyScripter work well]
0 Kudos
rafaelvargas
Emerging Contributor
xander_bakker;367296 wrote:
Hi Rafael,....

Xander31619[/ATTACH]
2) with program PyScripter work well [ATTACH=CONFIG]31618[/ATTACH]
0 Kudos