Select to view content in your preferred language

Replica Error while overwriting hosted feature layers using Python API docs

924
2
03-24-2022 08:33 AM
RehanChaudhary
Frequent Contributor

I am following the documentation here: https://developers.arcgis.com/python/sample-notebooks/overwriting-feature-layers/#Overwrite-the-feat... . I have published a hosted feature layer from csv file added to the portal. Now i want to overwrite the layer completely with a new data. The idea is that the layer should be updated/overwritten as soon as there is a new csv file locally which should be pushed to portal which should automatically updated the feature layer. The section in above documentation suggests that overwrite function should be able to do this but i am unable to get it work. I always get the error: "AttributeError: 'FeatureLayerCollection' object has no attribute 'replicas'"

import json
from arcgis.gis import GIS
import os
import shutil
import pandas as pd
from arcgis.features import FeatureLayerCollection

path_to_json = r"C:\Users\C\arcgis\pass.json"
with open(path_to_json, "r") as handler:
    info = json.load(handler)
username = info["user"]
password = info["password"]
gis = GIS('Home', username, password)

my_csv=r'C:\Users\C\arcgis\New\NO.csv'
data_xls = pd.read_excel(r"C:\Users\C\Desktop\plots_data1.xlsx", "NO", index_col=0)
p=data_xls.to_csv(my_csv, encoding='utf-8')

#result_item=gis.content.add(item_properties ={'title':'NO','item_type':'Feature Layer Collection'}, data=my_csv )
#NO_item=result_item.publish(overwrite=True)

table_ago = gis.content.search('57bb2586a6034f838369763f673b6d07')
cities_flayer_collection = FeatureLayerCollection.fromitem(table_ago[0])
cities_flayer_collection.manager.overwrite(my_csv)

So i first run the code with publish to publish the feature layer initially, then i comment it out and run the code as its shown above by getting ID directly from portal. Is it possible to make it work or should i go for the other method?

0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

Overwriting the whole feature class is usually unnecessary, and can cause issues. Unless you're altering the schema in some way, you're better off using append, perhaps paired with a truncate.

- Josh Carlson
Kendall County GIS
0 Kudos
RehanChaudhary
Frequent Contributor

@jcarlson i just found a solution that worked for me . i am posting it beneath but perhaps you can add as a solution an example of how to use append and truncate to solve this problem.

 

This worked for me:

 

csv = gis.content.get('2b339525042946e3a88f619ad457e84e')
csv.update({}, my_csv)
csv.publish(overwrite=True)