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.
Try using the full path with the addDataFromPath() method by concatenating the workspace path and layer name:
import os
...
for layer in list_layers:
m.addDataFromPath(os.path.join(arcpy.env.workspace, layer))
So I tried this and received a RuntimeError. I think you need to iterate over the file itself to add it to a map rather than a list object if I've understood this correctly
Correct. In my opinion, the arcpy.listLayers function should probably be used on objects already in an ESRI environment. Since you are adding data from a file path, you should dynamically build the layer source from the data locations (hard coded folder path and loop through layer names).
arcpy.ListFIles() returns a list of strings, for your case: ['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'] . Concatenating like above should give you the full path for each layer.
Path to my data\OS_AnnoPoint.lyrx
Path to my data\OS_Area_Buildings.lyrx
...
What did the Runtime error say?
RuntimeError Traceback (most recent call last)
In [82]:
Line 4: m.addDataFromPath(os.path.join(arcpy.env.workspace, layer))
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addDataFromPath:
Line 1971: return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError:
It's probably that the m. needs to be aprxMap. ?
RuntimeError Traceback (most recent call last)
In [86]:
Line 3: aprxMap.addDataFromPath(os.path.join(arcpy.env.workspace, layer))
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addDataFromPath:
Line 1971: return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError:
odd, because its essentially doing the same thing as your solution using glob. Without seeing the full code and we're now just looking at another way, I'd say keep using the glob version.
It's not my really solution I found it on GIT hub and modified it slightly but it does work. Correct me if I'm wrong (probably!) but I think the difference is the glob solution iterates over the directory rather than creating a list first then iterating over that?
That is right in the concept. The glob.iglob (iterator glob) creates a generator/ yield object, so instead of populating a complete list first like the arcpy.ListFIles, it lets you iterate over a list without storing it.
Same principle, you are still iterating over a list but a difference is speed.