Geographic Information Systems Asked by Stefano Potter on March 11, 2021
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 FilesArcGISProResourcesArcPyarcpyutils.py", line 191, in fn_
return fn(*args, **kw)
File "C:Program FilesArcGISProResourcesArcPyarcpy_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.gdbtest
I am not a fan of os.path.join
and instead of:
for_adding = os.path.join(dnbr, test)
I would use Python string formatting:
for_adding = "{0}/{1}".format(dnbr, test)
I think the reason you are getting a ValueError with your code is because you have created a path with a mix of forward slashes and a backslash.
Answered by PolyGeo on March 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP