Introduction

PostcodesioR is an API wrapper for postcodes.io. It allows acquiring geographic information about the UK postcodes and geographic coordinates.

Installation

if (!require("devtools")) install.packages("devtools")
devtools::install_github("ropensci/PostcodesioR")

Lookup postcodes and outcodes

Single postcode

Provide a postcode to obtain all available information

library(PostcodesioR)

lookup_result <- postcode_lookup("EC1Y8LX")

#overview
str(lookup_result)
## 'data.frame':    1 obs. of  40 variables:
##  $ postcode                            : chr "EC1Y 8LX"
##  $ quality                             : int 1
##  $ eastings                            : int 532544
##  $ northings                           : int 182128
##  $ country                             : chr "England"
##  $ nhs_ha                              : chr "London"
##  $ longitude                           : num -0.0909
##  $ latitude                            : num 51.5
##  $ european_electoral_region           : chr "London"
##  $ primary_care_trust                  : chr "Islington"
##  $ region                              : chr "London"
##  $ lsoa                                : chr "Islington 023D"
##  $ msoa                                : chr "Islington 023"
##  $ incode                              : chr "8LX"
##  $ outcode                             : chr "EC1Y"
##  $ parliamentary_constituency          : chr "Islington South and Finsbury"
##  $ parliamentary_constituency_2024     : chr "Islington South and Finsbury"
##  $ admin_district                      : chr "Islington"
##  $ parish                              : chr "Islington, unparished area"
##  $ admin_county                        : logi NA
##  $ date_of_introduction                : chr "198001"
##  $ admin_ward                          : chr "Bunhill"
##  $ ced                                 : logi NA
##  $ ccg                                 : chr "NHS North Central London"
##  $ nuts                                : chr "Islington"
##  $ pfa                                 : chr "Metropolitan Police"
##  $ admin_district_code                 : chr "E09000019"
##  $ admin_county_code                   : chr "E99999999"
##  $ admin_ward_code                     : chr "E05013699"
##  $ parish_code                         : chr "E43000209"
##  $ parliamentary_constituency_code     : chr "E14001306"
##  $ parliamentary_constituency_2024_code: chr "E14001306"
##  $ ccg_code                            : chr "E38000240"
##  $ ccg_id_code                         : chr "93C"
##  $ ced_code                            : chr "E99999999"
##  $ nuts_code                           : chr "TLI43"
##  $ lsoa_code                           : chr "E01002704"
##  $ msoa_code                           : chr "E02000576"
##  $ lau2_code                           : chr "E09000019"
##  $ pfa_code                            : chr "E23000001"

There is another function that returns the same data points but returns a list and allows optional parameters

query_result <- postcode_query("EC1Y8LX")

#overview
str(query_result)
## List of 1
##  $ :List of 27
##   ..$ postcode                       : chr "EC1Y 8LX"
##   ..$ quality                        : int 1
##   ..$ eastings                       : int 532544
##   ..$ northings                      : int 182128
##   ..$ country                        : chr "England"
##   ..$ nhs_ha                         : chr "London"
##   ..$ longitude                      : num -0.0909
##   ..$ latitude                       : num 51.5
##   ..$ european_electoral_region      : chr "London"
##   ..$ primary_care_trust             : chr "Islington"
##   ..$ region                         : chr "London"
##   ..$ lsoa                           : chr "Islington 023D"
##   ..$ msoa                           : chr "Islington 023"
##   ..$ incode                         : chr "8LX"
##   ..$ outcode                        : chr "EC1Y"
##   ..$ parliamentary_constituency     : chr "Islington South and Finsbury"
##   ..$ parliamentary_constituency_2024: chr "Islington South and Finsbury"
##   ..$ admin_district                 : chr "Islington"
##   ..$ parish                         : chr "Islington, unparished area"
##   ..$ admin_county                   : NULL
##   ..$ date_of_introduction           : chr "198001"
##   ..$ admin_ward                     : chr "Bunhill"
##   ..$ ced                            : NULL
##   ..$ ccg                            : chr "NHS North Central London"
##   ..$ nuts                           : chr "Islington"
##   ..$ pfa                            : chr "Metropolitan Police"
##   ..$ codes                          :List of 14
##   .. ..$ admin_district                 : chr "E09000019"
##   .. ..$ admin_county                   : chr "E99999999"
##   .. ..$ admin_ward                     : chr "E05013699"
##   .. ..$ parish                         : chr "E43000209"
##   .. ..$ parliamentary_constituency     : chr "E14001306"
##   .. ..$ parliamentary_constituency_2024: chr "E14001306"
##   .. ..$ ccg                            : chr "E38000240"
##   .. ..$ ccg_id                         : chr "93C"
##   .. ..$ ced                            : chr "E99999999"
##   .. ..$ nuts                           : chr "TLI43"
##   .. ..$ lsoa                           : chr "E01002704"
##   .. ..$ msoa                           : chr "E02000576"
##   .. ..$ lau2                           : chr "E09000019"
##   .. ..$ pfa                            : chr "E23000001"

This function creates a nested list with the codes for administrative district, county, ward, parish, parliamentary constituency, CCG, and NUTS.

Multiple postcodes

To query two or more postcodes, use bulk_ functions.

pc_list <- list(postcodes = c("PR3 0SG", "M45 6GN", "EX165BL"))
bulk_lookup_result <- bulk_postcode_lookup(pc_list)

#overview
str(bulk_lookup_result[1])
## List of 1
##  $ :List of 2
##   ..$ query : chr "PR3%200SG"
##   ..$ result: NULL

If you want to work with data frame then the nested list created above can be turned into a data frame

library(purrr)

bulk_list <- lapply(bulk_lookup_result, "[[", 2)

bulk_df <-
  map_dfr(bulk_list,
          `[`,
          c("postcode", "longitude", "latitude"))

Querying Scottish postcodes requires a separate function:

scottish_lookup <- scottish_postcode_lookup("EH12NG")

str(scottish_lookup)
## 'data.frame':    1 obs. of  3 variables:
##  $ postcode                                : chr "EH1 2NG"
##  $ scottish_parliamentary_constituency     : chr "Edinburgh Central"
##  $ scottish_parliamentary_constituency_code: chr "S16000104"

Outward code lookup

Provide an outcode to obtain geolocation data for the centroid of the specified outcode:

ocl <- outward_code_lookup("E1")

#overview
str(ocl)
## List of 11
##  $ outcode                   : chr "E1"
##  $ longitude                 : num -0.0594
##  $ latitude                  : num 51.5
##  $ northings                 : int 181615
##  $ eastings                  : int 534742
##  $ admin_district            :List of 3
##   ..$ : chr "Hackney"
##   ..$ : chr "City of London"
##   ..$ : chr "Tower Hamlets"
##  $ parish                    :List of 3
##   ..$ : chr "Hackney, unparished area"
##   ..$ : chr "City of London, unparished area"
##   ..$ : chr "Tower Hamlets, unparished area"
##  $ admin_county              : list()
##  $ admin_ward                :List of 14
##   ..$ : chr "Shadwell"
##   ..$ : chr "Spitalfields & Banglatown"
##   ..$ : chr "St Dunstan's"
##   ..$ : chr "Portsoken"
##   ..$ : chr "Stepney Green"
##   ..$ : chr "Weavers"
##   ..$ : chr "Bow West"
##   ..$ : chr "Whitechapel"
##   ..$ : chr "Bishopsgate"
##   ..$ : chr "Bethnal Green East"
##   ..$ : chr "Bethnal Green West"
##   ..$ : chr "Hoxton East & Shoreditch"
##   ..$ : chr "Tower"
##   ..$ : chr "Aldgate"
##  $ country                   :List of 1
##   ..$ : chr "England"
##  $ parliamentary_constituency:List of 4
##   ..$ : chr "Bethnal Green and Stepney"
##   ..$ : chr "Stratford and Bow"
##   ..$ : chr "Hackney South and Shoreditch"
##   ..$ : chr "Cities of London and Westminster"

Reverse geocoding

Provide latitude and longitude to obtain geographic information. Different levels of aggregation are available, i.e. postcode or outcode.

Single postcode

rev_geo <- reverse_geocoding(0.127, 51.507)

