While working with UK geographical data I often have to extract geolocation information about the UK postcodes. A convenient way to do it in R is to use geocode function from the ggmap package. This function provides latitude and longitude information using Google Maps API. This is very useful for mapping data points but doesn’t provide information about UK-specific administrative division.
I got fed up of merging my list of postcodes with a long list of corresponding wards etc., so I looked for smarter ways of getting this info.
That’s how I came across postcodes.io which is free, open source, and based solely on open data. This service is an API to geolocate UK postcode and provide additional administrative information. Full documentation explains in details many available options. Among geographic information you can pull using postcodes.io are:
-
Postcode
Eastings
Northings
Strategic
County
District
Ward
Longitude
Latitude
Westminster Parliamentary Constituency
European Electoral Region (EER)
Primary Care Trust (PCT)
Parish (England)/ community (Wales)
LSOA
MSOA
CCG
NUTS
ONS/GSS Codes
I conduct most of my analyses in R so I developed wrapper functions around the API. Developmental version of the PostcodesioR package can be found on GitHub and documentation is here. It still doesn’t support all optional arguments but should do the job in most cases. A reference manual is here.
A mini-vignette (more to follow) showing how to run a lookup on a postcode, turn the result into a data frame, and then create an interactive map with leaflet:
The code above produces a data frame with key information
> glimpse(pc_df)
Observations: 1
Variables: 28
$ postcode
$ quality
$ eastings
$ northings
$ country
$ nhs_ha
$ longitude
$ latitude
$ parliamentary_constituency
$ european_electoral_region
$ primary_care_trust
$ region
$ lsoa
$ msoa
$ incode
$ outcode
$ admin_district
$ parish
$ admin_county
$ admin_ward
$ ccg
$ nuts
$ admin_district
$ admin_county
$ admin_ward
$ parish
$ ccg
$ nuts
and an interactive map showing geocoded postcode as a blue dot: