├── .gitignore ├── README.Rmd ├── README.md ├── gis-visualization ├── spring-2019 │ ├── R │ │ ├── gis-learning-2.R │ │ ├── gis-learning.R │ │ ├── import-data.R │ │ └── spatial-join.R │ ├── data │ │ ├── ward1998.dbf │ │ ├── ward1998.prj │ │ ├── ward1998.shp │ │ └── ward1998.shx │ └── doc │ │ ├── custom-map.Rmd │ │ ├── figs │ │ └── green-red-libraries.jpg │ │ ├── interactive-maps.Rmd │ │ ├── rmarkdown-practice.Rmd │ │ └── rmarkdown-practice.html └── winter-2019 │ ├── R │ ├── 01-intro.R │ ├── 02-gis-1.R │ ├── 03-gis-2.R │ ├── 04-gis-3.R │ └── leaflet-example │ │ └── app.R │ ├── data │ ├── Waterways.geojson │ ├── ward1986.dbf │ ├── ward1986.prj │ ├── ward1986.shp │ ├── ward1986.shx │ ├── ward1992.dbf │ ├── ward1992.prj │ ├── ward1992.shp │ ├── ward1992.shx │ ├── ward1998.dbf │ ├── ward1998.prj │ ├── ward1998.shp │ └── ward1998.shx │ └── doc │ ├── 05-rmarkdown.Rmd │ ├── 05-rmarkdown.html │ ├── 05-rmarkdown.pdf │ ├── 06-custom-maps.Rmd │ ├── 06-custom-maps.html │ ├── 07-interactive-maps.Rmd │ ├── 07-interactive-maps.html │ ├── areas_without_libraries.jpg │ ├── areas_without_libraries.png │ ├── areas_without_libraries_big.png │ └── city-pop-anim.gif ├── intro-spatial-data-sci ├── 01-spatial-data-handling.R ├── intro-rgeoda.Rmd ├── week2-fall2019-workshop.R ├── week3-fall2019-workshop.R ├── week4-fall2019-workshop.R ├── week5-fall2019-workshop.R └── week6-fall2019-workshop.R ├── node-temps.csv ├── raster-kriging └── spring-2019 │ └── R │ ├── interpolation-aot.R │ ├── interpolation-meuse.R │ ├── raster-intro.R │ ├── raster-multiband.R │ ├── raster-plotting.R │ └── raster-projection.R └── workshop-scripts.Rproj /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | data/ 6 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | # workshop-scripts 6 | 7 | This repository contains scripts from the each quarter of the [R Spatial Workshop](https://spatialanalysis.github.io/events/) run at the Center for Spatial Data Science at the University of Chicago. 8 | 9 | Workshops are run by Angela Li, R Spatial Advocate, and detailed notes for the workshops can be found [online](https://spatialanalysis.github.io/workshop-notes/). 10 | 11 | Each quarter's contents can be found in a separate folder in each topic repository with that quarter's name, ie `gis-visualization/winter-2019`. 12 | 13 | ## Fall 2019 14 | 15 | ### Introduction to Spatial Data Science 16 | | Date | Topic | R Script / R Markdown | 17 | |------------------------|------------------------|------------------------| 18 | | 2019-10-08 | [Spatial data handling](https://spatialanalysis.github.io/workshop-notes/spatial-data-handling.html) | [01-spatial-data-handling.R](intro-spatial-data-sci/01-spatial-data-handling.R) | 19 | | 2019-10-15 | [Spatial data handling pt. 2](https://spatialanalysis.github.io/workshop-notes/spatial-data-handling.html) | [week2-fall2019-workshop.R](intro-spatial-data-sci/week2-fall2019-workshop.R) | 20 | | 2019-10-22 | [Basic mapping pt. 1](https://spatialanalysis.github.io/workshop-notes/basic-mapping.html) | [week3-fall2019-workshop.R](intro-spatial-data-sci/week3-fall2019-workshop.R) | 21 | | 2019-10-29 | [Basic mapping pt. 2](https://spatialanalysis.github.io/workshop-notes/basic-mapping.html) | [week4-fall2019-workshop.R](intro-spatial-data-sci/week4-fall2019-workshop.R) | 22 | | 2019-11-05 | [Contiguity weights](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation.html) | [week5-fall2019-workshop.R](intro-spatial-data-sci/week5-fall2019-workshop.R) | 23 | | 2019-11-12 | [Distance-based weights](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation.html) | [week6-fall2019-workshop.R](intro-spatial-data-sci/week6-fall2019-workshop.R) | 24 | | 2019-11-19 | [Spatial autocorrelation with rgeoda](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation-with-rgeoda.html) | [intro-rgeoda.Rmd](intro-spatial-data-sci/intro-rgeoda.Rmd) | 25 | | 2019-11-26 | Spatial clustering with rgeoda | | 26 | 27 | ## Spring 2019 28 | 29 | ### GIS and Visualization 30 | | Date | Topic | R Script / R Markdown | 31 | |------------------------|------------------------|------------------------| 32 | | 2019-04-10 | [Introduction to spatial data](https://spatialanalysis.github.io/workshop-notes/introduction-to-spatial-data.html) | [import-data.R](gis-visualization/spring-2019/R/import-data.R) | 33 | | 2019-04-17 | [Single-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/single-dataset-gis-operations.html) | [gis-learning.R](gis-visualization/spring-2019/R/gis-learning.R) | 34 | | 2019-04-24 | [Multi-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization.html) | [gis-learning-2.R](gis-visualization/spring-2019/R/gis-learning-2.R) | 35 | | 2019-05-01 | [Multi-dataset GIS operations pt. 2: spatial join](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization-pt-2.html) | [spatial-join.R](gis-visualization/spring-2019/R/spatial-join.R) | 36 | | 2019-05-08 | No workshop | | 37 | | 2019-05-15 | [Working with R Markdown](https://spatialanalysis.github.io/workshop-notes/r-markdown-and-custom-maps.html) | [rmarkdown-practice.Rmd](gis-visualization/spring-2019/doc/rmarkdown-practice.Rmd) | 38 | | 2019-05-22 | [Custom maps with tmap](https://spatialanalysis.github.io/workshop-notes/custom-and-animated-maps.html) | [custom-map.Rmd](gis-visualization/spring-2019/doc/custom-map.Rmd) | 39 | | 2019-05-29 | [Interactive map packages](https://spatialanalysis.github.io/workshop-notes/interactive-maps.html) | [interactive-maps.Rmd](gis-visualization/spring-2019/doc/interactive-maps.Rmd) | 40 | 41 | ### Raster Data and Kriging 42 | 43 | | Date | Topic | R Script / R Markdown | 44 | |------------------------|------------------------|------------------------| 45 | | 2019-04-10 | [Introduction to raster data](https://datacarpentry.org/r-raster-vector-geospatial/01-raster-structure/index.html) | [raster-intro.R](raster-kriging/spring-2019/R/raster-intro.R)| 46 | | 2019-04-17 | [Plotting raster data](https://datacarpentry.org/r-raster-vector-geospatial/02-raster-plot/index.html) | [raster-plotting.R](raster-kriging/spring-2019/R/raster-plotting.R)| 47 | | 2019-04-24 | [Projecting raster data](https://datacarpentry.org/r-raster-vector-geospatial/03-raster-reproject-in-r/index.html) | [raster-projection.R](raster-kriging/spring-2019/R/raster-projection.R) | 48 | | 2019-05-01 | [Multiband raster data](https://datacarpentry.org/r-raster-vector-geospatial/05-raster-multi-band-in-r/index.html), [cropping raster to vector data](https://datacarpentry.org/r-raster-vector-geospatial/11-vector-raster-integration/index.html) | [raster-multiband.R](raster-kriging/spring-2019/R/raster-multiband.R) | 49 | | 2019-05-08 | No workshop | | 50 | | 2019-05-15 | [Interpolation, pt. 1](https://spatialanalysis.github.io/workshop-notes/introduction-to-interpolation.html) | [interpolation-meuse.R](https://github.com/spatialanalysis/workshop-scripts/blob/master/raster-kriging/spring-2019/R/interpolation-meuse.R) | 51 | | 2019-05-22 | [Interpolation, pt. 2](https://spatialanalysis.github.io/workshop-notes/interpolation-with-array-of-things-data.html) | [interpolation-aot.R](https://github.com/spatialanalysis/workshop-scripts/blob/master/raster-kriging/spring-2019/R/interpolation-aot.R) | 52 | 53 | 54 | ## Winter 2019 55 | 56 | ### GIS and Visualization 57 | 58 | | Date | Topic | R Script / R Markdown | 59 | |------------------------|------------------------|------------------------| 60 | | 2019-01-15 | [Introduction to spatial data](https://spatialanalysis.github.io/workshop-notes/introduction-to-spatial-data.html) | [01-intro.R](gis-visualization/winter-2019/R/01-intro.R) | 61 | | 2019-01-22 | [Single-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/single-dataset-gis-operations.html) | [02-gis-1.R](gis-visualization/winter-2019/R/02-gis-1.R) | 62 | | 2019-01-29 | [Multi-dataset GIS operations pt 1](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization.html) | [03-gis-2.R](gis-visualization/winter-2019/R/03-gis-2.R) | 63 | | 2019-02-05 | [Multi-dataset GIS operations pt 2](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization-pt-2.html) | [04-gis-3.R](gis-visualization/winter-2019/R/04-gis-3.R) | 64 | | 2019-02-12 | [R Markdown](https://spatialanalysis.github.io/workshop-notes/r-markdown-and-custom-maps.html) | [05-rmarkdown.Rmd](gis-visualization/winter-2019/doc/05-rmarkdown.Rmd) | 65 | | 2019-02-19 | [Custom static maps and animated maps](https://spatialanalysis.github.io/workshop-notes/custom-and-animated-maps.html) | [06-custom-maps.Rmd](gis-visualization/winter-2019/doc/06-custom-maps.Rmd) | 66 | | 2019-02-26 | [Interactive maps](https://spatialanalysis.github.io/workshop-notes/interactive-maps.html) | [07-interactive-maps.Rmd](gis-visualization/winter-2019/doc/07-interactive-maps.Rmd) | 67 | | 2019-02-26 | [Interactive maps with Shiny](https://spatialanalysis.github.io/workshop-notes/interactive-maps-with-shiny.html) | [app.R](gis-visualization/winter-2019/R/leaflet-example/app.R) | 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # workshop-scripts 3 | 4 | This repository contains scripts from the each quarter of the [R Spatial 5 | Workshop](https://spatialanalysis.github.io/events/) run at the Center 6 | for Spatial Data Science at the University of Chicago. 7 | 8 | Workshops are run by Angela Li, R Spatial Advocate, and detailed notes 9 | for the workshops can be found 10 | [online](https://spatialanalysis.github.io/workshop-notes/). 11 | 12 | Each quarter’s contents can be found in a separate folder in each topic 13 | repository with that quarter’s name, ie 14 | `gis-visualization/winter-2019`. 15 | 16 | ## Fall 2019 17 | 18 | ### Introduction to Spatial Data Science 19 | 20 | | Date | Topic | R Script / R Markdown | 21 | | ---------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | 22 | | 2019-10-08 | [Spatial data handling](https://spatialanalysis.github.io/workshop-notes/spatial-data-handling.html) | [01-spatial-data-handling.R](intro-spatial-data-sci/01-spatial-data-handling.R) | 23 | | 2019-10-15 | [Spatial data handling pt. 2](https://spatialanalysis.github.io/workshop-notes/spatial-data-handling.html) | [week2-fall2019-workshop.R](intro-spatial-data-sci/week2-fall2019-workshop.R) | 24 | | 2019-10-22 | [Basic mapping pt. 1](https://spatialanalysis.github.io/workshop-notes/basic-mapping.html) | [week3-fall2019-workshop.R](intro-spatial-data-sci/week3-fall2019-workshop.R) | 25 | | 2019-10-29 | [Basic mapping pt. 2](https://spatialanalysis.github.io/workshop-notes/basic-mapping.html) | [week4-fall2019-workshop.R](intro-spatial-data-sci/week4-fall2019-workshop.R) | 26 | | 2019-11-05 | [Contiguity weights](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation.html) | [week5-fall2019-workshop.R](intro-spatial-data-sci/week5-fall2019-workshop.R) | 27 | | 2019-11-12 | [Distance-based weights](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation.html) | [week6-fall2019-workshop.R](intro-spatial-data-sci/week6-fall2019-workshop.R) | 28 | | 2019-11-19 | [Spatial autocorrelation with rgeoda](https://spatialanalysis.github.io/workshop-notes/spatial-autocorrelation-with-rgeoda.html) | [intro-rgeoda.Rmd](intro-spatial-data-sci/intro-rgeoda.Rmd) | 29 | | 2019-11-26 | Spatial clustering with rgeoda | | 30 | 31 | ## Spring 2019 32 | 33 | ### GIS and Visualization 34 | 35 | | Date | Topic | R Script / R Markdown | 36 | | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------- | 37 | | 2019-04-10 | [Introduction to spatial data](https://spatialanalysis.github.io/workshop-notes/introduction-to-spatial-data.html) | [import-data.R](gis-visualization/spring-2019/R/import-data.R) | 38 | | 2019-04-17 | [Single-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/single-dataset-gis-operations.html) | [gis-learning.R](gis-visualization/spring-2019/R/gis-learning.R) | 39 | | 2019-04-24 | [Multi-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization.html) | [gis-learning-2.R](gis-visualization/spring-2019/R/gis-learning-2.R) | 40 | | 2019-05-01 | [Multi-dataset GIS operations pt. 2: spatial join](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization-pt-2.html) | [spatial-join.R](gis-visualization/spring-2019/R/spatial-join.R) | 41 | | 2019-05-08 | No workshop | | 42 | | 2019-05-15 | [Working with R Markdown](https://spatialanalysis.github.io/workshop-notes/r-markdown-and-custom-maps.html) | [rmarkdown-practice.Rmd](gis-visualization/spring-2019/doc/rmarkdown-practice.Rmd) | 43 | | 2019-05-22 | [Custom maps with tmap](https://spatialanalysis.github.io/workshop-notes/custom-and-animated-maps.html) | [custom-map.Rmd](gis-visualization/spring-2019/doc/custom-map.Rmd) | 44 | | 2019-05-29 | [Interactive map packages](https://spatialanalysis.github.io/workshop-notes/interactive-maps.html) | [interactive-maps.Rmd](gis-visualization/spring-2019/doc/interactive-maps.Rmd) | 45 | 46 | ### Raster Data and Kriging 47 | 48 | | Date | Topic | R Script / R Markdown | 49 | | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | 50 | | 2019-04-10 | [Introduction to raster data](https://datacarpentry.org/r-raster-vector-geospatial/01-raster-structure/index.html) | [raster-intro.R](raster-kriging/spring-2019/R/raster-intro.R) | 51 | | 2019-04-17 | [Plotting raster data](https://datacarpentry.org/r-raster-vector-geospatial/02-raster-plot/index.html) | [raster-plotting.R](raster-kriging/spring-2019/R/raster-plotting.R) | 52 | | 2019-04-24 | [Projecting raster data](https://datacarpentry.org/r-raster-vector-geospatial/03-raster-reproject-in-r/index.html) | [raster-projection.R](raster-kriging/spring-2019/R/raster-projection.R) | 53 | | 2019-05-01 | [Multiband raster data](https://datacarpentry.org/r-raster-vector-geospatial/05-raster-multi-band-in-r/index.html), [cropping raster to vector data](https://datacarpentry.org/r-raster-vector-geospatial/11-vector-raster-integration/index.html) | [raster-multiband.R](raster-kriging/spring-2019/R/raster-multiband.R) | 54 | | 2019-05-08 | No workshop | | 55 | | 2019-05-15 | [Interpolation, pt. 1](https://spatialanalysis.github.io/workshop-notes/introduction-to-interpolation.html) | [interpolation-meuse.R](https://github.com/spatialanalysis/workshop-scripts/blob/master/raster-kriging/spring-2019/R/interpolation-meuse.R) | 56 | | 2019-05-22 | [Interpolation, pt. 2](https://spatialanalysis.github.io/workshop-notes/interpolation-with-array-of-things-data.html) | [interpolation-aot.R](https://github.com/spatialanalysis/workshop-scripts/blob/master/raster-kriging/spring-2019/R/interpolation-aot.R) | 57 | 58 | ## Winter 2019 59 | 60 | ### GIS and Visualization 61 | 62 | | Date | Topic | R Script / R Markdown | 63 | | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | 64 | | 2019-01-15 | [Introduction to spatial data](https://spatialanalysis.github.io/workshop-notes/introduction-to-spatial-data.html) | [01-intro.R](gis-visualization/winter-2019/R/01-intro.R) | 65 | | 2019-01-22 | [Single-dataset GIS operations](https://spatialanalysis.github.io/workshop-notes/single-dataset-gis-operations.html) | [02-gis-1.R](gis-visualization/winter-2019/R/02-gis-1.R) | 66 | | 2019-01-29 | [Multi-dataset GIS operations pt 1](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization.html) | [03-gis-2.R](gis-visualization/winter-2019/R/03-gis-2.R) | 67 | | 2019-02-05 | [Multi-dataset GIS operations pt 2](https://spatialanalysis.github.io/workshop-notes/multiple-dataset-gis-operations-visualization-pt-2.html) | [04-gis-3.R](gis-visualization/winter-2019/R/04-gis-3.R) | 68 | | 2019-02-12 | [R Markdown](https://spatialanalysis.github.io/workshop-notes/r-markdown-and-custom-maps.html) | [05-rmarkdown.Rmd](gis-visualization/winter-2019/doc/05-rmarkdown.Rmd) | 69 | | 2019-02-19 | [Custom static maps and animated maps](https://spatialanalysis.github.io/workshop-notes/custom-and-animated-maps.html) | [06-custom-maps.Rmd](gis-visualization/winter-2019/doc/06-custom-maps.Rmd) | 70 | | 2019-02-26 | [Interactive maps](https://spatialanalysis.github.io/workshop-notes/interactive-maps.html) | [07-interactive-maps.Rmd](gis-visualization/winter-2019/doc/07-interactive-maps.Rmd) | 71 | | 2019-02-26 | [Interactive maps with Shiny](https://spatialanalysis.github.io/workshop-notes/interactive-maps-with-shiny.html) | [app.R](gis-visualization/winter-2019/R/leaflet-example/app.R) | 72 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/R/gis-learning-2.R: -------------------------------------------------------------------------------- 1 | # load libraries 2 | library(sf) 3 | library(ggplot2) 4 | library(dplyr) 5 | 6 | # import with sf 7 | ward86 <- read_sf("gis-visualization/spring-2019/data/ward1986.shp") 8 | water <- read_sf("gis-visualization/spring-2019/data/Waterways.geojson") 9 | 10 | # project 11 | st_crs(ward86) 12 | st_buffer(ward86) # does not work with unprojected data!! 13 | st_crs(water) 14 | 15 | ward86 <- st_transform(ward86, 32616) 16 | st_crs(ward86) 17 | 18 | water <- st_transform(water, 32616) 19 | st_crs(water) 20 | 21 | ggplot() + 22 | geom_sf(data = ward86) + 23 | geom_sf(data = water, col = "blue") 24 | 25 | water <- filter(water, name != "LAKE MICHIGAN") 26 | 27 | # calculate centroids 28 | # plot with ggplot2 29 | ggplot() + 30 | geom_sf(data = ward86) 31 | 32 | centroids <- st_centroid(ward86) 33 | centroids_buffer <- st_buffer(centroids, 1000) 34 | st_crs(centroids) 35 | 36 | ggplot() + 37 | geom_sf(data = ward86) + 38 | geom_sf(data = centroids_buffer) + 39 | geom_sf(data = centroids) 40 | 41 | # spatial intersection (important!!) 42 | intersects <- st_intersects(ward86, water) 43 | water_wards <- filter(ward86, lengths(intersects) > 0) 44 | 45 | ggplot() + 46 | geom_sf(data = ward86) + 47 | geom_sf(data = water_wards, fill = "pink") + 48 | geom_sf(data = water, col = "blue") 49 | 50 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/R/gis-learning.R: -------------------------------------------------------------------------------- 1 | library(sf) 2 | library(ggplot2) 3 | 4 | ward92 <- read_sf("gis-visualization/spring-2019/data/ward1992.shp") 5 | ward98 <- read_sf("gis-visualization/spring-2019/data/ward1998.shp") 6 | 7 | ward92 <- st_transform(ward92, 32616) 8 | ward98 <- st_transform(ward98, 32616) 9 | 10 | # class(ward92) 11 | 12 | ggplot(ward92) + 13 | geom_sf() 14 | 15 | ggplot(ward98, aes(fill = WARD)) + 16 | geom_sf() 17 | 18 | names(ward98) 19 | 20 | View(ward92) 21 | names(ward92) 22 | 23 | st_crs(ward92) 24 | st_crs(ward98) 25 | 26 | ward92 27 | centroids <- st_centroid(ward92) 28 | 29 | ggplot() + 30 | geom_sf(data = ward92) + 31 | geom_sf(data = centroids, color = "red") 32 | 33 | ?geom_sf 34 | 35 | st_cast(ward92, "POLYGON") 36 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/R/import-data.R: -------------------------------------------------------------------------------- 1 | library(sf) 2 | 3 | wards <- read_sf("data/ward1998.shp") 4 | wards 5 | plot(wards) 6 | 7 | wards_utm <- st_transform(wards, 32616) 8 | wards_stateplane <- st_transform(wards, 3435) 9 | 10 | wards_utm 11 | wards_stateplane 12 | 13 | plot(st_geometry(wards_utm)) 14 | plot(st_geometry(wards_stateplane)) 15 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/R/spatial-join.R: -------------------------------------------------------------------------------- 1 | library(sf) 2 | library(ggplot2) 3 | library(dplyr) 4 | 5 | libraries <- read_sf("https://data.cityofchicago.org/resource/psqp-6rmg.geojson") 6 | areas <- read_sf("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 7 | 8 | # won't work if not projected 9 | st_intersects(areas, libraries) 10 | 11 | # project the data 12 | st_crs(libraries) 13 | libraries <- st_transform(libraries, 32616) 14 | areas <- st_transform(areas, 32616) 15 | 16 | # make a map of both! 17 | ggplot() + 18 | geom_sf(data = areas) + 19 | geom_sf(data = libraries) 20 | 21 | # find which libraries are in which areas 22 | intersects <- st_intersects(areas, libraries) 23 | no_library_areas <- filter(areas, lengths(intersects) == 0) 24 | 25 | # map places in chicago without libraries 26 | ggplot() + 27 | geom_sf(data = areas) + 28 | geom_sf(data = no_library_areas, fill = "pink") 29 | 30 | # try making a map of areas in chicago with more than 1 library! 31 | many_library_areas <- filter(areas, lengths(intersects) > 1) 32 | 33 | ggplot() + 34 | geom_sf(data = areas) + 35 | geom_sf(data = no_library_areas, fill = "red") + 36 | geom_sf(data = many_library_areas, fill = "green") 37 | 38 | ?ggsave 39 | ggsave("gis-visualization/spring-2019/doc/figs/green-red-libraries.jpg", width = 5) 40 | 41 | # SPATIAL JOIN 42 | ?st_join 43 | 44 | # count how many libraries in each area 45 | libraries_with_areas <- st_join(libraries, areas) 46 | count(libraries_with_areas, community) 47 | 48 | # use the pipe to do it in one line 49 | st_join(libraries, areas) %>% count(community) 50 | 51 | naniar::vis_miss(libraries_with_areas) 52 | naniar::gg_miss_var(libraries_with_areas) 53 | 54 | st_join(areas, libraries) 55 | 56 | ggplot() + 57 | geom_sf(data = areas) + 58 | geom_sf(data = libraries_with_areas, aes(color = community)) 59 | 60 | # alt - gives you <- -------------------------------------------------------------------------------- /gis-visualization/spring-2019/data/ward1998.dbf: -------------------------------------------------------------------------------- 1 | e 2aWWARDCCOUNTN 10 46 11 54 12 30 13 63 14 48 15 48 16 42 17 55 18 63 2 54 20 52 21 70 22 29 23 62 24 53 25 23 26 37 27 55 28 61 29 56 3 52 30 45 31 45 32 48 33 38 34 61 35 35 36 56 37 50 38 56 39 49 4 55 40 47 41 66 42 67 43 60 44 50 45 58 46 38 47 45 48 47 49 37 5 59 50 46 6 64 8 66 9 52 19 53 1 36 7 46 -------------------------------------------------------------------------------- /gis-visualization/spring-2019/data/ward1998.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /gis-visualization/spring-2019/data/ward1998.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/spring-2019/data/ward1998.shp -------------------------------------------------------------------------------- /gis-visualization/spring-2019/data/ward1998.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/spring-2019/data/ward1998.shx -------------------------------------------------------------------------------- /gis-visualization/spring-2019/doc/custom-map.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Making Custom Maps with tmap" 3 | author: "Angela Li" 4 | date: "5/22/2019" 5 | output: html_document 6 | --- 7 | 8 | ```{r setup, include=FALSE} 9 | knitr::opts_chunk$set(echo = TRUE) 10 | ``` 11 | 12 | ## Animated Map Example 13 | 14 | This is our ultimate goal! 15 | ```{r eval=FALSE} 16 | library(sf) 17 | library(tidyverse) 18 | library(spData) 19 | library(tmap) 20 | m_save = world %>% filter(continent != "Antarctica") %>% 21 | tm_shape() + 22 | tm_polygons() + 23 | tm_shape(urban_agglomerations) + 24 | tm_dots(size = "population_millions", title.size = "Population (m)", alpha = 0.5, col = "red") + 25 | tm_facets(along = "year", free.coords = FALSE) 26 | tmap::tmap_animation(tm = m_save, filename = "/tmp/urban-animated.gif", width = 1200, height = 800) 27 | magick::image_read("/tmp/urban-animated.gif") 28 | ``` 29 | 30 | # Load packages 31 | ```{r message=FALSE} 32 | library(sf) # for spatial data 33 | library(spData) # sample data I'm working with 34 | library(tidyverse) # for data wrangling 35 | library(tmap) # for custom maps 36 | ``` 37 | 38 | # Explore my data 39 | ```{r} 40 | head(urban_agglomerations) 41 | head(world) 42 | 43 | summary(urban_agglomerations) 44 | str(urban_agglomerations) # base R version 45 | glimpse(urban_agglomerations) # tidyverse version - I prefer this one 46 | class(urban_agglomerations) # tells you what type of object this is 47 | dim(urban_agglomerations) # dimensions of data 48 | ``` 49 | 50 | # Make a ggplot2 map 51 | ```{r} 52 | ggplot() + 53 | geom_sf(data = urban_agglomerations) 54 | ``` 55 | 56 | ```{r} 57 | ggplot() + 58 | geom_sf(data = world) + 59 | geom_sf(data = urban_agglomerations) 60 | ``` 61 | 62 | # Try making the same map in tmap 63 | ```{r} 64 | tm_shape(world) + 65 | tm_polygons() + 66 | tm_shape(urban_agglomerations) + 67 | tm_dots(size = "population_millions") + 68 | tm_compass() + 69 | tm_scale_bar(position = c("left", "bottom")) + 70 | tm_legend(legend.outside = TRUE) + 71 | tm_layout(panel.show = TRUE, panel.labels = c("World population in 1950")) 72 | ``` 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/doc/figs/green-red-libraries.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/spring-2019/doc/figs/green-red-libraries.jpg -------------------------------------------------------------------------------- /gis-visualization/spring-2019/doc/interactive-maps.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Practice Interactive Maps" 3 | author: "Angela Li" 4 | date: "5/29/2019" 5 | output: html_document 6 | --- 7 | 8 | ```{r setup, include=FALSE} 9 | knitr::opts_chunk$set(echo = TRUE) 10 | ``` 11 | 12 | ## Load packages 13 | 14 | ```{r} 15 | library(tmap) 16 | library(mapview) 17 | library(leaflet) 18 | library(spData) 19 | library(dplyr) 20 | ``` 21 | 22 | ## Data wrangling 23 | 24 | ```{r} 25 | urban10 <- filter(urban_agglomerations, year == 2010) 26 | urban15 <- filter(urban_agglomerations, year == 2015) 27 | ``` 28 | 29 | ## Arrange tmaps 30 | 31 | ```{r} 32 | urban10_map <- qtm(urban10) 33 | urban15_map <- qtm(urban15) 34 | 35 | tmap_mode("view") 36 | tmap_arrange(urban10_map, urban15_map, ncol = 2) 37 | ``` 38 | 39 | ```{r} 40 | leaflet::providers 41 | ``` 42 | 43 | 44 | ## Make static tmap 45 | 46 | ```{r} 47 | tmap_mode("plot") 48 | 49 | tm_shape(world) + 50 | tm_polygons() + 51 | tm_shape(urban) + 52 | tm_dots(size = "population_millions") 53 | ``` 54 | 55 | ## Make interactive tmap 56 | 57 | ```{r} 58 | tmap_mode("view") 59 | 60 | tm_shape(urban) + 61 | tm_dots("population_millions") + 62 | tm_basemap(server = "Stamen.Terrain") 63 | ``` 64 | 65 | ## Make mapview interactive map 66 | 67 | ```{r} 68 | mapview(urban10, popup = popupTable(urban10, 69 | zcol = c("year", 70 | "country_or_area", 71 | "urban_agglomeration", 72 | "population_millions"))) 73 | ``` 74 | 75 | ```{r} 76 | viewExtent(urban10, alpha.regions = 0.5) 77 | ``` 78 | 79 | # Leaflet map 80 | 81 | ```{r} 82 | leaflet(data = urban10) %>% 83 | addTiles() %>% 84 | addMarkers(popup = ~urban_agglomeration) 85 | ``` 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /gis-visualization/spring-2019/doc/rmarkdown-practice.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "R Markdown Practice" 3 | author: "Angela Li" 4 | date: "5/15/2019" 5 | output: 6 | html_document 7 | --- 8 | 9 | ```{r setup, include=FALSE} 10 | knitr::opts_chunk$set(echo = TRUE, cache = TRUE) 11 | ``` 12 | 13 | ## Data Used 14 | 15 | We will be using two datasets: 16 | 17 | - [Library **point** data](https://data.cityofchicago.org/Education/Libraries-Locations-Hours-and-Contact-Information/x8fc-8rcq) 18 | - [Community *area* polygons](https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6) 19 | 20 | We will store these as the `libraries` and `areas` datasets. 21 | 22 | ### Details of Data 23 | 24 | 1. The data was collected from Chicago Open Data. 25 | 2. The data was last updated in 2017. 26 | 27 | # Load Packages 28 | 29 | ```{r packages, message=FALSE} 30 | library(sf) 31 | library(ggplot2) 32 | library(dplyr) 33 | ``` 34 | 35 | # Read and project data 36 | 37 | ```{r project-data, results='hide', echo=FALSE} 38 | libraries <- read_sf("https://data.cityofchicago.org/resource/psqp-6rmg.geojson") 39 | areas <- read_sf("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 40 | 41 | libraries <- st_transform(libraries, 32616) 42 | areas <- st_transform(areas, 32616) 43 | 44 | libraries 45 | areas 46 | ``` 47 | 48 | # Make a map! 49 | 50 | ```{r map, fig.height=3, fig.width=3} 51 | ggplot() + 52 | geom_sf(data = areas) + 53 | geom_sf(data = libraries) 54 | ``` 55 | 56 | 57 | 58 | 59 | ## R Markdown 60 | 61 | This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . 62 | 63 | When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: 64 | 65 | ```{r cars} 66 | summary(cars) 67 | ``` 68 | 69 | ## Including Plots 70 | 71 | You can also embed plots, for example: 72 | 73 | ```{r pressure, echo=FALSE, eval=FALSE} 74 | plot(pressure) 75 | ``` 76 | 77 | Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. 78 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/R/01-intro.R: -------------------------------------------------------------------------------- 1 | # Import shapefile into R 2 | library(sf) 3 | 4 | ward86 <- st_read("data/ward1986.shp") 5 | 6 | ward86_utm <- st_transform(ward86, 32616) 7 | ward86_stateplane <- st_transform(ward86, 3435) 8 | # Bad 9 | # ward86_alaska <- st_transform(ward86, 3338) 10 | 11 | st_crs(ward86) 12 | st_crs(ward86_utm) 13 | st_crs(ward86_stateplane) 14 | 15 | plot(ward86) 16 | plot(ward86_utm) 17 | plot(ward86_stateplane) 18 | plot(ward86_alaska) 19 | 20 | # Challenge: project ward86 to Illinois East state plane projection (use Google to look up the EPSG code) 21 | #Ctrl-Enter to run a line of code -------------------------------------------------------------------------------- /gis-visualization/winter-2019/R/02-gis-1.R: -------------------------------------------------------------------------------- 1 | # Challenge: import 1986 ward data and project it into the correct UTM projection 2 | library(sf) 3 | ward86 <- st_read("data/ward1986.shp") 4 | ward86 <- st_transform(ward86, 32616) 5 | plot(ward86) 6 | 7 | plot(st_geometry(ward86)) 8 | ?st_centroid 9 | 10 | centroids <- st_centroid(ward86) 11 | plot(st_geometry(centroids), cex = 0.1) 12 | plot(st_geometry(ward86), add = T) 13 | 14 | plot(st_geometry(ward86)) 15 | plot(st_geometry(centroids), cex = 0.1, add = T) 16 | 17 | # challenge: figure out how to set a bounding box to something else 18 | st_bbox(centroids) 19 | ward_bbox <- st_bbox(ward86) 20 | 21 | # to get coordinates of something 22 | st_coordinates(centroids) 23 | 24 | # random detour - plot a single ward and centroid 25 | library(dplyr) 26 | ward1 <- filter(ward86, WARDNO == "01") 27 | centroid1 <- filter(centroids, WARDNO == "01") 28 | 29 | plot(st_geometry(ward1)) 30 | plot(st_geometry(centroid1), add = T, cex = 0.1) 31 | 32 | # make buffers 33 | st_crs(centroids) 34 | buffers <- st_buffer(centroids, 1000) 35 | 36 | # challenge: plot wards, centroids, buffers altogether on one map 37 | plot(st_geometry(buffers), col = "yellow") 38 | plot(st_geometry(ward86), add = T) 39 | plot(st_geometry(centroids), add = T, cex = 0.2, col = "red") 40 | 41 | # functions learned 42 | st_geometry() 43 | st_centroid() 44 | st_buffer() 45 | st_bbox() 46 | st_coordinates() -------------------------------------------------------------------------------- /gis-visualization/winter-2019/R/03-gis-2.R: -------------------------------------------------------------------------------- 1 | # challenge 1 time 2 | library(sf) 3 | library(ggplot2) 4 | library(dplyr) 5 | 6 | ward98 <- st_read("data/ward1998.shp") 7 | ward98 <- st_transform(ward98, 32616) 8 | water <- st_read("data/Waterways.geojson") 9 | water <- st_transform(water, 32616) 10 | 11 | centroids <- st_centroid(ward98) 12 | 13 | ggplot(data = ward98) + 14 | geom_sf() 15 | 16 | ggplot(data = centroids) + 17 | geom_sf() 18 | 19 | ggplot() + 20 | geom_sf(data = ward98, aes(fill = COUNT)) + 21 | geom_sf(data = centroids, color = "red") 22 | 23 | ggplot() + 24 | geom_sf(data = water) 25 | 26 | View(water) 27 | names(water) 28 | unique(water$name) 29 | 30 | water_clean <- filter(water, name != "LAKE MICHIGAN") 31 | 32 | ggplot() + 33 | geom_sf(data = ward98) + 34 | geom_sf(data = water_clean, color = "blue") 35 | 36 | intersects <- st_intersects(ward98, water_clean) 37 | water_wards <- filter(ward98, lengths(intersects) > 0) 38 | 39 | ggplot() + 40 | geom_sf(data = ward98) + 41 | geom_sf(data = water_wards, fill = "lightblue") + 42 | geom_sf(data = water_clean, color = "blue") 43 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/R/04-gis-3.R: -------------------------------------------------------------------------------- 1 | # Load libraries for use 2 | library(sf) 3 | library(ggplot2) 4 | library(dplyr) 5 | 6 | # Read in libraries and areas data 7 | libraries <- st_read("https://data.cityofchicago.org/resource/psqp-6rmg.geojson") 8 | areas <- st_read("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 9 | 10 | # Project both 11 | libraries <- st_transform(libraries, 32616) 12 | areas <- st_transform(areas, 32616) 13 | 14 | # Make a ggplot with libraries and community areas 15 | ggplot() + 16 | geom_sf(data = areas) + 17 | geom_sf(data = libraries) 18 | 19 | # Find which areas intersect with libraries and save as a variable called "intersects" 20 | intersects <- st_intersects(areas, libraries) 21 | intersects 22 | ?st_intersects 23 | 24 | # Filter areas by *without* libraries. Save as a variable called "no_lib" Hint: use "==" instead of ">" in the logical comparison 25 | no_lib <- filter(areas, lengths(intersects) == 0) 26 | 27 | # Make a ggplot with libraries, community areas, and community areas without libraries 28 | ggplot() + 29 | geom_sf(data = areas) + 30 | geom_sf(data = no_lib, fill = "pink") + 31 | geom_sf(data = libraries) 32 | 33 | ggsave("doc/areas_without_libraries_big.png", width = 4, height = 4) 34 | 35 | st_join(libraries, areas) 36 | st_join(libraries, areas["community"]) 37 | 38 | areas <- select(areas, community, area_numbe, geometry) 39 | 40 | lib_join <- st_join(libraries, areas) 41 | 42 | # This is just to help me figure out which columns to select 43 | names(areas) 44 | str(areas) 45 | glimpse(areas) 46 | 47 | lib_counts <- count(lib_join, community) 48 | 49 | arrange(lib_counts, desc(n)) 50 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/R/leaflet-example/app.R: -------------------------------------------------------------------------------- 1 | # 2 | # This is a Shiny web application. You can run the application by clicking 3 | # the 'Run App' button above. 4 | # 5 | # Find out more about building applications with Shiny here: 6 | # 7 | # http://shiny.rstudio.com/ 8 | # 9 | 10 | library(shiny) 11 | library(leaflet) 12 | library(spData) 13 | library(dplyr) 14 | 15 | # Define UI for application that filters map points based on year and minimum population 16 | ui <- fluidPage( 17 | 18 | # Application title 19 | titlePanel("World Population Over Time"), 20 | 21 | # Sidebar with a slider input for year, numeric input for population 22 | sidebarLayout( 23 | sidebarPanel( 24 | 25 | sliderInput("year", 26 | "Year", 27 | min = 1950, 28 | max = 2030, 29 | step = 5, 30 | sep = "", 31 | value = 1950), 32 | 33 | numericInput("pop_min", 34 | "Minimum Population (in millions)", 35 | min = 1, 36 | max = 20, 37 | value = 10) 38 | ), 39 | 40 | # Show the map and table 41 | mainPanel( 42 | # plotOutput("distPlot"), 43 | leafletOutput("map"), 44 | dataTableOutput("table") 45 | ) 46 | ) 47 | ) 48 | 49 | # Define server logic required to draw a map and table 50 | server <- function(input, output) { 51 | 52 | 53 | output$map <- renderLeaflet({ 54 | 55 | pop_by_year <- filter(urban_agglomerations, 56 | year == input$year, 57 | population_millions > input$pop_min) 58 | 59 | leaflet(data = pop_by_year) %>% 60 | addTiles() %>% 61 | addMarkers() 62 | }) 63 | 64 | output$table <- renderDataTable({ 65 | 66 | pop_by_year <- filter(urban_agglomerations, 67 | year == input$year, 68 | population_millions > input$pop_min) 69 | 70 | pop_by_year 71 | 72 | }) 73 | } 74 | 75 | # Run the application 76 | shinyApp(ui = ui, server = server) -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1986.dbf: -------------------------------------------------------------------------------- 1 | b3AWWARDNOC 01 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 02 30 31 32 33 34 35 36 37 38 39 03 40 41 42 43 44 45 46 47 48 49 04 50 05 06 08 09 10 07  -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1986.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1986.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1986.shp -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1986.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1986.shx -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1992.dbf: -------------------------------------------------------------------------------- 1 | b 2 | 2aWARD_NONCOUNTN 11 355 19 126 20 344 24 327 26 277 27 424 28 350 29 283 31 298 35 222 33 290 30 371 36 501 37 276 38 425 39 376 40 289 41 555 43 212 44 195 45 534 46 113 47 294 48 124 49 153 50 297 5 294 8 437 6 441 7 332 10 513 1 248 32 361 42 196 2 219 25 261 22 229 4 233 3 297 16 339 12 226 15 358 14 386 17 353 23 625 18 481 13 521 21 224 9 392 34 402 -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1992.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1992.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1992.shp -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1992.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1992.shx -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1998.dbf: -------------------------------------------------------------------------------- 1 | e 2aWWARDCCOUNTN 10 46 11 54 12 30 13 63 14 48 15 48 16 42 17 55 18 63 2 54 20 52 21 70 22 29 23 62 24 53 25 23 26 37 27 55 28 61 29 56 3 52 30 45 31 45 32 48 33 38 34 61 35 35 36 56 37 50 38 56 39 49 4 55 40 47 41 66 42 67 43 60 44 50 45 58 46 38 47 45 48 47 49 37 5 59 50 46 6 64 8 66 9 52 19 53 1 36 7 46 -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1998.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1998.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1998.shp -------------------------------------------------------------------------------- /gis-visualization/winter-2019/data/ward1998.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/data/ward1998.shx -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/05-rmarkdown.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "R Markdown Practice" 3 | author: "Angela Li" 4 | date: "2/12/2019" 5 | output: html_document 6 | --- 7 | 8 | ```{r setup, include=FALSE} 9 | knitr::opts_chunk$set(echo = TRUE) 10 | ``` 11 | 12 | ## Data Used 13 | 14 | Here is an explanation of the data I used in this analysis: 15 | 16 | - [**Library data**](https://data.cityofchicago.org/Parks-Recreation/Waterways/eg9f-z3t6): points 17 | - **Community area data**: polygons 18 | 19 | ### Rationale for Data 20 | Why I used this data: 21 | 22 | 1. *Different geometries* 23 | 2. *Relevant and interesting* 24 | 25 | ![](areas_without_libraries.png) 26 | 27 | # Load packages 28 | ```{r, message=FALSE, warning=FALSE} 29 | library(sf) 30 | library(ggplot2) 31 | library(dplyr) 32 | ``` 33 | 34 | # Read and project data 35 | ```{r results='hide'} 36 | areas <- st_read("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 37 | libraries <- st_read("https://data.cityofchicago.org/resource/psqp-6rmg.geojson") 38 | 39 | areas <- st_transform(areas, 32616) 40 | libraries <- st_transform(libraries, 32616) 41 | ``` 42 | 43 | # Make a ggplot! 44 | ```{r map, echo=FALSE, fig.height=2, fig.width=2} 45 | ggplot() + 46 | geom_sf(data = areas) + 47 | geom_sf(data = libraries) 48 | ``` 49 | 50 | # A useful shortcut 51 | Use `Ctrl-Alt-I` to insert a code chunk. Or click the "Insert" button at the top of a R Markdown document. 52 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/05-rmarkdown.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/doc/05-rmarkdown.pdf -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/06-custom-maps.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Custom and Animated Maps" 3 | author: "Angela Li" 4 | date: "2/19/2019" 5 | output: html_document 6 | --- 7 | 8 | ```{r setup, include=FALSE} 9 | knitr::opts_chunk$set(echo = TRUE) 10 | ``` 11 | 12 | > Tip: Use the `Ctrl-Opt-I` keyboard shortcut to insert a code chunk into your Markdown document. 13 | 14 | ## Load packages 15 | 16 | ```{r warning=FALSE, message=FALSE} 17 | library(sf) 18 | library(ggplot2) 19 | library(spData) 20 | library(tmap) 21 | library(magick) 22 | ``` 23 | 24 | ## Look at data 25 | 26 | ```{r} 27 | head(urban_agglomerations) 28 | str(urban_agglomerations) 29 | names(urban_agglomerations) 30 | ``` 31 | 32 | ```{r} 33 | tail(urban_agglomerations) 34 | ``` 35 | 36 | Looks like `urban_agglomerations` is a panel-data version of population of global cities, between 1950 and 2030. 37 | 38 | ## Make a ggplot2 map 39 | 40 | ```{r} 41 | ggplot(urban_agglomerations) + 42 | geom_sf() 43 | ``` 44 | ```{r echo=FALSE, warning=FALSE, message=FALSE, results='hide'} 45 | # I had to do this because my spData for some reason was weird, you may not need to do this 46 | library(dplyr) 47 | 48 | names(world) 49 | world <- rename(world, geometry = geom) 50 | 51 | names(world) 52 | ``` 53 | 54 | 55 | ```{r} 56 | ggplot(world) + 57 | geom_sf() 58 | ``` 59 | 60 | ```{r} 61 | ggplot() + 62 | geom_sf(data = world) + 63 | geom_sf(data = urban_agglomerations) 64 | ``` 65 | 66 | ## Move it over to tmap! 67 | 68 | ### Single custom map 69 | 70 | ```{r} 71 | tm_shape(world) + 72 | tm_polygons() + 73 | tm_shape(urban_agglomerations) + 74 | tm_dots(size = "population_millions", title.size = "Population (m)", col = "red") 75 | ``` 76 | 77 | ### Map with small multiples 78 | 79 | ```{r} 80 | tm_shape(world) + 81 | tm_polygons() + 82 | tm_shape(urban_agglomerations) + 83 | tm_dots(size = "population_millions", title.size = "Population (m)", col = "red") + 84 | tm_facets(by = "year") 85 | ``` 86 | 87 | ### Map (animated!!) 88 | 89 | ```{r} 90 | tm <- tm_shape(world) + 91 | tm_polygons() + 92 | tm_shape(urban_agglomerations) + 93 | tm_dots(size = "population_millions", title.size = "Population (m)", col = "red") + 94 | tm_facets(along = "year", free.coords = FALSE) 95 | 96 | tmap_animation(tm, "city-pop-anim.gif", loop = TRUE, delay = 80) 97 | ``` 98 | 99 | ```{r} 100 | magick::image_read("city-pop-anim.gif") 101 | ``` 102 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/07-interactive-maps.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Interactive Map Practice" 3 | author: "Angela Li" 4 | date: "2/26/2019" 5 | output: html_document 6 | --- 7 | 8 | ```{r setup, include=FALSE} 9 | knitr::opts_chunk$set(echo = TRUE) 10 | ``` 11 | 12 | ## Make static tmap 13 | 14 | Challenge: use `spData` and `tmap` to make a map of `world` and `urbanagglomerations`. 15 | 16 | ```{r} 17 | library(spData) 18 | library(tmap) 19 | library(mapview) 20 | library(leaflet) 21 | ``` 22 | 23 | ```{r} 24 | tmap_mode("view") 25 | 26 | tm <- tm_shape(urban_agglomerations) + 27 | tm_dots(title.size = "Population (m)") 28 | 29 | tm + tm_basemap(server = "Esri.OpenTopoMap") 30 | ``` 31 | 32 | ```{r} 33 | ?tm_basemap 34 | leaflet::providers 35 | ``` 36 | 37 | ```{r} 38 | ?mapview 39 | ``` 40 | 41 | 42 | ```{r} 43 | mapview(world) + 44 | mapview(urban_agglomerations) 45 | ``` 46 | 47 | ```{r} 48 | leaflet(data = urban_agglomerations) %>% 49 | addProviderTiles(providers$OpenStreetMap) %>% 50 | addCircles() %>% 51 | setView(lng = -41, lat = 30, zoom = 2) %>% 52 | addMiniMap() 53 | ``` 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/areas_without_libraries.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/doc/areas_without_libraries.jpg -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/areas_without_libraries.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/doc/areas_without_libraries.png -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/areas_without_libraries_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/doc/areas_without_libraries_big.png -------------------------------------------------------------------------------- /gis-visualization/winter-2019/doc/city-pop-anim.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spatialanalysis/workshop-scripts/f4ab527469c1026362cf094ab696897d290b63d0/gis-visualization/winter-2019/doc/city-pop-anim.gif -------------------------------------------------------------------------------- /intro-spatial-data-sci/01-spatial-data-handling.R: -------------------------------------------------------------------------------- 1 | install.packages(c("tidyverse", 2 | "RSocrata", 3 | "sf", 4 | "tmap", 5 | "lubridate")) 6 | 7 | library(tidyverse) # Ctrl-Enter to run a line 8 | library(RSocrata) 9 | library(sf) 10 | library(tmap) 11 | library(lubridate) 12 | 13 | vehicle_data <- read.socrata("https://data.cityofchicago.org/resource/suj7-cg3j.csv") 14 | 15 | head(vehicle_data) 16 | dim(vehicle_data) 17 | class(vehicle_data) 18 | glimpse(vehicle_data) # other ways to explore 19 | str(vehicle_data) 20 | 21 | vehicle_data %>% 22 | filter(year(creation_date) == 2005) 23 | 24 | vehicle_data$creation_date 25 | year(vehicle_data$creation_date) 26 | 27 | angela %>% 28 | brush_hair %>% 29 | eat_breakfast %>% 30 | go_to_work %>% 31 | teach_r_spatial 32 | 33 | group_by(select(filter(vehicle_data, year(creation_date) == 2005), x)) 34 | 35 | names(vehicle_data) 36 | unique(month(vehicle_data$creation_date)) 37 | 38 | vehicle_sept16 <- vehicle_data %>% 39 | filter(year(creation_date) == 2016, 40 | month(creation_date) == 9) 41 | 42 | head(vehicle_sept16) 43 | glimpse(vehicle_sept16) 44 | str(vehicle_sept16) 45 | dim(vehicle_sept16) 46 | 47 | vehicle_final <- vehicle_sept16 %>% 48 | select(lon = longitude, 49 | lat = latitude, 50 | comm = community_area) 51 | 52 | vehicle_coord <- filter(vehicle_final, !is.na(lon), !is.na(lat)) 53 | 54 | vehicle_points <- st_as_sf(vehicle_coord, 55 | coords = c("lon", "lat"), 56 | crs = 4326, 57 | agr = "constant") 58 | 59 | plot(vehicle_points) 60 | class(vehicle_points) 61 | st_crs(vehicle_points) 62 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/intro-rgeoda.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Intro to rgeoda" 3 | author: "Angela Li" 4 | date: "11/19/2019" 5 | output: 6 | pdf_document: default 7 | html_document: default 8 | --- 9 | 10 | ```{r setup, include=FALSE} 11 | knitr::opts_chunk$set(echo = TRUE) 12 | ``` 13 | 14 | ## Load data 15 | 16 | ```{r} 17 | library(rgeoda) 18 | library(geodaData) 19 | library(sf) 20 | 21 | guerry_sf <- geodaData::guerry 22 | ``` 23 | 24 | ## Plot maps 25 | 26 | ```{r} 27 | plot(guerry_sf) 28 | ``` 29 | 30 | Explore data, looks like this a a Lambert Conformal Conic Projection. 31 | 32 | ```{r} 33 | head(guerry_sf) 34 | st_crs(guerry_sf) 35 | ``` 36 | 37 | ```{r} 38 | guerry <- sf_to_geoda(guerry_sf, with_table = TRUE) 39 | class(guerry) 40 | ``` 41 | 42 | ```{r} 43 | guerry$table 44 | guerry$n_cols 45 | ``` 46 | 47 | ## Calculate queen weights 48 | 49 | ```{r} 50 | queen_w <- queen_weights(guerry) 51 | ``` 52 | 53 | To save this.... 54 | 55 | ```{r} 56 | class(queen_w) 57 | queen_w$SaveToFile("queen_weights.gal", 58 | "guerry", 59 | "CODE_DE", 60 | guerry$GetIntegerCol("CODE_DE")) 61 | ``` 62 | 63 | ## Local Moran 64 | 65 | ```{r} 66 | lisa <- local_moran(queen_w, guerry$GetIntegerCol("crm_prp")) 67 | 68 | class(lisa) 69 | ``` 70 | 71 | ```{r} 72 | lisa$GetLISAValues() 73 | lisa_colors <- lisa$GetColors() 74 | lisa_labels <- lisa$GetLabels() 75 | lisa_clusters <- lisa$GetClusterIndicators() 76 | 77 | lisa_colors 78 | lisa_clusters 79 | ``` 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/week2-fall2019-workshop.R: -------------------------------------------------------------------------------- 1 | library(RSocrata) 2 | library(sf) 3 | library(lubridate) 4 | library(tidyverse) 5 | 6 | vehicle_data <- read.socrata("https://data.cityofchicago.org/resource/suj7-cg3j.csv") 7 | 8 | vehicle_final <- vehicle_data %>% 9 | filter(year(creation_date) == 2016, 10 | month(creation_date) == 9) %>% 11 | select(comm = community_area, 12 | lon = longitude, 13 | lat = latitude) %>% 14 | filter(!is.na(lon), !is.na(lat)) 15 | 16 | vehicle_pts <- st_as_sf(vehicle_final, 17 | coords = c("lon", "lat"), 18 | crs = 4326, 19 | agr = "constant") 20 | 21 | plot(vehicle_pts) 22 | 23 | st_crs(vehicle_pts) 24 | vehicle_pts <- st_transform(vehicle_pts, 32616) 25 | 26 | st_crs(vehicle_pts) 27 | vehicle_pts 28 | 29 | vehicle_pts_buffer <- st_buffer(vehicle_pts, 1000) 30 | plot(vehicle_pts_buffer) 31 | 32 | comm_areas <- read_sf("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 33 | 34 | st_crs(comm_areas) 35 | 36 | comm_areas <- st_transform(comm_areas, 32616) 37 | comm_pts <- st_join(vehicle_pts, comm_areas) 38 | 39 | count(comm_pts, community) %>% 40 | filter(community == "HYDE PARK") %>% 41 | plot() 42 | 43 | count(comm_pts, community) %>% 44 | arrange(desc(n)) 45 | 46 | merge(x, y, by = "id") # attribute join 47 | st_join(x, y) # spatial join 48 | 49 | 50 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/week3-fall2019-workshop.R: -------------------------------------------------------------------------------- 1 | install.packages("spData") 2 | library(spData) 3 | library(sf) 4 | us_states 5 | 6 | plot(us_states) 7 | 8 | install.packages("remotes") 9 | remotes::install_github("spatialanalysis/geodaData") 10 | 11 | library(geodaData) 12 | data("chicago_comm") 13 | data("vehicle_pts") 14 | 15 | head(vehicle_pts) 16 | head(chicago_comm) 17 | str(chicago_comm) 18 | summary() 19 | plot(chicago_comm["POP2010"]) 20 | st_crs(chicago_comm) 21 | chicago_comm <- st_transform(chicago_comm, 32616) 22 | st_crs(chicago_comm) 23 | 24 | st_crs(vehicle_pts) 25 | vehicle_pts <- st_transform(vehicle_pts, 32616) 26 | 27 | # Order matters! 28 | # st_join(chicago_comm, vehicle_pts) 29 | comm_pts <- st_join(vehicle_pts, chicago_comm) # spatial join 30 | head(comm_pts) 31 | 32 | library(dplyr) 33 | counts_by_area <- count(comm_pts, community) 34 | 35 | counts_by_area <- st_drop_geometry(counts_by_area) %>% 36 | rename(number_vehicles = n) 37 | 38 | chicago_comm <- left_join(chicago_comm, counts_by_area) #attribute join 39 | 40 | library(tmap) 41 | 42 | chicago_comm <- chicago_comm %>% 43 | mutate(community = as.character(community)) 44 | 45 | tm_shape(chicago_comm) + 46 | tm_polygons("number_vehicles") 47 | 48 | str(chicago_comm) 49 | 50 | nyc_sf 51 | plot(nyc_sf) 52 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/week4-fall2019-workshop.R: -------------------------------------------------------------------------------- 1 | # install_github("spatialanalysis/geodaData") 2 | 3 | library(geodaData) 4 | library(sf) 5 | library(tmap) 6 | 7 | head(nyc_sf) 8 | dim(nyc_sf) 9 | str(nyc_sf) 10 | plot(nyc_sf) # all attributes 11 | names(nyc_sf) 12 | plot(nyc_sf["geometry"]) # just outlines 13 | 14 | summary(nyc_sf) 15 | ?nyc_sf 16 | 17 | st_crs(nyc_sf) 18 | 19 | tm_shape(nyc_sf) + 20 | tm_polygons() + 21 | tm_shape(st_centroid(nyc_sf)) + 22 | tm_dots(size = 0.5) 23 | 24 | tm_shape(nyc_sf) + 25 | tm_polygons("rent2008") 26 | 27 | tm_shape(nyc_sf) + 28 | tm_fill("rent2008") + 29 | tm_borders() 30 | 31 | tmap_mode("view") 32 | 33 | tm_shape(nyc_sf) + 34 | tm_polygons("rent2008") + 35 | tm_basemap(server = "OpenStreetMap") 36 | 37 | tmap_mode("plot") 38 | 39 | leaflet::providers 40 | 41 | ?tm_polygons 42 | 43 | tm_shape(nyc_sf) + 44 | tm_fill("rent2008", title = "Rent in 2008", legend.hist = TRUE) + 45 | tm_borders() + 46 | tm_layout(main.title = "Rent in NYC Boroughs, 2008", compass.type = "4star", legend.outside = TRUE) + 47 | tm_compass() + 48 | tm_scale_bar(position = c("left", "top")) 49 | 50 | tm_shape(nyc_sf) + 51 | tm_fill("rent2008", title = "Rent in 2008", legend.hist = TRUE) + 52 | tm_borders() + 53 | tm_style_cobalt() 54 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/week5-fall2019-workshop.R: -------------------------------------------------------------------------------- 1 | library(geodaData) 2 | library(sf) 3 | library(tmap) 4 | library(sfExtras) 5 | library(spdep) 6 | 7 | head(ncovr) 8 | ?ncovr 9 | str(ncovr) 10 | summary(ncovr) 11 | 12 | plot(ncovr) 13 | plot(ncovr["geometry"]) 14 | 15 | st_crs(ncovr) 16 | 17 | tm_shape(ncovr) + 18 | tm_polygons("HR60") 19 | 20 | tmap_mode("view") 21 | 22 | tm_shape(ncovr) + 23 | tm_polygons("HR60") 24 | 25 | ncovr_rook <- st_rook(ncovr) 26 | ncovr_queen <- st_queen(ncovr) 27 | 28 | # check average number of neighbors per county 29 | rook_neighbors <- lengths(ncovr_rook) 30 | queen_neighbors <- lengths(ncovr_queen) 31 | 32 | mean(rook_neighbors) # 5.6 ish 33 | mean(queen_neighbors) # 5.8 ish 34 | 35 | # convert lists of neighbors to "nb" object to make map 36 | rook_nb <- st_as_nb(ncovr_rook) 37 | summary(rook_nb) 38 | 39 | queen_nb <- st_as_nb(ncovr_queen) 40 | summary(queen_nb) 41 | 42 | centroid_coords <- st_centroid_coords(ncovr) 43 | 44 | plot(queen_nb, centroid_coords, lwd = 0.2, cex = 0.5, col = "blue") 45 | 46 | 47 | st_coordinates(clev_pts) 48 | -------------------------------------------------------------------------------- /intro-spatial-data-sci/week6-fall2019-workshop.R: -------------------------------------------------------------------------------- 1 | library(geodaData) 2 | library(sf) 3 | library(spdep) 4 | library(tmap) 5 | 6 | head(clev_pts) 7 | dim(clev_pts) 8 | names(clev_pts) 9 | 10 | str(clev_pts) 11 | st_crs(clev_pts) 12 | 13 | plot(clev_pts) 14 | plot(clev_pts["geometry"]) 15 | 16 | tmap_mode("view") 17 | 18 | tm_shape(clev_pts) + 19 | tm_dots() 20 | 21 | ?dnearneigh 22 | 23 | dist_1000_nb <- dnearneigh(clev_pts, 0, 1000) 24 | 25 | coords <- st_coordinates(clev_pts) 26 | plot(dist_1000_nb, coords) 27 | 28 | ?knearneigh 29 | knn_matrix <- knearneigh(clev_pts) 30 | 31 | k1_nb <- knn2nb(knn_matrix) 32 | plot(k1_nb, coords) 33 | 34 | # look at distances between neighbors in knn 35 | k1_dists <- unlist(nbdists(k1_nb, coords)) 36 | summary(k1_dists) 37 | 38 | # set fixed distance so that each point has at least 1 neighbor 39 | critical_thres <- max(k1_dists) 40 | dist_critical_nb <- dnearneigh(clev_pts, 0, critical_thres) 41 | plot(dist_critical_nb, coords) 42 | 43 | # how many neighbors does each point have? 44 | number_neighbors <- card(dist_critical_nb) 45 | 46 | # plot histogram with # of neighbors? 47 | summary(number_neighbors) 48 | 49 | k6_nb <- knn2nb(knearneigh(clev_pts, k = 6)) 50 | plot(k6_nb, st_coordinates(clev_pts), cex = 0.5) 51 | 52 | # can we do this with tmap? investigate 53 | 54 | 55 | st_intersection(tracts, chicago_boundary) 56 | 57 | clev_pts_joined <- st_join(clev_pts, polygon) 58 | st_write(clev_pts_joined, "clev_pts_joined.shp") 59 | -------------------------------------------------------------------------------- /node-temps.csv: -------------------------------------------------------------------------------- 1 | node_id,avg_temp,avg_temp_f,project_id,vsn,address,lat,lon,description,start_timestamp 2 | 001e06109416,26.0126532,78.82277577,AoT_Chicago,090B,Elston and Cortland Chicago IL,41.916586,-87.666406,AoT Chicago (S) [C],1/1/18 0:00 3 | 001e0610b9e9,25.8794788,78.58306185,AoT_Chicago,80,Broadway Ave & Lawrence Ave Chicago IL,41.96904,-87.659672,AoT Chicago (S) [C],2/14/18 0:00 4 | 001e0610ba8f,28.30287234,82.94517021,AoT_Chicago,00D,Cornell & 47th St Chicago IL,41.810342,-87.590228,AoT Chicago (S),8/8/17 0:00 5 | 001e0610bbf9,25.96714385,78.74085893,AoT_Chicago,20,Western Ave & 69th St Chicago IL,41.768319,-87.683396,AoT Chicago (S) [C],2/13/18 0:00 6 | 001e0610bbff,27.93933989,82.2908118,AoT_Chicago,25,Western Ave & 18th St Chicago IL,41.857797,-87.685806,AoT Chicago (S),12/15/17 0:00 7 | 001e0610bc10,25.51712251,77.93082051,AoT_Chicago,01F,State St & 87th Chicago IL,41.736314,-87.624179,AoT Chicago (S) [C],2/22/18 0:00 8 | 001e0610bc12,27.49716927,81.49490469,AoT_Chicago,85,Ashland Ave & 79th St Chicago IL,41.75034,-87.663518,AoT Chicago (S) [C],2/8/18 0:00 9 | 001e0610e532,29.30590451,84.75062811,AoT_Chicago,53,Racine Ave & 18th St Chicago IL,41.857959,-87.656427,AoT Chicago (S) [C],12/15/17 0:00 10 | 001e0610e537,25.85050491,78.53090884,AoT_Chicago,08F,Clark St & Montrose Ave Chicago IL,41.961622,-87.665948,AoT Chicago (S) [C],2/14/18 0:00 11 | 001e0610e540,27.02802949,80.65045308,AoT_Chicago,05A,Fort Dearborn Dr & 31st St Chicago IL,41.838618,-87.607817,AoT Chicago (S) [C],11/29/17 0:00 12 | 001e0610ee33,28.1521823,82.67392814,AoT_Chicago,02D,Damen Ave & Wilson Ave Chicago IL,41.965089,-87.679076,AoT Chicago (S) [C],2/15/18 0:00 13 | 001e0610ee36,28.85853472,83.9453625,AoT_Chicago,51,Cottage Grove Ave & 79th St Chicago IL,41.751295,-87.605288,AoT Chicago (S) [C],2/22/18 0:00 14 | 001e0610ee41,25.91323609,78.64382497,AoT_Chicago,32,Wood St & Warren Blv Chicago IL,41.882304,-87.671773,AoT Chicago (S) [C],12/1/17 0:00 15 | 001e0610ee43,28.1648991,82.69681837,AoT_Chicago,08B,UChicago Chicago IL,41.788608,-87.598713,AoT Chicago (S) [C] {UChicago},2/23/18 0:00 16 | 001e0610ee5d,26.64553846,79.96196923,AoT_Chicago,56,Long Ave & Fullerton Ave Chicago IL,41.923996,-87.761072,AoT Chicago (S) [C],2/23/18 0:00 17 | 001e0610ee82,27.566751,81.6201518,AoT_Chicago,48,Damen Ave & Webster Chicago IL,41.921405,-87.677766,AoT Chicago (S) [C],2/15/18 0:00 18 | 001e0610eef4,27.29992192,81.13985945,AoT_Chicago,34,Milwaukee Ave & Wabansia Ave Chicago IL,41.912681,-87.681052,AoT Chicago (S),10/9/17 0:00 19 | 001e0610ef27,29.17522905,84.51541229,AoT_Chicago,04C,Western Ave & 25th St Chicago IL,41.846579,-87.685557,AoT Chicago (S) [C],12/15/17 0:00 20 | 001e0610f05c,26.37880306,79.48184551,AoT_Chicago,08C,Western Ave & Fullerton Ave Chicago IL,41.924903,-87.687703,AoT Chicago (S) [C],2/7/18 0:00 21 | 001e0610fb4c,26.94163953,80.49495115,AoT_Chicago,04D,Leavitt St & Milwaukee Ave Chicago IL,41.913583,-87.682414,AoT Chicago (S),10/9/17 0:00 22 | 001e061130f4,26.22268937,79.20084086,AoT_Chicago,89,Chicago Ave & Noble St Chicago IL,41.896157,-87.662391,AoT Chicago (S) [C],2/6/18 0:00 23 | 001e061135cb,26.96443704,80.53598668,AoT_Chicago,52,Ashland & 63rd St Chicago IL,41.779369,-87.664421,AoT Chicago (S) [C],2/13/18 0:00 24 | 001e06113a48,25.90858464,78.63545235,AoT_Chicago,0A4,Western Ave & Roscoe Ave Chicago IL,41.943263,-87.688069,AoT Chicago (S) [P],3/15/18 0:00 25 | 001e06113acb,26.73457885,80.12224193,AoT_Chicago,09D,Ashland Ave & 31st Pl Chicago IL,41.839066,-87.665685,AoT Chicago (S) [C],4/12/18 0:00 26 | 001e06113ad8,28.86621732,83.95919117,AoT_Chicago,62,Ashland Ave & Roosevelt Rd Chicago IL,41.866786,-87.666306,AoT Chicago (S) [C],2/27/18 0:00 27 | 001e06113cf1,26.18570728,79.13427311,AoT_Chicago,09C,State St & Randolph St Chicago IL,41.884688,-87.627864,AoT Chicago (S) [C],3/23/18 0:00 28 | 001e06113d22,26.06504963,78.91708933,AoT_Chicago,92,Kedzie Ave & 51st St Chicago IL,41.800846,-87.703739,AoT Chicago (S) [C],3/2/18 0:00 29 | 001e06113f54,25.54507307,77.98113152,AoT_Chicago,0A0,Michigan Ave & Randolph St Chicago IL,41.884607,-87.624577,AoT Chicago (S) [C],5/7/18 0:00 30 | 001e061144be,27.25688673,81.06239611,AoT_Chicago,890,"UChicago, Regenstine Chicago IL",41.792543,-87.600008,AoT Chicago (S) [C] {UChicago},3/15/18 0:00 31 | 001e0611462f,25.89288194,78.6071875,AoT_Chicago,81,Wallace St & Pershing Rd Chicago IL,41.823527,-87.641054,AoT Chicago (S) [C],2/27/18 0:00 32 | 001e061146ba,27.38242143,81.28835857,AoT_Chicago,0A3,Long Ave & Lawrence Ave Chicago IL,41.96759,-87.76257,AoT Chicago (S) [P],3/15/18 0:00 -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/interpolation-aot.R: -------------------------------------------------------------------------------- 1 | library(sf) 2 | library(ggplot2) 3 | library(gstat) 4 | library(sp) 5 | 6 | download.file("https://github.com/spatialanalysis/workshop-notes/raw/master/data/node_temps.csv", destfile = "raster-kriging/spring-2019/data/node-temps.csv") 7 | 8 | # change csv to sf object 9 | nodes <- read.csv("node-temps.csv") 10 | nodes_sf <- st_as_sf(nodes, coords = c("lon", "lat"), crs = 4326) 11 | nodes_sf <- st_transform(nodes_sf, 32616) 12 | 13 | areas <- st_read("https://data.cityofchicago.org/resource/igwz-8jzy.geojson") 14 | areas <- st_transform(areas, 32616) 15 | 16 | # make a ggplot2 map? 17 | ggplot() + 18 | geom_sf(data = areas) + 19 | geom_sf(data = nodes_sf, aes(color = avg_temp)) 20 | 21 | # change sf to sp to use in gstat 22 | nodes_sp <- as_Spatial(nodes_sf) 23 | 24 | # make a variogram 25 | nodes_var <- variogram(nodes_sp$avg_temp ~ 1, nodes_sp) 26 | plot(nodes_var) 27 | 28 | nodes_var_fit <- fit.variogram(nodes_var, model = vgm("Sph")) 29 | plot(nodes_var, nodes_var_fit) 30 | 31 | areas_grid <- st_make_grid(areas, n = c(20, 20)) 32 | areas_grid_sp <- as_Spatial(areas_grid) 33 | plot(areas_grid) 34 | 35 | nodes_kriged <- krige(nodes_sp$avg_temp ~ 1, nodes_sp, areas_grid_sp, model = nodes_var_fit) 36 | 37 | # need to debug 38 | nodes_kriged_df <- as.data.frame(nodes_kriged) 39 | ggplot() + 40 | geom_raster(data = nodes_kriged_df, 41 | aes(x = x, 42 | y = y, 43 | fill = var1.pred)) 44 | 45 | spplot(nodes_kriged["var1.pred"]) 46 | 47 | # any idea of how to clip to boundary? 48 | nodes_kriged_sf <- st_as_sf(nodes_kriged) 49 | clipped <- st_intersection(areas, nodes_kriged_sf) 50 | 51 | ggplot() + 52 | geom_raster(data = clipped, 53 | aes(x = x, 54 | y = y, 55 | fill = var1.pred)) 56 | -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/interpolation-meuse.R: -------------------------------------------------------------------------------- 1 | library(sp) 2 | library(gstat) 3 | library(ggplot2) 4 | library(sf) 5 | 6 | # Load and look at dataset 7 | data("meuse") 8 | 9 | head(meuse) 10 | str(meuse) 11 | summary(meuse) 12 | class(meuse) 13 | names(meuse) 14 | ?meuse 15 | 16 | # Map the data 17 | ggplot(meuse, aes(x = x, y = y, size = lead, color = dist)) + 18 | geom_point() + 19 | theme_bw() 20 | 21 | # Convert to a spatial object 22 | # older sp method 23 | coordinates(meuse) <- ~x+y 24 | proj4string(meuse) 25 | 26 | proj4string(meuse) <- CRS("+init=epsg:28992") 27 | proj4string(meuse) 28 | 29 | # alternate method using sf 30 | data(meuse) 31 | meuse_sf <- st_as_sf(meuse, coords = c("x", "y")) 32 | st_crs(meuse_sf) <- 28992 33 | st_crs(meuse_sf) 34 | meuse_sp <- as_Spatial(meuse_sf) 35 | 36 | # Make a variogram 37 | vgm <- variogram(log(zinc)~1, meuse_sp) 38 | plot(vgm) 39 | 40 | # Fit a model to the variogram 41 | show.vgms() 42 | ?vgm 43 | 44 | vgm_fit <- fit.variogram(vgm, model = vgm("Sph")) 45 | plot(vgm, vgm_fit) 46 | 47 | # Get a gridded surface 48 | data("meuse.grid") 49 | ggplot(meuse.grid, aes(x = x, y = y)) + 50 | geom_point() 51 | 52 | meuse_grid_sf <- st_as_sf(meuse.grid, coords = c("x", "y")) 53 | st_crs(meuse_grid_sf) <- 28992 54 | meuse_grid <- as_Spatial(meuse_grid_sf) 55 | 56 | # Krige! 57 | meuse_kriged <- krige(log(zinc)~1, meuse_sp, meuse_grid, model = vgm_fit) 58 | 59 | head(meuse_kriged) 60 | 61 | # Plot your kriged surface 62 | # old way 63 | spplot(meuse_kriged["var1.pred"]) 64 | 65 | # new way 66 | meuse_kriged_df <- as.data.frame(meuse_kriged) 67 | ggplot() + 68 | geom_raster(data = meuse_kriged_df, 69 | aes(x = coords.x1, 70 | y = coords.x2, 71 | fill = var1.pred)) + 72 | scale_fill_gradient(low = "yellow", high = "red") + 73 | geom_point(data = meuse, 74 | aes(x = x, 75 | y = y)) 76 | -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/raster-intro.R: -------------------------------------------------------------------------------- 1 | # Working through the intro raster course found here: 2 | # https://datacarpentry.org/r-raster-vector-geospatial/01-raster-structure/index.html 3 | 4 | library(raster) 5 | library(rgdal) 6 | library(ggplot2) 7 | 8 | # Replace with path to file on your computer 9 | data_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif" 10 | 11 | data_path2 <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif" 12 | 13 | GDALinfo(data_path) 14 | harv_dsm_metadata <- capture.output(GDALinfo(data_path)) 15 | 16 | harv_dsm_metadata 17 | 18 | # Import raster data 19 | harv_dsm <- raster(data_path) 20 | 21 | harv_dsm 22 | 23 | summary(harv_dsm, maxsamp = ncell(harv_dsm)) 24 | 25 | ncell(harv_dsm) 26 | class(harv_dsm) 27 | 28 | harv_df <- as.data.frame(harv_dsm, xy = TRUE) 29 | class(harv_df) 30 | head(harv_df) 31 | str(harv_df) 32 | 33 | ggplot() + 34 | geom_raster(data = harv_df, aes(x = x, y = y, fill = HARV_dsmCrop)) + 35 | scale_fill_viridis_c() + 36 | coord_quickmap() 37 | 38 | ?coord_map 39 | 40 | crs(harv_dsm) 41 | 42 | minValue(harv_dsm) 43 | maxValue(harv_dsm) 44 | 45 | nlayers(harv_dsm) 46 | 47 | GDALinfo(data_path) 48 | 49 | ggplot() + 50 | geom_histogram(data = harv_df, 51 | aes(HARV_dsmCrop), bins = 40) 52 | 53 | GDALinfo(data_path2) 54 | -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/raster-multiband.R: -------------------------------------------------------------------------------- 1 | # to make a new folder from R 2 | dir.create("scripts") 3 | 4 | library(raster) # raster data 5 | library(ggplot2) # plotting 6 | library(sf) # vector data 7 | 8 | file_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/RGB_Imagery/HARV_RGB_Ortho.tif" 9 | 10 | rgb_band1_harv <- raster(file_path) 11 | rgb_band2_harv <- raster(file_path, band = 2) 12 | 13 | class(rgb_band1_harv) 14 | 15 | rgb_band1_harv_df <- as.data.frame(rgb_band1_harv, xy = TRUE) 16 | rgb_band2_harv_df <- as.data.frame(rgb_band2_harv, xy = TRUE) 17 | 18 | ggplot() + 19 | geom_raster(data = rgb_band1_harv_df, aes(x = x, y = y, alpha = HARV_RGB_Ortho)) + 20 | coord_quickmap() 21 | 22 | rgb_stack_harv <- stack(file_path) 23 | rgb_stack_harv@layers 24 | 25 | ?plotRGB 26 | plotRGB(rgb_stack_harv, r = 1, g = 2, b = 3) 27 | 28 | rgb_brick_harv <- brick(rgb_stack_harv) 29 | plotRGB(rgb_brick_harv) 30 | 31 | # something new!! cropping rasters 32 | chm_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif" 33 | 34 | boundary_path <- "raster-kriging/spring-2019/data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp" 35 | 36 | chm_harv <- raster(chm_path) 37 | chm_harv_df <- as.data.frame(chm_harv, xy = TRUE) 38 | 39 | boundary_harv <- read_sf(boundary_path) 40 | 41 | ggplot() + 42 | geom_raster(data = chm_harv_df, aes(x = x, y = y, fill = HARV_chmCrop)) + 43 | scale_fill_gradientn(colors = terrain.colors(10)) + 44 | geom_sf(data = boundary_harv, fill = NA) 45 | 46 | ?crop 47 | 48 | boundary_sp <- as(boundary_harv, "Spatial") 49 | chm_crop <- crop(chm_harv, boundary_sp) 50 | 51 | chm_crop_df <- as.data.frame(chm_crop, xy = TRUE) 52 | 53 | ggplot() + 54 | geom_raster(data = chm_crop_df, aes(x = x, y = y, fill = HARV_chmCrop)) + 55 | scale_fill_gradientn(colors = terrain.colors(10)) + 56 | geom_sf(data = boundary_harv, fill = NA) 57 | -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/raster-plotting.R: -------------------------------------------------------------------------------- 1 | # challenge: import harvard dsm data into a format that we can use with ggplot2 2 | library(raster) 3 | library(ggplot2) 4 | library(dplyr) 5 | 6 | file_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif" 7 | hillshade_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_DSMhill.tif" 8 | 9 | harv_dsm <- raster(file_path) 10 | harv_hill <- raster(hillshade_path) 11 | harv_df <- as.data.frame(harv_dsm, xy = TRUE) 12 | harv_hill_df <- as.data.frame(harv_hill, xy = TRUE) 13 | 14 | harv_df <- harv_df %>% 15 | mutate(fct_elevation = cut(HARV_dsmCrop, breaks = 3)) 16 | 17 | ggplot() + 18 | geom_bar(data = harv_df, aes(fct_elevation)) 19 | 20 | unique(harv_df$fct_elevation) 21 | harv_df %>% 22 | group_by(fct_elevation) %>% 23 | count() 24 | 25 | custom_bins <- c(300, 350, 400, 450) 26 | 27 | harv_df <- harv_df %>% 28 | mutate(fct_elevation2 = cut(HARV_dsmCrop, breaks = custom_bins)) 29 | 30 | ggplot() + 31 | geom_bar(data = harv_df, aes(fct_elevation2)) 32 | 33 | unique(harv_df$fct_elevation2) 34 | harv_df %>% 35 | group_by(fct_elevation2) %>% 36 | count() 37 | 38 | names(harv_df) 39 | 40 | ## challenge: try using ggplot to make map with binned elevation data 41 | ggplot() + 42 | geom_raster(data = harv_df, aes(x = x, 43 | y = y, 44 | fill = fct_elevation2)) + 45 | scale_fill_manual(values = terrain.colors(3), name = "Elevation") + 46 | coord_quickmap() + 47 | theme(axis.title = element_blank()) 48 | 49 | terrain.colors(3) 50 | 51 | # title of map: Classified Elevation Map - NEON Harvard Forest Field Site 52 | # x axis: UTM Westing Coordinate (m) 53 | # y axis: UTM Northing Coordinate (m) 54 | 55 | harv_df <- mutate(harv_df, fct_elevation_6 = cut(HARV_dsmCrop, breaks = 6)) 56 | 57 | ggplot() + 58 | geom_raster(data = harv_df, aes(x = x, 59 | y = y, 60 | fill = fct_elevation_6)) + 61 | scale_fill_manual(values = terrain.colors(6), name = "Elevation") + 62 | coord_quickmap() + 63 | xlab("UTM Westing Coordinate (m)") + 64 | ylab("UTM Northing Coordinate (m)") + 65 | ggtitle("Classified Elevation Map") 66 | 67 | harv_hill_df 68 | 69 | ggplot() + 70 | geom_raster(data = harv_df, aes(x = x, y = y, fill = HARV_dsmCrop)) + 71 | geom_raster(data = harv_hill_df, aes(x = x, 72 | y = y, 73 | alpha = HARV_DSMhill)) + 74 | coord_quickmap() + 75 | scale_fill_viridis_c() + 76 | scale_alpha(range = c(0.15, 0.65), guide = "none") + 77 | ggtitle("Elevation with hillshade") 78 | -------------------------------------------------------------------------------- /raster-kriging/spring-2019/R/raster-projection.R: -------------------------------------------------------------------------------- 1 | library(raster) 2 | library(ggplot2) 3 | library(dplyr) 4 | 5 | dsm_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DSM/HARV_dsmCrop.tif" 6 | dtm_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_dtmCrop.tif" 7 | hillshade_path <- "raster-kriging/spring-2019/data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_DTMhill_WGS84.tif" 8 | 9 | harv_dtm <- raster(dtm_path) 10 | harv_dsm <- raster(dsm_path) 11 | 12 | harv_hill <- raster(hillshade_path) 13 | harv_dtm_df <- as.data.frame(harv_dtm, xy = TRUE) 14 | harv_dsm_df <- as.data.frame(harv_dsm, xy = TRUE) 15 | harv_hill_df <- as.data.frame(harv_hill, xy = TRUE) 16 | 17 | head(harv_df) 18 | head(harv_hill_df) 19 | 20 | ggplot() + 21 | geom_raster(data = harv_df, aes(x = x, y = y, fill = HARV_dtmCrop)) + 22 | geom_raster(data = harv_hill_df, aes(x = x, 23 | y = y, 24 | alpha = HARV_DTMhill_WGS84)) + 25 | coord_quickmap() + 26 | scale_fill_viridis_c() + 27 | scale_alpha(range = c(0.15, 0.65), guide = "none") + 28 | ggtitle("Elevation with hillshade") 29 | 30 | ggplot() + 31 | geom_raster(data = harv_df, 32 | aes(x = x, y = y, fill = HARV_dtmCrop)) + 33 | scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) + 34 | coord_quickmap() 35 | 36 | ggplot() + 37 | geom_raster(data = harv_hill_df, 38 | aes(x = x, y = y, alpha = HARV_DTMhill_WGS84)) + 39 | coord_quickmap() 40 | 41 | crs(harv) 42 | crs(harv_hill) 43 | 44 | # Project our raster! And make it the same res as the original elevation data 45 | harv_hill_proj <- projectRaster(harv_hill, crs = crs(harv), res = 1) 46 | 47 | crs(harv_hill_proj) 48 | 49 | extent(harv) 50 | extent(harv_hill) 51 | extent(harv_hill_proj) 52 | 53 | res(harv_hill) 54 | res(harv_hill_proj) 55 | 56 | harv_hill_proj_df <- as.data.frame(harv_hill_proj, xy = TRUE) 57 | 58 | # Plot together 59 | ggplot() + 60 | geom_raster(data = harv_df, 61 | aes(x = x, y = y, fill = HARV_dtmCrop)) + 62 | scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) + 63 | geom_raster(data = harv_hill_proj_df, 64 | aes(x = x, y = y, alpha = HARV_DTMhill_WGS84)) + 65 | coord_quickmap() 66 | 67 | # To do raster calcs, can just subtract one raster from the other! 68 | harv_chm <- harv_dsm - harv_dtm 69 | harv_chm_df <- as.data.frame(harv_chm, xy = TRUE) 70 | 71 | ggplot() + 72 | geom_raster(data = harv_chm_df, 73 | aes(x = x, y = y, fill = layer)) + 74 | scale_fill_gradientn(name = "Canopy Height", colors = terrain.colors(10)) + 75 | coord_quickmap() 76 | 77 | harv_chm2 <- overlay(harv_dsm, harv_dtm, fun = function(x, y) x - y ) 78 | 79 | harv_chm2_df <- as.data.frame(harv_chm2, xy = TRUE) 80 | 81 | ggplot() + 82 | geom_raster(data = harv_chm2_df, 83 | aes(x = x, y = y, fill = layer)) + 84 | scale_fill_gradientn(name = "Canopy Height", colors = terrain.colors(10)) + 85 | coord_quickmap() 86 | 87 | writeRaster(harv_chm2, "CHM_HARV.tiff", format = "GTiff", overwrite = TRUE, NAflag = -9999) 88 | -------------------------------------------------------------------------------- /workshop-scripts.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | --------------------------------------------------------------------------------