1

What I am trying to do: Take a layer from ArcGIS Enterprise, clone it as a temporary file, overwrite a file in SQL DB, delete temporary file.

What I have tried so far:

def overwrite_existing_layer(source_layer_name, sql_database, layer_name):
    try:
  
        # Connect to the ArcGIS Portal
        gis = GIS('pro')
        portal_dstore = gis.datastore


        # Retrieve the feature layer you want to clone
        source_layer = gis.content.search(source_layer_name, item_type="Feature Layer")[0]
        
        # Create a temporary copy of the layer
        temporary_copy = source_layer.layers[0].query()
        
        # Save the temporary copy to overwrite the existing layer in the SQL database
Don't know how to do this using arcgis.gis. (tried a variety of things)

        print("Temporary copy overwritten in SQL database.")

        # Delete the temporary copy
        portal_dstore.delete_layers(temporary_copy)

        if not temporary_copy.exists:
            print("Temporary copy deleted.") 
        
        print('Finished running script')

    except Exception as e:
        # Handle any errors that occur during the process
        print(f"An error occurred: {str(e)}")`

Issue: I have not been able to find a way to save the copy to our SQL DB

Alternative option: Just clone the layer in portal and publish it back to portal (ultimate goal is to create view layers of clone which cannot be done on original)

Code I have tried for that:

def overwrite_existing_layer(source_layer_name, layer_name):
    try:
        # Connect to the ArcGIS Portal
        gis = GIS('pro')
        portal_dstore = gis.datastore


        # Retrieve the feature layer you want to clone
        source_layer = gis.content.search(source_layer_name, item_type="Feature Layer")[0]
        
        # Create a temporary copy of the layer
        temporary_copy = ContentManager.clone_items(items=source_layer, folder=folder)


        print("Temporary copy overwritten in SQL database.")

        # Delete the temporary copy
        portal_dstore.delete_layers(temporary_copy)

        if not temporary_copy.exists:
            print("Temporary copy deleted.") 
        
        print('Finished running script')

    except Exception as e:
        # Handle any errors that occur during the process
        print(f"An error occurred: {str(e)}")

Issue: I have not been able to find a way to save the cloned layer to portal (this is probably confused on what is meant by folder. Is that just a folder I'd like it to be saved in within my enterprise portal?

1
  • For anyone that runs into a similar workflow, I have found a bit of a work around since I found no solution to copy and publish a layer... so instead I converted the original layer to a spatial dataframe using the .sdf property. Then just republished a new layer from the data frame. Seems like there has to be a better way but that's what I have found using ESRI's documentation. Commented Jun 7, 2023 at 16:27

1 Answer 1

0

For anyone that runs into a similar workflow, I have found a bit of a work around since I found no solution to copy and publish a layer... so instead I converted the original layer to a spatial dataframe using the .sdf property. Then just republished a new layer from the data frame. Seems like there has to be a better way but that's what I have found using ESRI's documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.