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

715
5
08-03-2021 07:53 AM
MattHowe
New Contributor III

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)
5 Replies
JoshuaBixby
MVP Esteemed Contributor

Most ArcPy functions and GP tools understand the "memory" and "in_memory" aliases, but not all of them.  The addDataFromPath method of the ArcPy Map object does not understand or honor it.

I suggest changing your workflow to start with Make Feature Layer (Data Management)—ArcGIS Pro | Documentation and then use addLayer method of Map—ArcGIS Pro | Documentation.

MattHowe
New Contributor III

Thanks Joshua. Unfortunately I get the same error. I'll contact Esri support.

 

0 Kudos
JoeBorgione
MVP Esteemed Contributor

@MattHowe ; can you tell us what version of arcgis pro you are using? 

That should just about do it....
0 Kudos
MattHowe
New Contributor III

@JoeBorgione  sure, I'm using 2.7.2

0 Kudos
MatthewDriscoll
MVP Regular Contributor

Still does not work in 2.9.3 - Copy Features works but not Make Feature Layer. 

Code runs fine in 2.4.2.

Code runs fine in 2.9.3 when ran in the interactive widow.  If fails when run in a Toolbox.  

 

 

Tempcopy = arcpy.CopyFeatures_management(ParOrionLayer, "memory/tempParReg")

arcpy.MakeFeatureLayer_management(Tempcopy, "memory/tempO")

 

 

Traceback (most recent call last):
  File "<string>", line 91, in execute
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py", line 1287, in Describe
    return gp.describe(value, data_type)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 384, in describe
    self._gp.Describe(*gp_fixargs(args, True)))
OSError: "memory/tempO" does not exist

Failed to execute (PDFTool).

 

With addLayer

 

ParOTempcopy = arcpy.CopyFeatures_management(ParOrionLayer, "memory/tempParReg")
ParOrion = arcpy.MakeFeatureLayer_management(ParOTempcopy, "memory/tempOrion")
m.addLayer(ParOrion, "TOP")
  File "<string>", line 85, in execute
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 1843, in addLayer
    return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: memory/tempOrion