Chapter exercise: ArcGIS Data with arcpullr
Medical Facilities from NC OneMap
This exercise goes with Chapter 14, ArcGIS Data with arcpullr. It builds on the Census Data exercise from Chapter 13, which also downloaded data through an API. It should take you about one hour. Read this whole section before you start.
Download arcpullr-exercise.Rmd
Save it in a new folder called arcpullr-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.
Purpose
Skills you’ll practice. By the end of this exercise you will be able to:
- Read a layer’s REST Services Directory page and record its geometry type, coordinate system, record limit, and field names.
- Download a layer from an ArcGIS feature service into R as an sf object with
get_spatial_layer(). - Write
whereclauses in SQL, with=,IN,AND, and text values in single quotes. - Explain why filtering on the server is better than downloading everything and filtering in R.
- Check, project, map, and save a downloaded layer, and record when you downloaded it.
Knowledge you’ll gain. You’ll find out how many licensed medical facilities North Carolina has, what kinds they are, and how Orange and Durham counties compare.
Why this matters. State and local governments publish most of their GIS data (parcels, schools, roads, parks, hospitals) on ArcGIS portals such as NC OneMap. Being able to pull exactly the features you need straight into R, with a script that records where they came from, makes your work faster and reproducible. Portal layers change over time, so knowing how to document a download is part of doing honest analysis.
Task
Work through Parts 1 to 7 in order, top to bottom. In each part:
- Read the short explanation, then do each step marked with a bullet (•).
- Wherever you see
___, replace it with the missing code before you run the chunk. Where you see# your code here, write the code yourself. - Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
- This exercise downloads data, so you need an internet connection. The full statewide download in Part 5 takes a little while.
- Your numbers may differ slightly from a classmate’s if NC OneMap updates the layer between your downloads. That’s expected; say when you downloaded it.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and load the packages | 4 min |
| 2 | Read the layer’s REST page | 10 min |
| 3 | Download one county | 8 min |
| 4 | Write where clauses |
12 min |
| 5 | Filter on the server or in R? | 8 min |
| 6 | Project, map, and save | 13 min |
| 7 | Reflect, knit, and turn in | 5 min |
Turn in your .Rmd file and your knitted .html file on Canvas.
Criteria for success
Use this checklist before you turn your work in. Each item matches a row of the rubric below.
| Criterion | Full credit | Partial credit | No credit |
|---|---|---|---|
| Knits (2 pts) | Knits to HTML with no errors | Knits, but some chunks have errors shown | Doesn’t knit, or no HTML turned in |
| REST page (2 pts) | Q1 and Q2 record the details correctly | Some details missing | Not done |
| Downloads and where clauses (5 pts) | Every request correct, with correct SQL | Most correct | Many missing or wrong |
| Questions (7 pts) | Q3 to Q10 answered with numbers and reasons | Some answers missing or without reasons | Most answers missing |
| Map (3 pts) | Projected, with title, legend title, and credits with the download date | Some parts missing | Not done |
| Reflection (1 pt) | All three answered | Missing |
What a good answer looks like. Here is a question, a weak answer, and a strong one.
Question: Why use a
whereclause instead offilter()?Weak answer: “It’s faster.”
Strong answer: “With
where, the server sends only the 61 Orange County facilities instead of all 6,800 in the state, so the download took a fraction of the time. For a big layer like statewide parcels, downloading everything might not even be possible, and thewhereclause also records in my script exactly which features I used.”
The strong answer uses numbers from the output, explains why, and says when it matters most.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
get_spatial_layer().
Application Programming Interface: a set of rules for how one program
asks another for data or services, such as your R script asking the
Census Bureau’s server for a table.
The integrated development environment (IDE) where you write and run R.
R does the computing; RStudio is the workspace around it.
The plain web page an ArcGIS server shows for each service and layer,
listing its layer ID, geometry type, MaxRecordCount, and fields.
The rules that tie coordinates to places on the Earth, including how the
round Earth is flattened onto a map (the projection). Layers must share
a coordinate system before you can combine them.
A layer published on an ArcGIS server whose features and attributes can
be requested through its REST endpoint.
“Simple features”: the R package for spatial vector data. An sf object
is a data frame with a geometry column holding each row’s point, line,
or polygon.
Structured Query Language, the language of databases. ArcGIS
where clauses use it: fcounty = 'Orange'.
A plain file of R code, ending in .R. Anything that isn’t
code must be written as a comment.
The ability for anyone to rerun an analysis from its code and data and
get the same result.
A block of R code inside an R Markdown file, starting with
```{r} and ending with three backticks.
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.
A bundle of code, written by other users, that adds new functions to R.
Install it once with install.packages(), then load it in
each session with library().
The most common style of web API: each dataset has its own URL, you ask
for data with ordinary web requests, and the answer usually comes back
as JSON.
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
A method for flattening the curved graticule onto a flat map. Every
projection distorts shape, area, distance, or direction.
An SQL filter, such as "fcounty = 'Orange'", that the
server applies before sending data back, so you download only what you
need.