ArcGIS Pro and Python - Session 2

This session comprises three parts. First, you will learn several useful tips for exploring spatial data using ArcPy. Next, you will work on Hurricane Dorian data to find out which counties of South Carolina were in the probable hit zone of Dorian during September 4-6 of 2019. Finally, you will learn how to work with GIS data using Python outside of ArcGIS Pro.

Before we get started, open ArcGIS Pro.

In the window that appears, click create a new project using the Map template.

Type in your project name: Hurricanes_and_Evacuation_Routes.

Navigate to C:\Users\YourClemsonID\Documents.

In the top right corner of ArcGIS Pro, make sure to sign into ArcGIS Online with your Clemson ID and password.

Before we start to explore any GIS data, it is important to have a basic understanding of the term geodatabase in ArcGIS. The term geodatabase has the following meanings in ArcGIS,

    • The native data structure for ArcGIS and is the primary data format used for editing and data management.

    • The physical store of geographic information, primarily using a database management system or file system.

    • A comprehensive information model for representing and managing geographic information.

When you create a new project in ArcGIS Pro, a geodataset is also created by default and is assigned to the project.

The figure shows the default geodatabase in Catalog Pane for the project you just created.

If you hover your mouse over the geodatabase, you can see the type, when last modified, and the physical path of the default geodatabase.

1. Exploring spatial data using ArcGIS Notebook

This section describes several approaches to exploring spatial data, including checking for the existence of datasets, describing datasets, and listing datasets in a workspace.

Before you can explore spatial data, you need to add the spatial data to your map!

In the previous workshops, you saw how you can add data to your map. Let add Hurricane_Evacuation_Routes data which is available on Living Atlas.

If you forgot how to do so, these are the steps to take:

1. Open the Catalog Pane. In the top menu, select Portal.

2. In the menu bar below Portal, select the Living Atlas button.

3. Use the search bar, and type Hurricane_Evacuation_Routes.

4. Right-click the Hurricane_Evacuation_Routes layer in the results and select Add to Current Map.

5. Go to the Analysis tab.

6. Open a new Python Notebook.

1.1 Checking for the existence of data

The dialog boxes of geoprocessing tools in ArcGIS Pro have built-in mechanisms to validate tool parameters. For example, if you enter the name of a dataset that does not exist, an error message will automatically appear next to the parameter on the tool dialog box, preventing you from running the tool. In Python scripts, this task can be done using the arcpy.Exists() function. This function returns a Boolean value of True or False, indicating whether the element exists or not.

You can also check the existence of layers, datasets, feature classes, and tables in your computer. For example this code determines whether a shapefile exists.

1.2 Adding data to geodatabase

You should note that when you add data from Portal to your map, the data is not added to your geodatabase. If you need to add a copy of data to your geodatabase, you can use the CopyFeature tool.

For example this code makes a copy of the evacuation routes and adds to your geodatabase.

1.3 Describing data

ArcPy functions can determine the properties of all types of geospatial data. The properties of a dataset are often needed to validate tool parameters. For example when using the Clip tool, the clip feature type must match the input feature type. For instance, if the input feature is polygon, the clip feature must also be polygon.

The da.Describe() function returns a dictionary that you can use to check dataset properties. Test the following code in your notebook.

1.4 Listing data

ArcPy includes several functions to inventory data. These functions return a list, which consists of a list of items. A list can contain any type of data, but strings are the most common type when working with ArcPy’s list functions.

The ArcPy list functions include ListFields(), ListIndexes(), ListDatasets(), ListFeatureClasses(), ListFiles(), ListRasters(), ListTables(), ListWorkspaces(), and ListVersions(). The parameters of these functions are similar. Some functions require an input dataset because the items of interest are located within a dataset or object.

Use this code as an example and run all of the list functions above.

2. Finding SC counties in the hit zone of hurricane Dorian

In this section, you will use the data for hurricane Dorian and the borders of SC counties to find out which counties intersected with the Dorian hit zone between September 4-6, 2019. Then you will symbolize your results to better illustrate the hit zones with different probabilities.

1. Open the Catalog Pane. In the top menu, select Portal.First add USA Counties to your map. To do so,

2. In the menu bar below Portal, select the Living Atlas button.

3. Use the search bar, and type USA Counties.

4. Right-click the layer in the results and select Add to Current Map.

The hurricane Dorian data are available on National Hurricane Center on NOAA's website.

Open this link in a browser, https://www.nhc.noaa.gov/gis/

Find Wind Speed Probabilities and Select 2019 in the third column.