# overview
str(rev_geo[1])
## List of 1
##  $ :List of 28
##   ..$ postcode                       : chr "SE28 8NH"
##   ..$ quality                        : int 1
##   ..$ eastings                       : int 547715
##   ..$ northings                      : int 180780
##   ..$ country                        : chr "England"
##   ..$ nhs_ha                         : chr "London"
##   ..$ longitude                      : num 0.127
##   ..$ latitude                       : num 51.5
##   ..$ european_electoral_region      : chr "London"
##   ..$ primary_care_trust             : chr "Bexley"
##   ..$ region                         : chr "London"
##   ..$ lsoa                           : chr "Bexley 001D"
##   ..$ msoa                           : chr "Bexley 001"
##   ..$ incode                         : chr "8NH"
##   ..$ outcode                        : chr "SE28"
##   ..$ parliamentary_constituency     : chr "Erith and Thamesmead"
##   ..$ parliamentary_constituency_2024: chr "Erith and Thamesmead"
##   ..$ admin_district                 : chr "Bexley"
##   ..$ parish                         : chr "Bexley, unparished area"
##   ..$ admin_county                   : NULL
##   ..$ date_of_introduction           : chr "198410"
##   ..$ admin_ward                     : chr "Thamesmead East"
##   ..$ ced                            : NULL
##   ..$ ccg                            : chr "NHS South East London"
##   ..$ nuts                           : chr "Bexley"
##   ..$ pfa                            : chr "Metropolitan Police"
##   ..$ codes                          :List of 14
##   .. ..$ admin_district                 : chr "E09000004"
##   .. ..$ admin_county                   : chr "E99999999"
##   .. ..$ admin_ward                     : chr "E05011232"
##   .. ..$ parish                         : chr "E43000194"
##   .. ..$ parliamentary_constituency     : chr "E14001229"
##   .. ..$ parliamentary_constituency_2024: chr "E14001229"
##   .. ..$ ccg                            : chr "E38000244"
##   .. ..$ ccg_id                         : chr "72Q"
##   .. ..$ ced                            : chr "E99999999"
##   .. ..$ nuts                           : chr "TLI51"
##   .. ..$ lsoa                           : chr "E01000469"
##   .. ..$ msoa                           : chr "E02000065"
##   .. ..$ lau2                           : chr "E09000004"
##   .. ..$ pfa                            : chr "E23000001"
##   ..$ distance                       : num 38.9

Multiple postcodes

To reverse geocode multiple values use the function underneath. The result is a nested list, which might be a bit intimidating, but it allows storing unequal number of elements.

# create a list with the 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]]
## $postcode
## [1] "CF24 2BT"
## 
## $quality
## [1] 1
## 
## $eastings
## [1] 319675
## 
## $northings
## [1] 176305
## 
## $country
## [1] "Wales"
## 
## $nhs_ha
## [1] "Cardiff and Vale University Health Board"
## 
## $longitude
## [1] -3.158076
## 
## $latitude
## [1] 51.47998
## 
## $european_electoral_region
## [1] "Wales"
## 
## $primary_care_trust
## [1] "Cardiff and Vale University Health Board"
## 
## $region
## NULL
## 
## $lsoa
## [1] "Cardiff 038D"
## 
## $msoa
## [1] "Cardiff 038"
## 
## $incode
## [1] "2BT"
## 
## $outcode
## [1] "CF24"
## 
## $parliamentary_constituency
## [1] "Cardiff South and Penarth"
## 
## $parliamentary_constituency_2024
## [1] "Cardiff South and Penarth"
## 
## $admin_district
## [1] "Cardiff"
## 
## $parish
## [1] "Splott"
## 
## $admin_county
## NULL
## 
## $date_of_introduction
## [1] "199906"
## 
## $admin_ward
## [1] "Splott"
## 
## $ced
## NULL
## 
## $ccg
## [1] "Cardiff and Vale University"
## 
## $nuts
## [1] "Cardiff"
## 
## $pfa
## [1] "South Wales"
## 
## $codes
## $codes$admin_district
## [1] "W06000015"
## 
## $codes$admin_county
## [1] "W99999999"
## 
## $codes$admin_ward
## [1] "W05001295"
## 
## $codes$parish
## [1] "W04001005"
## 
## $codes$parliamentary_constituency
## [1] "W07000091"
## 
## $codes$parliamentary_constituency_2024
## [1] "W07000091"
## 
## $codes$ccg
## [1] "W11000029"
## 
## $codes$ccg_id
## [1] "7A4"
## 
## $codes$ced
## [1] "W99999999"
## 
## $codes$nuts
## [1] "TLL22"
## 
## $codes$lsoa
## [1] "W01001874"
## 
## $codes$msoa
## [1] "W02000404"
## 
## $codes$lau2
## [1] "W06000015"
## 
## $codes$pfa
## [1] "W15000003"
## 
## 
## $distance
## [1] 1.567236

