arcpy.map symbology - why do I need "saveACopy"?

728
9
Jump to solution
11-06-2018 06:44 PM
KentaOkuyama
New Contributor III

Hi,

I'm using arcpy.mp module, and facing some basic concerns. I would really appreciate if anyone could help out with this.

I ran the code below, and it worked fine. I want to symbolyze the point feature class as graduated color with 5 quantile here.

But I wonder why the last sentence "aprx.saveACopy(r"C:\Users\kenta\Documents\ArcGIS\Projects\CoHRE_Collaboration\Output.aprx)" is needed. I want my code be reflected on the project defined as "aprx", but it is not possible? Do I always need to do saveACopy to reflect the symbology changes? Thank you so much for your help!

import arcpy,os
from arcpy import env
env.overwriteOutput = True
env.workspace = r"C:\Users\kenta\Documents\ArcGIS\Projects\CoHRE_Collaboration\CoHRE_Collaboration.gdb"

# First, reference the project
aprx = arcpy.mp.ArcGISProject(r"C:\Users\kenta\Documents\ArcGIS\Projects\CoHRE_Collaboration\CoHRE_Collaboration.aprx")

m = aprx.listMaps("Oki")[0]
lyr = m.listLayers("oki2018_point")[0]
if lyr.isFeatureLayer:
sym = lyr.symbology
if hasattr(sym, 'renderer'):
sym.renderer.classificationField = 'SMI2'
sym.updateRenderer('GraduatedColorsRenderer')
sym.renderer.classificationMethod = 'Quantile'
sym.renderer.breakCount = 5
lyr.symbology = sym
aprx.saveACopy(r"C:\Users\kenta\Documents\ArcGIS\Projects\CoHRE_Collaboration\Output.aprx")

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

You don't have to use aprx.saveACopy(file_name), you can just use aprx.save()

save method documentation

save ()

Saves changes to an ArcGISProject (.aprx).

saveACopy (file_name)

Saves an ArcGISProject (.aprx) to a new file path or name.

View solution in original post

9 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Not a bad idea, since it still references the original data

http://pro.arcgis.com/en/pro-app/arcpy/mapping/arcgisproject-class.htm

SaveACopy (file_name)
ParameterExplanationData Type
file_name

A string used to save an ArcGISProject (.aprx) to a new file path or file name.

String

The method creates a new output project file, but the project variable continues to reference the original ArcGISProject object. It does not duplicate all the content within a project folder.

0 Kudos
Luke_Pinner
MVP Regular Contributor

You don't have to use aprx.saveACopy(file_name), you can just use aprx.save()

save method documentation

save ()

Saves changes to an ArcGISProject (.aprx).

saveACopy (file_name)

Saves an ArcGISProject (.aprx) to a new file path or name.

DanPatterson_Retired
MVP Esteemed Contributor

With the obvious caveat... you have backed up the original... just in case

KentaOkuyama
New Contributor III

Thank you so much Luke.

It worked out perfectly!

Then I have an additional question.

I'm running my python scripts in jupyter notebook. Once I ran my script, nothing happens in my ArcGIS Pro map. When I restart the ArcGIS the code I run gets reflected (ex. update graduate color). 

I tried refreshing the map (ctrl + F5), but nothing happens. Is there a way to reflect my code on map on the spot? 

Thank you so much for your help!!

Kenta

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

aprx = arcpy.mp.ArcGISProject("CURRENT")

if you are running from the python window... haven't test jupyter notebook or jupyter lab

KentaOkuyama
New Contributor III

Thank you Dan.

Looks like it's better to work in python window within ArcGIS Pro as of now.

0 Kudos
Luke_Pinner
MVP Regular Contributor
aprx = arcpy.mp.ArcGISProject(r"C:\Path\To\Project.aprx")‍‍

The above changes the project .aprx file on disk.  It can be used in jupyter, IDE, scripts, in a python interpreter, etc.... 

If you have ArcGIS Pro open with this project loaded and call "aprx.save()" externally from your jupyter/IDE/scripts/python interpreter, any changes you make will not be reflected in the open ArcGIS Pro project because Pro reads the project file into memory when it opens it.  If you save the project from within Pro, any changes you have made externally with "aprx.save()" will be overwritten.

aprx = arcpy.mp.ArcGISProject("CURRENT")

The above will only work from within Pro in the Python Window as it references the in memory project, not the project .aprx file on disk.

Basically, you can't control an open Pro project from outside Pro.  It's the same with an ArcMap mxd.

KentaOkuyama
New Contributor III

Thank you for the explanation Luke!

Now that makes sense that script outside of ArcGIS pro does not change anything on opened pro map.

I should try, but if I write scripts that 1. update symbology 2. update layout 3. export it as pdf as one stream work flow, do you think the all updated symbology can be reflected in final pdf output? 

I will try this and see what happens shortly, but if you have any insights about this I would appreciate.

0 Kudos
MikeBallard
New Contributor II

Using Arcpy.mapping in Python Script using MXD, I am able to SAVE the MXD that I am changing ( eg. change extent, save MXD, Export layout to PDF)

Using Arcpy.MP in Python Script (not current window, but in Visual Studio Code or other IDE), I am NOT able to SAVE the APRX and then print a layout. You MUST saveAsCopy, which is a real pain. Hoping this gets fixed

0 Kudos