Select to view content in your preferred language

How do I add a layer in the 'memory' workspace to the current active map in ArcGIS Pro using Python

4400
10
Jump to solution
08-03-2021 07:53 AM
MattHowe
Occasional Contributor

According to the ArcGIS Pro support pages, "You can add memory datasets to a map in ArcGIS Pro.".

I have a layer in the 'memory' workspace that I would like to add to my current active map.  I've tried:

mem_lyr = r"memory\Test_Layer"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx_map = aprx.activeMap
aprx_map.addDataFromPath(mem_lyr)

but get the below error. Is there a correct way to add layers in memory to the current map?

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 1862, in addDataFromPath
    return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError

 

Tags (3)
10 Replies
EricEagle
Occasional Contributor III

@Anonymous User Thanks for this! I ran into this same issue in a larger geoprocessing tool where I wanted to display rapid, ephemeral results and the IO expense really hurt user experience.  Based on your solution I threw together a helper function that I reference in a bunch of scripts now:

def memory_to_active_map(self, memory_fc):
    active_map = arcpy.mp.ArcGISProject("CURRENT").activeMap
    lyr_result = arcpy.MakeFeatureLayer_management(
        memory_fc, arcpy.Describe(memory_fc).name)
    mem_lyr = lyr_result.getOutput(0)
    return active_map.addLayer(mem_lyr)[0]