Select to view content in your preferred language

Can I use clone_items() to make a deep copy of a Feature Service within the same ArcGIS Online?

368
6
08-08-2024 06:36 AM
Labels (2)
Clubdebambos
Frequent Contributor

API version 2.3.0

The Item object copy() and copy_item() both create a shallow copy of a feature service. If I edit the data in the copy it is reflected in the original.

The Item object copy_feature_layer_collection() creates a deep copy but does not include data and only copies the schema.

When I attempt to use clone_items() from the manager I get a consistent error.

 

from arcgis.gis import GIS

## Access AGOL
agol = GIS("home")

## get item object to clone
item = agol.content.get("ITEM_ID")

## use the ContentManager clone_items()
new_item = agol.content.clone_items(
    items = [item]
)

print(new_item)

 

 

The error...

 

arcgis._impl.common._clone._ItemCreateException: ("Failed to create Feature Service MY_NEW_FEATURE_SERVICE: A general exception was raised: HTTPSConnectionPool(host='services-eu1.arcgis.com', port=443): Max retries exceeded with url: /xImpnbbhc3YDkwTJ/arcgis/rest/services/County_Boundaries_d56b3/FeatureServer/0/applyEdits (Caused by ResponseError('too many 413 error responses'))", <Item title:"MY_NEW_FEATURE_SERVICE" type:Feature Layer Collection owner:FinalDraftMapping>)

 

 

Any help greatly appreciated. How do I make a deep copy of a feature service within the same ArcGIS Online instance. Is the only way to use copy_feature_layer_collection() and then append in the data from the original feature service?

 

Cheers,

Glen

~ learn.finaldraftmapping.com
0 Kudos
6 Replies
Clubdebambos
Frequent Contributor

This is how I can do it by adding in the data from the original. But surely there's a better way?

from arcgis.gis import GIS

## access agol
agol = GIS("home")

## item to copy
item = agol.content.get("FS_ITEM_ID")

## {layer_name : data}
layer_info = {fl.properties.name : fl.query() for fl in item.layers}
table_info = {tbl.properties.name : tbl.query() for tbl in item.tables}

## all ids for layers and tables to copy
## from https://community.esri.com/t5/arcgis-online-questions/create-a-duplicate-hosted-feature-layer-with/td-p/1267524
layer_ids = [fl.properties.id for fl in item.layers]
table_ids = [tbl.properties.id for tbl in item.tables]

## copy the feture service
copied_item = item.copy_feature_layer_collection(
    service_name = "My_Copied_FS_Item",
    layers = layer_ids,
    tables = table_ids
)

## loop through copied and add the data from teh original where the name
## of each layer matches from the new to the original.
for fl in copied_item.layers:
    feature_set = layer_info[fl.properties.name]
    fl.edit_features(adds=feature_set)
    print(fl.properties.name)
~ learn.finaldraftmapping.com
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi @Clubdebambos,

You could use the Copy Hosted Feature Services tool in the following toolset.  You would just have to update the 'title' in the item_properties variable on line 67 so it does error trying to create a service with the same name.  Ex:

JakeSkinner_0-1723128250226.png

 

Clubdebambos
Frequent Contributor

Hi @JakeSkinner 

Thanks for the reply. It seems like it is a little overkill to have to download and re-publish just to make a copy a feature service. I am also looking for a solution that doesn't use ArcPy (although I can see that is mainly for the custom tool and setting the workspace in ArcGIS Pro and can be taken out to just use the API) and is purely the ArcGIS API for Python / Python. I was hoping that I have simply missed a simple method call amongst the copy methods 😅

Fantastic toolset all the same, thanks for sharing, I definitely have use for that for other workflows.

Perhaps one for the IDEA suggestions (if not already in there)

Cheers,

Glen

~ learn.finaldraftmapping.com
0 Kudos
EarlMedina
Esri Regular Contributor

In the past, I regularly did same-portal clones to make backups of services for testing. I've just tested it again now on version 2.3.1 of the API and had no problems.

Is it possible the problem is isolated to certain items? Are you cloning a hosted feature layer? Also, does it make any difference if you explicitly log into the GIS instead of using "home" ?

0 Kudos
Clubdebambos
Frequent Contributor

Hi @EarlMedina 

Yes cloning a Hosted Feature Service in the same ArcGIS Online. I had tested on multiple Hosted Feature Services and all failed, but your response prompted me to try some more and one has just worked, which is interesting and frustrating at the same time 😅 

I'll now go down the rabbit hole to find out the differences between those that didn't work and the one that did! They're all standard with no views or anything exciting done to them.

~ learn.finaldraftmapping.com
0 Kudos
Clubdebambos
Frequent Contributor

Ok, I see the difference in those that work and those that do not and it seems to be the size of the data within them. This Community entry here seems to have the resolution but it is probably for Portal is they are talking about IIS..

~ learn.finaldraftmapping.com
0 Kudos