arcgis-python-api icon indicating copy to clipboard operation
arcgis-python-api copied to clipboard

error: "maximum recursion depth exceeded while calling a Python object"

Open jimoreira opened this issue 1 year ago • 9 comments

hi, new with this package. trying to bring feature layer to local python. Ideally to shapely object. I found the spatially enabled dataframe as the closest object but couldn't really get one. i'm doing

url=f"..url-secret../FeatureServer/0/?token={token}"
feature_layer = FeatureLayer(url)
feature_layer
feature_layer.properties.extent

df = feature_layer.query(where="ID_PREDIO=308").sdf.iloc[0]
```
or 
```
sdf = pd.DataFrame.spatial.from_layer(feature_layer)
sdf.head()
```

`properties.extent` gives the right response, but the spatial dataframe creation fails with: 

`Exception: A general error occurred: maximum recursion depth exceeded while calling a Python object`

I can guess this is a too big dataframe? 
In arcgis online, the query gives a 2 row attribute table.

thanks for your help!!

jimoreira avatar Oct 02 '23 17:10 jimoreira

@jimoreira Hello, the best way to get started with the SeDF is to look at our guides: https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/#reading-feature-layer-data

Try seeing if this way of getting the Feature Layer and reading it can help you.

If you still get the same error let us know!

Similarly, here is the section on export options: https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/#export-options

nanaeaubry avatar Oct 03 '23 06:10 nanaeaubry

@jimoreira also to address getting a Shapely object

The Spatially Enabled DataFrame has a 'SHAPE' column where the geometry is held. You can then work with this geometry to get what you want.

https://developers.arcgis.com/python/guide/part2-working-with-geometries/#get-geometries-from--a-spatially-enabled-dataframe-object

If you scroll this guide you will get a better idea of what to do. Also check out the API reference if you want to see all available methods with geometries

nanaeaubry avatar Oct 03 '23 06:10 nanaeaubry

hi, thanks for your help. i was checking the same page. i keep getting the same Error.

the object that i get, the feature layer, is of type <class 'arcgis.features.layer.FeatureLayer'> and in that object, neither the .spatial.from_layer() or the .query().sdf works, both with the same Error mentioned on the issue title. Actually, the whole Error is this one:

Traceback (most recent call last):
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\gis\_impl\_con\_connection.py", line 1206, in post
    resp = self._session.post(
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\auth\api.py", line 471, in post
    return self._session.post(
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\sessions.py", line 637, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\sessions.py", line 710, in send
    r = dispatch_hook("response", hooks, r, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\hooks.py", line 30, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\auth\_auth\_winauth.py", line 143, in generate_portal_server_token
    token = requests.post(token_url, data=postdata, auth=self.auth)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\urllib3\connectionpool.py", line 714, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\urllib3\connectionpool.py", line 466, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\urllib3\connectionpool.py", line 461, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\http\client.py", line 1348, in getresponse
    response.begin()
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\http\client.py", line 335, in begin
    self.headers = self.msg = parse_headers(self.fp)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\http\client.py", line 234, in parse_headers
    return email.parser.Parser(_class=_class).parsestr(hstring)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\parser.py", line 67, in parsestr
    return self.parse(StringIO(text), headersonly=headersonly)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\parser.py", line 56, in parse
    feedparser.feed(data)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\feedparser.py", line 176, in feed
    self._call_parse()
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\feedparser.py", line 180, in _call_parse
    self._parse()
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\feedparser.py", line 295, in _parsegen
    if self._cur.get_content_maintype() == 'message':
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\message.py", line 594, in get_content_maintype
    ctype = self.get_content_type()
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\message.py", line 578, in get_content_type
    value = self.get('content-type', missing)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\message.py", line 471, in get
    return self.policy.header_fetch_parse(k, v)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\_policybase.py", line 316, in header_fetch_parse
    return self._sanitize_header(name, value)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\_policybase.py", line 287, in _sanitize_header
    if _has_surrogates(value):
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\email\utils.py", line 57, in _has_surrogates
    s.encode()
RecursionError: maximum recursion depth exceeded while calling a Python object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "queyPD.py", line 24, in <module>
    df = feature_layer.query(where="ID_PREDIO = 308")#.sdf.iloc[0]
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 1754, in query
    record_count = self._query(url, params, raw=as_raw)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 3044, in _query
    return self._query(url, params, raw=False)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 3044, in _query
    return self._query(url, params, raw=False)
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 3044, in _query
    return self._query(url, params, raw=False)
  [Previous line repeated 965 more times]
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 3080, in _query
    raise queryException
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\features\layer.py", line 3033, in _query
    result = self._con.post(
  File "C:\Users\javie\.conda\envs\hlstutorial\lib\site-packages\arcgis\gis\_impl\_con\_connection.py", line 1248, in post
    raise Exception("A general error occurred: %s" % e)
Exception: A general error occurred: maximum recursion depth exceeded while calling a Python object

jimoreira avatar Oct 03 '23 12:10 jimoreira

@jimoreira Interesting... have you tried using it with another layer that has fewer features to see if it does the same? This way we can determine if it's the code or the layer that is causing the issue.

If it's the layer then it would be good for us to have the data if possible so we can test it on our end. If this is not something you can share then we suggest opening a case with Support so they can better assist you

nanaeaubry avatar Oct 03 '23 12:10 nanaeaubry

hi, i have tried with other feature_layer and same result. my hole code is:

import pandas as pd
from arcgis.features import FeatureLayer
import requests

###obtiene token
url = "https://..specific../rest/generateToken"
payload = 'username=..usr..&password=..pass..&client=referer&referer=https://..specific../FeatureServer/0/&expiration=1800&f=json'

headers = {
  'Content-Type': 'application/x-www-form-urlencoded'#,
  #'Authorization': 'Basic amF2aWVyLm1vcmVpcmEuZXh0QG1kcDpwWGFCVzFmV2ZK'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.json()["token"])
token=response.json()["token"]

###lee feature Layer y lo transforma en pandas spatial dataframe
url=f"https://..specific../rest/services/Pais/FeatureServer/0/?token={token}"

feature_layer = FeatureLayer(url)
feature_layer
feature_layer.properties.extent
print(type(feature_layer))
#df = feature_layer.query(where="ID_PREDIO = 308")#.sdf.iloc[0]
#df

sdf = pd.DataFrame.spatial.from_layer(feature_layer)
sdf.head()

perhaps something rings a bell

thanks again!

jimoreira avatar Oct 03 '23 14:10 jimoreira

@jimoreira

I would recommend logging in and accessing the layer in a different way. Since it seems you are using OAuth2 I would look here to understand how to login to the API: https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/#user-authentication-with-oauth-20

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

gis = GIS("https://pythonapi.playground.esri.com/portal", client_id='<your_client_id>')

# define url
url=f"https://..specific../rest/services/Pais/FeatureServer/0

fl = FeatureLayer(url)

# Try various queries
fl.query()
fl.query(as_df=True)
fl.query(where="ID_PREDIO = 308).sdf

I cannot reproduce the issue on our end so I would attempt to see if the login or the creation of the Feature Layer is causing the issue. My guess is that it has to do with the authentication but it's difficult to test on our end

nanaeaubry avatar Oct 03 '23 14:10 nanaeaubry

OK, i understand your suggestion, but I'm having issues finding a way to connect, I don't have a cliente_id. Also initially I saw tutorials using object numbers using GIS method. I guess i cannot use that since I don't have any feature_layer in my content. I'm just trying to access a feature_layer that was shared with my user. Don't fully understand how.

thanks again

jimoreira avatar Oct 03 '23 18:10 jimoreira

@achapkowski Do you have any insight on this?

nanaeaubry avatar Oct 04 '23 05:10 nanaeaubry

@jimoreira I recommend you create a GIS object first using your login information to the enterprise. Once that is done, pass it into your URL like this:

layer = FeatureLayer(url="<some url>", gis=gis)

Then do your query. There is no need to create your own token the GIS object does that for you.

achapkowski avatar Oct 11 '23 16:10 achapkowski

Closing since this is resolved with workaround

nanaeaubry avatar May 13 '24 10:05 nanaeaubry

thanks

jimoreira avatar May 13 '24 12:05 jimoreira