方法

ある ArcGIS Online 組織または Portal for ArcGIS から別の ArcGIS Online 組織または Portal for ArcGIS へのコンテンツのコピー/クローン作成

Last Published: March 11, 2024

サマリー

ArcGIS Online Assistant is often considered as the only way to copy contents from one ArcGIS Online organization or Portal for ArcGIS to another. However, the instructions provided below describe how to do this using the ArcGIS API for Python to copy contents web maps, configurable apps, dashboards, shapefiles, file geodatabases, and others.

手順

The following Python script (run from a Jupyter Notebook interface) uses the clone_items method in the arcgis module to copy hosted feature layers and the underlying service from one ArcGIS Online organization to another. The sample cells used are intended for use in Jupyter Notebook, but can be modified for use in a standalone script.

Note:
The user executing the copy should be an admin in the target GIS.
  1. Import the necessary module.
from arcgis.gis import GIS
  1. Specify the URL and credential of the origin ArcGIS Online account. 
username_1 = input("Enter username of source organization: ")

#For ArcGIS Online
gis1=GIS(r"https://arcgis.com", username_1)

#For Portal for ArcGIS
gis1=GIS(r"https://<gisserver>.<domain>.com/portal", username_1)
  1. Specify the username of the account to export the cloned items. 
username_2 = input("Enter username of target organization: ")

#For ArcGIS Online
gis2 = GIS(r"https://arcgis.com", username_2)

#For Portal for ArcGIS
gis2 = GIS(r"https://<gisserver>.<domain>.com/portal", username_2)
  1. Create a list of items to clone. There are a few options to do so:
  • Set the maximum number of items to export:
num_items = 5
items = gis1.content.search(query="owner: {}".format(username_1), max_items=num_items, sort_field='id', sort_order='desc')
  • Get the maximum number of items as input parameter:
num_items = int(input("How many items would you like to clone? "))
items = gis1.content.search(query="owner: {}".format(username_1), max_items=num_items, sort_field='id', sort_order='desc')
  • Get a specific item using item id:
itemid = '<item_ID>' #insert the item id
items = gis1.content.search(itemid)
  • Use the Search function with a query to search for specific items to clone. The general format is as follows:
items = gis1.content.search(query, item_type=None, sort_field='avgRating', sort_order='desc', max_items=10, outside_org=False, categories=None, category_filters=None)
  1. Print the list of the items to be cloned.
print(str(len(items)) + " items will be cloned. See the list below:") 
items
  1. Create a logic to clone the items using the clone_items() function and to handle an error if the cloning process fails.  
def deep_copy_content(input_list):
    for item in input_list:
        try:
            print("Cloning " + item.title)
            copy_list = []
            copy_list.append(item)
            gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            print("Successfully cloned " + item.title)
        except Exception as e:
            print(e)
    print("The function has completed")
deep_copy_content(items)
Note:
As an alternative, the clone_items() function can also be used outside of the logic code block if handling exception is unnecessary. After step 5, the clone_items() function can be used as such:

gis2.content.clone_items([items])

The following shows a sample of a full script:

from arcgis.gis import GIS

username_1 = input("Enter username of source organization: ")
gis1=GIS(r"https://arcgis.com", username_1)

username_2 = input("Enter username of target organization: ")
gis2 = GIS(r"https://arcgis.com", username_2)

#Get a specific item using item id:
itemid = 'cc94b27a35d14f40987d96f3d2a39e67'
items = gis1.content.search(itemid) 

print(str(len(items)) + " items will be cloned. See the list below:") 
items

def deep_copy_content(input_list):
    for item in input_list:
        try:
            print("Cloning " + item.title)
            copy_list = []
            copy_list.append(item)
            gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            print("Successfully cloned " + item.title)
        except Exception as e:
            print(e)
    print("The function has completed")
deep_copy_content(items)

記事 ID:000022252

ArcGIS の専門家からヘルプを受ける

テクニカル サポートへのお問い合わせ

Esri Support アプリのダウンロード

ダウンロード オプションに移動

関連情報

このトピックについてさらに調べる