It will automatically redirect you to a new page where you can see the record for 2019.

to download the data, find and click the following datasets. (or you can simply click the links below).

2019090400_wsp_120hr5km.zip

2019090500_wsp_120hr5km.zip

2019090600_wsp_120hr5km.zip

Create a new folder in your Documents directory, and name it: Hurricane_Dorian_3_Days

Unzip the three datasets to Hurricane_Dorian_3_Days folder.

In ArcGIS Pro, go to Catalog Pane.

Right click on Folders and Select Add Folder Connection.

Navigate to C:\Users\YourClemsonID\Documents\.

Select H

urricane_Dorian_3_Days and hit Ok.

In Catalog Pane, navigate to each dataset and right on the following layers and add to your map,

  • 2019090400_wsp64knt120hr_5km

  • 2019090500_wsp64knt120hr_5km

  • 2019090600_wsp64knt120hr_5km

Now you will use the Spatial Join tool to create a new dataset for hurricane dataset (Sep 4-6) that includes which counties intersected with the hurricane data. Use your learning from last week and run the Spatial Join tool in ArcGIS Notebook to create three new datasets. Name the new datasets as follows correspondingly,

  • Dorian_crossing_SC_Counties_2019090400

  • Dorian_crossing_SC_Counties_2019090500

  • Dorian_crossing_SC_Counties_2019090600

After you created the three datasets,

1. Go to Contents on the left.

2. Right click on Droian_crossing_SC_Counties_2019090400

3. Select Symbology.

4. Change Primary symbology to Graduated Colors.

5. Set the symbology parameters according to this figure.

Repeat steps 1-3 above for the other two datasets,

Click on the three horizontal bars on the top right of the Symbology window.

Select Import Symbology.

Set Input layer to the layer you want to change the symbology (e.g. Droian_crossing_SC_Counties_2019090400).

Hit Run.

3. Using ArcPy outside of ArcGIS Pro

ArcPy is the core package that allows for a close integration of ArcGIS Pro and Python. ArcPy includes many important modules, classes, and functions, which makes it possible to use all the geoprocessing tools in ArcGIS Pro in a Python script. Once you import ArcPy, you can run all the geoprocessing tools found in the toolboxes installed with ArcGIS Pro.

Like running a script directly from the operating system, you do not need to have the ArcGIS Pro application open for the script to run, although you do need to have ArcGIS Pro installed and licensed on your computer to use the ArcPy package.

Any Python script that runs outside ArcGIS Pro—for example, using IDLE or PyCharm—must use import arcpy. Importing ArcPy performs two important checks:

  • Availability of ArcPy

  • Availability of a license.

Example of Python editors are, IDLE, Spyder, and Pycharm.

When you are using IDLE to write your code, and if you are using the version of IDLE installed as part of the ArcGIS Pro installation, ArcPy is available.

The default installation of other IDEs—for example, PyCharm—requires some additional steps for ArcPy to be available.

If ArcPy is not available, the line of code import arcpy will generate the following error message:

ImportError: No module named arcpy

When ArcPy is available, the second check is whether a license is available. It is important to recognize that ArcPy is part of ArcGIS Pro and therefore requires licensing. If ArcPy is installed, but no license is available, the line of code import arcpy will generate the following error message:

RuntimeError: NotInitialized

ArcGIS Pro includes a default, read-only conda-based Python. This Python is used in ArcGIS Pro, and it is also the Python you use to run stand-alone scripts. The default location of ArcGIS Pro Python is "C:\Program Files\ArcGIS\Pro\bin\Python".

The following script will display the feature classes to Windows Command Prompt.

To run the script,

Open a notepad in MS Windows.

Copy the script and paste it to notepad.

Replace YourClemsonID with yours.

Press Ctrl+S to save the file.

Navigate to C:\Users\YourClemsonID\Documents.

Name the script Return_FC.py.

Now open Windows Command Prompt.

Type in the following command to change your active directory to where you saved the script and press Enter.

cd Documents

Now you can run your script using the following command.

C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.bat Return_FC.py

Note: propy.bat acts as a replacement for calling python.exe.

If the script runs successfully, you should see a list of feature classes names for the ArcGIS Pro project that you created in session 1.

import arcpy

arcpy.env.workspace ='C:\\Users\\YourClemsonID\\Documents\\ArcGIS\\Projects\\ArcGIS_Pro_and_Python\\ArcGIS_Pro_and_Python.gdb'

fc_list = arcpy.ListFeatureClasses()

print(fc_list)

for i, fc in enumerate(fc_list):

print(fc)