Still having problems stepping through a list inside of a tool

1549
20
04-05-2012 12:14 PM
JohnNunnali
New Contributor
I had started a thread about a month ago on this same topic but want to start again at the beginning.

I am attempting to iterate through a list inside of the SelectLayerByAttributes tool in ArcPy.  My goal is to be able to select attributes from a field (column), export the current map view with the selected attributes as a jpeg, clear the selection, and then start the process again for a different field.  I am currently trying to run all three steps within a loop.  My issue is that I can not figure out how to make the SelectLayerByAttributes tool step to the next field name after the first completes.  Currently the loop runs but just keeps selecting the values from the initial field and not changing to the next in the list.  I have attached an example of how the data is setup.


I am trying to select for values greater than zero for each column, then export the results, clear the selection, and them move to next column.


The code currently looks like this:
#Script to create distribution maps for wetland species
#Import arcpy and set workspace
import arcpy
import arcpy.mapping
arcpy.env.workspace = r"P:\CoWetlandTools\maps\distribution_maps_Johns_pytest"
import arcpy.sa
#generate a list
speciesList =("Achillea_millefolium", "Acroptilon_repens", "Actinea_osterhoutii")

#Create Loop and List to run processing for
for species in speciesList:

  #select counties each species is known to occur  
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", "'Achillea_milefolium' > '0'")

  #Export selected to jpeg
  mxd = arcpy.mapping.MapDocument(r"CURRENT")
  
  arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\Achillea_milefolium.jpg")

  #clear selected attributes
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "CLEAR_SELECTION")

#finished
print "Finished"


Where I need help is in the where clause of the arcpy.SelectLayerByAttribute_management function and the arcpy.mapping.ExportToJPEG function.



Any suggestions are greatly appreciated.
Tags (2)
0 Kudos
20 Replies
DarrenWiens2
MVP Honored Contributor
You need to feed in the variable (species) value.

for species in speciesList:

  #select counties each species is known to occur  
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", " ' " + species + " ' > '0'")

and...
arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\" + species + ".jpg")
0 Kudos
JohnNunnali
New Contributor
Looks like that may be a step in the right direction.  However when I run the script with the recommended changes it is now selecting all features and also giving me the following error:

Runtime error <type 'exceptions.AttributeError'>: PageLayoutObject: Error in executing ExportToJPEG

With the suggested changes here is how the code looks:
#Script to create distribution maps for wetland species
#Import arcpy and set workspace
import arcpy
import arcpy.mapping
arcpy.env.workspace = r"P:\CoWetlandTools\maps\distribution_maps_Johns_pytest"
import arcpy.sa
#generate a list
speciesList =("Achillea_millefolium", "Acroptilon_repens", "Actinea_osterhoutii")

#Create Loop and List to run processing for
for species in speciesList:

  #select counties each species is known to occur  
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", " ' " + species + " ' > '0'")

  #Export selected to jpeg
  mxd = arcpy.mapping.MapDocument(r"CURRENT")
  
  arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\" + species " + ".jpg")

  #clear selected attributes
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "CLEAR_SELECTION")

#finished
print "Finished"


I added another double quote to the ExportToJPEG function because I received a parsing error when testing the script.

Any thought on why it is selecting all features and not just the desired features?
0 Kudos
DarrenWiens2
MVP Honored Contributor
Numbers don't get quotes in SQL. And we had the quotes wrong anyways: field name gets double quotes, the whole thing is in single quotes.

arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", ' " ' + species + ' " > 0')


I'm pretty sure this is right (you have to have an even number of matching quotes):
arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\" + species + ".jpg")
0 Kudos
JohnNunnali
New Contributor
This time I received the following error:

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

#Script to create distribution maps for wetland species
#Import arcpy and set workspace
import arcpy
import arcpy.mapping
arcpy.env.workspace = r"P:\CoWetlandTools\maps\distribution_maps_Johns_pytest"
import arcpy.sa
#generate a list
speciesList =("Achillea_millefolium", "Acroptilon_repens", "Actinea_osterhoutii")

#Create Loop and List to run processing for
for species in speciesList:

  #select counties each species is known to occur  
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", ' " ' + species + ' " > 0')

  #Export selected to jpeg
  mxd = arcpy.mapping.MapDocument(r"CURRENT")
  
  arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\' + species + '.jpg")

  #clear selected attributes
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "CLEAR_SELECTION")

#finished
print "Finished"


Should I be using

arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", ' " ' + speciesList + ' " > 0')


instead?


Never mind.  Can't concatenate str and list or str and tuple objects.
0 Kudos
DarrenWiens2
MVP Honored Contributor
What kind of file are you trying to select from?

Your workspace is set to: "P:\CoWetlandTools\maps\distribution_maps_Johns_pytest"
The file is: "counties_edited_datamergetest"
...which amounts to a file called "P:\CoWetlandTools\maps\distribution_maps_Johns_pytest\counties_edited_datamergetest"
...which is not any sort of feature class I know of.

If it's inside a geodatabase, that should be in your workspace (e.g. "P:\CoWetlandTools\maps\distribution_maps_Johns_pytest.gdb" if that's what it's called).
If it's a shapefile, that should be in the file name (e.g. "counties_edited_datamergetest.shp").
0 Kudos
JohnNunnali
New Contributor
I'm selecting from a shapefile in an mxd.

I tried adding .shp to the file name but get the following runtime error which I did not encounter before. 

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000732: Layer Name or Table View: Dataset counties_edited_datamergetest.shp does not exist or is not supported

I think the function is written to locate a layer name or table view as it appears in the environment that you set as the workspace.  When testing the process for a single species I did not encounter any problems with data paths.
0 Kudos
DarrenWiens2
MVP Honored Contributor
Are the fields actually numeric (integer, float, double) or text? You can check in the field properties. They should be a numeric data type.

Also, try putting "print species" (no quotes) as the first line in your loop just to make sure it's bringing in the right name.
0 Kudos
JohnNunnali
New Contributor
The field types are "double".

I added print species as the first line of the loop and it returned

Achillea_millefolim

so it appears it is bringing them in correctly.  I am still getting the following error message:

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

My current code looks like this:

#Script to create distribution maps for wetland species
#Import arcpy and set workspace
import arcpy
import arcpy.mapping
arcpy.env.workspace = r"P:\CoWetlandTools\maps\distribution_maps_Johns_pytest.mxd"
import arcpy.sa
#generate a list
speciesList =("Achillea_millefolium", "Acroptilon_repens", "Actinea_osterhoutii")
species = ' ' in speciesList

#Create Loop and List to run processing for
for species in speciesList:
  print species   
  #select counties each species is known to occur  
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "NEW_SELECTION", ' " ' + species + ' " > 0')

  #Export selected to jpeg
  mxd = arcpy.mapping.MapDocument(r"CURRENT")
  
  arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\' + speciesList + '.jpg")

  #clear selected attributes
  arcpy.SelectLayerByAttribute_management ("counties_edited_datamergetest", "CLEAR_SELECTION")

#finished
print "Finished"
0 Kudos
DarrenWiens2
MVP Honored Contributor
I'm almost positive your workspace can't be an mxd.

Also, this line needs to have matching quotes, and just "species", like so:

arcpy.mapping.ExportToJPEG(mxd, r"P:\CoWetlandTools\maps\" + species + ".jpg")
0 Kudos