TypeError: 'FeatureLayer' object is not iterable

504
4
09-01-2021 02:14 AM
StuartMoore
Occasional Contributor III

i am trying to iterate the features from a feature service within ArcGIS Pro Notebook

this is my code and it seems to connect to the feature service and returns the name correctly

items = gis.content.search('title:LiveData owner:xxxxxx', 'feature layer')
OP1item = items[0]
PLS = OP1item.layers[11]
print(PLS.properties["name"])
xxSection

but when i try and iterate it

for x in PLS:
    print(x)

i get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
In  [35]:
Line 3:     for x in PLS:

TypeError: 'FeatureLayer' object is not iterable
---------------------------------------------------------------------------

any ideas what i'm doing wrong?

0 Kudos
4 Replies
JohannesLindner
MVP Regular Contributor

You can't iterate FeatureLayers. Create a SearchCursor and iterate that:

fields = [f.name for f in arcpy.ListFields(PLS)] # or define a subset of fields
print(fields)
with arcpy.da.SearchCursor(PLS, fields) as cursor:
    for row in cursor:
        print(row)

Have a great day!
Johannes
StuartMoore
Occasional Contributor III

thanks @JohannesLindner i tried that but got a different error:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
In  [39]:
Line 3:     fields = [f.name for f in arcpy.ListFields(PLS)] # or define a subset of fields

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py, in ListFields:
Line 1134:  return gp.listFields(dataset, wild_card, field_type)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in listFields:
Line 353:   self._gp.ListFields(*gp_fixargs(args, True)))

OSError: "<FeatureLayer url:"https://xxx.xxx.xxx.net/arcgis/rest/services/Collector/LiveData/FeatureServer/11">" does not exist
---------------------------------------------------------------------------

i also tried it with all fields and got another error:

with arcpy.da.SearchCursor(PLS, "*") as cursor:
    for row in cursor:
        print(row)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
In  [40]:
Line 5:     with arcpy.da.SearchCursor(PLS, "*") as cursor:

RuntimeError: 'in_table' is not a table or a featureclass
---------------------------------------------------------------------------

 

its got me stumped as i've iterated over feature layers in an AGOL Notebook in a similar way and it worked

0 Kudos
DanPatterson
MVP Esteemed Contributor

Example

gdb = r"C:\arcpro_npg\npg\Project_npg\tests.gdb" 
arcpy.env.workspace = gdb
fcs = arcpy.ListFeatureClasses()
nmes = [i for i in fcs]

# take the first 3 fcs as an example
for i in fcs[:3]:
    fc = "\\".join([gdb, i])
    fnames = arcpy.ListFields(fc)
    flds.append([j.name for j in fnames])
    

nmes[:3]
['big', 'odd', 'odd_inters']

flds
['OBJECTID',
 'Shape',
 'Shape_Length',
... snip
 'PART_COUNT_1',
 'PNT_COUNT_1',
 ['OBJECTID', 'Shape', 'Shape_Length', 'Shape_Area'],
 ['OBJECTID',
  'Shape',
  'ORIG_FID',
.... snip
  'INSIDE_Y_1',
  'PART_COUNT_1',
  'PNT_COUNT_1']]

... sort of retired...
StuartMoore
Occasional Contributor III

thanks @DanPatterson & @JohannesLindner 

i've managed to get it working

from arcgis.gis import GIS
gis = GIS("home")
xxM = gis.content.get("54e2xxxeae")
xxLive = xxM.layers[11]
NewxxLive = xxLive.query(where = "OBJECTID = 11")
print("--Start Run--")
for feature in NewxxLive:
    print("---Start--")
    print(feature)
    print("----")
--Start Run--
---Start--
{"geometry": {"paths": [[[51....]]]}, "attributes": {"OBJECTID": 11 }}
----
0 Kudos