I have started recently using some of the ArcObjects inside my Python modules. Having all the useful posts and insights shared by @matt wilkie et al, I was able to get started pretty quickly (installing the comtypes with pip and downloading the 10.2 snippet from Pierssen and changing "10.2" to "10.3" everywhere).
I am trying to iterate IFeatureCursor and get all the features inside a feature class. However, I am getting back only the latest feature (with the highest ObjectID value).
There are 6 features in the feature class hence xrange(6) to keep it simple.
from comtypes.client import GetModule, CreateObject
from snippets102 import GetStandaloneModules, InitStandalone
# First time through, need to import the “StandaloneModules”. Can comment out later.
#GetStandaloneModules()
InitStandalone()
def iterate_features():
# Get the GDB module
esriGeodatabase = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.3\com\esriGeoDatabase.olb")
esriDataSourcesGDB = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.3\com\esriDataSourcesGDB.olb")
# Create a file geodatabase pointer
file_gdb_pointer = CreateObject(progid=esriDataSourcesGDB.FileGDBWorkspaceFactory,
interface=esriGeodatabase.IWorkspaceFactory)
file_gdb = file_gdb_pointer.OpenFromFile(r"C:\GIS\arcobjects\MyData.gdb",hWnd=0)
#access contents inside gdb
feature_workspace = file_gdb.QueryInterface(esriGeodatabase.IFeatureWorkspace)
in_fc = feature_workspace.OpenFeatureClass("Warehouses")
def enum_features(in_fc):
"""returns pointers to IFeature objects inside Feature Class"""
cur = in_fc.Search(None,True)
for i in xrange(6):
feature_obj = yield cur.NextFeature()
feats = [feat for feat in enum_features(in_fc)]
print [f.OID for f in feats]
iterate_features()
The line print [f.OID for f in feats] returns [6, 6, 6, 6, 6, 6].
What am I doing wrong? The same logic with generator/yield (def enum_features()) works fine when iterating feature classes inside the feature dataset.
the feats_OIDs = [feat.OID for feat in enum_features(in_fc)] will give correct results, [1, 2, 3, 4, 5, 6], without me making any modifications to the code. The problem seems to be in that when I create a list of features [feat for feat in enum_features(in_fc)], they all refer to the same feature (because when I explore each of them later, each of them have the same OID).
feature_obj = cur.NextFeature()thenyield feature_obj).