Skip to main content

Section 2.6 Geocoding

We were making use of point of interest data from Open Street Map above, but it is possible that we have a dataset of bars from official, administrative data sets, but that are not geocoded. In this case, we may have a list of bars with an associated address, which is clearly some sort of spatial information, but how would you put this on a map?
One solution to this problem is to geocode these addresses. We can use the package tidygeocoder to achieve this. This package takes an address given as character values, for example "221B Baker Street, Marylebone, London NW1 6XE" and returns coordinates, geocoding this address. Let’s say we have a dataframe of addresses (in this case, only one observation):
addresses <- data.frame(name = "Sherlock Holmes",
                        address = "221B Baker Street, London, UK")
We can then use the geocode() function from the tidygeocoder package to get coordinates for this address. We have to specify the column which has the address (in this case address), and the method to use for geocoding. See the help file for the function for the many options. For example, if you are in the USA, you may use "census". Since we are global, we will use "osm", which uses nominatim (OSM) to provide worldwide coverage. So, given the above example:
addresses %>% geocode(address, method = 'osm')
## Passing 1 address to the Nominatim single address geocoder
## Query completed in: 1 seconds
## # A tibble: 1 × 4
##   name            address                    lat   long
##   <chr>           <chr>                    <dbl>  <dbl>
## 1 Sherlock Holmes 221B Baker Street, Lond…  51.5 -0.158
To illustrate on scale, let’s have a look at another source of data of bars. Manchester City Council have an Open Data Catalogue on their website, which you can use to browse through what sort of data they release to the public. Like in many city open data portals, there are some more and some less interesting datasets made available here. It’s not quite as impressive as the open data from some of the cities in the US such as New York or Dallas, but we’ll take it.
One interesting dataset, especially for our questions about the different alcohol outlets, is the Licensed Premises dataset. This details all the currently active licenced premises in Manchester.
There are a few ways we can download this data set. On the manual side of things, we can simply right-click on the download link from the website, save it to your computer, and read it in from there, by specifying the file path. Remember, if you save it in your R working directory, then you just need to specify the file name, as the working directory folder is where R will first look for this file.
Another way is to read into R directly from the URL provided. Here, let’s read in the licensed premises data directly from the web using the URL "http://www.manchester.gov.uk/open/download/downloads/id/169/licensed_premises.csv"
data_url <- "http://www.manchester.gov.uk/open/download/downloads/id/169/licensed_premises.csv"

lic_prem <- read_csv(data_url) %>-% clean_names()
You will likely get some warnings when reading this data, but you can safely ignore those. You can always check if this worked by looking to your global environment on the right-hand side and seeing if this lic_prem object has appeared. If it has, you should see it has a number of observations (rows) and variables (columns).
Let’s have a look at what this dataset looks like. You can use the View() function for this:
View(lic_prem)
We see that there is a field for "premisesname" which is the name of the premise, and two fields, "locationtext" and "postcode" which refer to address information. To geocode these, let’s create a new column which combines the address and post code, and then use the geocode() function introduced above. This will take a while for the whole addresses dataset; so just for illustration purposes, we take the first 30.
lic_prem <- lic_prem %>%
  slice(1:30) %>%    # Select first 30 venues
  # Create new complete_address column from locationtext and postcode using paste()
  mutate(complete_address = paste(locationtext,  postcode,  sep=", ")) %>%
  geocode(complete_address, method = 'osm')  # geocode with osm method
## Passing 30 addresses to the Nominatim single address geocoder
## Query completed in: 30.2 seconds
Now we have these licenced premises geocoded, with brand new latitude and longitude information! We can use this to make a leaflet map of our venues!
# make sure coordinates are numeric values
lic_prem$latitude <- as.numeric(lic_prem$lat)
lic_prem$longitude <- as.numeric(lic_prem$long)

# create map
leaflet(data = lic_prem) %>%
  addTiles() %>%
  addMarkers(lng=~longitude,  lat=~latitude, popup=~as.character(premisesname),
             label = ~as.character(premisesname))
Geocoding may come in handy when we have address data, or something similar but no geometry to use to map it.