Select Output of TraceGeometricNetwork_management()

1878
4
01-15-2014 06:35 PM
Status: Open
Labels (1)
ChadMarch1
New Contributor III

It's great that I can trace a geometric network with python now, but that's all that I can do. The output is a 'Group Layer' which isn't usable in stand alone python.

Add an option or change the output in the TraceGeometricNetwork tool to select the traced features classes in the network (or just select them by default!). That way I can iterate through the polyline feature classes and do the obscure

print "Number of selected features:", str(len(desc.fidSet.split(";")))

to see if any of them were selected by the trace (By the way, y'all should add 'Selected' as a argument in the GetCount_management tool too).

That way I can do things like get the total length of the trace, total volume of the traced path, merge/dissolve/export the trace, etc.

4 Comments
DavidCrawford
Hi,

I'm glad you like our new tracing GP tool.  And the access through Python.

What you are requesting is actually something I was looking for when we initially wrote this tool.  Here's a simple python snippet for iterating through a group layer, and getting the selected features. Remember, for the GetCount_management() and other GP tools, if they take in a layer, they will honor the selection (if there is one) on the input layer.

Please excuse any formatting issues, and remember to re-format for proper compilation (this window is not the best for code snippets that care about white space)

#Run the Trace, passing in all the required parameters
arcpy.TraceGeometricNetwork_management({FULL PATH TO YOUR GEOMETRIC NETWORK}, {OUTPUT GROUP LAYER NAME}, "flags", "FIND_CONNECTED")
#Assign the output group layer to a variable, to be used later
lyrs = {OUTPUT GROUP LAYER NAME}
#Create a Mapping Layer out of the Group Layer
groupLayer = arcpy.mapping.Layer(lyrs)
#Get a list of all the mappping layers, the first return will be the Group Layer itself
newLyrs = arcpy.mapping.ListLayers(groupLayer)
#Iterate through all of the Layers (mapping layers) 
for lyr in newLyrs:
    #Because the first return is the group layer itself, we check for this, and move on.
    if not lyr.isGroupLayer:
        #For each of the Layers you can run any analysis you'd like, here, I'm getting the count of selected features in each layer
        #I'm printing out a string of the name for each layer, and it's selected feature count.
        print "Layer name is: {0}. Count is: {1}.".format(lyr.name, str(arcpy.GetCount_management(lyr)))


If you would like any other information on this topic, and are planning to attend the Developers Summit in Palm Springs, I put on a Demo Theatre session about how to consume all of the Geometric Network Geoprocessing tools using python, and the web.

Using Geometric Networks with Python and Geoprocessing Services
Wednesday, 3/12/2014:  
10:30 AM - 11:30 AM
ChadMarch1
Thanks for letting me know and for the example. Shortly after posting this I was able to get the script to work luckily. It always seems to happen that way. I was thrown off by the documentation which talked about the "SelectData" tool which is a model builder only tool and the script examples didn't demonstrate anything being done with the output.

I do wish though that the tool would return an array of the features selected by the trace rather than a "group layer". Not only would it be more intuitive for users and easier, it also takes care of having to extend the code by iterating through all the layers that have a selected record count with the python snippet you provided (although I do appreciate that).  

Python is quick, but since the trace function already knows which features it's selecting when it's doing it's thing, why not have it populate an array to pass back? The way I'm using it, the script iterates through a point feature class of ~10,000 and traces downstream from every one of them which takes about 5 hours to complete. And that's on a network of a medium sized city within LA County.  To run it against the entire county would take forever so to have to then loop through the layers to see which are selected after every trace would probably double the script time because it only takes under 3 seconds to complete a trace, and perform the calculations I need.

Thanks again for your response. I do plan on attending and will definitely stop by your area.
ChadMarch1
Oh and the script examples in the documentation are also wrong.
The pic below shows the tool being called from the "GP" module but to successfully use it, I had to refer to it as you did "arcpy.TraceGeometricNetwork_management()". That's also why I struggled to get it to work.

The python examples in the pic work when used in the immediate Python window of ArcMap, but not in stand alone scripts as the documentation suggests.

0EME0000000TjRy
DavidCrawford
Thanks for pointing out the errors in the help document, and i will do what I can to get the samples corrected.  There is actually a very good technical explanation for why we needed to make the output of the trace a group layer, but it requires more engagement than I can provide in this comments section.

My name is David Crawford and I will be working at the GDB island in the ESRI Showcase area throughout Monday, Tuesday and Wednesday of the Dev summit.  Please swing by and we can have a conversation all about this topic.  Also, you can find me at the Demo Theatre.  

Hope to see you there!