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

4383
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)
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I think I found the solution, at least for 3.0.2.

You have to make a feature layer, and then use the .getOutput() method on the results object to get the actual layer object. That can be added to a map. You still need to run the script from a toolbox inside of the map, since it's in memory, a standalone python script will add the layer to the map, but the connection to the data source will be broken.

If you look at the datatypes in the second screenshot you can see why it works.

fix1.PNG

fix2.PNG

View solution in original post

10 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
Occasional Contributor

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

 

0 Kudos
JoeBorgione
MVP Emeritus

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

That should just about do it....
0 Kudos
MattHowe
Occasional Contributor

@JoeBorgione  sure, I'm using 2.7.2

0 Kudos
MatthewDriscoll
MVP Alum

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

 

 

 

by Anonymous User
Not applicable

Still seems to be a problem in ArcGIS Pro 3.0.2. 

0 Kudos
by Anonymous User
Not applicable

I think I found the solution, at least for 3.0.2.

You have to make a feature layer, and then use the .getOutput() method on the results object to get the actual layer object. That can be added to a map. You still need to run the script from a toolbox inside of the map, since it's in memory, a standalone python script will add the layer to the map, but the connection to the data source will be broken.

If you look at the datatypes in the second screenshot you can see why it works.

fix1.PNG

fix2.PNG

MattHowe
Occasional Contributor

Interesting, thanks @Anonymous User. I'll give that a shot.

MattHowe
Occasional Contributor

@Anonymous User I can confirm this works. Thanks a lot and happy holidays when you get there!