Error when querying online layer with API

951
2
Jump to solution
03-26-2019 12:01 PM
JasonJordan00
Occasional Contributor

All of a sudden I am experiencing errors when working with the ArcGIS Python API with a code taken straight from the API guide. There was no difference in my PC's configuration from when my (very simple) code worked and when it stopped. Literally the errors started within minutes in between runs. No updates, no new installations, nothing. I've tried restarting, reinstalling the API, Python, NumPy, Pandas, and others. I've also tried downgrading most of their versions. Currently I have API version 1.6.0 with Python 3.7. Any help with this would be greatly appreciated.

import arcgis
from arcgis.gis import GIS

gis_url = '####'

username = '####'

password = '####'

gis = GIS(gis_url, username, password)

object_point = gis.content.search("Points_Layer_Test")[0]

object_point    ##<-------This displays the intended layer, so I know it's seeing it

object_layer = object_point.layers
obj_fset = object_layer[0].query()

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\arcgis\gis\__init__.py in __getitem__(self, k)
 6397 try:
-> 6398 return dict.__getitem__(self, k)
 6399 except KeyError:

KeyError: 'layers'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-9-974ff12180c0> in <module>
----> 1 object_layer = object_point.layers
 2 obj_fset = object_layer[0].query()

c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\arcgis\gis\__init__.py in __getattribute__(self, name)
 6369 def __getattribute__ (self, name):
 6370 if name == 'layers':
-> 6371 if self['layers'] == None or self['layers'] == []:
 6372 try:
 6373 with _DisableLogger():

c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\arcgis\gis\__init__.py in __getitem__(self, k)
 6400 if not self._hydrated and not k.startswith('_'):
 6401 self._hydrate()
-> 6402 return dict.__getitem__(self, k)
 6403 #----------------------------------------------------------------------
 6404 @property

KeyError: 'layers'
0 Kudos
1 Solution

Accepted Solutions
EarlMedina1
Occasional Contributor III

This looks right from afar, but I wonder if it's just not letting you drill down into that individual layer. Try something like this instead:

def search_layer(layer_name):
    search_results = agol.content.search(layer_name, item_type='Feature Layer')
    item = search_results[0]
    geofeatures = item.layers[0]
    return geofeatures

search_pnt = search_layer('pointFeatureLayer')
query_pnt = search_pnt.query(where='OBJECTID=63')

-Earl

View solution in original post

2 Replies
EarlMedina1
Occasional Contributor III

This looks right from afar, but I wonder if it's just not letting you drill down into that individual layer. Try something like this instead:

def search_layer(layer_name):
    search_results = agol.content.search(layer_name, item_type='Feature Layer')
    item = search_results[0]
    geofeatures = item.layers[0]
    return geofeatures

search_pnt = search_layer('pointFeatureLayer')
query_pnt = search_pnt.query(where='OBJECTID=63')

-Earl

JasonJordan00
Occasional Contributor

That did the trick! I'm still trying to figure out what changed to cause it to stop working so suddenly but at least I can proceed now. Thanks!

0 Kudos