0% found this document useful (0 votes)
2 views12 pages

Python Scripting 11

Exercise 11 focuses on map scripting using ArcGIS Pro, guiding users through opening a project, saving copies, and modifying map properties and layers using Python scripts. It includes detailed steps for creating scripts to list maps, modify map names, work with layers, and change layer symbology. The exercise emphasizes the importance of referencing project files correctly and saving changes to avoid file locks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Python Scripting 11

Exercise 11 focuses on map scripting using ArcGIS Pro, guiding users through opening a project, saving copies, and modifying map properties and layers using Python scripts. It includes detailed steps for creating scripts to list maps, modify map names, work with layers, and change layer symbology. The exercise emphasizes the importance of referencing project files correctly and saving changes to avoid file locks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 11

Map scripting

Exercise data
Exercise data for this book can be downloaded from

[Link]/PythonPro3rdEditionData. . This is a link to the ArcGIS Online group called

Python Scripting for ArcGIS Pro – 2024 (Esri Press). The data for exercise 11 is posted as a zip

file called PythonScripting_Ex11_Data.zip. Download this file and extract it to a folder of your

choice. The instructions use a folder called C:\PythonPro, but you can use a different folder

provided you update any paths.

Open and save a project


Map scripting can open a project, examine some of the properties of the project, and

save a copy of the project.

1. Start ArcGIS Pro.

2. Open the project C:\PythonPro\Ex11\[Link].

Notice how the project includes two maps: Downtown and Region, each with a handful

of feature layers.
3. Open the Python window and run the following code:

aprx = [Link]("CURRENT")

Running this code creates an ArcGISProject object by referencing the project that

is currently open. You can now access the properties and methods of this object.

4. Run the following code:

print([Link])

The result is:

C:\PythonPro\Ex11\Austin_Data.gdb.

Although using the CURRENT keyword is convenient when working with the Python

windows, stand-alone scripts must reference the full path and name of the project.

5. Save changes and close ArcGIS Pro.

6. Start IDLE.

7. Create a new script file, and save your script as [Link] to the

C:\PythonPro\Ex11 folder.
8. Enter the following lines of code:

import arcpy

aprx = [Link]("CURRENT")

print([Link])

9. Save and run the script.

The code results in an error because CURRENT is not a meaningful reference outside

ArcGIS Pro, even if the software application is up and running. Instead, the project must be

referenced by its file name.

10. Correct line 2 of the code as follows:

aprx = [Link] ("[Link]")

11. Save and run the script.

This time no error occurs, and the path of the default geodatabase prints. Because the

[Link] script and the Austin_Downtown.aprx file reside in the same directory, it is not

necessary to specify the full path. However, when referencing a project in a different directory,

the full path is necessary:

aprx_path = "C:/PythonPro/Ex11/[Link]"

aprx = [Link](aprx_path)

Also note that setting a workspace using [Link] has no effect, so

unless the script and .aprx file reside in the same directory, you must specify the full path.
Note: The code in the rest of the exercise assumes that that script and .aprx file reside in the

same directory, and therefore only the file name is used, not the full path.

Map scripting often modifies the existing .aprx file, and for any changes to take effect,

you must save the .aprx file. However, to review the changes, it is common to save the results

to a new file using the saveACopy() method. In addition, it is common to add the del

statement to remove the reference to a project to avoid unwanted locks on the .aprx file. You

will practice this in the next step.

12. Modify the script as follows:

import arcpy

aprx = [Link]("[Link]")

[Link]("Austin_Copy.aprx")

del aprx

13. Save and run the script.

Note that the line del aprx does not delete the project but only the reference to the

project file in the script to avoid a lock on the file.

14. Use File Explorer to confirm that the new Austin_Copy.aprx file has been

created in the C:\PythonPro\Ex11 folder.


In the remaining sections, you will use the saveACopy() method at the end of some of

the scripts to save your changes.

Work with maps


Map scripting can work with one or more maps in a project. First, you will determine a

list of all the maps in a project.

1. In IDLE, create a new script file, and save your script as list_maps.py to the

