0

I have a file from a file geodatabase, which contains all raster files, that I want to save to an already existing layer file. I am using ArcGIS Pro.

I am trying this like so:

import arcpy
import os
arcpy.env.overwriteOutput = True

#read in the file geodatabase
dnbr = "X:/user/pre_85/dnbr.gdb"

#save all raster names
arcpy.env.workspace = dnbr
all_rasters = arcpy.ListRasters() #this returns ['test', 'test2']

#get the first raster in the list
test = all_rasters[0]

#read in the lyr file
lyr = arcpy.mp.LayerFile("X:/user/arcpy_maps/New_Group_Layer.lyrx")

#list the existing layers in the group layer file
lyr.listLayers() #this returns [<arcpy._mp.Layer at 0x91f3e9c8d0>, <arcpy._mp.Layer at 0x91f3e9c7f0>]

#now lets add the test layer to the third location of lyr
for_adding = os.path.join(dnbr, test)

lyr.addLayerToGroup(for_adding, [2]) #add to the third location, since two exist

but this returns the following value error:

  File "<ipython-input-15-07b8bd305e3c>", line 1, in <module>
    lyr.addLayerToGroup(one, [2])

  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 739, in addLayerToGroup
    return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))

ValueError: X:/user/pre_85/dnbr.gdb\test

1 Answer 1

1

A few issues with your code:

  1. You are trying to add a raster dataset not a raster layer to your group layer
  2. You are not specifying a group layer to add your raster to.
  3. You are trying to specify an index for the add_position argument, when it should be one of "AUTO_ARRANGE", "BOTTOM" or "TOP" (The default value is AUTO_ARRANGE).

The correct syntax is LayerFile.addLayerToGroup(target_group_layer, add_layer_or_layerfile, {add_position})

Don't forget the LayerFile is not a GroupLayer, it is the parent of the GroupLayer.

Here's a worked example:

import arcpy
import os

arcpy.env.overwriteOutput = True

dnbr = r"X:/user/pre_85/dnbr.gdb"
arcpy.env.workspace = dnbr
all_rasters = arcpy.ListRasters() #this returns ['test', 'test2']

#get the first raster in the list
test = all_rasters[0]

#read in the lyr file
lyr = arcpy.mp.LayerFile("X:/user/arcpy_maps/New_Group_Layer.lyrx")
 
# make a raster Layer
for_adding = arcpy.management.MakeRasterLayer(os.path.join(dnbr, test), "TEST RASTER LAYER")[0]

#Get the group layer, assumes there's at least one GroupLayer in the LayerFile
grplyr = [grplyr for grplyr in lyr.listLayers()  if grplyr.isGroupLayer][0]

# add the raster layer to the group in the layer file
lyr.addLayerToGroup(grplyr, for_adding, "BOTTOM") #add to the BOTTOM location

# Do what you want with the LayerFile, save it, save a copy, add it to a map etc...
lyr.saveACopy (r"X:/user/arcpy_maps/New_Group_Layer_with_added_rasteriness.lyrx")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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