The list above is not the most common way of storing files. It’s more likely that a data frame will be used to store the geodata. In that case, it has to be turned into a list of a specific format required by the API:

geolocations_df <- 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"),
  row.names = 1:2,
  class = "data.frame")

geolocations_df
##   longitude latitude limit radius
## 1 -3.158077 51.47999    NA     NA
## 2 -1.129358 50.71864   100    500
# turn a data frame into a list
geolocations_df2list <- list(geolocations_df)

# add a list name
names(geolocations_df2list) <- "geolocations"

# display correct input for the function
geolocations_df2list
## $geolocations
##   longitude latitude limit radius
## 1 -3.158077 51.47999    NA     NA
## 2 -1.129358 50.71864   100    500

Common usage of this function might be extracting particular variables. You can extract one variable like this:

# extract one postcode
bulk_rev_geo[[1]]$result[[8]]$postcode
## [1] "CF24 2AB"

But more likely you will want more than one result. After all, that’s the point of using a bulk function:

# function to extract variables of interest
extract_bulk_geo_variable <- function(x) {
  bulk_results <- lapply(bulk_rev_geo, `[[`, "result")
  sapply(unlist(bulk_results, recursive = FALSE), `[[`, x)
}

# define the variables you need
variables_of_interest <- c("postcode", "latitude", "longitude")

# return a data frame with the variables
data.frame(
  sapply(variables_of_interest, extract_bulk_geo_variable))
##    postcode  latitude longitude
## 1  CF24 2BT 51.479976 -3.158076
## 2  CF24 2ED 51.479719 -3.158712
## 3  CF24 2AA 51.480209 -3.159062
## 4  CF24 5NW  51.47936 -3.158478
## 5  CF24 2AJ 51.480682 -3.158526
## 6  CF24 2AH 51.480552 -3.158912
## 7  CF24 2DZ 51.480105 -3.156798
## 8  CF24 2AB 51.480615 -3.157223
## 9  CF24 2AL  51.48083 -3.158141
## 10 PO33 1PS 50.718856 -1.129271
## 11 PO33 1PT 50.718573 -1.128467
## 12 PO33 1PX 50.717878 -1.127136
## 13 PO33 1QB 50.717046 -1.129826
## 14 PO33 1QD 50.717191 -1.127843
## 15 PO33 1PU 50.718465 -1.126032
## 16 PO33 1PZ 50.716247 -1.127932
## 17 PO33 1FS  50.71753  -1.12397
## 18 PO33 1QR  50.71574 -1.125998
## 19 PO33 1PB 50.721101 -1.133735
## 20 PO33 1PR 50.721486 -1.133187
## 21 PO33 1PY 50.715159 -1.126734
## 22 PO33 1QP 50.715694 -1.124911
## 23 PO33 1GL 50.716031 -1.124295
## 24 PO34 5AP 50.721536 -1.124476

Single outcode

out_rev_geocode <- outcode_reverse_geocoding("-3.15", "51.47")
# overview
str(out_rev_geocode[1])
## List of 1
##  $ :List of 11
##   ..$ outcode                   : chr "CF99"
##   ..$ longitude                 : num -3.16
##   ..$ latitude                  : num 51.5
##   ..$ northings                 : int 174506
##   ..$ eastings                  : int 319353
##   ..$ admin_district            :List of 1
##   .. ..$ : chr "Cardiff"
##   ..$ parish                    :List of 1
##   .. ..$ : chr "Butetown"
##   ..$ admin_county              : list()
##   ..$ admin_ward                :List of 1
##   .. ..$ : chr "Butetown"
##   ..$ country                   :List of 1
##   .. ..$ : chr "Wales"
##   ..$ parliamentary_constituency:List of 1
##   .. ..$ : chr "Cardiff South and Penarth"

Generate random entries

Postcodes

Generates a list with a random UK postcode and corresponding geographic information:

# without restrictions
random_postcode()
## $postcode
## [1] "SW2 5AJ"
## 
## $quality
## [1] 1
## 
## $eastings
## [1] 530826
## 
## $northings
## [1] 174925
## 
## $country
## [1] "England"
## 
## $nhs_ha
## [1] "London"
## 
## $longitude
## [1] -0.118338
## 
## $latitude
## [1] 51.45819
## 
## $european_electoral_region
## [1] "London"
## 
## $primary_care_trust
## [1] "Lambeth"
## 
## $region
## [1] "London"
## 
## $lsoa
## [1] "Lambeth 016A"
## 
## $msoa
## [1] "Lambeth 016"
## 
## $incode
## [1] "5AJ"
## 
## $outcode
## [1] "SW2"
## 
## $parliamentary_constituency
## [1] "Clapham and Brixton Hill"
## 
## $parliamentary_constituency_2024
## [1] "Clapham and Brixton Hill"
## 
## $admin_district
## [1] "Lambeth"
## 
## $parish
## [1] "Lambeth, unparished area"
## 
## $admin_county
## NULL
## 
## $date_of_introduction
## [1] "198001"
## 
## $admin_ward
## [1] "Brixton Acre Lane"
## 
## $ced
## NULL
## 
## $ccg
## [1] "NHS South East London"
## 
## $nuts
## [1] "Lambeth"
## 
## $pfa
## [1] "Metropolitan Police"
## 
## $codes
## $codes$admin_district
## [1] "E09000022"
## 
## $codes$admin_county
## [1] "E99999999"
## 
## $codes$admin_ward
## [1] "E05014095"
## 
## $codes$parish
## [1] "E43000212"
## 
## $codes$parliamentary_constituency
## [1] "E14001175"
## 
## $codes$parliamentary_constituency_2024
## [1] "E14001175"
## 
## $codes$ccg
## [1] "E38000244"
## 
## $codes$ccg_id
## [1] "72Q"
## 
## $codes$ced
## [1] "E99999999"
## 
## $codes$nuts
## [1] "TLI45"
## 
## $codes$lsoa
## [1] "E01003020"
## 
## $codes$msoa
## [1] "E02000633"
## 
## $codes$lau2
## [1] "E09000022"
## 
## $codes$pfa
## [1] "E23000001"

A randomly generated postcode can also belong to a particular outcode:

# restrict to an outcode
random_postcode("N1")
## $postcode
## [1] "N1 3DH"
## 
## $quality
## [1] 1
## 
## $eastings
## [1] 532653
## 
## $northings
## [1] 183818
## 
## $country
## [1] "England"
## 
## $nhs_ha
## [1] "London"
## 
## $longitude
## [1] -0.088716
## 
## $latitude
## [1] 51.53769
## 
## $european_electoral_region
## [1] "London"
## 
## $primary_care_trust
## [1] "Islington"
## 
## $region
## [1] "London"
## 
## $lsoa
## [1] "Islington 018C"
## 
## $msoa
## [1] "Islington 018"
## 
## $incode
## [1] "3DH"
## 
## $outcode
## [1] "N1"
## 
## $parliamentary_constituency
## [1] "Islington South and Finsbury"
## 
## $parliamentary_constituency_2024
## [1] "Islington South and Finsbury"
## 
## $admin_district
## [1] "Islington"
## 
## $parish
## [1] "Islington, unparished area"
## 
## $admin_county
## NULL
## 
## $date_of_introduction
## [1] "198001"
## 
## $admin_ward
## [1] "St Mary's & St James'"
## 
## $ced
## NULL
## 
## $ccg
## [1] "NHS North Central London"
## 
## $nuts
## [1] "Islington"
## 
## $pfa
## [1] "Metropolitan Police"
## 
## $codes
## $codes$admin_district
## [1] "E09000019"
## 
## $codes$admin_county
## [1] "E99999999"
## 
## $codes$admin_ward
## [1] "E05013710"
## 
## $codes$parish
## [1] "E43000209"
## 
## $codes$parliamentary_constituency
## [1] "E14001306"
## 
## $codes$parliamentary_constituency_2024
## [1] "E14001306"
## 
## $codes$ccg
## [1] "E38000240"
## 
## $codes$ccg_id
## [1] "93C"
## 
## $codes$ced
## [1] "E99999999"
## 
## $codes$nuts
## [1] "TLI43"
## 
## $codes$lsoa
## [1] "E01002798"
## 
## $codes$msoa
## [1] "E02000571"
## 
## $codes$lau2
## [1] "E09000019"
## 
## $codes$pfa
## [1] "E23000001"

Places

You can also generate a random place, specified by an OSGB code, with corresponding geographic information:

