├── .gitignore ├── LICENSE ├── README.Rmd ├── README.md ├── code └── sotmus-r-complete.R ├── data ├── detroit_bb.Rda ├── detroit_cycleways.Rda ├── kunming_dian_names.Rda └── kunming_water.Rda ├── notebooks ├── sotmus-r-complete.Rmd ├── sotmus-r-complete.html ├── sotmus-r.Rmd └── sotmus-r.html ├── slides ├── libs │ └── remark-css │ │ ├── default-fonts.css │ │ └── default.css ├── sotmus-r-slides.Rmd ├── sotmus-r-slides.html └── sotmus-r-slides.pdf └── sotmus-r.Rproj /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC BY-SA 4.0 -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | ```{r setup, include=FALSE} 6 | knitr::opts_chunk$set(echo = TRUE) 7 | ``` 8 | 9 | # Introduction to R + OSM 10 | 11 | This is a workshop put together by Angela Li of the [Center for Spatial Data Science (CSDS)](https://spatial.uchicago.edu) at UChicago for [State of the Map 2018](https://2018.stateofthemap.us). 12 | 13 | ## Learning Objectives 14 | By the end of this workshop, workshop attendees should be able to: 15 | 16 | 1. Install useful R packages 17 | 2. Download OSM data from the OpenStreetMap API, using the `osmdata` R package 18 | 3. Map downloaded OSM data in R 19 | 4. Understand the two formats in which R stores spatial data 20 | 21 | ## `r emo::ji("star")` Do This Now! `r emo::ji("star")` 22 | ### Install Necessary Packages 23 | Open RStudio and paste the following code into your console, then press Enter to run it: 24 | ```{r eval = FALSE} 25 | # Download packages from CRAN 26 | install.packages(c("devtools", "knitr", "magrittr", "sf", "sp", "rmarkdown", "usethis", "osmdata")) 27 | ``` 28 | 29 | ### Download Lesson Materials 30 | Download this entire lesson (all code, notebooks, and data) to your computer desktop by running the following code in your RStudio console. Respond `yes` to all prompts: 31 | ```{r eval = FALSE} 32 | usethis::use_course("https://github.com/angela-li/sotmus-r/archive/master.zip") 33 | ``` 34 | 35 | Once you've downloaded this lesson, open the `.Rproj` file to get started. 36 | 37 | Use the `sotmus-r-complete.Rmd` notebook as a guide to fill out the `sotmus-r.Rmd` lesson notebook. 38 | 39 | ## Contents 40 | - [`code`](https://github.com/angela-li/sotmus-r/tree/master/code): Raw R code generated from notebooks 41 | - [`data`](https://github.com/angela-li/sotmus-r/tree/master/data): Intermediate R data objects (to be used in case of bad Internet connection) 42 | - [`notebooks`](https://github.com/angela-li/sotmus-r/tree/master/notebooks): R Markdown notebooks with R code, produced during workshop. Includes empty notebook (`sotmus-r.html`) and filled-out notebook (`sotmus-r-complete.html`). 43 | - [`slides`](https://github.com/angela-li/sotmus-r/tree/master/slides): Intro slides (there are only 2 because I worked live in the `sotmus-r.Rmd` notebook) 44 | 45 | ## Troubleshooting 46 | If you're running into issues with RTools (mostly Windows users), check out this [solution on Github](https://github.com/r-lib/devtools/issues/1772#issuecomment-393669488). 47 | 48 | Typing ? before your R command in the console will bring up function documentation in R. 49 | 50 | ## Contact Me 51 | Feel free to email me at ali6@uchicago.edu or reach out [on Twitter](https://twitter.com/CivicAngela) for questions! Or submit a Github Issue on this page for problems. 52 | 53 | ## Additional Resources 54 | - `osmdata` [package website](https://ropensci.github.io/osmdata/index.html), [Github](https://github.com/ropensci/osmdata) 55 | - `sf` [package website](https://r-spatial.github.io/sf/), [Github](https://github.com/r-spatial/sf/) 56 | - [overpass turbo website](http://overpass-turbo.eu): try out sample Overpass API queries 57 | - [Overpass API Documentation](https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_API_by_Example): examples for learning more about structuring API queries to OpenStreetMap 58 | - [*Geocomputation in R*](https://geocompr.robinlovelace.net): free online book about analyzing, visualizing, and modeling spatial data in R 59 | - [*R for Data Science*](http://r4ds.had.co.nz): free online book about doing data science in R 60 | 61 | ## References 62 | Thanks to [Mark Padgham](https://github.com/mpadge) and [rOpenSci](https://ropensci.org/) for maintaining the osmdata package! 63 | 64 | Thanks to [Jared Lander](https://swcarpentry.github.io/r-novice-gapminder/), [Chris Prener](https://github.com/slu-dss/lesson-template), [Jenny Bryan](https://github.com/jennybc/whattheyforgot), [Software Carpentry](https://swcarpentry.github.io/r-novice-gapminder/), and [RStudio Education](https://github.com/rstudio-education/datascience-box) for informing the design of this workshop repository. In the same vein, please feel free to use this workshop as a template for your own R workshop. (If you do, give me a hoot!) 65 | 66 | Note: For exporting xaringan slides to a PDF, I used the `webshot` package, instructions for which are found in the [xaringan wiki](https://github.com/yihui/xaringan/wiki/Export-Slides-to-PDF). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Introduction to R + OSM 3 | ======================= 4 | 5 | This is a workshop put together by Angela Li of the [Center for Spatial Data Science (CSDS)](https://spatial.uchicago.edu) at UChicago for [State of the Map 2018](https://2018.stateofthemap.us). 6 | 7 | Learning Objectives 8 | ------------------- 9 | 10 | By the end of this workshop, workshop attendees should be able to: 11 | 12 | 1. Install useful R packages 13 | 2. Download OSM data from the OpenStreetMap API, using the `osmdata` R package 14 | 3. Map downloaded OSM data in R 15 | 4. Understand the two formats in which R stores spatial data 16 | 17 | ⭐️ Do This Now! ⭐️ 18 | ------------------ 19 | 20 | ### Install Necessary Packages 21 | 22 | Open RStudio and paste the following code into your console, then press Enter to run it: 23 | 24 | ``` r 25 | # Download packages from CRAN 26 | install.packages(c("devtools", "knitr", "magrittr", "sf", "sp", "rmarkdown", "usethis", "osmdata")) 27 | ``` 28 | 29 | ### Download Lesson Materials 30 | 31 | Download this entire lesson (all code, notebooks, and data) to your computer desktop by running the following code in your RStudio console. Respond `yes` to all prompts: 32 | 33 | ``` r 34 | usethis::use_course("https://github.com/angela-li/sotmus-r/archive/master.zip") 35 | ``` 36 | 37 | Once you've downloaded this lesson, open the `.Rproj` file to get started. 38 | 39 | Use the `sotmus-r-complete.Rmd` notebook as a guide to fill out the `sotmus-r.Rmd` lesson notebook. 40 | 41 | Contents 42 | -------- 43 | 44 | - [`code`](https://github.com/angela-li/sotmus-r/tree/master/code): Raw R code generated from notebooks 45 | - [`data`](https://github.com/angela-li/sotmus-r/tree/master/data): Intermediate R data objects (to be used in case of bad Internet connection) 46 | - [`notebooks`](https://github.com/angela-li/sotmus-r/tree/master/notebooks): R Markdown notebooks with R code, produced during workshop. Includes empty notebook (`sotmus-r.html`) and filled-out notebook (`sotmus-r-complete.html`). 47 | - [`slides`](https://github.com/angela-li/sotmus-r/tree/master/slides): Intro slides (there are only 2 because I worked live in the `sotmus-r.Rmd` notebook) 48 | 49 | Troubleshooting 50 | --------------- 51 | 52 | If you're running into issues with RTools (mostly Windows users), check out this [solution on Github](https://github.com/r-lib/devtools/issues/1772#issuecomment-393669488). 53 | 54 | Typing ? before your R command in the console will bring up function documentation in R. 55 | 56 | Contact Me 57 | ---------- 58 | 59 | Feel free to email me at or reach out [on Twitter](https://twitter.com/CivicAngela) for questions! Or submit a Github Issue on this page for problems. 60 | 61 | Additional Resources 62 | -------------------- 63 | 64 | - `osmdata` [package website](https://ropensci.github.io/osmdata/index.html), [Github](https://github.com/ropensci/osmdata) 65 | - `sf` [package website](https://r-spatial.github.io/sf/), [Github](https://github.com/r-spatial/sf/) 66 | - [overpass turbo website](http://overpass-turbo.eu): try out sample Overpass API queries 67 | - [Overpass API Documentation](https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_API_by_Example): examples for learning more about structuring API queries to OpenStreetMap 68 | - [*Geocomputation in R*](https://geocompr.robinlovelace.net): free online book about analyzing, visualizing, and modeling spatial data in R 69 | - [*R for Data Science*](http://r4ds.had.co.nz): free online book about doing data science in R 70 | 71 | References 72 | ---------- 73 | 74 | Thanks to [Mark Padgham](https://github.com/mpadge) and [rOpenSci](https://ropensci.org/) for maintaining the osmdata package! 75 | 76 | Thanks to [Jared Lander](https://swcarpentry.github.io/r-novice-gapminder/), [Chris Prener](https://github.com/slu-dss/lesson-template), [Jenny Bryan](https://github.com/jennybc/whattheyforgot), [Software Carpentry](https://swcarpentry.github.io/r-novice-gapminder/), and [RStudio Education](https://github.com/rstudio-education/datascience-box) for informing the design of this workshop repository. In the same vein, please feel free to use this workshop as a template for your own R workshop. (If you do, give me a hoot!) 77 | 78 | Note: For exporting xaringan slides to a PDF, I used the `webshot` package, instructions for which are found in the [xaringan wiki](https://github.com/yihui/xaringan/wiki/Export-Slides-to-PDF). 79 | -------------------------------------------------------------------------------- /code/sotmus-r-complete.R: -------------------------------------------------------------------------------- 1 | # All the code from the RMarkdown doc, in one script 2 | 3 | pkgs <- installed.packages() 4 | head(pkgs) 5 | 6 | nrow(pkgs) 7 | 8 | ## update.packages() 9 | ## remove.packages("osmdata") 10 | 11 | library(magrittr) 12 | library(osmdata) 13 | library(sf) 14 | library(sp) 15 | 16 | ## opq(bbox = 'detroit') %>% 17 | ## add_osm_feature(key = 'highway', value = "cycleway") %>% 18 | ## osmdata_sp() 19 | 20 | detroit_bb <- getbb(place_name = "detroit") 21 | 22 | opq(detroit_bb) 23 | opq(bbox = "detroit") # Produces the same thing 24 | opq(bbox = c("42.255192,-83.287959,42.450232,-82.9104391")) # Also produces the same thing 25 | 26 | opq(detroit_bb) %>% 27 | add_osm_feature(key = "highway", value = "cycleway") 28 | 29 | opq(detroit_bb) %>% 30 | add_osm_feature(key = "highway", value = "cycleway") %>% 31 | opq_string() 32 | 33 | available_features() 34 | 35 | available_tags("highway") 36 | 37 | opq(detroit_bb) %>% 38 | add_osm_feature(key = "highway", value = "cycleway") %>% 39 | osmdata_sp() 40 | 41 | opq(detroit_bb) %>% 42 | add_osm_feature(key = "highway", value = "cycleway") %>% 43 | osmdata_sf() 44 | 45 | detroit_cycleways <- opq(detroit_bb) %>% 46 | add_osm_feature(key = "highway", value = "cycleway") %>% 47 | osmdata_sp() 48 | 49 | plot(detroit_cycleways$osm_lines) 50 | 51 | ## kunming_water <- opq(bbox = 'Kunming, China') %>% 52 | ## add_osm_feature(key = 'natural', value = 'water') %>% 53 | ## osmdata_sf() 54 | ## 55 | ## kunming_dian_names <- opq(bbox = 'Kunming, China') %>% 56 | ## add_osm_feature(key = 'name:en', value = 'Dian', value_exact = FALSE) %>% 57 | ## osmdata_sf() 58 | ## 59 | ## kunming_all_features <- c(kunming_water, kunming_dian_names) 60 | -------------------------------------------------------------------------------- /data/detroit_bb.Rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angela-li/sotmus-r/504c8814d804538c080681466fbda023ee2049a0/data/detroit_bb.Rda -------------------------------------------------------------------------------- /data/detroit_cycleways.Rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angela-li/sotmus-r/504c8814d804538c080681466fbda023ee2049a0/data/detroit_cycleways.Rda -------------------------------------------------------------------------------- /data/kunming_dian_names.Rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angela-li/sotmus-r/504c8814d804538c080681466fbda023ee2049a0/data/kunming_dian_names.Rda -------------------------------------------------------------------------------- /data/kunming_water.Rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angela-li/sotmus-r/504c8814d804538c080681466fbda023ee2049a0/data/kunming_water.Rda -------------------------------------------------------------------------------- /notebooks/sotmus-r-complete.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to R + OSM: Completed Notebook" 3 | output: html_document 4 | --- 5 | 6 | ```{r setup, include=FALSE} 7 | knitr::opts_chunk$set(echo = TRUE) 8 | ``` 9 | 10 | This notebook provides an introduction to R, RStudio, and the osmdata packages. It's intended for R beginners, so you can skip the first section if you have a background in R. 11 | 12 | ## Using R and RStudio 13 | 14 | ### R Markdown 15 | 16 | This is an R Markdown document. It enables you to do "literate programming", where you can include code and written text in the same document. You can run chunks of code separately through the document. If you're familiar with Jupyter notebooks, it's basically the same idea. For more details on using R Markdown see . 17 | 18 | You can execute a chunk of code by putting your cursor in the chunk and pressing *Ctrl-Shift-Enter*, or by clicking the Run button (the green triangle) in the chunk. You can execute all code by clicking the Run button at the top of this document. 19 | 20 | Try running the following code to check what packages you have installed: 21 | 22 | ```{r } 23 | pkgs <- installed.packages() 24 | head(pkgs) 25 | ``` 26 | You can also count how many packages you have installed. 27 | ```{r } 28 | nrow(pkgs) 29 | ``` 30 | 31 | ### Packages 32 | 33 | What is a package anyway? It's a way to extend R by grouping together useful functions (you can write your own and put it on Github/submit it to CRAN!). You should have already installed all the packages you need for this tutorial. 34 | 35 | To update your installed packages, run the following command: 36 | 37 | ```{r eval = FALSE} 38 | update.packages() 39 | ``` 40 | 41 | To remove packages, run the following command: 42 | ```{r eval = FALSE} 43 | remove.packages("osmdata") 44 | ``` 45 | 46 | Before you use a package, you need to load it into your workspace with a library command. Here, we're loading all the packages we need for this tutorial. Go ahead and load all the packages you just downloaded. 47 | 48 | ```{r} 49 | library(magrittr) 50 | library(osmdata) 51 | library(sf) 52 | library(sp) 53 | ``` 54 | 55 | ## Download OSM data 56 | 57 | The `osmdata` package works by taking R input, creating an Overpass API query, and returning the API output to R. 58 | 59 | For the Overpass query, you need: 60 | 61 | - The bounding box of your area 62 | - The name of the feature you're looking for (i.e. "name", "highway") 63 | - The desired value of that feature (i.e. "Detroit", "bikeway") 64 | 65 | The final overpass query looks something like this: 66 | ```{r eval = FALSE} 67 | opq(bbox = 'detroit') %>% 68 | add_osm_feature(key = 'highway', value = "cycleway") %>% 69 | osmdata_sp() 70 | ``` 71 | 72 | ### Get the bounding box 73 | 74 | Let's look at how we can construct a query. First, let's get the bounding box for Detroit. 75 | ```{r} 76 | detroit_bb <- getbb(place_name = "detroit") 77 | ``` 78 | 79 | Pass the bounding box to the `opq()` command. 80 | ```{r} 81 | opq(detroit_bb) 82 | opq(bbox = "detroit") # Produces the same thing 83 | opq(bbox = c(-83.287959,42.255192,-82.9104391,42.450232)) # Also produces the same thing 84 | ``` 85 | 86 | ### Add the feature(s) we're interested in 87 | 88 | Let's say we want to find all OSM cycleways in Detroit. Take the output from the previous section and add a feature! 89 | 90 | ```{r} 91 | opq(detroit_bb) %>% 92 | add_osm_feature(key = "highway", value = "cycleway") 93 | ``` 94 | 95 | Compare this to the output of the previous and you can see the `$features` section now has XML content. 96 | 97 | #### Under the hood 98 | What `osmdata` does is paste all this information together in a query that will work with the Overpass API. You can see the query that gets submitted to the API by using the `opq_string` function. 99 | 100 | ```{r} 101 | opq(detroit_bb) %>% 102 | add_osm_feature(key = "highway", value = "cycleway") %>% 103 | opq_string() 104 | ``` 105 | 106 | #### How do I know the OSM names of the features I want? 107 | For a list of all features, run the `available_features()` command: 108 | ```{r} 109 | available_features() 110 | ``` 111 | 112 | And for the values associated with that feature, run the `available_tags` function: 113 | ```{r} 114 | available_tags("highway") 115 | ``` 116 | 117 | ### Submit query to Overpass API 118 | 119 | Let's submit this query to the Overpass API. 120 | 121 | ```{r} 122 | opq(detroit_bb) %>% 123 | add_osm_feature(key = "highway", value = "cycleway") %>% 124 | osmdata_sp() 125 | ``` 126 | 127 | We request this back as an `sp` object, but I actually prefer `sf` objects for analysis. If you've used PostGIS before, `sf` data is essentially in the same format. 128 | ```{r} 129 | opq(detroit_bb) %>% 130 | add_osm_feature(key = "highway", value = "cycleway") %>% 131 | osmdata_sf() 132 | ``` 133 | 134 | However, for ease of mapping, I'm going to go ahead and use the `sp` data format. I'm going to save this as a local variable so I don't have to hit the API each time. 135 | ```{r} 136 | detroit_cycleways <- opq(detroit_bb) %>% 137 | add_osm_feature(key = "highway", value = "cycleway") %>% 138 | osmdata_sp() 139 | ``` 140 | 141 | ### Map the output 142 | 143 | Now let's map the data! 144 | ```{r} 145 | plot(detroit_cycleways$osm_lines) 146 | ``` 147 | 148 | ## Try it yourself 149 | 150 | Maybe you want to add boundaries? Try a different features, or a different city? Try it out! 151 | Go to the [detailed lesson](https://ropensci.github.io/osmdata/articles/osmdata.html) and the [function reference](https://ropensci.github.io/osmdata/reference/index.html) for help. 152 | 153 | Note that some data may take a while to download from Overpass. 154 | 155 | FYI, this is the syntax for adding multiple features to the same map. You can unfortunately only do this with `sf` for the time being: 156 | ```{r eval = FALSE} 157 | kunming_water <- opq(bbox = 'Kunming, China') %>% 158 | add_osm_feature(key = 'natural', value = 'water') %>% 159 | osmdata_sf() 160 | 161 | kunming_dian_names <- opq(bbox = 'Kunming, China') %>% 162 | add_osm_feature(key = 'name:en', value = 'Dian', value_exact = FALSE) %>% 163 | osmdata_sf() 164 | 165 | kunming_all_features <- c(kunming_water, kunming_dian_names) 166 | ``` 167 | 168 | Also, to add multiple layers to a map with base R plotting (with sp), use the "add = T" syntax: 169 | ```{r eval = FALSE} 170 | # Can you make the detroit_boundaries variable on your own? 171 | plot(detroit_cycleways$osm_lines) 172 | plot(detroit_boundaries$osm_lines, add = T, col = "red") 173 | ``` 174 | 175 | ```{r} 176 | # Put your code here! 177 | ``` 178 | -------------------------------------------------------------------------------- /notebooks/sotmus-r.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to R + OSM: Lesson Notebook" 3 | output: html_document 4 | --- 5 | 6 | ```{r setup, include=FALSE} 7 | knitr::opts_chunk$set(echo = TRUE) 8 | ``` 9 | 10 | This notebook provides an introduction to R, RStudio, and the osmdata packages. It's intended for R beginners, so you can skip the first section if you have a background in R. 11 | 12 | ## Using R and RStudio 13 | 14 | ### R Markdown 15 | 16 | This is an R Markdown document. It enables you to do "literate programming", where you can include code and written text in the same document. You can run chunks of code separately through the document. If you're familiar with Jupyter notebooks, it's basically the same idea. For more details on using R Markdown see . 17 | 18 | You can execute a chunk of code by putting your cursor in the chunk and pressing *Ctrl-Shift-Enter*, or by clicking the Run button (the green triangle) in the chunk. You can execute all code by clicking the Run button at the top of this document. 19 | 20 | Try running the following code to check what packages you have installed: 21 | 22 | ```{r } 23 | 24 | ``` 25 | You can also count how many packages you have installed. 26 | ```{r } 27 | 28 | ``` 29 | 30 | ### Packages 31 | 32 | What is a package anyway? It's a way to extend R by grouping together useful functions (you can write your own and put it on Github/submit it to CRAN!). You should have already installed all the packages you need for this tutorial. 33 | 34 | To update your installed packages, run the following command: 35 | 36 | ```{r eval = FALSE} 37 | update.packages() 38 | ``` 39 | 40 | To remove packages, run the following command: 41 | ```{r eval = FALSE} 42 | remove.packages("osmdata") 43 | ``` 44 | 45 | Before you use a package, you need to load it into your workspace with a library command. Here, we're loading all the packages we need for this tutorial. Go ahead and load all the packages you just downloaded. 46 | 47 | ```{r} 48 | 49 | ``` 50 | 51 | ## Download OSM data 52 | 53 | The `osmdata` package works by taking R input, creating an Overpass API query, and returning the API output to R. 54 | 55 | For the Overpass query, you need: 56 | 57 | - The bounding box of your area 58 | - The name of the feature you're looking for (i.e. "name", "highway") 59 | - The desired value of that feature (i.e. "Detroit", "bikeway") 60 | 61 | The final overpass query looks something like this: 62 | ```{r eval = FALSE} 63 | opq(bbox = 'detroit') %>% 64 | add_osm_feature(key = 'highway', value = "cycleway") %>% 65 | osmdata_sp() 66 | ``` 67 | 68 | ### Get the bounding box 69 | 70 | Let's look at how we can construct a query. First, let's get the bounding box for Detroit. 71 | ```{r} 72 | 73 | ``` 74 | 75 | Pass the bounding box to the `opq()` command. 76 | ```{r} 77 | 78 | # opq( ) # Produces the same thing 79 | # opq( ) # Also produces the same thing 80 | ``` 81 | 82 | ### Add the feature(s) we're interested in 83 | 84 | Let's say we want to find all OSM cycleways in Detroit. Take the output from the previous section and add a feature! 85 | 86 | ```{r} 87 | 88 | ``` 89 | 90 | Compare this to the output of the previous and you can see the `$features` section now has XML content. 91 | 92 | #### Under the hood 93 | What `osmdata` does is paste all this information together in a query that will work with the Overpass API. You can see the query that gets submitted to the API by using the `opq_string` function. 94 | 95 | ```{r} 96 | 97 | ``` 98 | 99 | #### How do I know the OSM names of the features I want? 100 | For a list of all features, run the `available_features()` command: 101 | ```{r} 102 | 103 | ``` 104 | 105 | And for the values associated with that feature, run the `available_tags` function: 106 | ```{r} 107 | 108 | ``` 109 | 110 | ### Submit query to Overpass API 111 | 112 | Let's submit this query to the Overpass API. 113 | 114 | ```{r} 115 | 116 | ``` 117 | 118 | We request this back as an `sp` object, but I actually prefer `sf` objects for analysis. If you've used PostGIS before, `sf` data is essentially in the same format. 119 | ```{r} 120 | 121 | ``` 122 | 123 | However, for ease of mapping, I'm going to go ahead and use the `sp` data format. I'm going to save this as a local variable so I don't have to hit the API each time. 124 | ```{r} 125 | 126 | ``` 127 | 128 | ### Map the output 129 | 130 | Now let's map the data! 131 | ```{r} 132 | 133 | ``` 134 | 135 | ## Try it yourself 136 | 137 | Maybe you want to add boundaries? Try a different features, or a different city? Try it out! 138 | Go to the [detailed lesson](https://ropensci.github.io/osmdata/articles/osmdata.html) and the [function reference](https://ropensci.github.io/osmdata/reference/index.html) for help. 139 | 140 | Note that some data may take a while to download from Overpass. 141 | 142 | FYI, this is the syntax for adding multiple features to the same map. You can unfortunately only do this with `sf` for the time being: 143 | ```{r eval = FALSE} 144 | kunming_water <- opq(bbox = 'Kunming, China') %>% 145 | add_osm_feature(key = 'natural', value = 'water') %>% 146 | osmdata_sf() 147 | 148 | kunming_dian_names <- opq(bbox = 'Kunming, China') %>% 149 | add_osm_feature(key = 'name:en', value = 'Dian', value_exact = FALSE) %>% 150 | osmdata_sf() 151 | 152 | kunming_all_features <- c(kunming_water, kunming_dian_names) 153 | ``` 154 | 155 | Also, to add multiple layers to a map with base R plotting (with sp), use the "add = T" syntax: 156 | ```{r eval = FALSE} 157 | # Can you make the detroit_boundaries variable on your own? 158 | plot(detroit_cycleways$osm_lines) 159 | plot(detroit_boundaries$osm_lines, add = T, col = "red") 160 | ``` 161 | 162 | ```{r} 163 | # Put your code here! 164 | ``` 165 | -------------------------------------------------------------------------------- /slides/libs/remark-css/default-fonts.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz); 2 | @import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic); 3 | @import url(https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700); 4 | 5 | body { font-family: 'Droid Serif', 'Palatino Linotype', 'Book Antiqua', Palatino, 'Microsoft YaHei', 'Songti SC', serif; } 6 | h1, h2, h3 { 7 | font-family: 'Yanone Kaffeesatz'; 8 | font-weight: normal; 9 | } 10 | .remark-code, .remark-inline-code { font-family: 'Source Code Pro', 'Lucida Console', Monaco, monospace; } 11 | -------------------------------------------------------------------------------- /slides/libs/remark-css/default.css: -------------------------------------------------------------------------------- 1 | a, a > code { 2 | color: rgb(249, 38, 114); 3 | text-decoration: none; 4 | } 5 | .footnote { 6 | position: absolute; 7 | bottom: 3em; 8 | padding-right: 4em; 9 | font-size: 90%; 10 | } 11 | .remark-code-line-highlighted { background-color: #ffff88; } 12 | 13 | .inverse { 14 | background-color: #272822; 15 | color: #d6d6d6; 16 | text-shadow: 0 0 20px #333; 17 | } 18 | .inverse h1, .inverse h2, .inverse h3 { 19 | color: #f3f3f3; 20 | } 21 | /* Two-column layout */ 22 | .left-column { 23 | color: #777; 24 | width: 20%; 25 | height: 92%; 26 | float: left; 27 | } 28 | .left-column h2:last-of-type, .left-column h3:last-child { 29 | color: #000; 30 | } 31 | .right-column { 32 | width: 75%; 33 | float: right; 34 | padding-top: 1em; 35 | } 36 | .pull-left { 37 | float: left; 38 | width: 47%; 39 | } 40 | .pull-right { 41 | float: right; 42 | width: 47%; 43 | } 44 | .pull-right ~ * { 45 | clear: both; 46 | } 47 | img, video, iframe { 48 | max-width: 100%; 49 | } 50 | blockquote { 51 | border-left: solid 5px lightgray; 52 | padding-left: 1em; 53 | } 54 | table { 55 | margin: auto; 56 | border-top: 1px solid #666; 57 | border-bottom: 1px solid #666; 58 | } 59 | table thead th { border-bottom: 1px solid #ddd; } 60 | th, td { padding: 5px; } 61 | thead, tfoot, tr:nth-child(even) { background: #eee } 62 | 63 | @page { margin: 0; } 64 | @media print { 65 | .remark-slide-scaler { 66 | width: 100% !important; 67 | height: 100% !important; 68 | transform: scale(1) !important; 69 | top: 0 !important; 70 | left: 0 !important; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /slides/sotmus-r-slides.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to R + OSM" 3 | subtitle: "🌎 🗺
with the osmdata R package" 4 | author: "Angela Li
@CivicAngela
Center for Spatial Data Science, UChicago" 5 | date: "2018-10-06" 6 | output: 7 | xaringan::moon_reader: 8 | lib_dir: libs 9 | nature: 10 | highlightStyle: github 11 | highlightLines: true 12 | countIncrementalSlides: false 13 | --- 14 | 15 | ```{r setup, include=FALSE} 16 | options(htmltools.dir.version = FALSE) 17 | ``` 18 | 19 | class: center, middle, inverse 20 | 21 | # Go to this link now, and follow the instructions there! 22 | 23 | # [bit.ly/sotmus-r](https://bit.ly/sotmus-r) 24 | 25 | -------------------------------------------------------------------------------- /slides/sotmus-r-slides.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Introduction to R + OSM 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 31 | 32 | 46 | 47 | 54 | 55 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /slides/sotmus-r-slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angela-li/sotmus-r/504c8814d804538c080681466fbda023ee2049a0/slides/sotmus-r-slides.pdf -------------------------------------------------------------------------------- /sotmus-r.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: Sweave 13 | LaTeX: pdfLaTeX 14 | --------------------------------------------------------------------------------