-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoding.qmd
51 lines (36 loc) · 1.71 KB
/
geocoding.qmd
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
# Geocoding
## City Locator
There are several ways to geocode addresses from R, but the easiest (and cheapest) way is with the [{tidygeocoder} package](https://jessecambon.github.io/tidygeocoder/) and one of the city's [internal locators.](<https://gis.sf.gov/svc/rest/services/loc/c83_eas_str_ctrl_composite/GeocodeServer/findAddressCandidates>)
::: callout-note
The locator will only geocode San Francisco addresses.
:::
```{r warning = FALSE}
library(tidygeocoder)
library(sf)
library(mapview)
library(tidyverse)
df <- tibble(address = c("1 South Van Ness", "1 Dr Carlton B Goodlett Pl"))
locator <- "https://gis.sf.gov/svc/rest/services/loc/c83_eas_str_ctrl_composite/GeocodeServer/findAddressCandidates"
coords <- df %>%
geocode(
api_url = locator,
address = address,
custom_query = list(outSR = "4326"), # outSR (Spatial Reference) is a required parameter
method = "arcgis"
)
coords
coords_sf <- coords %>% st_as_sf(coords = c("long", "lat"), crs = 4326)
mapview(coords_sf)
```
## geocodio
If you need to geocode addresses outside the city, [the geocodio service](https://www.geocod.io/) is a nice option, but you'll first need to obtain your API key. Sign up for an account and register for an API key. Once you have it, you need to put it in your .Renviron file, a special text file that runs every time you open/restart R.
Edit your .Renviron file with the usethis package:
```{r eval = FALSE}
library(usethis)
edit_r_environ() # this opens the file in RStudio
```
Paste your API key like so:
```bash
GEOCODIO_API_KEY='<your_api_key>'
```
Save the file and restart R. You should then be able to call `geocode` with the `method = 'geocodio'` argument. Note that there is a rate limit of 1000 addresses per hour.