random_place()
##                   code   name_1 name_1_lang name_2 name_2_lang local_type
## 1 osgb4000000074547192 Hanslope        NULL   NULL        NULL    Village
##   outcode county_unitary county_unitary_type district_borough
## 1    MK19  Milton Keynes    UnitaryAuthority             NULL
##   district_borough_type     region country  longitude latitude eastings
## 1                  NULL South East England -0.8267955 52.11414   480434
##   northings min_eastings min_northings max_eastings max_northings
## 1    246851       479615        246175       480999        247682

Postcode validation

This function can validate a UK postcode:

postcode_validation("EC1Y8LX") # actual UK postcode
## [1] TRUE
postcode_validation("XYZ") # incorrect UK postcode
## [1] FALSE

Autocomplete postcodes

Find the potential candidates for a postcode if you only know the beginning characters

postcode_autocomplete("EC1")
##    postcode
## 1  EC1A 1AA
## 2  EC1A 1AH
## 3  EC1A 1AZ
## 4  EC1A 1BB
## 5  EC1A 1DN
## 6  EC1A 1DU
## 7  EC1A 1HQ
## 8  EC1A 1TA
## 9  EC1A 1TB
## 10 EC1A 1TF

It defaults to 10 candidates, but can be changed by specifying the limit argument.

Find nearest postcodes or outcodes

Provide a postcode to get a list of the nearest postcodes:

near_pc <- nearest_postcode("EC1Y8LX")

#overview
str(near_pc[1])
## List of 1
##  $ :List of 28
##   ..$ postcode                       : chr "EC1Y 8LX"
##   ..$ quality                        : int 1
##   ..$ eastings                       : int 532544
##   ..$ northings                      : int 182128
##   ..$ country                        : chr "England"
##   ..$ nhs_ha                         : chr "London"
##   ..$ longitude                      : num -0.0909
##   ..$ latitude                       : num 51.5
##   ..$ european_electoral_region      : chr "London"
##   ..$ primary_care_trust             : chr "Islington"
##   ..$ region                         : chr "London"
##   ..$ lsoa                           : chr "Islington 023D"
##   ..$ msoa                           : chr "Islington 023"
##   ..$ incode                         : chr "8LX"
##   ..$ outcode                        : chr "EC1Y"
##   ..$ parliamentary_constituency     : chr "Islington South and Finsbury"
##   ..$ parliamentary_constituency_2024: chr "Islington South and Finsbury"
##   ..$ admin_district                 : chr "Islington"
##   ..$ parish                         : chr "Islington, unparished area"
##   ..$ admin_county                   : NULL
##   ..$ date_of_introduction           : chr "198001"
##   ..$ admin_ward                     : chr "Bunhill"
##   ..$ ced                            : NULL
##   ..$ ccg                            : chr "NHS North Central London"
##   ..$ nuts                           : chr "Islington"
##   ..$ pfa                            : chr "Metropolitan Police"
##   ..$ codes                          :List of 14
##   .. ..$ admin_district                 : chr "E09000019"
##   .. ..$ admin_county                   : chr "E99999999"
##   .. ..$ admin_ward                     : chr "E05013699"
##   .. ..$ parish                         : chr "E43000209"
##   .. ..$ parliamentary_constituency     : chr "E14001306"
##   .. ..$ parliamentary_constituency_2024: chr "E14001306"
##   .. ..$ ccg                            : chr "E38000240"
##   .. ..$ ccg_id                         : chr "93C"
##   .. ..$ ced                            : chr "E99999999"
##   .. ..$ nuts                           : chr "TLI43"
##   .. ..$ lsoa                           : chr "E01002704"
##   .. ..$ msoa                           : chr "E02000576"
##   .. ..$ lau2                           : chr "E09000019"
##   .. ..$ pfa                            : chr "E23000001"
##   ..$ distance                       : int 0

You can also use outcodes:

near_outcode <- nearest_outcode("EC1Y")

