0

I am making a feature layer and applying an expression to essentially apply a definition query to the source feature class. I then want to add the created feature layer to the map. I am aware of addDataFromPath but I don't want to add the entire feature class. I want to add the layer with a definition query applied. I could save the new layer to file and add it that way but it seems redundant to have the feature class as well as its subset stored on file. Does anyone know how I can add this type of layer to the map in ArcGIS Pro?

c_project=arcpy.mp.ArcGISProject("current")
c_map=c_project.listMaps()[0]
arcpy.MakeFeatureLayer_management ("pts","pts_layer", 'Solution = 1')
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Feb 26, 2019 at 21:04

1 Answer 1

5

You can use the Map.addLayer method. You'll need to get a reference to the layer from the Result object that MakeFeatureLayer returns.

# MakeFeatureLayer returns a Result object. 
# Use the Result.getOutput method to get a reference to the Layer object
pts_layer = arcpy.MakeFeatureLayer_management ("pts","pts_layer", 'Solution = 1').getOutput(0) 
c_map.addLayer(pts_layer)
answered Feb 26, 2019 at 23:05
3
  • Is this documented anywhere? I couldn't locate it in their MakeFeatureLayer help. Commented Feb 27, 2019 at 19:08
  • @ketar It's documented in the Tool Output section of the Using tools in Python page. But yes I think it should be referred to in the help pages of each tool. Commented Feb 27, 2019 at 20:34
  • 1
    @ketar, if you are writing a toolbox script or python toolbox tool, you can also just define an output parameter, then set the layer to the output parameter and it will get added to the map automatically when the tool completes. Commented Feb 28, 2019 at 12:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.