I'm new to the esri community and would like to know if there is a way to overwrite the data in a hosted feature layer while retaining the layer definition. The issue I am running into is after I execute the python script to overwrite the existing layer, all of custom layer definitions, such as "drawingInfo", "maxRecordCount", and "typeIdFieldID", are set back to the default.
As a workaround, I attempted to retrieve the layer definition by retrieving the 'properties' of the feature layer and then using 'manager.update_definition' to re-establish the custom definition but when I do that I get 'TypeError: Object of type PropertyMap is not JSON serializable'
Any help would be appreciated.
Thank you
Is it possible to share your script?
Yes....below is the code we are currently using the over write the feature layer:
--- Grab the properties of the point layer from the feature layer
gis = GIS("https://YOUR.arcgis.com",'USERNAME', 'PASSWORD')
search_results = gis.content.search('title: THE_LAYER_TITLE','Feature Layer')
item = search_results[0]
the_point_layer = item.layers[0]
the_properties = the_point_layer.properties
--- Over write the feature layer
from arcgis.features.managers import FeatureLayerCollectionManager
the_flc = FeatureLayerCollectionManager.fromitem(item)
the_flc.overwrite(THE_CVS_FILE)
--- Attempt to update the Feature Definition with the properties retrieved from the point layer prior to over writing the layer
the_point_layer.manager.update_definition({'properties': the_properties})
The code above produces the following error message:
'TypeError: Object of type PropertyMap is not JSON serializable'
have a try with:
the_point_layer.manager.update_definition({'properties': json.dumps( the_properties}))
Hi Minbin:
I appreciate your time and help.
When I do this, I get the error:
TypeError: Object of type PropertyMap is not JSON serializable
It was the same for me. It is getting confused here {'properties': the_properties}
In the update_definition method it will do a json.dumps trying to encode the dictionary {'properties': the_properties} to JSON. The issue here is that the_properties is already a json_string, based on how it was retrieved, and in a json string you see json values like null, true, & false. So when creating the dict {'properties': the_properties} you are creating a python dictionary with the key set to a json string.
This confuses the process because json.dumps is looking for None, True, & False values instead of null, true, & false when encoding the string.
So, since the_properties is already a json string, what you want to do before calling the update_definition is convert the json string to a python dictionary or list of dictionaries whatever it may be. Get everything in a python format then pass it to the update_definition where it will encode the dictionary to a json string successfully. At least this is what I have to do in a Jupyter Notebook. It looks something like this:
# parsing _field_info creates a python dictionary,
# where true=True, false=False, & null=None
json_string_to_dict = json.loads(f'{_field_info}')
# Create the update_dict
update_dict = {"fields": json_string_to_dict}
# Update the definition --> the update_definition method uses json.dumps
# to convert the python dictionary to json_string
result = feature_layer.manager.update_definition(update_dict)
print(str(result))
Or in your case:
json_string_to_dict = json.loads(f'{the_properties}')
dict_update = {"properties": json_string_to_dict}
result = feature_layer.manager.update_definition(dict_update)