17  Geocoding

17.1 Call Libraries

Show the code
library(tidygeocoder) # geocoding

library(dplyr) # for %>% operator

library(readr) # import CSV

library(DT) # nice tables

17.2 Get Data To Be Geocoded

Show the code
simulated_address_data <- read_csv("simulated-address-data/simulated-address-data.csv")

DT::datatable(simulated_address_data,
              extensions = 'Buttons', 
              options = list(
                dom = 'Bfrtip',
                buttons = c('copy', 
                            'csv', 
                            'print'))) # nice table

17.3 Concatenate Addresses

Show the code
simulated_address_data$address <- paste(simulated_address_data$street,
                                        ", ",
                                        simulated_address_data$city,
                                        ", ",
                                        simulated_address_data$state)

DT::datatable(simulated_address_data,
              extensions = 'Buttons', 
              options = list(
                dom = 'Bfrtip',
                buttons = c('copy', 
                            'csv', 
                            'print'))) # nice table

17.4 Geocode

Caution

ArcGIS geocoding has LOW success rate with this data. You will want to find a process with HIGH success rate.

Tip

You could also try batchgeo -> KML -> Latitude/Longitude

Show the code
geocoded_data <- simulated_address_data %>% 
  tidygeocoder::geocode(address, 
                        method = 'arcgis', 
                        lat = latitude, 
                        long = longitude)
Passing 3 addresses to the ArcGIS single address geocoder
Query completed in: 1.6 seconds
Show the code
DT::datatable(geocoded_data,
              extensions = 'Buttons', 
              options = list(
                dom = 'Bfrtip',
                buttons = c('copy', 
                            'csv', 
                            'print'))) # nice table
Geocoding Can Make Errors!

NB that for whatever reason–perhaps because there is no address information–the geocoder has made a mistake: Agency Z has been placed in the Southern Hemisphere. Geocoding can be an error prone process and requires careful inspection of your tabular and mapped data.

A geocoder may also be unable to geocode some of your data. Low success rates are not uncommon, and you may have to work hard to ensure that the majority, or all, of your data are geocoded.

Geocoded data can then be mapped using procedures outlined in Chapter 16.