How to remove and create structural associations in python toolbox

356
4
04-15-2024 05:35 PM
EstherSmith_Dev
New Contributor III

Hi all,

I have a requirement to develop a python tool to import  streetlights (Electric Junction) using a CSV/Excel file. UNM editor runs this tool from ArcGIS pro map by providing CSV file, StructureJunction layer and ElectricJunction layer. CSV file provides details about the streetlights including unique pole ID (Structure Junction) it resides on and also if any existing streetlight on the pole to be removed. Unique pole id is used for the XYZ location of the streetlight. Processing needs to happen in a non-default branch version that current ArcGIS Pro map is connecting to. Here is my workflow

  1. Ensure current active map has UN topology layer and other required layers such as StructureJunction (for poles) and ElectricJunction (for streetlights) and is connecting to a non-default version.
  2. For each line in CSV file
    1. Read streetlight details including pole id.
    2. Find pole by the id and get its geometry to add a streetlight using.
    3. If there is existing streetlight on the pole, disassociate this streetlight from the pole and change its state to "Removed".
    4. Add a new streetlight.
    5. Create structural association between the streetlight and pole.
    6. Validate the location for topology.
    7. Go to next line  

 I can create a new streetlight using arcpy.da.InsertCursor on the streetlights layer. But I cannot see a python tool for creating association. Only tool available is import associations  https://pro.arcgis.com/en/pro-app/latest/tool-reference/utility-networks/import-associations.htm   It feels this is a overkill.

Even if I create a CSV file for the association to be created and import it, I cannot see a way to remove association for existing streetlight that needs to be disassociated from the pole for the new streetlight to be added. Is there a better way to create and remove associations using arcpy?

I tried to go down the URL way by finding the REST URL for the Streetlight layer and add a record using "addFeatures". And then I tried to add / remove association record using association layer's (layer id 500001) addFeatures and deleteFeatures. But calling REST API from a python toolbox gives me 

Error: Cannot acquire a lock error.

Failed to add Streetlight ID #xxxxxx: {'error': {'code': 500, 'extendedCode': -2147220947, 'message': 'Unable to complete operation.', 'details': ['Error: Cannot acquire a lock. [StreetlightsProcessing].']}}

StreetlightsProcessing is the branch version name.

The add features and other REST operations called from python tool are using same version the current map in Pro is connecting to so that user can ensure everything is correct and then reconcile and post. It seems that having Pro map open connecting to a branch version puts a lock on the version and stops any other thread being able to perform any edits. Is this by design? Should version readers block version writer?  

Any idea on how I can achieve my workflow?

Thanks in advance

 

0 Kudos
4 Replies
JuanCarlosOrtiz
New Contributor

Hi Esther,

I guess you could change the version of your map UN workspace to DEFAULT (using arcpy.management.ChangeVersion) in order to release the lock on your target version, make changes through REST or Python API and change back to your target version. 

That worked for me before, but changing the version through the user interface.

 

0 Kudos
EstherSmith_Dev
New Contributor III

Thanks for your reply. I have added version name as a parameter to the python tool and expect current map to be using Default version before running the tool. That way I can still call REST API to add update etc in the specific version.

What I really need is an arcpy tool to add an association record between two UNM assets. Seems like it is missing from the UNM toolbox.

0 Kudos
RobertKrisher
Esri Regular Contributor

In order to edit the data you will need to start an edit operation then start editing. once you've completed your edits you will need stop your edit operation and stop editing (with save edits = true).

If you want to go down the route of performing these edits in a version, which is a good idea since it lets you QaQc or roll back changes if something fails partway through, you should look at using the ArcGIS API for Python. It will allow you to create a version, perform your edits, validate, reconcile and post, all from a standalone python script (or within ArcGIS Pro if you prefer that approach).

0 Kudos
EstherSmith_Dev
New Contributor III

Thanks for your reply. 

I can add or update to the UNM layers using arcpy cursors etc with or without start/stop editing. But I do not have any arcpy tool to create structural association between light added and a pole.

 

0 Kudos