0

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.")
  • 2
    This looks like you're trying to add layers into a Project (aprx) on disk. I don't see a save. You'll need to save the APRX. And then you're confirming it didn't work by opening the APRX? Or are you trying to add layers into the current project from the Python window in an open session? If that's the case you'll need to use CURRENT for your arcpy.mp.ArcGISProject instead of the path to the APRX file. – KHibma Jun 19 at 14:35
  • You try to add data using aprxMap.addDataFromPath(test) but test is never defined. Should you be using aprxMap.addDataFromPath(path) instead? Ie, path instead of test? – Son of a Beach Jun 20 at 2:25
  • You should remove the try: and except:! 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 about test being undefined.) – Son of a Beach Jun 20 at 2:26
-1

The solution was to close down the project, add a save function, and then reopen the project to see that the files had been added. Simple as that.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.