Select to view content in your preferred language

OverwriteFS - Python example of OverwriteFS using View to A/B Service

1210
3
Jump to solution
02-13-2023 10:30 PM
GeorgieCassar1
Regular Contributor

I have imported the OverwriteFS module and wish to overwrite a feature service using the Feature layer View that alternates pointing to an A and a B version.

The tool's documentation does not provide an example code for this approach.  It's difficult to fully understand without an example.

Is anybody able to share how they applied python commands for this overwriting approach ?

 

Here is a link to download the OverwriteFS module if needed.

https://www.arcgis.com/home/item.html?id=d45f80eb53c748e7aa3d938a46b48836

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GeorgieCassar1
Regular Contributor

I worked it out and have uploaded my code here.  I hope it helps somebody.

 

import sys
sys.path.insert(0, r'C:\myscripts\OverwriteFS')

import os
import OverwriteFS

# Import ArcGIS Python API and make a connection to Portal
from arcgis.gis import GIS
gis = GIS(username="johndoe", password="johnspwd")

# Get View item from Portal
itemId = "84a62b29fff5477f814ac7db77fb223c"  # ID for feature layer view
parcelview = gis.content.get(itemId)

# set up the relationship of the view to A and B feature service items  (I only had to run this once and the relationships held)
#relitems = ["6bbd1f51215f459796c8f026344e13a4","bf5680ef5e294d7t507810b3e448a735"]  # related feature layers A & B versions. The view will point only to one of these alternating.
#OverwriteFS.updateRelationships( parcelview, relateIds=relitems, unRelate=False, verbose=True, outcome=None, dryRun=False)

# Determine which layer is Current and which layer is Target at the moment 
outcome = OverwriteFS.getFeatureServiceTarget(parcelview, verbose=None, outcome=None, ignoreDataItemCheck=False)
#  will check for error here and action it

# Set correct path-filename for source service definition
sdpath = r'C:\service_definitions'    #  In this folder, I have 2 service definition files, one named after the A version and one after the B
sd_fname = outcome["filename"]   # name of target service definition.  
print ('sdfname = ', sd_fname)
sourcefile = os.path.join(sdpath, sd_fname)

# Run the swapFeatureViewLayers function that updates target (idle) service and then Points the view to newly updated service.
# This also backs up the service's properties (eg. Summary, Tag etc) and restores it back to the service after overwrite
dry_run = False  # Set it to true to run it in test mode where it just veries everything without making an overwrite or swapping
Service_Prop_Backup_Folder = r'C:\property_service_backup'
outcome = OverwriteFS.swapFeatureViewLayers(parcelview, updateFile=sourcefile, touchItems=True, verbose=True, touchTimeSeries=False, outcome=None, noIndexes=False, preserveProps=True, noWait=False, noProps=False, converter=None, outPath=Service_Prop_Backup_Folder, dryRun=dry_run, noSwap=False, ignoreAge=False)

# Check results
if outcome["success"]:
    print( "Service Overwrite was a Success!")

elif outcome["success"] == False:
    print( "Service Overwrite Failed!")

 

 

View solution in original post

3 Replies
GeorgieCassar1
Regular Contributor

I worked it out and have uploaded my code here.  I hope it helps somebody.

 

import sys
sys.path.insert(0, r'C:\myscripts\OverwriteFS')

import os
import OverwriteFS

# Import ArcGIS Python API and make a connection to Portal
from arcgis.gis import GIS
gis = GIS(username="johndoe", password="johnspwd")

# Get View item from Portal
itemId = "84a62b29fff5477f814ac7db77fb223c"  # ID for feature layer view
parcelview = gis.content.get(itemId)

# set up the relationship of the view to A and B feature service items  (I only had to run this once and the relationships held)
#relitems = ["6bbd1f51215f459796c8f026344e13a4","bf5680ef5e294d7t507810b3e448a735"]  # related feature layers A & B versions. The view will point only to one of these alternating.
#OverwriteFS.updateRelationships( parcelview, relateIds=relitems, unRelate=False, verbose=True, outcome=None, dryRun=False)

# Determine which layer is Current and which layer is Target at the moment 
outcome = OverwriteFS.getFeatureServiceTarget(parcelview, verbose=None, outcome=None, ignoreDataItemCheck=False)
#  will check for error here and action it

# Set correct path-filename for source service definition
sdpath = r'C:\service_definitions'    #  In this folder, I have 2 service definition files, one named after the A version and one after the B
sd_fname = outcome["filename"]   # name of target service definition.  
print ('sdfname = ', sd_fname)
sourcefile = os.path.join(sdpath, sd_fname)

# Run the swapFeatureViewLayers function that updates target (idle) service and then Points the view to newly updated service.
# This also backs up the service's properties (eg. Summary, Tag etc) and restores it back to the service after overwrite
dry_run = False  # Set it to true to run it in test mode where it just veries everything without making an overwrite or swapping
Service_Prop_Backup_Folder = r'C:\property_service_backup'
outcome = OverwriteFS.swapFeatureViewLayers(parcelview, updateFile=sourcefile, touchItems=True, verbose=True, touchTimeSeries=False, outcome=None, noIndexes=False, preserveProps=True, noWait=False, noProps=False, converter=None, outPath=Service_Prop_Backup_Folder, dryRun=dry_run, noSwap=False, ignoreAge=False)

# Check results
if outcome["success"]:
    print( "Service Overwrite was a Success!")

elif outcome["success"] == False:
    print( "Service Overwrite Failed!")

 

 

Bee_Smith
New Contributor

Hi,

Did you get this solution working? We are attempting a similar work flow for updating our data and switching views (and your script has been really helpful) but have been having issues with updating relationships part. Just wondering if you have come across any issues?

0 Kudos
GeorgieCassar1
Regular Contributor

Hi

I did get it working but have since purchased FME and am building much better processes that detect  and update with changed records only.   Main reason for FME was reducing need for a python coder to maintain it in future.  Also, I am not a developer so my code design may have been somewhat flimsy. Anyhow, I will show you some python code fragments that I used in my original solution.  

The main steps we ended up doing were:

1.   Create a temporary File gdb of the origin layer.  Zip it and upload to portal.  This will be used as the source data in Portal.

Then the following steps.  The code snippet for each is in the attached file.

# Determine which feature service (A or B version) is the target for updating

# Delete rows in the Target layer (by chunks)

# Truncating Feature Service Before Append to avoid growing object id values

# Append the data from the source file Gdb onto target hosted layer

# Touch the target feature service to update it's last modified date

# Swap the Current and Target layers for the service's view

# Delete Uploaded File Geodatabases. (All FGDBs owned by this user.)

 

 

 

 

 

0 Kudos