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')

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)
3
  • Is this documented anywhere? I couldn't locate it in their MakeFeatureLayer help.
    – ketar
    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.
    – user2856
    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.
    – user2856
    Commented Feb 28, 2019 at 12:40

Your Answer

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

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