I have some problems adding multiple feature classes (not only a single one) from a geodatabase to a map file in an ArcGIS Pro project and I'm wondering if someone knows of a solution to this. To be able to add more than just one layer from the gdb I've tried to use ListFeatureClasses in order to select all feature classes before using AddDatafromPath. But this doesn't seem to do the trick either.
Here's the script. I've tried several other solutions as well, and while some of them did not return any errors they still did not add the data to the map file.
arcpy.env.workspace = "path to gdb"
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
rootPath_aprx = "path to aprx"
aprx = arcpy.mp.ArcGISProject(rootPath_aprx+ 'Test.aprx')
aprxMap = aprx.listMaps("Map")[0]
for ds in datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
path = os.path.join(arcpy.env.workspace, ds, fc)
print(path)
try:
aprxMap.addDataFromPath(test)
print("yeah.")
except:
print("gah.")
CURRENT
for yourarcpy.mp.ArcGISProject
instead of the path to the APRX file.aprxMap.addDataFromPath(test)
buttest
is never defined. Should you be usingaprxMap.addDataFromPath(path)
instead? Ie,path
instead oftest
?try:
andexcept:
! They will be hiding the real error. Run it without the try/except, and then update your post here with the text of the real error. (With the code above, I would expect it to output an error abouttest
being undefined.)