# overview
str(near_outcode[2])
## List of 1
##  $ :List of 11
##   ..$ outcode                   : chr "EC2Y"
##   ..$ longitude                 : num -0.0936
##   ..$ latitude                  : num 51.5
##   ..$ northings                 : int 181783
##   ..$ eastings                  : int 532368
##   ..$ admin_district            :List of 2
##   .. ..$ : chr "Islington"
##   .. ..$ : chr "City of London"
##   ..$ parish                    :List of 2
##   .. ..$ : chr "Islington, unparished area"
##   .. ..$ : chr "City of London, unparished area"
##   ..$ admin_county              : list()
##   ..$ admin_ward                :List of 6
##   .. ..$ : chr "Coleman Street"
##   .. ..$ : chr "Aldersgate"
##   .. ..$ : chr "Bunhill"
##   .. ..$ : chr "Bassishaw"
##   .. ..$ : chr "Clerkenwell"
##   .. ..$ : chr "Cripplegate"
##   ..$ country                   :List of 1
##   .. ..$ : chr "England"
##   ..$ parliamentary_constituency:List of 2
##   .. ..$ : chr "Islington South and Finsbury"
##   .. ..$ : chr "Cities of London and Westminster"

Or longitude and latitude

near_ll <- nearest_outcode_lonlat(0.127, 51.507)

#overview
str(near_ll[1])
## List of 1
##  $ :List of 11
##   ..$ outcode                   : chr "DA18"
##   ..$ longitude                 : num 0.136
##   ..$ latitude                  : num 51.5
##   ..$ northings                 : int 179423
##   ..$ eastings                  : int 548396
##   ..$ admin_district            :List of 1
##   .. ..$ : chr "Bexley"
##   ..$ parish                    :List of 1
##   .. ..$ : chr "Bexley, unparished area"
##   ..$ admin_county              : list()
##   ..$ admin_ward                :List of 2
##   .. ..$ : chr "Slade Green & Northend"
##   .. ..$ : chr "Thamesmead East"
##   ..$ country                   :List of 1
##   .. ..$ : chr "England"
##   ..$ parliamentary_constituency:List of 2
##   .. ..$ : chr "Bexleyheath and Crayford"
##   .. ..$ : chr "Erith and Thamesmead"

Find places

Provide a name of a place of interest. You can specify the number of results (default is 10):

place_query_result <- place_query("Hills", limit = 11)

# overview
str(place_query_result[1])
## List of 1
##  $ :List of 21
##   ..$ code                 : chr "osgb4000000074574731"
##   ..$ name_1               : chr "Berwick Hills"
##   ..$ name_1_lang          : NULL
##   ..$ name_2               : NULL
##   ..$ name_2_lang          : NULL
##   ..$ local_type           : chr "Suburban Area"
##   ..$ outcode              : chr "TS3"
##   ..$ county_unitary       : chr "Middlesbrough"
##   ..$ county_unitary_type  : chr "UnitaryAuthority"
##   ..$ district_borough     : NULL
##   ..$ district_borough_type: NULL
##   ..$ region               : chr "North East"
##   ..$ country              : chr "England"
##   ..$ longitude            : num -1.21
##   ..$ latitude             : num 54.6
##   ..$ eastings             : int 451459
##   ..$ northings            : int 518602
##   ..$ min_eastings         : int 450839
##   ..$ min_northings        : int 517640
##   ..$ max_eastings         : int 451963
##   ..$ max_northings        : int 519361

You can also find a place using an OSGB code:

place_lookup_result <- place_lookup("osgb4000000074544700")

# overview
str(place_lookup_result)
## List of 21
##  $ code                 : chr "osgb4000000074544700"
##  $ name_1               : chr "Cutler Heights"
##  $ name_1_lang          : NULL
##  $ name_2               : NULL
##  $ name_2_lang          : NULL
##  $ local_type           : chr "Suburban Area"
##  $ outcode              : chr "BD4"
##  $ county_unitary       : NULL
##  $ county_unitary_type  : NULL
##  $ district_borough     : chr "Bradford"
##  $ district_borough_type: chr "MetropolitanDistrict"
##  $ region               : chr "Yorkshire and the Humber"
##  $ country              : chr "England"
##  $ longitude            : num -1.72
##  $ latitude             : num 53.8
##  $ eastings             : int 418830
##  $ northings            : int 431785
##  $ min_eastings         : int 418449
##  $ min_northings        : int 431455
##  $ max_eastings         : int 419076
##  $ max_northings        : int 432106

Terminated postcodes

You might end up having terminated postcodes in your data set. These are postcodes that are no longer active. UK postcodes can change so it’s worth checking whether used postcodes are still active. If you need more information about when a particular postcode was terminated use:

terminated_postcode("E1W 1UU")
##   postcode year_terminated month_terminated longitude latitude
## 1  E1W 1UU            2015                2 -0.073732 51.50801