I want to update an arcgis.gis.Item but not change the original. Running update on an item does alter the original. I tried item.copy but this makes a literal copy in portal which I do not want. I just want to copy in memory so that I can make changes without altering the original. What I really want is deepcopy...
But using copy or deepcopy immediately crashes my program without any error. Below demonstrates why I want to do this, in case there's concern around that.
Please note: this is a made up simplified demo example, so if I made any typos in the code by mistake here, don't infer those are connected to my local problem.
gis = GIS(url=url, username=username, password=password)
item = gis.content.get(some_id)
item_copy = copy.copy(item) # crashes the program, so does deepcopy
print("hello") # never runs further than prev line
item_copy.update({'title': 'New_Item_Title"})
gis.content.clone_items(items=[item_copy])
I don't understand why this kills the program, if it's an Esri error or an error with my environment, since there is no error message, except the exception __deepcopy_
If I can't use these copies is there another way to do this?
Hi @chris_del101,
When you say copy an item, do you want to perform on any item or more specifically a Feature Service (Feature Layer Collection) item?
Hi @Clubdebambos,
In this case the the artifacts are objects of the arcgis.gis.Item, but the type is Feature Layer Collection and others are FeatureLayer . These are the only types I am worried about for this question. I am not using the FeatureLayer or Feature Layer Collection types directly, but I am open to if it will do what I need.
This what an item looks like:
<Item title:"Polygon_Map_01" type:Feature Layer Collection owner:Me>
Hi @chris_del101,
Thanks for the info. Does the below meet your requirements? Code is commented to follow along but basically it copies the Feature Layer Collection item which transfers the shell (layers/tables without data) and then we take the records from the original and add them in.
from arcgis.gis import GIS
## Access AGOL
agol = GIS("home")
## get the Feature Service as an Item object
item = agol.content.get("FS_ITEM_ID") # get the Item id for the FLC item to copy
## get the layer indexes
layers = list(range(len(item.layers)))
## get the table indexes
tables = list(range(len(item.tables)))
## copy the item - copies the shell, not data is added to the layers/tables
copied_item = item.copy_feature_layer_collection(
service_name = "MY_COPIED_SERVICE", # rename your new Item here
layers = layers,
tables = tables,
description = item.description,
snippet = item.snippet
)
## for each layer in the original item
for lyr in item.layers:
## get the layer name
item_layer_name = lyr.properties.name
## get the FeatureLayer object for the corresponding layer in the copied item
copied_layer = [lyr for lyr in copied_item.layers if lyr.properties.name == item_layer_name][0]
## get all records from the original layer
item_layer_records = lyr.query()
## add the records to the copied layer
copied_layer.edit_features(
adds = item_layer_records
)
## repeat for any tables
for tbl in item.tables:
item_table_name = tbl.properties.name
copied_table = [tbl for tbl in copied_item.tables if tbl.properties.name == item_table_name][0]
item_table_records = tbl.query()
copied_table.edit_features(
adds = item_table_records
)