Esri / arcgis-python-api

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

Unable to append to AGOL feature layer using featureCollection #1604

Closed anandak closed 1 year ago

anandak commented 1 year ago

Describe the bug While submitting an featurelayer.append() request using python api v2.1.0.3, we get an unknown error.

To Reproduce Steps to reproduce the behavior:

  1. Create a FeatureSet.from_dict() using a list of feature objects
  2. Create a FeatureCollection.from_featureset()
  3. Run featurelayer.append()

error: See screenshot below, the featureCollection gets passed as string object name instead of JSON, as seen in fiddler capture

Screenshots image

This is the response you get when you try to get status

image

Expected behavior The featureCollection must be sent as json

nanaeaubry commented 1 year ago

Thanks for reporting this, we will take a look and post any updates

nanaeaubry commented 1 year ago

How are you making the append call? What parameters are you defining?

I was able to get it to work with your described workflow. Here is some sample code:

item = gis.content.get(<item_id>)
fl = item.layers[0]

feature_set_dict = {.....} #your feature set dict
feature_set = FeatureSet.from_dict(feature_set_dict)
fc = FeatureCollection.from_featureset(feature_set)

fl.append(upload_format="featureCollection", edits=fc.properties)
anandak commented 1 year ago

@nanaeaubry my apologies, I logged this issue too soon. The root cause was that some properties of the featurecollection like its name were coming from a different configuration settings module and was failing during conversion to JSON. I was able to get append to run successfully.

anandak commented 1 year ago

@nanaeaubry I have run into a strange issue with using append. It fails when there is date field in the feature collection and succeeds if we dont supply a date field in the feature collection. Here is the code you can use to reproduce the issue. You can either create a copy of this feature layer or use my layer as it is anonymously accessible and created only for reproducing this issue purpose.

The code below fails

from arcgis import GIS
from arcgis.features import Feature, FeatureSet, FeatureLayer, FeatureCollection
import uuid
from datetime import datetime

gis = GIS(url="https://www.arcgis.com", username="xx", password="xx")

# this layer is anonymously accessible
layer_url = "https://services1.arcgis.com/DlnuvLGpDczjeSgG/arcgis/rest/services/appendissue/FeatureServer/0"

fl = FeatureLayer(url=layer_url, gis=gis)

pt_geom = {"x":11.5, "y":20.5,"spatialReference": {"wkid":4326}}
pt_attributes = {"appendkeyfield":str(uuid.uuid4()), "nibrs_code":"345", "reported_date": datetime.now()}
feature = Feature(geometry=pt_geom, attributes=pt_attributes)
fs = FeatureSet(features=[feature])
fc = FeatureCollection.from_featureset(fs)

result = fl.append(upload_format='featureCollection',
                                edits=fc.properties,
                                upsert=True,
                                skip_inserts=False,
                                skip_updates=False,
                                update_geometry=True, 
                                use_globalids=False,
                                rollback=True,
                                upsert_matching_field="appendkeyfield",
                                return_messages=True)

print(result)

The error message is image

But, if you re-run the code after removing the date field in this line, pt_attributes = {"appendkeyfield":str(uuid.uuid4()), "nibrs_code":"345" you will see that it succeeds with this message

image

nanaeaubry commented 1 year ago

@anandak Ok we will look at it, thanks for providing sample code!

anandak commented 1 year ago

Hi @nanaeaubry were you able to reproduce the issue? Can you please confirm if my approach is incorrect or if you know of a workaround?

nanaeaubry commented 1 year ago

@anandak I was able to reproduce your issue at the current version. However, with the newest release: 2.2.0 it has been fixed.

I would recommend upgrading to this version to avoid having to format on your end (which is the workaround). The pre-release on conda was on Monday and the final release for conda/pip will be on September 25th

anandak commented 1 year ago

@nanaeaubry Thanks, will wait for next release.