Tutorial: Adding Data to a Map in ArcGIS Pro with Python

by | Oct 27, 2022

In this tutorial, you’ll learn multiple ways to add data to a map using Python. Specifically, we’ll add single shapefile feature classes and folders containing shapefile feature classes to the map using both arcpy and the os module.

There are various ways to add data to your map in ArcGIS Pro. The easiest way is to just drag and drop files from the Catalog pane into the map window, and you’ll see that the files get added to the map and the contents pane. From the ribbon interface, you can also choose “Map” and “Add Data” to add various data types to the map. Other options are using a database connection if your data is stored in an external database. These type of connections can be made through the ribbon interface through “Insert” and next “Connections”.

Here, we’ll explore two ways that use the Python scripting language and Python Notebooks to add data to a map. The first option uses the arcpy package, while the second one uses both arcpy and the os module, that is used to interact with the operation system. Using both arcpy and the os module lets you add multiple files at once to your current map.

Step 1: Using arcpy to add feature class shapefiles to a current project

In this tutorial, we’ll be using the Natural Earth quick start kit. Download the dataset, unzip the files to your hard drive, and open a new, empty project in Pro. Open a Python Notebook and use the following code to reference the current project, map and add a shapefile feature class from the downloaded dataset to the map:

If you run all three code cells, the countries shapefile gets added to the map and contents pane automatically. As you can see, you can use the addDataFromPath() method on the map object to add shapefile feature classes directly to the map. However, you cannot add more than one file at a time because each file needs to be referenced on disk explicitly before using the addDataFromPath() method. As you’ll see below, the os module solves this problem easily.

Step 2: Using the os module to add a file folder with feature class shapefiles to a current project

The following code snippet uses both the arcpy and os modules to locate a file folder on disk, loop over each file and add it the map if the file extension is .shp. As we’ve seen in step 1, you need a file path together with a file name to be able to be able to use the addDataFromPath() method. This is done using some basic string concatenation in Python. Note that the code can be easy modified to add specific file names/types, or change the file path where the files can be found. If you run the code, all shapefiles in the specified file folder are added the map and table of contents. Also note that you need the os module to specify the directory where the files are located on disk. Here is the code:

As you can see, this code adds all shapefiles in the file folder at once to the map window.

Learn more about programming ArcGIS Pro with Python:

Introduction to Programming ArcGIS Pro with Python

Intermediate ArcGIS Pro Programming with Python

Categories

Recent Posts

Eric van Rees
Eric van Rees is a freelance writer and editor. His specialty is GIS technology. He has more than eight years of proven expertise in editing, writing and interviewing as editor and editor-in-chief for the international geospatial publication GeoInformatics, as well as GIS Magazine and CAD Magazine, both published in Dutch. Currently, he writes about geospatial technology for various clients, publications and blogs.

Sign up for our weekly newsletter
to receive content like this in your email box.