Naveed
Naveed

Reputation: 21

Turning Longitudes and Latitudes into UK Postcodes in R

I have a large set of data with Longitudes and Latitudes that I want to convert into UK Postcodes. I first tried downloading all of the UK postcodes with their corresponding long/lat and then joining the data together. This worked for some of the data but the majority didn't match due to postcode latitude and longitude being the centre of each postcode, where as my data is more accurate.

I've also tried a bit of code that converts Lat/long in America to give the corresponding state (given by Josh O'Brien here Latitude Longitude Coordinates to State Code in R), but I couldn't find a way to alter this to UK postcodes.

I've also tried running a calculation that tries to find the closest postcode to the long/lat but this create a file too large for R to handle.

Also seen some code that uses google maps (geocoding) and this does seem to work but I've read it only allows 2000 calculations a day, I have much more than this (around 5 million rows of data)

Upvotes: 2

Views: 5621

Answers (2)

epo3
epo3

Reputation: 3121

You might want to try my PostcodesioR package which includes reverse geocoding functions. However, there is a limit to the number of API calls to postcodes.io.

devtools::install_github("ropensci/PostcodesioR")
library(PostcodesioR)
reverse_geocoding(0.127, 51.507)

Another option is to use this function for reverse geocoding more than one pair of geographical coordinates:

geolocations_list <- structure(
 list(
 geolocations = structure(
 list(
 longitude = c(-3.15807731271522, -1.12935802905177),
 latitude = c(51.4799900627036, 50.7186356978817),
 limit = c(NA, 100L),
 radius = c(NA, 500L)),
 .Names = c("longitude", "latitude", "limit", "radius"),
 class = "data.frame",
 row.names = 1:2)),
 .Names = "geolocations")

bulk_rev_geo <- bulk_reverse_geocoding(geolocations_list)

bulk_rev_geo[[1]]$result[[1]]

Given the size of your data set and usual limitations to the API calls, you might want to download the latest database with the UK geographical data and join it to your files.

Upvotes: 2

Borislav Aymaliev
Borislav Aymaliev

Reputation: 833

I believe you want to do "Reverse Geocoding" with the google maps API. That is to parse the latitude and longitude and get the closest address. After that you can easily take just the post code from the address. (It is an item in the list you receive as an address from the google maps API.)

The api (last time I checked) allows 2500 free calls per day, but you can do several tricks (depending on your dataset) to match more records:

  1. You can populate your dataset with 2400 records each day until it is complete or
  2. You can change your IP and API key a few times to get more records in a single day or
  3. You can always get a premium API key and pay for the number of requests you make

I did such geocoding in R a few years ago by following this popular tutorial: https://www.r-bloggers.com/using-google-maps-api-and-r/

Unfortunately the tutorial code is a bit out-of-date, so you will need to fix a few things to adapt it to your needs.

Upvotes: 0

Related Questions