Select to view content in your preferred language

Iterate though folder and add lyrx to a map

2597
22
Jump to solution
08-02-2022 02:21 AM
by Anonymous User
Not applicable

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!

0 Kudos
22 Replies
by Anonymous User
Not applicable

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))

 

by Anonymous User
Not applicable

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

0 Kudos
mattkramer
Frequent Contributor

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).

by Anonymous User
Not applicable

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?

 

0 Kudos
by Anonymous User
Not applicable

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:

0 Kudos
by Anonymous User
Not applicable

It's probably that the m. needs to be aprxMap. ?

0 Kudos
by Anonymous User
Not applicable

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:

0 Kudos
by Anonymous User
Not applicable

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.

by Anonymous User
Not applicable

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?

0 Kudos
by Anonymous User
Not applicable

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.