Geography 193 GIS & Python, 2nd mod., Spring 2023 Updated

 

I’ve updated the course syllabus and schedule for this course, Geography 193: Geospatial Analysis with Python for¬†Module 2 of the Spring ’23 semester. The dates are: 3/8/2023 – 4/3/2023.

This is largely an independent study, using a tutorial to learn about Python and ArcGIS software (online and desktop).

As with other courses in this sequence (Geog 191, Geog 192) the idea is to have a flexible but structured course that fits into your schedule. Please be flexible with the tutorial – it will undoubtedly be a bit out of date, or something may not work the way it should. Give it a try and move on if its just not functioning.

 

McConkey: Python Scripting for Geoprocessing (ERRORS)

Right off the bat, this course seems to cover directions in more detail than in the last course, Python for Everyone. I would actually recommend taking this course first so you can better interpret the directions of the other course. That is of course if you don’t encounter the same errors I came across. Sadly, I was not able to complete this course during the semester as I could not fix these errors.

Errors:

In the first exercise, an error may occur at step 5 when setting the environment for the script. When you set the Python Interpreter an error message may pop up in the Python console. It appears as written below (you may have to scroll to read the whole thing):

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\numpy\__init__.py:143: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the-box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init

Basically, it wants you to install something called an “mkl service package.” I discovered if you go into the settings and look at the interpreter there is actually an add (plus sign) button that you can add packages. I clicked the add button and made a search for the mkl service package. When I found it I pressed install but some type of administration error occurred. The error may be from the version of Python that you are using so you might have to do some research to find what version of Python works with ArcPro, which I believe is Python 3.7. I also tried to download this version but I am not sure if it was actually successful.

The script for exercise 1 should look like this:

import arcpy
# Set environments
arcpy.env.workspace = r"C:EsriTraining\PythonGP\Data\SanJuan.gdb"
arcpy.env.overwriteOutput = True
# Set parameters used to join the BufferDistance table to the Roads feature class
inFeatures = "Roads"
inField = "ROUTE_TYPE"
joinTable = "BufferDistance"
joinField = "ROUTE_TYPE"
# Script Joins table to feature class
arcpy.JoinField_management(inFeatures, inField, joinTable, joinField)
# Set parameters used to buffer Roads feature class
outBuffers = "RoadBuffers"
buffField = "DISTANCE"
# Buffer the roads based on DISTANCE attribute
arcpy.Buffer_analysis(inFeatures, outBuffers, buffField)

 

McConkey: Python for Everyone (OnlineCourse)

Overview of the Course:

This course services as both an introduction and walkthrough basic Python scripting. The course is composed of reading material with easy to understand figures and exercises with step-by-step instructions for a hands on experience. The course also has simple multiple choice questions with immediate feedback for improved understanding. There is some supplemental reading provided by some links in the beginning that took about 25 minutes to complete.  At the end of the course there is a brief review and then 10 question exam. The exam generates different questions if you go off the page and you will need an 8 out 10 to receive certification that you passed the course. (The exam is not that difficult, but I do not know if you are able to take it multiple times as I passed it on my first attempt. I recommend double checking your answers before submitting.)

The walkthrough sections are a bit more challenging and are definitely going to make the course longer than it estimates. This is especially true if you are unfamiliar with PyCharm (which you will be asked to download) as you will be spending a good deal of time getting your bearings with software.¬† If there are any courses that go over the basic layout of PyCharm I would recommend taking it before tackling this course. You can get through the course if you’re not familiar with PyCharm, but it will take you some time getting used to it.

You will have to download PyCharm and some data. The data did not take long to download and there is a link provided so you can download PyCharm. The PyCharm download is more complicated and will take some time (~30 minutes). If you’re getting the free trail version it should last about 30 days and you will have to make a JetBrains account. Also, when you have downloaded the data you should put it in a folder named ErsiTraining in C so it resembles the walkthrough. You will actually want to extract the data before this step. The data path for the first exercise should look something like this:¬†C:\EsriTraining\PythEveryone\RunningScripts/Polk_County. (I extracted the data after putting it into the EsriTraining folder so there were two PythEveryone folders back-to-back. Oops.)

Notes from the Course:

When choosing a script environment you may want to consider what you are trying to accomplish. If you are trying to test geoprocessing workflows and easily verify the results, you may want to stick with ArcGIS’s built in Python window. However, if you want to work with tools to enter, edit, and check syntax within your code, you may want to use an IDE, such as PyCharm. PyCharm also includes an integrated debugging environment.

Scripts can be built from data types, statements or functions. Statements and F=function both perform an action, but only functions return a value.

Tuples are a sequence of items, just like a list; however, tuples cannot be changed. Tuples are useful for when the sequence is significant in a script. Tuples are closed in parenthesis while lists are surrounded by brackets.

Reflection for the Exercises:

Make sure you follow the directions as best you can. If something isn’t working, you probably misread or skipped over something. If your positive you did not misread anything, try not to get to frustrated. You can try troubleshooting and if that does not work, it may be best to keep moving on. Every if you biffed one step (which can mess up the script in the end) it is better to see the process that the course is trying to teach you. Getting the end results are optimal though.

Not all of the directions are described as thoroughly as they should, especially when it comes to working with PyCharm. The exercise walkthroughs provide pictures that go along with the types of coding you will be asked to write out. (This is great but they do not provide images of the PyCharm screen  to improve your navigation of the software.) One thing you will have to look out for that some walkthroughs do not mention that you have to use print statements to make sure your scripts are working. A lot of times the walkthrough will tell you to write a script and then press enter and see the immediate output. From what I experienced this is misleading as this does not happen. Instead, I had to make a print statement and then run the script to see the output. (The output will be visible in the Python Console. This will appear on the bottom half of the screen when you run your scripts.) The good thing about this type of work is that even though it can be frustrating, the more you do what you intended, the more confident you get.

Review of Course:

Some of the directions for the exercises are a bit vague, especially when giving directions in PyCharm. I think PyCharm my have gone through an update so some of the actions you need to take may not directly correspond with what is being directed. It definitely took me longer than the estimated time, but that is probably due to me never using PyCharm before and only using ArcGIS Pro a few times before attempting this course. The course is in no way perfect but it does combine different learning styles and is well structured. The walkthroughs could be more thorough, but they also help you apply what you’ve learned.

McConkey – ArcPy: An Introduction (Video Tutorial Notes)

This video tutorial is part of the Python Essentials Learning Plan. The video is about 40 minutes long and gives a basic overview of using Python in ArcGIS.  The video has two presenters and switches back and forth between them during the presentation. One of them focuses are providing information about Python and its applications while the other actually demonstrates how the code is written for some of these applications. A lot of the details are things already covered in the book, but I will include several of the points made to provide an overview of the video.

Python makes coding a lot simpler by automating repetitive tasks such as data analysis and processing.  Scripts can be saved in a text file so they can be shared or used at a later time.  Unique tools that are not found in ArcGIS programs can be made from scratch by using Python. In fact, several tools in ArcGIS, that you may be familiar with are made from Python coding.

Python is dynamically typed. It can support strings, numbers, and functions as well as work with and produce files, lists, and dictionaries. (Dictionaries – objects with key-value pairs).

ArcPy is an API, or Application Programming Interface, and works for programs such as ArcGIS Pro, ArcGIS Server, and ArcGIS Desktop. It must be imported before running certain tools for those applications. If you have these applications you already have ArcPy, you just need to import it in a scripting window. ArcPy is composed of a series of unique modules, functions, classes, and tools.

When you open a tool in ArcGIS, you can select the blue question mark at the top right of the tool window to open syntax help, which will explain the function of the tool and its parameters. You will also be able to see a code sample written in Python at the bottom of the syntax help page which may be very useful when writing scripts for that tool.

You can open a Python window directly into ArcGIS Pro by going to the view tab and clicking the Python Window button.¬† If you type “arcpy.” then a list of available modules and functions will appear. You can narrow down your search by continuing to type out the tool you want and it should appear above. If there are multiple tools available you can scroll to the desired one with the arrow keys and then press the tab button to make a selection.

The parameters of the tool will be seen above and the Python window may automatically show optional inputs that are within your tab o contents. For example, if you have a series of shapefiles in your table of contents and the first parameter is an input feature class, then those shapefiles may appear above. (You can then use the arrow keys to scroll and then press tab to auto complete/select the layer.)  Once you one the tool, the results should be available in the history pane.

Another way to access Python in ArcGIS Pro is by opening a notebook. You can access or create a notebook by going to the insert tab and  pressing the New Notebook or Add Notebook buttons below.