This tutorial walks you through some common arcpy.mp workflows and introduces some important concepts along the way. It is aimed to help users who are new to Python scripting and arcpy.mp. The steps are generic enough to work with any data and all license levels.
Note:
The steps below present a high-level overview of arcpy.mp. Throughout the tutorial, there are links to detailed help topics that provide more information about the concepts involved, and in nearly all cases, the topics include additional sample snippets of code.
Tutorial setup
You need to set up a basic project that contains a single map and a layout.
- Open a new, blank project.
- On the Insert tab, in the Project group, click New Map and choose New Map again from the drop-down menu.
- On the Insert tab, click New Layout and select a layout from the gallery.
- On the Insert tab, in the Map Frames group, choose a map from your project in the map frames drop-down menu and add it to the layout.
- On the Project tab, click Save.
Python window
The easiest place to start learning arcpy.mp is in the Python window. It allows you to perform small Python-based workflows and work through concepts directly in the application before being refined in larger scripts. The Python window is part of the ArcGIS Pro application framework and provides intelliSense, autocompletion, and command syntax so you can quickly enter the appropriate parameter information in the correct order.
- On the Analysis tab, click Python.
- Click in the code pane where it says Enter Python code here.
By default, the Python window appears docked at the bottom of the application.
Reference an existing project
Typically, one of the first operations you do with an arcpy.mp script is reference an existing project () or layer file (.lyrx or .lyr) that you want to work with. In this section, you'll reference a project.
There are two ways to reference a project. The first is to reference it on disk by providing a path to the .aprx file. If you are building a script that is to be run outside the ArcGIS environment, you must reference a project using its full system path. The second way is to reference the project that is currently loaded into the ArcGIS Pro application. When working in the Python window, it is convenient to reference the currently loaded project because changes made to it can be seen directly in the application. The following steps reference a project currently loaded into ArcGIS Pro.
- In the Python window, type the following line of code and press Enter. Keep in mind that Python syntax is case sensitive.
- In the Python window, type the following:
- In the Python window, continue to type the following and press Enter. You will need to provide your own path to an existing geodatabase.
- To see the change you just made to the project, click the Project tab and select Options.
- Click Cancel.
- In the Python window, type the following and press Enter:
- In the Python window, type the following and press Enter:
>>> aprx = arcpy.mp.ArcGISProject("CURRENT")
>>> aprx.
>>> aprx.defaultGeodatabase = r"path to a geodatabase of yours" #### for example, aprx.defaultGeodatabase = r"C:\Projects\YosemiteNP\Data\Yosemite.gdb")
>>> aprx.save()
Check the timestamp on the file. It should be updated to the current date and time.
The save() method does not have any parameters, but because it is a method, you must include the parentheses. To verify the location where the project was saved, do the following:>>> print(aprx.filePath)
Add a layer file to the map
Now that you have a reference to a project, add a layer file (.lyr or .lyrx) to the map. First, reference the map in the project and reference a layer file on disk; finally, add the layer file to the map.
Note:
For the following steps, locate an existing layer file. If you don't have one, you need to author one.- In the Python window, type the following:
- In the Python window, type the following:
- In the Python window, backspace to remove the addLayer method, type the following, and press Enter:
- In the Python window, type the following and press Enter:
>>> m = aprx.listMaps("Map")[0]
>>> m.addLayer(
>>> lyrFile = arcpy.mp.LayerFile(r"path to a layer file") ### for example, lyrFile = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\LYRS\rivers.lyrx")
>>> m.addLayer(lyrFile, "TOP")
Export a layout to PDF
Exporting a layout to PDF only requires a couple lines of code. First, reference the layout you want to export and call the exportToPDF method.
- In the Python window, type the following and press Enter:
- In the Python window, type the following and press Enter. The path you provide will most likely be different than the example below.
>>> lyt = aprx.listLayouts("Layout")[0]
>>> lyt = aprx.listLayouts()[0]
>>> lyt.exportToPDF(r"C:\Projects\YosemiteNP\Output\Page1.pdf")
You have completed a common workflow using arcpy.mp. The next step is to apply this workflow to your own projects and layers. Also explore the arcpy.mp help topics. There are dozens of small code samples at the end of each topic that can be easily copied and pasted into the Python window.