Select to view content in your preferred language

Using AddLayer in a python loop to add layers to a map

1230
3
Jump to solution
01-20-2023 07:56 AM
JohnFix1
Occasional Contributor

I'm running ArcGIS Pro 3.x and trying to add a list of layers to a map. My guess is I'm not getting the python correct for the path to the feature classes in a FGDB.

Here's my python:

import arcpy

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("MMPK Output*")[0]

aoi = "PA_SCENTRAL"
LayerPath = ("D:\\Mobile Map Package Creator\\DEPLOYMENT_DATA_PACKAGES\\" + aoi + "\\map_data.gdb\\")

LayerList = ("COMPLEXES", "MILEMARKERS", "Zones", "ROAD_NAMES")

for Layer in LayerList:
  Layer2Add = (LayerPath + Layer )
  AddStatus = m.addLayer(Layer2Add)

 

And here the error message I get back:

Traceback (most recent call last):
File "<string>", line 14, in <module>
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 2217, in addLayer
return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\COMPLEXES

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

did you try

addDataFromPath (data_path)

The paths are as follows

aoi = "PA_SCENTRAL"
LayerPath = ("D:\\Mobile Map Package Creator\\DEPLOYMENT_DATA_PACKAGES\\" + aoi + "\\map_data.gdb\\")
LayerList = ("COMPLEXES", "MILEMARKERS", "Zones", "ROAD_NAMES")
for Layer in LayerList:
    print(LayerPath + Layer )
    
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\COMPLEXES
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\MILEMARKERS
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\Zones
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\ROAD_NAMES

.  Are they all in that gdb in that path 


... sort of retired...

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

did you try

addDataFromPath (data_path)

The paths are as follows

aoi = "PA_SCENTRAL"
LayerPath = ("D:\\Mobile Map Package Creator\\DEPLOYMENT_DATA_PACKAGES\\" + aoi + "\\map_data.gdb\\")
LayerList = ("COMPLEXES", "MILEMARKERS", "Zones", "ROAD_NAMES")
for Layer in LayerList:
    print(LayerPath + Layer )
    
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\COMPLEXES
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\MILEMARKERS
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\Zones
D:\Mobile Map Package Creator\DEPLOYMENT_DATA_PACKAGES\PA_SCENTRAL\map_data.gdb\ROAD_NAMES

.  Are they all in that gdb in that path 


... sort of retired...
JohnFix1
Occasional Contributor

Dan, I thought I did try it without success. But I just tried it and it works! Thank you.

I did:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("MMPK Output*")[0]

aoi = "PA_SCENTRAL"
LayerPath = ("D:\\Mobile Map Package Creator\\DEPLOYMENT_DATA_PACKAGES\\" + aoi + "\\map_data.gdb\\")

LayerList = ("COMPLEXES", "MILEMARKERS", "Zones", "ROAD_NAMES")
for Layer in LayerList:
  AddStatus = m.addDataFromPath(LayerPath + Layer)
0 Kudos
JohnFix1
Occasional Contributor

I do have a question for you. In my layers list, I also need to include a network dataset. It's in the same FGDB. The feature dataset is "My_Network" and the network dataset is "My_Network_ND".

I can't seem to figure out how to include this feature in my loop. 

0 Kudos