Select Data tool for Python?

994
10
04-06-2017 12:56 PM
by Anonymous User
Not applicable

There is a modelbuilder tool called 'Select Data' that pulls a single  layer out of a group layer.

I am using Python to do geometric tracing and the outputs are group layers.

Does anyone know the Python equivalent of "Select Data"?

I guess I could cycle through and find the right layer, but if there's an easier way, I'd like to know.

I am assuming there is a (relatively) easy way to do this in Python. Otherwise, it would make no sense to deny Python this handy tool.

Thank you,

Randy McGregor

0 Kudos
10 Replies
DanPatterson_Retired
MVP Esteemed Contributor

In arcpy/python you want to get  list things by their type. like list featureclasses etc.  Have a look there to begin with

0 Kudos
IanMurray
Frequent Contributor

Conversely, if you are working a map document you can ListLayers and create a layer object of the layer you need. 

by Anonymous User
Not applicable

Thank you. Yes, I would have to select the group layer output of the "FindPath" tool, then select (in this case) "NHD Flowlines" from the group.

I appreciate the tip. It probably wouldn't be too bad, but I was hoping there would be a single tool available for this.

0 Kudos
by Anonymous User
Not applicable

Thanks. Looks like ESRI is going to make me work for this one  'Select Data' is very handy when you are in ModelBuilder.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

so working outside of arcmap, there is the arcpy.mapping module... tuck that away since it may come in handy

0 Kudos
curtvprice
MVP Esteemed Contributor

The help discusses using Select Data with this tool but indeed is missing guidance on how to best go at this in Python: Trace Geometric Network—Help | ArcGIS Desktop 

An issue here (mentioned in the tool help) is that group layers are not supported in some environments. 

This is especially useful when publishing your model to ArcGIS Server, since group layers are not supported as an output parameter type from a geoprocessing task. Because they aren't supported as direct output, you need to use the Select Data tool to create a single (nongroup) layer for output.

I posted feedback to the help author that they should add some Python guidance to the help page. 

We ran into this and the solution was (as Dan suggested) arcpy.mapping.ListLayers run on the group layer output. Below I use a list comphension to get the layer you want by search string

trace_lyr = arcpy.TraceGeometricNetwork_management(...)
#Create a Mapping Layer out of the Group Layer
glyr = arcpy.mapping.Layer(trace_lyr)
# Extract the layer based on a search string
tlyr = [lyr for lyr in arcpy.mapping.ListLayers(glyr) 
        if "net" in str(lyr).lower()][0]
# copy out to a feature class
arcpy.CopyFeatures_management(tlyr, tfeat)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DanPatterson_Retired
MVP Esteemed Contributor

Curtis... r1 ... repr(r1)?

0 Kudos
curtvprice
MVP Esteemed Contributor

Dan, sorry I deleted my previous post, as I found the answer in an old email exchange, and you jumped on it too fast!

For future readers, my previous (deleted) post suggested examining the output of the trace geometric networks tool to figure out what to do next:

r = arcpy.TraceGeometricNetwork_management(...)
r1 = r.getOutput(0) # get output from result object
print(r1)
print(repr(r1))
‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The reason to print both is because sometimes the str() and repr() of an object are different and looking at both can give you a clue what the tool actually returned if you are unsure.

In this case I think you'd get something like "Trace" for str() and '<Group layer geoprocessing object at 239478219821>' for repr()

0 Kudos
by Anonymous User
Not applicable

Thanks for the information.

Having thunk on it over the weekend, I think there is a fairly straightforward solution in Python for my purposes.

With the trace tool, you can set the name of the output layer group.

The layer I am interested in is always (for now) "NHDFlowlines"

So, if I set the name of the output group as, "Downstream_Trace_Group" I can simply run:

arcpy.CopyFeatures_management("Downstream_Trace_Group\\NHDFlowlines",in_memory\downstream_trace_line")

I have not tried it, but I'm hoping it'll do the trick.