7

I have a geometric network set-up with stream lines and water users. I am trying to use ArcPy to automate tracing downstream of each node/junction in my geometric network to get userIDs for all downstream nodes. I can't figure out how to access the selected nodes in the layer group that is output from the tracing. Arcpy.searchcursor("sublayer name") does not seem to be able to find my sublayer. How can I access just the selected nodes from my tracing?

When I use ListLayers as follows, it doesn't include the trace output layer group with selections even if I can see it in the TOC:

    arcpy.TraceGeometricNetwork_management(myNetwork,outNet,mypts,"TRACE_UPSTREAM")
    layers = arcpy.mapping.ListLayers(mxd)
    for layer in layers:
       if layer.isGroupLayer:
          if layer.name == arcpy.mapping.Layer(outNet):
             for subLayer in layer:
                if subLayer.name == userLayer:
                #Get a list of downstream user IDs
                   downstreamUsers = []
                   rows = arcpy.SearchCursor(userLayer)
                   for row in rows:
                      downstreamUsers.append(row.HYDROID)
                   del rows, row
           print "downstream user IDs", downstreamUsers

So I also tried referring specifically to the trace output layer and then the SearchCursor says it can't find my sublayer with the user nodes:

  arcpy.TraceGeometricNetwork_management(myNetwork,outNet,mypts,"TRACE_UPSTREAM")
    layer = arcpy.mapping.Layer(outNet)
    for sublayer in layer:
       if subLayer.name == userLayer:
          #Get a list of downstream user IDs
          downstreamUsers = []
          rows = arcpy.SearchCursor(userLayer)
          for row in rows:
             downstreamUsers.append(row.HYDROID)
             del rows, row
      print "downstream user IDs", downstreamUsers
4
  • If you look at the Usage section of this tool in the Help file it talks about using the Select Data tool to access the layers. Although that tool says it is principally for model builder have a go?
    – Hornbydd
    Nov 22, 2015 at 19:55
  • 2
    Apparently r'group layer name/sublayername' works for referring to a sublayer. So I used that and some of the tricks here (github.com/marchchad/Trace-StormDrains/blob/master/Scripts/…) to automate my trace in python. I think Select data is not available in arcpy.
    – Cicatrixx
    Nov 30, 2015 at 22:39
  • @Polygeo these are very basic tasks providing you can step away from using standard tools. NetworkX module does it with ease. Is this of interest?
    – FelixIP
    Nov 7, 2017 at 3:15
  • @FelixIP I'm not seeking an alternative technology for this. The trick given in the only answer is exactly what I needed and want to reward.
    – PolyGeo
    Nov 7, 2017 at 3:23

1 Answer 1

8
+200

A helpful trick is to access the group layer output from its in_memory location:

arcpy.env.workspace = r'in_memory' #Group Layer resides in in_memory
#forget the map reference, and go straight to its source    

userLayer = #make sure this is defined
downstreamUsers = []    

arcpy.TraceGeometricNetwork_management(myNetwork,outNet,mypts,"TRACE_UPSTREAM")

for layer in arcpy.mapping.Layer(outNet): #referencing Group Layer in_memory
    for x in arcpy.mapping.ListLayers(layer): #ListLayers works in_memory too, to list layers inside of group layer
      if x.name == userLayer:
        rows = arcpy.da.SearchCursor(userLayer, 'HYDROID') #Better to use the newer search cursor with a specified field
        for row in rows:
          downstreamUsers.append(row[0]) #cursors make tuples, and you want elements in the 0th position of the tuple

print "downstream user IDs", downstreamUsers

This is very similar to a question I posted around the same time. I pasted my full working code, if you want to check it out: https://gis.stackexchange.com/a/176926/56700

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.