5  Geocoding

5.1 City Locator

There are several ways to geocode addresses from R, but the easiest (and cheapest) way is with the {tidygeocoder} package and one of the city’s internal locators.

Note

The locator will only geocode San Francisco addresses.

library(tidygeocoder)
library(sf)
Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(mapview)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2
──
✔ ggplot2 3.4.0     ✔ purrr   0.3.4
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.2.1     ✔ stringr 1.5.0
✔ readr   2.1.3     ✔ forcats 0.5.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
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"
  )
Passing 2 addresses to the ArcGIS single address geocoder
Query completed in: 0.3 seconds
coords
# A tibble: 2 × 3
  address                      lat  long
  <chr>                      <dbl> <dbl>
1 1 South Van Ness            37.8 -122.
2 1 Dr Carlton B Goodlett Pl  37.8 -122.
coords_sf <- coords %>% st_as_sf(coords = c("long", "lat"), crs = 4326)
mapview(coords_sf)

5.2 geocodio

If you need to geocode addresses outside the city, the geocodio service 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:

library(usethis)
edit_r_environ() # this opens the file in RStudio

Paste your API key like so:

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.