Doing multiple queries with a loop

1650
12
01-18-2012 09:10 AM
julienhubert
New Contributor
Hello everyone !

I work on ArcGIS 9.3 and I'd want to execute many queries in a row with Python.

Actually I have a layer, called "lines" that contains all the bus lines of a city. And my question is : how to save each line in a different layer ?
I thought the best thing to do would be :
- a query by attributes : field used : "blines". Characterizes the number of the lines (1, 2, 3 and so on)
- and then to save each query in a distinct layer

A good solution would be a loop in order to make the computation faster but I don't really know how to do.

I tried to make that script but I guess I'm very far from the right result :


import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Variables in local
lines_shp = "d:\\Travaux\\NantesMetropole\\lines.shp"

test = ["blines"]
while 1:
    for i in test:
        try:
            gp.Select_analysis(lines_shp, i_shp, i)
            print "Query successful"
        except:
            print "Error"
    break


Have you got any ideas ?

Thank you !
Julien
Tags (2)
0 Kudos
12 Replies
DarrenWiens2
MVP Honored Contributor
You can adapt this to suit your needs:
import arcpy

arcpy.env.overwriteOutput = True

fc = "d:/Travaux/NantesMetropole/lines.shp" # path to input feature class
field = "blines" # field name to get unique values
arcpy.MakeFeatureLayer_management(fc, "lyr") # make feature layer
rows = arcpy.SearchCursor(fc, "", "", "", field) # make cursor, sort by field

firsttime = 1 
prev = -1

for row in rows: # loop through each feature
    current = row.getValue(field) # get current field value
    if firsttime == 1 or prev != current: 
        prev = row.getValue(field) # set prev to current value
        arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", field + " = " + str(row.mapnumber))
        arcpy.CopyFeatures_management("lyr", "d:/Travaux/NantesMetropole/lines" + str(current) + ".shp") 
arcpy.Delete_management("lyr")
del row
del rows
0 Kudos
julienhubert
New Contributor
Thanks for your reply.

I tried your code in my script "test_rte.py" (adapting it by using arcgisscripting instead of acrpy) but I have an error message :

  File "C:\_basedata_arcgis\test_rte.PY", line 14, in <module>
    for row in rows: # loop through each feature
TypeError: 'geoprocessing cursor object' object is not iterable


What could I do to correct this ?
Thanks for the help.
0 Kudos
DarrenWiens2
MVP Honored Contributor
Oh, sorry - didn't see you're on 9.3. Arcpy only works on 10. You can basically use the same script format, just substitute arcgisscripting for arcpy and create the geoprocessor object as you did above.
0 Kudos
DarrenWiens2
MVP Honored Contributor
In arcgisscripting, I think you need to add the line
row = rows.next()

between creating the SearchCursor and the loop. You also need to add that line to the very end of the loop to advance the cursor. See this help page to see an example.
0 Kudos
julienhubert
New Contributor
Yeah it's right, I corrected it and it looks good....

... but another new error message :

    gp.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", field + " = " + str(row.mapnumber))
RuntimeError: Row: Field mapnumber does not exist


What exactly is the row.mapnumber function ? I took a look in both the python docs and the arcgis help but it's not explained..
0 Kudos
DarrenWiens2
MVP Honored Contributor
Sorry, "mapnumber" was the field I tested the script with (I thought I changed them all). You can change str(row.mapnumber) to str(current).
0 Kudos
julienhubert
New Contributor
Ok thanks for row.mapnumber !

So new try and there is an error on the execution of SelectLayerByAttribute :

    gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", field + " = " + str(current))
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
0 Kudos
DarrenWiens2
MVP Honored Contributor
It works for me. Instead of: gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", field + " = " + str(current))
try:
expr = field + " = " + str(current)
gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", expr)
0 Kudos
julienhubert
New Contributor
Ok, I tried many and many things including your last advice but it still does not work.

I don't want to waste your time but could you write the entire code (since it works for you) ?
0 Kudos