Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.81k stars 1.08k forks source link

to_featurelayer with overwrite capability from spatial dataframe #1477

Closed swilson2023 closed 1 year ago

swilson2023 commented 1 year ago

Hi,

I'm attempting to overwrite a feature layer in arcgis online from python using the to_featurelayer method defined in the following:

https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html

I have a spatial dataframe and am able to create the feature layer without issue, but when I pass overwrite=True and the service parameter, as in the example above, I'm getting an error that the parameters provided to the to_featurelayer method are invalid/unexpected. I'm confused as the documentation indicates that these parameters can be provided as optional arguments.

I'm wondering if I'm simply passing these parameters incorrectly, lacking the appropriate version of the arcgis api, or something else.

Thank you and best regards, Sam

swilson2023 commented 1 year ago

Just to add - the specific issue I'm running into is the following:

I have a spatially enabled dataframe, like so:

sedf = pd.DataFrame.spatial.from_xy(df, 'lon', 'lat')

When I run sedf.spatial.to_featurelayer without the overwrite=True and service arguments, it successfully creates a new feature layer in arcgis online. However, when I pass overwrite=True and service arguments, to the to_featurelayer method, it returns the following error, which seems inconsistent with the documentation (unless I'm doing it wrong!):

TypeError: to_featurelayer() got an unexpected keyword argument 'overwrite'

Thanks in advance for any help you can provide.

Best, Sam

nanaeaubry commented 1 year ago

@swilson2023 Hello can you please provide some more complete code on how you are passing everything in this way we can test it on our end?

Thanks!

swilson2023 commented 1 year ago

Hi - sure thing!

Starting with a spatially enabled dataframe like so:

sedf = pd.DataFrame.spatial.from_xy(df, 'lon', 'lat')

When I execute the following, it creates a file geodatabase and associated hosted feature layer in arcgis online with the provided 'Name' and using the provided gis connection without issue:

sedf.spatial.to_featurelayer(title='Name', gis=gis)

What I'd like to do now is overwrite this feature layer with new data (same schema) as it comes in. According to the following, I can do so using the optional "overwrite" and "service" arguments of the to_featurelayer() method:

https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html

However, when I execute the following using the ID of the hosted feature layer, I get an error:

sedf.spatial.to_featurelayer(title='Name', gis=gis, overwrite=True, service={"featureServiceId": "ID of hosted feature layer"})

The specific error is:

TypeError: to_featurelayer() got an unexpected keyword argument 'overwrite'

Thanks again for the help! Sam

nanaeaubry commented 1 year ago

@swilson2023

Awesome thanks for the explanation. So the error you are getting it making me think you are not at the most recent version of the API. We added overwrite at version 2.1.0 (current version is 2.1.0.2). So I would try updating the version you are using.

Second thing is that for the service parameter make sure you are passing in the layer id as well:

service = {“featureServiceId” : “9311d21a9a2047d19c0faaebd6f2cca6”, “layer”: 0}

You have to specify the layer that will be overwritten since your feature service could contain more than one layer. If you only have one layer just go ahead and pass in 0 like the example above.

Hope this helps! Let me know if this fixes your issue

swilson2023 commented 1 year ago

Bingo.. the version update worked - thanks so much!

nanaeaubry commented 1 year ago

Awesome! Closing this issue then :)

swilson2023 commented 1 year ago

Thanks again!