I am using ArcGIS Pro 2.7.2.
I'm creating a Python script that creates data in the 'memory' workspace as part of a task. I would then like to add the dataset to the current active map as a manual QC step. I run the script via a toolbox from ArcGIS Pro with an active map open. According to the support page on writing geoprocessing output to memory, "You can add memory datasets to a map in ArcGIS Pro.", but I receive the below error with the following code.
mem_lyr = r"memory\OffshoreObjects"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx_map = aprx.activeMap
arcpy.MakeFeatureLayer_management(mem_lyr, "OffshoreObjects_layer")
aprx_map.addLayer(mem_lyr)
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 1734, in addLayer
return
convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile,
add_position), True)))
ValueError: memory\OffshoreObjects
I also get the same error when using .addDataFromPath but from what I understand this is as expected when working with memory datasets.
mem_lyr = r"memory\OffshoreObjects"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx_map = aprx.activeMap
aprx_map.addDataFromPath(mem_lyr)
1 Answer 1
I was able to replicate the problem. I think the issue here is that the default behaviour of the Make FeatureLayer tool is to add the layer to the map. Then immediately using the addLayer() method you add the layer that has already been added and it blows up.
The very simple solution is to remove from your code the line:
aprx_map.addLayer(mem_lyr)
As for addDataFromPath() I am not surprised that did not work as it states in the help file:
The addDataFromPath method provides a way to add a layer to a map in a similar way to how the Add Data From Path button works in the application
Having read that statement I doubt that you have ever been able to navigate to a in_memory dataset through the add data button in ArcMap or ArcGIS Pro.
Explore related questions
See similar questions with these tags.