I am working with a notebook in ArcGIS Pro and I want to add all .lyrx files within a local folder to a map.
I have set the workspace and created a list with:
arcpy.env.workspace = "path to my data"
list_layers = arcpy.ListFiles("*.lyrx")
which works fine. I have tried to create a for loop to iterate though the list and add the layers to the map in my current project without success.
I realise this is probably easy to do but I can't work it out. Any help would be greatly appreciated!
Solved! Go to Solution.
this works
import glob
directory = "path to my data"
for filename in glob.iglob(f'{directory}/*'):
aprxMap.addDataFromPath(filename)
Hey MH84, I can give this a shot. What you are going to need to do is crate a Map object to add the layers to. That can be done through something like this:
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]
once you have a map object ("m" in this case), you should be able to loop through those layers and add them to the map with an addLayer call. That would look something like this:
m.addLayer(inLayer)
I grabbed the first code block from ESRI's docs, the link is below (I also highly recommend checking out other pages on that site, they can be really helpful):
Hey @ mattkramer
So I already have a map object as I've added a single feature from AGOL in the previous part of the program. Adding individual layers/feature etc is ok I just can't get the loop right in this instance. I ran the following:
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
arcpy.env.workspace = "Path to my data"
list_layers = arcpy.ListFiles("*.lyrx")
for layer in list_layers:
m.addLayer(list_layers)
and receive the following error
ValueError Traceback (most recent call last)
In [43]:
Line 8: m.addLayer(list_layers)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py, in fn_:
Line 191: return fn(*args, **kw)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addLayer:
Line 1843: return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: ['OS_AnnoPoint.lyrx', 'OS_Area_Buildings.lyrx', 'OS_Area_FeatureCode.lyrx', 'OS_Area_Landform.lyrx', 'OS_Area_NatEnv.lyrx', 'OS_Bnd.lyrx', 'OS_Line.lyrx', 'OS_Line_Landform.lyrx', 'OS_Pnt.lyrx', 'OS_Sym.lyrx']
This works for one .lyrx
OS_Anno = (r"path to my data")
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
aprxMap.addDataFromPath(OS_Anno)
but I want to loop over all layers in the folder and add them
I've also tried this with the associated error
arcpy.env.workspace = "Path to my data"
list_layers = arcpy.ListFiles("*.lyrx")
for layer in list_layers:
m.addLayer(layer)
ValueError Traceback (most recent call last)
In [50]:
Line 8: m.addLayer(layer)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py, in fn_:
Line 191: return fn(*args, **kw)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addLayer:
Line 1843: return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: OS_AnnoPoint.lyrx
Make sure that the "m.addLayer(layer)" is indented.
Try updating it to look like this:
for layer in list_layers:
m.addLayer(layer)
It was and it's still throwing a value error.
this works
import glob
directory = "path to my data"
for filename in glob.iglob(f'{directory}/*'):
aprxMap.addDataFromPath(filename)
You are iterating through the list of layers, but, then you try to add the entire list instead of the layer(s):
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
arcpy.env.workspace = "Path to my data"
list_layers = arcpy.ListFiles("*.lyrx")
for layer in list_layers:
m.addLayer(layer)
Does it work if you add the individual layers you are iterating through instead of m.addLayer(list_layers)?
R_
ValueError Traceback (most recent call last)
In [89]:
Line 9: m.addLayer(layer)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py, in fn_:
Line 191: return fn(*args, **kw)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addLayer:
Line 1843: return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: OS_AnnoPoint.lyrx