Select to view content in your preferred language

Doing multiple queries with a loop

2320
12
01-18-2012 09:10 AM
julienhubert
Emerging 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 Alum
import arcpy

arcpy.env.overwriteOutput = True

fc = "H:/GIS_Data/TEMP.gdb/points" # path to input feature class
field = "mapnumber" # 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(current)) 
        arcpy.CopyFeatures_management("lyr", "H:/GIS_Data/TEMP.gdb/pt_" + str(current)) 
arcpy.Delete_management("lyr")
del row
del rows
0 Kudos
ChrisSnyder
Honored Contributor
If you want every feature exported as a seperate feature class, you would probably want to use the OBJECTID field (or some other unique ID field).

#DISCLAIMER: Untested code - just typed it on the fly - may not work "out of the box"
import arcpy
myFC = r"C:\temp\my_fc.shp"
oidFieldName = arcpy.Describe(myFC).OIDFieldName
searchRows = arcpy.SearchCursor(myFC)
for searchRow in searchRows:
   oidFieldValue = searchRow.getValue(oidFieldName)
   outFC = r"C:\temp\output_" + str(oidFieldValue) + ".shp"
   arcpy.Select_analysis(myFC, outFC, oidFieldName + " = " + str(oidFielValue))
del searchRow, searchRows
0 Kudos
DarrenWiens2
MVP Alum
If you want every feature exported as a seperate feature class, you would probably want to use the OBJECTID field (or some other unique ID field).

That assumes that each bus line is made of only one feature...
0 Kudos