Select to view content in your preferred language

arcpy.mp.ArcGISProject('CURRENT') works but arcpy.mp.ArcGISProject(project_path) does not.

1073
5
06-12-2023 11:45 AM
ZacharyKasson
New Contributor III

I have created a script to add images into a map. Both arcpy.mp.ArcGISProject('CURRENT') and  arcpy.mp.ArcGISProject(project_path) allow me to access the project and print the current layers but using the path does not allow me to add data in. Here is my script: 

tif_folder = r"C:\folder"

# Set aprx as your current project
aprx = arcpy.mp.ArcGISProject('CURRENT') 
#aprx = arcpy.mp.ArcGISProject('project_path')

# Get the map from the project using the index for a given map
map = aprx.listMaps()[0]  

#assign existing layers
existinglayernames = [l.name for l in map.listLayers()]
print(f'existinglayernames: {existinglayernames}')

#add files that are not in the project yet
for file in os.listdir(tif_folder):
    fName, fExt = os.path.splitext(file)
    if fExt != '.tif' or fName + fExt in existinglayernames:
        #  Skip this file and continue to the next one
        continue
    filepath = os.path.join(tif_folder, file)
    map.addDataFromPath(filepath)
    print(f'File name: {fName}')
    

Both options print the existing layers and the layers that are being added. but,  the path does not actually add the file.

Tags (1)
0 Kudos
5 Replies
BlakeTerhune
MVP Regular Contributor

Are you getting an error message?

0 Kudos
ZacharyKasson
New Contributor III

No error, just no action.

0 Kudos
JohannesLindner
MVP Frequent Contributor

This is expected behavior, current is different to a file path.

When you use current, ArcGIS knows that you want to work with the currently opened project. When you use a file path, ArcGIS loads that project into RAM, without actually opening it in the application. IT does all its operations on that copy in RAM, regardless of whether you currently have opened that project or not.

Your code works perfectly fine. Just add that piece at the end of it and then take a look at the copy:

aprx.saveACopy(r"project_folder\ProjectCopy")

 

You can also use the save() method, but for that to work, you need to call the script from outside the current project.


Have a great day!
Johannes
0 Kudos
ZacharyKasson
New Contributor III

I appreciate the response. Won't that just save another copy? I am pulling imagery files in so many copies will take up a lot of data. 

I could save a copy and delete the old one, but will it save the entire project or just that map (I have multiple maps).

Also how well would creating a copy really do as a tool because that is the end goal.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Yes, or will create a copy, of the whole project. It shouldn't take up too much space, because it doesn't copy the tater data.

As I said, you can also use the aprx.save() method, but only if you don't have that project open.


Have a great day!
Johannes
0 Kudos