1-intro

May 2024 · 6 minute read
1-intro background-size: cover class: title-slide-rstudio --- # Spatial Birds of a Feather on Thursday at lunch ![:imgcenterscale 100%](images/section-1/birds_of_feather.png) --- class: middle, center # A little detail about the people in the room --- # Starting with me ![:imgcenterscale 100%](images/ZevRoss_Spatial_Analysis.png) --- ![:imgcenterscale 100%](images/team_zrsa.jpg) --- background-image: url("images/ithacafalls.jpg") background-size: cover class: center, middle # Ithaca .footnote[ Image by Matthew Conheady, [nyfalls.com](http://nyfalls.com/waterfalls/ithaca-falls/) ] --- # Workshop assistants ![:imgcenterscale 100%](images/section-1/rstudio_tas-sara.png) --- # What is your experience with spatial data? -- * Have you worked with spatial data before? -- * Have you worked with ArcGIS or QGIS (or related)? -- * Have you made a map in R? -- * Have you done any geoprocessing in R? --- class: middle, center # My expectations about what you already know about R --- # I'm assuming you already know R ![:imgcenterscale 75%](images/section-1/hadley-typing.gif) --- # Including {dplyr} * `summarize()` * `mutate()` * `select()` * `group_by()` --- # You should know the pipe ![:imgcenterscale 75%](images/section-1/pipe_pie.jpg) --- # An example of the pipe ```r starwars %>% group_by(hair_color) %>% summarize(mean_height = mean(height, na.rm = TRUE)) ``` --- # As well as the `::` ```r readr::read_csv("data/mydata.csv") ``` --- # If `%>%` and `::` are unfamiliar... -- This is fine, but talk with a TA and use Google during a break to get comfortable with them --- # If {dplyr} is a mystery to you -- Talk with a TA please, the workshop won't be impossible but... -- it will be a challenge --- # With respect to what you know about spatial data -- No experience is expected and I'm guessing there is a huge range! --- background-image: url("images/background.png") background-size: cover class: center, middle, separator-slide # Workshop agenda --- # Workshop agenda - Intro (this section) -- - Getting your spatial data into R -- - Mapping your spatial data -- - Coordinate reference systems (CRS) --- # Workshop agenda continued -- - Getting to know vector data in R -- - Getting to know raster data in R -- - Geoprocessing with vectors and rasters (with three pieces) - Single vector layer geoprocessing - Multi vector layer geoprocessing - Raster layer geoprocessing --- # A note on why I chose to organize the way I did --- background-image: url("images/background.png") background-size: cover class: center, middle, separator-slide # What is spatial data? --- # A note on the terms spatial vs geospatial vs geographic -- * I use them interchangeably -- * Technically "spatial" can refer to non-earth based positions and geographic/geospatial is a subset --- # Vector vs raster spatial data ![:imgcenterscale 60%](images/section-1/vector_raster2-sara.png) --- class: center, middle # Vector data --- # Points, lines and polygons ![:imgcenterscale 55%](images/section-1/pt_line_poly-sara.png) --- # Vector data also can have non-spatial variables -- * Points, lines and polygons can have associated, non-spatial data -- * In the example below the **non-spatial** variables of building footprints in Philadelphia are `id`, `area`, `base_height`, `avg_height` and `max_height`. ![:imgcenterscale 100%](images/section-1/nonspatial_attributes.png) --- # Vector data comes in a variety of different file formats * Shapefiles * Geopackages * GeoJSON -- We will cover this in more detail --- class: center, middle # Raster data --- # Raster data is a grid of pixels with values ![:imgcenterscale 65%](images/section-1/raster-sara.png) --- # Image rasters vs data rasters ![:imgcenterscale 100%](images/section-1/image_raster-sara.png) --- # Raster data comes in a variety of different file formats * IMG * TIF * SID --- background-image: url("images/background.png") background-size: cover class: center, middle, separator-slide # Working with spatial data --- # Traditionally, spatial data has been handled with dedicated spatial software (e.g., GIS) * ArcGIS * QGIS * ERDAS IMAGINE * ENVI --- # Historically, my workflow looked something like this ![:imgcenterscale 100%](images/section-1/workflow-01.png) --- # But spatial analysis in R has gotten so good that I do most of my spatial work in R --- # Example 1: Health equity maps ![:imgcenterscale 100%](images/section-1/who-example.png) --- # Example 2: Air quality modeling ![:imgcenterscale 100%](images/section-1/philly-example.png) --- background-image: url("images/background.png") background-size: cover class: center, middle, separator-slide # The #rspatial package landscape --- # By the way the curly brackets {} denote a package --- # Most spatial processing and visualization can be done with these packages * {sf} * {raster} * {tmap} or {ggplot2} * {mapview} or {leaflet} --- # {sf} A package for vector data <!-- ![:imgcenterscale 75%](images/section-1/library-sf.png) --> --- # {raster} For working with raster data (obviously!) --- # {tmap} For creating static (and interactive!) maps --- # {mapview} For creating interactive maps --- # But there are dozens more spatial packages for specific needs * {ggspatial} * {leaflet} * {concaveman} * {cartography} * {ggmap} * {tidycensus} * {rayshader} * {rgrass7} * {stars} * {geogrid} * {arcgisbridge} --- # Many of these packages (including {sf}) have non-R dependencies --- # Key authors of spatial (and spatial-adjacent) packages ![:imgcenterscale 90%](images/section-1/spatial_authors-sara.png) --- # rOpenSci sponsors a lot of great (spatial) work! ![:imgcenterscale 100%](images/section-1/ropensci-logo.png) --- background-image: url("images/dive_in.jpg") background-size: cover class: center, bottom, inverse # Let's dive in... --- # Code along with me Goal here is to give you a sense of what we'll be doing in the workshop --- # Open a new R script --- # By the way -- use tab to autocomplete paths ![:imgcenterscale 100%](images/section-1/hit_tab.png) --- # Load {dplyr} ```r library(dplyr) ``` --- # Read in US counties near San Francisco -- ```r library(sf) bayarea <- read_sf("data/san-francisco/counties-bayarea.shp") ``` -- ```r glimpse(bayarea) ``` ``` ## Observations: 9 ## Variables: 4 ## $ county <chr> "Alameda", "Contra Costa", "Marin",… ## $ fipsstco <chr> "06001", "06013", "06041", "06055",… ## $ objectid <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9 ## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-122.2… ``` --- # Make a quick static map with {tmap} ```r library(tmap) tm_shape(bayarea) + tm_polygons(col = "cadetblue") ``` -- ![:imgcenterscale 50%](images/section-1/mapbay.png) --- # Compute centroids with {sf} -- ```r bayarea_cent <- st_centroid(bayarea) ``` --- # Make a quick interactive map with {mapview} -- ```r library(mapview) mapview(list(bayarea, bayarea_cent)) ``` ![:imgcenterscale 75%](images/section-1/mapview-bayarea.png) --- # Pre-created examples make it look easy but... --- # Working with spatial data is not always smooth sailing ![:imgcenterscale 100%](images/section-1/sailboat.gif) --- # Spatial data is more complex than "standard" tabular data * Coordinate systems * List columns * Different geometry types * Vector + Raster --- # The goal for the workshop: smooth[er] sailing with spatial data in R ![:imgcenterscale 80%](images/section-1/smooth-sailing.jpg) --- background-image: url("images/background.png") background-size: cover class: center, middle, separator-slide # Workshop materials --- # There is a package for this workshop -- ```r library(zrsaSpatialWorkshop) ``` --- # Slides, data and the package are in folders in the Files tab ![:imgcenterscale 100%](images/section-1/dataetc_folders.png) --- # You can download the package (and slides etc) ![:imgcenterscale 100%](images/section-1/filespane.png) --- # The workshop exercises data paths expect that you're using the server --- # If you install the package on your own machine you'll need to change the paths -- ```r system.file("data-raw/san-francisco/counties-bayarea.shp", package = "zrsaSpatialWorkshop") ``` --- # But you don't need to worry about this working on the server --- # To open an exercise: * `open_exercise(1)` --- # To open a solution * `open_solution(1)` --- # A note on exercises and solutions If you run `open_exercise(1)` a second time you will get an error (to prevent you from overwriting the existing file). You can: -- * Find the exercise script in the RStudio file explorer and open it there -- * Overwrite the existing file and start over with ```r open_exercise(1, overwrite = TRUE) ``` --- # Some exercises have code with `---` The `---` is a placeholder for something you need to fill in ```r # st_buffer(---) ``` --- # Ready for Exercise 1 * This exercise throws you in to the deep end with explanations to come! --- class: center, middle, doExercise # library(zrsaSpatialWorkshop) # open_exercise(1)

ncG1vNJzZmirY2Ourq3ZqKWar6NjsLC5jp%2BgpZ2jY8emwtGoqqxmk6S6cMPOq6KsoJ%2BlwHC%2Fz5qropmcZMCttcOeqmigpKK5cH2MoqWtqp9jtbW5yw%3D%3D