-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathREADME.Rmd
148 lines (109 loc) · 7.39 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file directly and reknit -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.width = 8,
fig.height = 5,
fig.align = 'center'
)
options(tibble.print_min = 5, tibble.print_max = 5)
```
# tidygeocoder<a href='https://jessecambon.github.io/tidygeocoder/'><img src="man/figures/tidygeocoder_hex.png" align="right" width="150"/></a>
<!-- badges: start -->
[![JOSS](https://joss.theoj.org/papers/10.21105/joss.03544/status.svg)](https://doi.org/10.21105/joss.03544)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/jessecambon/tidygeocoder/blob/master/LICENSE.md)
[![CRAN](https://www.r-pkg.org/badges/version/tidygeocoder)](https://cran.r-project.org/package=tidygeocoder)
[![CRAN Total Downloads](http://cranlogs.r-pkg.org/badges/grand-total/tidygeocoder)](https://CRAN.R-project.org/package=tidygeocoder)
[![CRAN Downloads Per Month](http://cranlogs.r-pkg.org/badges/tidygeocoder)](https://cran.r-project.org/package=tidygeocoder)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![R Build Status](https://github.com/jessecambon/tidygeocoder/workflows/R-CMD-check/badge.svg)](https://github.com/jessecambon/tidygeocoder/actions?workflow=R-CMD-check)
[![Zenodo](https://zenodo.org/badge/DOI/10.5281/zenodo.5627341.svg)](https://doi.org/10.5281/zenodo.5627341)
<!-- badges: end -->
Tidygeocoder makes getting data from geocoding services easy. A unified high-level interface is provided for a selection of [supported geocoding services](https://jessecambon.github.io/tidygeocoder/articles/geocoder_services.html) and results are returned in [tibble](https://tibble.tidyverse.org/) (dataframe) format.
Note that you should exercise due diligence when geocoding sensitive data as tidygeocoder utilizes third party web services to perform geocoding. Refer to the documentation on your selected geocoding service for information on how your data will be utilized and stored. See further information on this subject [here](https://jessecambon.github.io/tidygeocoder/articles/geocoder_services.html#data-privacy).
**Features:**
- Forward geocoding (addresses ⮕ coordinates)
- Reverse geocoding (coordinates ⮕ addresses)
- Batch geocoding (geocoding multiple addresses or coordinates in a single query) is automatically used if applicable.
- Duplicate, NA, and blank input data is handled elegantly; only unique inputs are submitted in queries, but the rows in the original data are preserved by default.
- The maximum rate of querying is automatically set according to the usage policies of the selected geocoding service.
In addition to the usage examples below, see the [Getting Started Vignette](https://jessecambon.github.io/tidygeocoder/articles/tidygeocoder.html) and [blog posts on tidygeocoder](https://jessecambon.github.io/tag/tidygeocoder).
## Installation
To install the stable version from CRAN (the official R package servers):
```{r, eval = FALSE}
install.packages('tidygeocoder')
```
Alternatively, you can install the latest development version from GitHub:
```{r, eval = FALSE}
devtools::install_github("jessecambon/tidygeocoder")
```
## Usage
In this first example we will geocode a few addresses using the `geocode()` function and plot them on a map with ggplot.
```{r}
library(dplyr, warn.conflicts = FALSE)
library(tidygeocoder)
# create a dataframe with addresses
some_addresses <- tibble::tribble(
~name, ~addr,
"White House", "1600 Pennsylvania Ave NW, Washington, DC",
"Transamerica Pyramid", "600 Montgomery St, San Francisco, CA 94111",
"Willis Tower", "233 S Wacker Dr, Chicago, IL 60606"
)
# geocode the addresses
lat_longs <- some_addresses %>%
geocode(addr, method = 'osm', lat = latitude , long = longitude)
```
The `geocode()` function geocodes addresses contained in a dataframe. The [Nominatim ("osm")](https://nominatim.org/) geocoding service is used here, but other services can be specified with the `method` argument. Only latitude and longitude are returned from the geocoding service in this example, but `full_results = TRUE` can be used to return all of the data from the geocoding service. See the `geo()` function documentation for details.
```{r, echo = FALSE}
knitr::kable(lat_longs)
```
Now that we have the longitude and latitude coordinates, we can use ggplot to plot our addresses on a map.
```{r usamap}
library(ggplot2)
ggplot(lat_longs, aes(longitude, latitude), color = "grey99") +
borders("state") + geom_point() +
ggrepel::geom_label_repel(aes(label = name)) +
theme_void()
```
To perform reverse geocoding (obtaining addresses from geographic coordinates), we can use the `reverse_geocode()` function. The arguments are similar to the `geocode()` function, but now we specify the input data columns with the `lat` and `long` arguments. The input dataset used here is the results of the geocoding query above.
The single line address is returned in a column named by the `address` argument and all columns from the geocoding service results are returned because `full_results = TRUE`. See the `reverse_geo()` function documentation for more details.
<!--
Removing the licence column is done just to prevent a note from
occurring in automated CRAN checks for an improper/old link.
-->
```{r}
reverse <- lat_longs %>%
reverse_geocode(lat = latitude, long = longitude, method = 'osm',
address = address_found, full_results = TRUE) %>%
select(-addr, -licence)
```
```{r, echo = FALSE}
knitr::kable(reverse)
```
## In the Wild
For inspiration, here are a few articles (with code) that leverage tidygeocoder:
- [Exercises: Spatial Data Wrangling with sf](http://www2.stat.duke.edu/courses/Spring21/sta323.001/exercises/lec_12.html) - part of a [statistical computing course](http://www2.stat.duke.edu/courses/Spring21/sta323.001/) at Duke
- [Geocoding the Minard Map](https://www.jla-data.net/eng/minard-map-tidygeocoder/) - recreating a famous infographic with geocoding
- [Mapping a network of women in demography](https://www.monicaalexander.com/posts/2021-21-02-mapping/) - using rvest and tidygeocoder to map Google Scholar data
- [Mapping Routes](https://bensstats.wordpress.com/2021/10/21/robservations-15-i-reverse-engineered-atlas-co-well-some-of-it/) - mapping routes with tidygeocoder and osrm
- [Road Routing in R](https://www.jla-data.net/eng/routing-in-r-context/) - demonstration of three different routing APIs
- [Mapping Texas Ports With R](https://www.sharpsightlabs.com/blog/mapping-texas-ports-with-r-part1/) - mapping the Texas coast with rnaturalearth and sf
## Contributing
Contributions to the tidygeocoder package are welcome. File [an issue](https://github.com/jessecambon/tidygeocoder/issues) for bug fixes or suggested features. If you would like to contribute code such as adding support for a new geocoding service, reference the [developer notes](https://jessecambon.github.io/tidygeocoder/articles/developer_notes.html) for instructions and documentation.
## Citing tidygeocoder
Use the `citation()` function:
``` r
citation('tidygeocoder')
```
</br>
<blockquote>
```{r, comment = '', echo = FALSE}
citation('tidygeocoder')
```
</blockquote>
Or refer to the [citation page](https://jessecambon.github.io/tidygeocoder/authors.html).