Select to view content in your preferred language

Create selection from Python list

4388
2
04-16-2010 12:30 AM
TomGeo
by
Frequent Contributor
Dear all,

I was wondering if it is possible to create a query for a selection from a Python list.
The Python list is created from features in a FeatureClass but since the geoprocessor is very slow I write all the features into a Python list and do all the analysis with Python lists for better performance.
While the performance of getting all the features of interest is good, I do not know how to select all the features in the actual FeatureClass based on my resulting list.

If someone has an example for?

Thanks in advance, Thomas
- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
2 Replies
RDHarles
Regular Contributor
I can't tell exactly what you are trying to do, but here's an example of using items in a list for selections using a query.

myList = ["apple","pear","orange"]
for item in myList:

    # Select item in list based on field COUNTRY.
    qry = "\"COUNTRY\" = '"+item+"'"

    gp.Select_analysis(inFile, outDir, qry)
0 Kudos
KimOllivier
Honored Contributor
Yes, this is a very successful way of creating an SQL query that is efficient.
A keyfile type selection is often wanted, and the MakeQueryTable is buggy at 9.3 and has a lot of limitations.
You have to have all features in the same geodatabase, complex expressions are not supported in file geodatabase and so on.

Here is my workaround:
Loop through the first table with a cursor to collect a list of keys.
Pass it through a SET structure to remove duplicates.
Create a (potentially huge) sql expression
Use this expression on the source featureclass to create a layer
Process or export the layer to a new featureclass

# to make a keyfile
# 26 April 2010 kimo
# much better than MakeQueryTable and it works across geodatabases
# still uses an SQL query but python generates the set for the expression
# will work for tens of thousands of keys (amazing)
# if you want to save the featureclass or table as a layer or view definition
# to use in ArcMap (without making a copy) you MUST have an index on the key field
# or it will be extremely slow to draw.
# see the Keyfile tool in ArcScripts for an alternative method using selection sets in ArcMap

import arcgisscripting,sys,os,datetime,fileinput
gp = arcgisscripting.create(9.3)

def titlekey(title) :
    gp.Workspace = ws
    # Make a title set to use as a keyfile
    lstTitle = []
    cur = gp.SearchCursor(title)
    row = cur.next()
    while row :
        lstTitle.append(row.ttl_title_no)
        row = cur.next()
    del row,cur
    return set(lstTitle)

def title_tab(src) :
    " use setTitle to export records to a table"
    start = datetime.datetime.now()
    gp.Workspace = ws
    setTitle = titlekey(title)
    expTitle = ",".join(["'"+t+"'" for t in setTitle])
    sqlQuery = "ttl_title_no in ("+expTitle+")"
    gp.MakeTableView_management(src,src+"_view",sqlQuery,wsout)
    gp.OverwriteOutput = True
    gp.CopyRows_management(src+"_view",wsout+"/"+src+"_sub")
    print gp.GetCount(wsout+"/"+src+"_sub").Getoutput(0),"records in "+src+"_sub"
    print datetime.datetime.now() - start
# ------------------------- main -------------------------
title = "e:/arrears.mdb/title"
ws = "e:/memo.gdb"
wsout = "e:/arrears.mdb"
# create subsets based on title keys
if not gp.Exists(wsout+"/tta_sub") :
    title_tab("tta")
0 Kudos