Select to view content in your preferred language

Python list to where clause

2529
3
10-26-2011 05:51 AM
MathewCoyle
Honored Contributor
What I am trying to do is create a definition query or where clause on a polygon grid feature layer. Each grid feature has a name and that name references a unique geodatabase in a directory. What I am doing is checking for a particular layer(s) in each geodatabase, saving that corresponding name in the feature layer if found, and outputting another feature layer with just the polygons that have a geodatabase with a corresponding feature class. Hopefully that wasn't too convoluted.

My problem is, no matter what I've tried, I can't seem to pass this list on to something arcpy will take. Here's my code so far, simplified to just test if the geodatabase has features at all. The bold lines are where I would think I need to change to get this to work, crashes on the makefeaturelayer.

inputWorkspace = myWorkspace
dataList = list()
twnshpLyr = r"TFM_BKG.NEATLINE"
selectSearch = arcpy.SearchCursor(twnshpLyr)
for row in selectSearch:
    township = row.getValue("TILE_NAME")
    arcpy.env.workspace = inputWorkspace+os.sep+township+".mdb"
    fcList = arcpy.ListFeatureClasses()
    if fcList:
        dataList.append(str(township))
    else: continue
arcpy.MakeFeatureLayer_management(twnshpLyr, "townshipData", "TILE_NAME in (",str(dataList)+")")


Here is the error I get from the python window:
Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Workspace or Feature Dataset: Dataset (...all the feature values in the list...) does not exist or is not supported Failed to execute (MakeFeatureLayer).
Tags (2)
0 Kudos
3 Replies
KyleShannon
Deactivated User
Try using join:
where = "TILE_NAME in (" + ','.join(dataList) + ")"

also try using idle to validate code snippets.
0 Kudos
HemingZhu
Frequent Contributor
What I am trying to do is create a definition query or where clause on a polygon grid feature layer. Each grid feature has a name and that name references a unique geodatabase in a directory. What I am doing is checking for a particular layer(s) in each geodatabase, saving that corresponding name in the feature layer if found, and outputting another feature layer with just the polygons that have a geodatabase with a corresponding feature class. Hopefully that wasn't too convoluted.

My problem is, no matter what I've tried, I can't seem to pass this list on to something arcpy will take. Here's my code so far, simplified to just test if the geodatabase has features at all. The bold lines are where I would think I need to change to get this to work, crashes on the makefeaturelayer.

inputWorkspace = myWorkspace
dataList = list()
twnshpLyr = r"TFM_BKG.NEATLINE"
selectSearch = arcpy.SearchCursor(twnshpLyr)
for row in selectSearch:
    township = row.getValue("TILE_NAME")
    arcpy.env.workspace = inputWorkspace+os.sep+township+".mdb"
    fcList = arcpy.ListFeatureClasses()
    if fcList:
        dataList.append(str(township))
    else: continue
arcpy.MakeFeatureLayer_management(twnshpLyr, "townshipData", "TILE_NAME in (",str(dataList)+")")


Here is the error I get from the python window:
Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Workspace or Feature Dataset: Dataset (...all the feature values in the list...) does not exist or is not supported Failed to execute (MakeFeatureLayer).


Based on your script, you tried to creat an out_layer in inputWorkspace+os.sep+township+".mdb which is invalid workspace for lyr file.
0 Kudos
MathewCoyle
Honored Contributor
Thanks for all the suggestions, the where clause got me rolling in the right direction, slight modification and worked perfectly.
where = "TILE_NAME in ('" + '\',\''.join(dataList) + "')"


The workspace wasn't really an issue since I was working in memory, but a good habit to get in to nonetheless. Here's the working script now.

import arcpy, os
sde = r"Database Connections\wisprod.sde"
inputWorkspace = r"\\winms-gissvr\data\new_avi_pgdb\data"
dataList = list()
twnshpLyr = r"TFM_BKG.NEATLINE"
selectSearch = arcpy.SearchCursor(twnshpLyr)
for row in selectSearch:
    township = row.getValue("TILE_NAME")
    arcpy.env.workspace = inputWorkspace+os.sep+township+".mdb"
    fcList = arcpy.ListFeatureClasses()
    if fcList:
        dataList.append(str(township))
    else: pass
arcpy.env.workspace = inputWorkspace
where = "TILE_NAME in ('" + '\',\''.join(dataList) + "')"
arcpy.MakeFeatureLayer_management(sde+os.sep+twnshpLyr, "townshipData", where)
0 Kudos