TransWikia.com

Adding a feature class to the current ArcGIS Pro map with ArcPy

Geographic Information Systems Asked by svd on March 1, 2021

I wrote some simple code that created multiple polygons using the Buffer tool.
I will use it in a toolbox and therefore want the outFeatureClass to be automatically added to the current map.

The following code doesn’t work. "outcome" is the outFeatureClass I want to add.

pw = r"C:UsersMainGISDataArcGISProjectArcGISProject.gdboutcome"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
aprxMap.addLayer(pw)

Running the code in the ArcGIS Pro Python command line I receive an error:

 "ValueError: r"C:UsersMainGISDataArcGISProjectArcGISProject.gdboutcome"

The env.workspace for the Python file is set to r"C:UsersMainGISDataArcGISProjectArcGISProject.gdb".

One Answer

If you read the help file on the addLayer() method for a map it takes a "Layer or LayerFile object". You have passed in pw which is a string which happens to represent a path to a Feature Class, so not a Layer Object. ArcPro wants you to access a dataset via a layer file but as you are directly accessing the FeatureClass I assume a lyrx file does not exist.

You create a Layer object using the MakeFeatureLayer tool.

So this is what you can do in ArcPro 2.6:

with arcpy.EnvManager(addOutputsToMap=True):
  pw = r"C:UsersMainGISDataArcGISProjectArcGISProject.gdboutcome"
  arcpy.MakeFeatureLayer_management(pw,"Outcome")

Answered by Hornbydd on March 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP