Module 02
Essential geoprocessing and analysis
Yesterday in the Great Smokies
Webcam
archive
GEO 409: Advanced GIS
Announcements
-
Lab is now open after-hours.
-
Use student ID for swipe access.
Virtual field trip to
Natural Bridge State Resort Park
37.773072,-83.685751
Goals
-
Review first lab
-
Build a Notebook that will clip any number of layers to any AOI.
-
Perform summary statistics on the clipped layers
-
Start making a base map for Kentucky (to use in module 4).
Vertices
-
make points, lines, and polygons.
-
The basis of the vector data model.
-
Programatically create geometries with this notebook.
Lab 1
-
Find points within search area.
-
Now we have a density measure.
-
3.14 * 52 = 78.5 sq mi
-
2 places per 10 sq mi.
-
How does this compare to other locations?
-
Trivial question because we have a script!
Imagine...
-
if you had a hundred locations to search and compare.
-
Wouldn't it be awesome to automate this redundant task?
-
Will discover the
for
loop in this lesson.
Hold up!
-
We found 15 and 16 places.
-
How do you represent a polygon as a point?
Center of polygon?
-
Irregular and multipart polygons?
-
Centroid?
-
Why not buffer the polygon?
Lab 2
-
You have a geodatabase with many layers
-
that need extraction to an AOI.
-
Perform summary statistics on the layers.
-
Automate workflow in Notebook.
Go from massive amounts of data
to analysis and visualization.
In-class task
-
Create a Notebook called, 'practice-02.ipynb'
-
Practice along in the following examples.
What you need to know for ArcPy?
-
Functions
-
Lists
-
for loops
Functions
-
Block of statements that execute when called.
-
The function might accept input data as arguments.
-
The function might return data.
-
Many ArcPy functions output data to the disk.
-
📚 ➡ 🔨 ➡ 🌟
Most important function?
-
The
print()
function
-
reveals the contents of variables
-
and tells you if a block of code runs.
-
Tip: whenever code is indented, print something!
Parameters
-
Variables inside function
-
which allow us to pass information to the function.
-
Their sequence is critical.
Clip function
-
Perhaps most used GIS function
-
Clips spatial data to an Area of Interest (AOI)
-
Requires three parameters
-
Most everyone, initially, gets the parameter order mixed up.
Clip function
-
The spatial data is the cookie dough
-
The AOI is the cookie cutter
-
The output is the cookie
Documentation
-
ALWAYS read the documentation.
-
Do a web search with 'arcpy {function name}'
F-Strings
-
String formatting that allows concatenation of:
-
variables
-
expressions
-
and numbers.
-
Build strings from variables.
Lists
-
Sequence of values, aka items or elements
-
Also known as arrays.
-
Enclosed in square brackets [ ]
-
Elements separated by commas ,
-
Elements are ordered.
-
Common way to store data.
for loops
-
iterate over a sequence of values.
-
Use with a list
-
to access each element in the list.
Toward completing the lab
In-class task
-
Create a Notebook called, 'state-base-map.ipynb'
-
Open lesson documentation and build Notebook cells.
-
Finish notebook using the section called "Refactored Notebook".
Notebook output
-
Layers clipped to AOI (Kentucky)
-
ready for cartographic excellence
-
and statistical analysis.
Summary Statistics
-
Aggregate features based on shared attributes.
-
How long is the Kentucky River?
-
How much forested land in Kentucky?
-
How much of x is in y?
Vector layers
-
Look for common attributes.
-
Combine features
-
and look at
Shape_Length
and Shape_Area
attributes.
In-class task
-
Explore streams layer
-
both geometry and attribute table
-
and use these two functions.
Statistics on-the-fly
-
SearchCursor function
-
iterates over records in a table
-
accessing any field value.
Raster layers
-
Matrix of cell values
-
always a number.
-
Look for cell size.
Continuous raster
-
Cell number represent varying quantity.
-
Elevation, temperature, precipitation, etc.
-
Digital elevation model (DEM).
Imaging raster
-
Cell number represent reflected/emitted light.
-
Multiple bands.
-
Photography, satellite imagery, renderings, etc.
-
Shaded relief (artificial illumination).
We'll look at these in the next module
RAT
-
Count and value fields
-
with total count * area of cell = total area
In-class task
-
Explore NLCD layer
-
and find total area of forest.
Lab 2: Land use in the Bluegrass
Lab 2 Goals
-
Find percent of total and area of NLCD classes
-
in a custom area of interest.
Lab 2 Requirements
-
Check Deliverables.
-
Basic flow:
-
Set up local environment
-
Define area of interest
-
Clip
-
Summary Statistics
Let's jump in
-
Tip: work with your neighbor.
-
Code is very sharable.
Custom area of interest?
-
Use lab 1 to create circular AOI
-
then use FeatureEnvelopeToPolygon
function, convert this circular polygon to square polygon.
-
Check the Parameters > Python tab in the documentation.
Let's look at a sample Notebook
Input/output
-
Where's my data? What database are you using?
-
Functions have input AND output.
Errors?
-
If you are not getting them, then you are
- lucky
- or not practicing. 🤦
-
Errors are your friend.
Syntax errors
-
Typeos
-
in file path names, variables, etc.
-
Using functions properly? Parameters correct?
-
Throws an error, look at last line.
Solutions
-
If the error has a code, ERROR 000670, search it.
-
Remove layers from Contents pane.
-
Rebuild your Notebook from scratch.
Big Picture
-
Once you have a working Notebook
-
you can answer how much of x is in y *
-
for any area in Kentucky.
Challenge
Build a function
Build a function
-
Store and reuse statements
-
def
keyword defines a function
-
followed by unique name and
():
-
Parameters? i.e., does the function use data?
-
Indent function body four spaces
Documentation
-
Whenever you use a function
-
look up its documentation.
-
ArcGIS Pro has excellent docs 🚀
Methods
-
Functions that only work on certain types. 🔮🐉
-
String type, integer type, csv type, etc.
-
Uses dot notation, i.e.,
myString.upper()
String Methods
-
Perform common tasks on strings
-
Find substrings, change case, etc.
-
Most return new values, others return `True` or `False`
-
They do not change the original string.
-
Built-in methods
Example
justWords = "If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
String slice
-
Access substring using
[start : end]
indexes
-
Two index values separated by a colon
:
inside bracket []
notation
-
End index is up-to-but-not-including
-
"Let's Hike"[0:2]
returns "Le"
String stride
-
Optional third number in
[start:end:stride]
-
Third number returns every nth character
-
By default, 1
-
Negative numbers reverse the string
Challenge
Slice a URL
A uniform resource locator is the unique address of anything on the Web.
Loop
-
Interrupts sequential execution of code
-
runs same block of code over and over
-
modifying it with each new run
-
until a desired condition is found.
Syntax
-
Loop statement ends with semicolon
:
-
loop block is indented four spaces
-
runs block until a condition is met.
for
loop
-
Iterate over sequence
-
value by value
-
with an iterating variable
-
until all values have been accessed
-
or until a
break
occurs in the block.
while
loop
-
Runs while statement remains true
-
or until a
break
occurs in the block.
-
Can run indefinite number of times.
Python lists
-
A collection of values in a sequence.
-
One of the most common data collections.
-
Values enclosed in
[ ]
square brackets
-
and separated by commas.