I would like to search all feature classes in a geodatabase using a search cursor but I want to use two separate where clauses. I'm gathering information about a particular field and I need to know how it is different for each where clause.
The first where clause would be:
GIS_STATUS = 'Superseded'
and the other would be:
GIS_STATUS <> 'Superseded'
My suspicion is that I need to traverse the entire geodatabase twice using two loops. However, I wonder if there a way I can use two simultaneous search cursors? Parallel search cursors?
I'm using ArcGIS Desktop 10.4.1.
There seemed to be a bit of confusion about what I was trying to do, so I'll show what I had in mind.
Firstly, I define two sets. One to hold the values in the METADATA field where GIS_STATUS = 'Superseded'
and another where GIS_STATUS <> 'Superseded'
.
superSet = set()
notSuperSet = set()
Then, I loop through all feature classes twice. One for each where clause:
for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace, datatype = 'FeatureClass'):
for filename in filenames:
with arcpy.da.SearchCursor(filename, "METADATA", """GIS_STATUS = 'Superseded'""") as sCur:
for row in sCur:
superSet.add(row[0])
and
for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace, datatype = 'FeatureClass'):
for filename in filenames:
with arcpy.da.SearchCursor(filename, "METADATA", """GIS_STATUS <> 'Superseded'""") as sCur:
for row in sCur:
notSuperSet.add(row[0])
Then, I can use the set methods to determine how they are different.
superSet.difference(notSuperSet)
So, my question is: is this the best way to do this? Do I need two loops?
Answer
It turns out I was thinking about this the wrong way. I was trying to run two search cursors when I could do what I needed with one. Thanks to @Vince for the clarification.
I just create two empty sets prior to looping through my file geodatabase:
superSet = set()
notSuperSet = set()
Then, I just use one loop but include the GIS_STATUS
field in the search cursor. Then, use an if/else statement to determine whether GIS_STATUS
was 'Superseded' or not.
for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace, datatype = 'FeatureClass'):
for filename in filenames:
with arcpy.da.SearchCursor(filename, ["METADATA","GIS_STATUS"]) as sCur:
for row in sCur:
if row[1] == 'Superseded':
superSet.add(row[0])
else:
notSuperSet.add(row[0])
The results are almost twice as fast as the original two loops (which is expected).
No comments:
Post a Comment