C:\PythonPro\Ex11 folder.

2. Enter the following lines of code:

import arcpy

aprx = [Link]("[Link]")

maps = [Link]()

for m in maps:

print([Link])

print([Link])

del aprx

3. Save and run the script.

Running the script prints the names and map units of the two maps in the project to the

interactive interpreter:
Downtown

Foot_US

Region

Foot_US

Some properties also can be modified.

4. Modify the code as follows:

import arcpy

aprx = [Link]("[Link]")

m = [Link]("Region")[0]

[Link] = "County"

[Link]("Austin_County.aprx")

del aprx

This script references one specific map by name, changes the name of the map, and

then saves the project as a copy.

5. Save and run the script.

6. Start ArcGIS Pro.

7. Open the project C:\PythonPro\Ex11\Austin_County.aprx.

8. Confirm that the map previously called Region has been renamed to County.
9. Close ArcGIS Pro.

Work with map layers


Map scripting also can work with map layers. In the next example, you will create a list

of all the layers in a map and modify the properties of a specific layer.

1. In IDLE, create a new script file, and save your script as work_layers.py to the

C:\PythonPro\Ex11 folder.

2. Enter the following lines of code:

import arcpy

aprx = [Link]("[Link]")

maps = [Link]()

for m in maps:

print("Map: " + [Link])

lyrs = [Link]()

for lyr in lyrs:

print([Link])

del aprx
3. Save and run the script.

Running the script prints the names of the two maps in the project and the layers

present in each map:

Map: Downtown

parks

trees

base

Topographic

Map: Region

facilities

hospitals

parks

Topographic

You can check the type of layer by using the is* properties of the Layer object.

4. Modify the code as follows:


import arcpy

aprx = [Link]("[Link]")

m = [Link]("Downtown")[0]

lyrs = [Link]()

for lyr in lyrs:

if [Link]:

print([Link] + " is a basemap layer")

if [Link]:

print([Link] + " is a feature layer")

del aprx

5. Save and run the script.

Running the script prints the layer type of all the layers in the specified map:

parks is a feature layer

trees is a feature layer

base is a feature layer

Topographic is a basemap layer

You can manipulate the layers in a map using methods of the Map object. The following

code illustrates how to modify the basemap:


6. Modify the code as follows:
import arcpy

aprx = [Link]("[Link]")

m = [Link]("Downtown")[0]

[Link]("Light Gray Canvas")

[Link]("Austin_Canvas.aprx")

del aprx

7. Save and run the script.

Running the script replaces the existing basemap.

8. Start ArcGIS Pro.

9. Open the project C:\PythonPro\Ex11\Austin_Canvas .aprx.

10. Confirm that the basemap in the Downtown map has been replaced.
11. Close ArcGIS Pro.

Modifying layer symbology


Symbology can be modified using properties of the Layer object. The following script

creates a Symbology object, which is then modified, and this new symbology is applied to the

layer of interest.

1. In IDLE, create a new script file, and save your script as layer_symbology.py to

the C:\PythonPro\Ex11 folder.

2. Enter the following lines of code:

import arcpy

aprx = [Link]("[Link]")

m = [Link]("Downtown")[0]

lyr = [Link]("parks")[0]

sym = [Link]

red = {"RGB": [100, 175, 0, 100]}

if [Link] and hasattr(sym, "renderer"):

[Link] = red

[Link] = sym

[Link]("Austin_Symbology.aprx")

del aprx
3. Save and run the script.

The script performs two checks to ensure the layer of interest is of the right type and

supports the renderer. Although not required, these checks make the script more robust

because it prevents errors when modifying the symbology of layers that don't support this type

of symbology. Running the script modifies the fill color of the polygon of the layer of interest.

4. Start ArcGIS Pro.

5. Open the project C:\PythonPro\Ex11\Austin_Symbology.aprx.

6. Confirm that the color of the parks feature layer in the Downtown map has

been modified to green.

7. Close ArcGIS Pro.

End of Exercise 11.

You might also like