0

I am trying to remove layers from an ArcGIS Pro 1.1 project, but I receive a ValueError with the traceback of

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 1280, in removeLayer
    return convertArcObjectToPythonObject(self._arc_object.removeLayer(*gp_fixargs((remove_layer,), True)))

Here is code to add raster layers from a geodatabase

rasters = arcpy.ListRasters("*",None)
aprx = arcpy.mp.ArcGISProject("Path/to/aprx")
aprxMap = aprx.listMaps("Map")[0]
for raster in rasters:
     raster = env.workspace + "\\" + raster
     aprxMap.addDataFromPath(raster)
     aprx.save()

This successfully adds the raster objects as layers to the current map, but when I try to remove using the following code, I get an error.

for raster in rasters:
         aprxMap.removeLayer(raster)
         aprx.save()

Any ideas why this is happening?

Raster is a string, and when I have tried to write as an input string, I still receive the error.

1

The removeLayer method is expecting a Layer object not the raster name as a string.

Try using the listLayers method on the map object. Then you could identify the raster name from the layer object and check it against your list of strings.

Something like:

layers = aprxMap.listLayers()
for layer in layers:    
    if layer.name in rasters:
        aprxMap.removeLayer(layer)
        aprx.save()
  • This advice applies to other issues that I am having. The problem is that the python snippet one can export from the project displays the input as a string. It does not state that it is to be a pointer to an object. – user44796 May 15 '18 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.