Learning Objectives By the end of this workshop, you will be able to:
1. Getting started with ArcGIS Pro and Python: Finding SC Counties within the probable hit zoneIn this exercise, your goal is to find the SC counties that are within the probable hit zone of a hurricane. You will first use traditional spatial analysis tools, and then will learn how to accomplish the same result using Python inside ArcGIS Pro. To get started, open ArcGIS Pro. In the window that appears, click create a new project using the Map template. Type in your project name: SC_Counties_within_Hit_Zone. 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. If you are signed in, you should see your name on the top right corner of ArcGIS Pro window as shown in the figure below. 1.1 Adding and exploring data: Wind speed probability & SC counties1.1.1 Adding wind speed probabilityFor this session, one of the datasets you will utilize is the wind speed probability provided by National Hurricane Center. A sample dataset for very recent hurricanes are available on ArcGIS Online. To add the historical hurricanes tracks to your map, 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 ArcGIS Online button. 3. Use the search bar, and type 2021100118_wsp64knt120hr_5km. 4. Find the layer with the same name in the search results. Right-click the layer and select Add to Current Map. If you need detailed information about a layer, you can hover the mouse on the layer in Catalog Pane. A message box will appear showing a link to where you can learn more about the layer, its owner(s), and terms of use. In order to find the SC counties that are within the probable hit zone of the hurricane, you will need the spatial data of South Carolina counties. 1.1.2 Adding South Carolina countiesFollow steps 1 and 2 from the above to search USA Counties in the Living Atlas. In the results list, find USA Counties, then right click the layer and add it to your current map. Now you have two GIS datasets, one containing wind speed probability and the other containing the spatial data for the SC counties. In this section, I will walk you through the conventional approach in running spatial analysis tools. Sections 1.3 and 1.4 will show how to learn about the corresponding Python command that essentially performs the same analysis and outputs the same results as you analyzed without Python. First you will create a layer containing SC counties from the USA_Counties data. Go to the Map tab. Click on Select by Attributes. The Input Rows are USA_Counties. Click on New Expression. Set Where to State Name. Set the middle parameter to is equal. Select South Carolina in the drop-down list. Click Ok. Go to Analysis tab. Use the search field and type in Copy Feature. Find and click the Copy Feature tool. In the geoprocessing window, set the input feature as USA_Counties. Name the output feature class to SC_Counties. Hit Run. In the Contents pane uncheck USA_Counties. Using the SC_Counties and the hurricane data, you can find the counties within the hit zone of the hurricane. In this example, we use an arbitrary definition of the probable "hit zone" of a hurricane as the areas within 1200 miles of the hurricane forecast. Spatial Join is one tool that you can use to create a new dataset with that indicates which counties are within the probable hit zone. Go to the Analysis tab. Select Tools to open the Geoprocessing window. Use the search field and type in Spatial. Find and click the Spatial Join tool. In the Geoprocessing window for the spatial join tool, The SC counties is your Target Feature. The hurricane layer is the Join Feature. For the Output Feature Class type in: SC_counties_in_hit_zone. Make sure Join Operation is set to Join one to many. Use the dropdown list for Match Option and select Within a distance. Set search radius to 1100. Make sure you selected Miles. Click Run. Once the analysis is complete, a new layer will be created and automatically added to your map. 1.3 Exploring the syntax of a geoprocessing tool With the geoprocessing window open, if you click the help icon , it will take you to the ArcGIS help page for the spatial join tool where you can find the Python syntax of the spatial join tool. In the help page for the spatial join tool, scroll down to Parameters section, Select the Python tab. Find the Python command for the spatial join tool. Check out the required and optional parameters in the table below the Python command. 2. Using the Python window inside ArcGIS ProThe Python window in ArcGIS Pro is a quick and convenient way to write short Python code to run geoprocessing tools while taking advantage of the full functionality of Python without having to work in a separate application. When writing code in the Python window in ArcGIS Pro, there is no need to import ArcPy or check for licensing. Because you are running code from within the ArcGIS Pro application, the availability and licensing of ArcPy is implicit. To open the Python window in ArcGIS Pro, Click on the Analysis tab, Find the Python button, Click the small triangle, Click Show Python Window. A screenshot of the Python Window in ArcGIS Pro is shown below. In the Python window, the top section is called the “transcript,” and the bottom section is called the Python “prompt.” You can type your line of code in the Python prompt. Once the code is typed, press Enter to execute the code. 2.1 Running the spatial join tool using the Python WindowThe availability of Python functionality directly within ArcGIS Pro also provides an efficient mechanism for accessing and executing geoprocessing tasks. You can drag many elements from other panes in ArcGIS Pro, including tools from the Geoprocessing pane, one or more data layers from the Contents pane, and even tools you have already run from the History pane. Now let's bring the spatial join tool that you have already run, directly into the Python window. Click the Analysis tab. On the left side, click the History button. In the History pane, right click the spatial join tool. Select Open in Python Window. Tip: You could also drag the tool into the prompt section of the Python window. Rename the output feature class to SC_counties_in_hit_zone_pw. Press Enter to run the spatial join tool from the Python window. As you can see, the Python command for geoprocessing tools could be too lengthy to type. Dragging and dropping things directly into the Python window can greatly save on typing. Congratulations! You successfully performed a spatial analysis using the Python window in ArcGIS Pro. 2.2 Saving your work in the Python window To save the code that you have already written in the Python window, Right-click in the Python window. Select Save Transcript. Name your script, Find_vulnerable_counties.py Note: The contents of the Python window can be saved as a Python file (.py) that you can then open in your preferred IDE. 2.3 Loading code into the Python window You can copy code from a Python editor or text editor and paste it into the Python window. You can also use the Load Code option to import the complete contents of a script. Consider the script that was previously exported (i.e. Find_vulnerable_counties.py). Starting off with an empty prompt section in the Python window, right-click and click Load Code. Then choose a Python script file, such as the Find_vulnerable_counties.py script. When the script is loaded, all the lines are added to the prompt section, including any comments. You can preview all the lines of code and make any necessary changes before running the code. When you run the code by pressing Enter, the code is executed and appears in the transcript, again including any comments. Another way to use Python in ArcGIS Pro is through Python notebooks. With ArcGIS notebooks integrated into ArcGIS Pro, you can perform analysis and immediately view results in a geographic context, interact with the emerging data, document and automate your workflow, and save it for later use or share it. We are going to learn more about ArcGIS Notebooks later on this section. 3.1 Running the spatial join tool in ArcGIS NotebooksNow let's run the spatial join tool that you have run in the previous sections, but this time using the Python notebook in ArcGIS Pro. Go to Analysis tab. Click the History button. Right click the Spatial Join tool. Select Copy Python command. Paste the command you just copied into the ArcGIS notebook. Rename the output feature class to SC_counties_in_hit_zone_nb. Click the Run button to run the tool. 3.2 Exercise: Finding evacuation routes in South CarolinaIn this section you will do an exercise on your own. You can choose either the Python window or ArcGIS notebook to perform this analysis. You will use what you have learned today to find evacuation routes in South Carolina. Hint 1: Hurricane evacuation routes are available on living atlas as, Hurricane_Evacuation_Routes. Hint 2: You can use the Clip tool for this exercise. The Intersect or Spatial Join tools are alternative tools for such analysis. Note: For the Output Feature Class type in: Hurricane_Evacuation_Routes_SC. Note: New ArcGIS notebooks are automatically added to your project. To add an existing notebook to the project, right-click the notebooks folder and choose Add Notebook . Note: You cannot drag and drop things directly into the notebook. However, you can copy code snippets and paste into the notebook. 3.3 What is an ArcGIS Notebook?Who has used Python within a Jupyter Notebook? ArcGIS Notebooks within ArcGIS Pro are very similar to Python Jupyter Notebooks. ArcGIS Notebooks are basically a self contained Python IDE (Integrated Development Environment) and interpreter. ArcGIS Notebooks provide an environment, where you can document your code, run it, look at the outcome, visualize data, and see the results without leaving the environment. ArcGIS Notebooks incorporate spatial analysis functionality and visualizations in addition to what a normal Jupyter Notebook provides. If you're not familiar with Jupyter Notebooks, that's okay. We'll go over basic Notebook functionality. 3.3.1 What are some of the ways that ArcGIS Notebooks can help you?
3.3.2 What are things for which ArcGIS Notebooks shouldn't be used
3.4 ArcGIS Notebooks: A Dive In3.4.1 Creating a new Notebook
3.4.2 Other options for manipulating notebooks
3.4.2 Parts of an ArcGIS Notebook
3.4.3 Some other helpful tidbits about ArcGIS Notebooks
3.4.4 Getting help in ArcGIS NotebooksPython tools, modules, functions, classes, and keywords all store documentation that provides information on their use. You can activate documentations at the pointer by pressing Shift+Tab. The figure shows the documentations displayed for the spatial join tool. Alternatively, the same documentation can be accessed using the built-in Python help function. The figure shows the help documentation displayed for arcpy.analysis.SpatialJoin. . |
ArcGIS Pro and Python - Session 1
Selection | File type icon | File name | Description | Size | Revision | Time | User |
---|
Subpages (1):ArcGIS Pro and Python - Session 2