Skip to content

Commit 257136d

Browse files
authored
Merge pull request #5163 from alphagov/update-postcode-documentation
Update documentation about incorrect postcode data
2 parents 33cf8eb + 787d105 commit 257136d

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

source/manual/incorrect-postcode-data.html.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,47 @@ layout: manual_layout
66
section: Applications
77
---
88

9-
We get our postcode data from Locations API, which uses OS Places API (Ordnance Survey) under the hood. Locations API postcodes [keep themselves up to date automatically](https://github.com/alphagov/locations-api/blob/main/docs/postcodes-added-cached-updated.md#how-postcodes-are-updated) and should never be more than about a week out of date.
9+
We get our postcode data from Locations API, which uses OS Places API (Ordnance Survey) and the ONS Postcode Directory (Office for National Statistics) under the hood:
10+
11+
- OS Places Api: This is a detailed dataset, including individual addresses, local custodian codes (ie mapping to councils), and supports split postcodes. These records [keep themselves up to date automatically](https://github.com/alphagov/locations-api/blob/main/docs/postcodes-added-cached-updated.md#how-postcodes-are-updated) and should never be more than about a week out of date
12+
- ONS Postcode Directory: This is a less detailed data set, which cannot map to a council (it only has a lat/long for postcodes), but also includes retired postcodes and large user postcodes. These records are [downloaded by an automated task](https://github.com/alphagov/locations-api/blob/main/docs/updating-ons-postcode-data.md) when an update is spotted in their RSS feed. It's refreshed roughly 2-3 times a year.
1013

1114
In case the data is incorrect, a first step would be checking the Locations API result with the OS Places API result.
1215

1316
```shell
14-
gds govuk connect --environment integration app-console locations-api
17+
kubectl -n apps exec -it deploy/locations-api -- rails c
1518
```
1619

1720
```ruby
1821
# The OS Places API response
19-
token_manager = OsPlacesApi::AccessTokenManager.new
20-
response = HTTParty.get(
21-
"https://api.os.uk/search/places/v1/postcode",
22-
{
23-
query: { postcode: "E18QS", output_srs: "WGS84", "dataset": "DPA,LPI" },
24-
headers: { "Authorization": "Bearer #{token_manager.access_token}" },
25-
}
22+
client = OsPlacesApi::Client.new(OsPlacesApi::AccessTokenManager.new)
23+
client.retrieve_locations_for_postcode("E18QS").results
2624

2725
# The information we currently have in Locations API
2826
Postcode.find_by(postcode: "E18QS").results
2927
```
3028

31-
If the results are not the same (especially the Local Custodian Code), then either the user happens to have looked up the postcode in the exact week where it has been updated by OS Places API but we haven't updated our cache yet, or - more likely - our mechanism for self-updating postcodes has broken, and requires further investigation.
29+
### If OS Places API returns results
30+
31+
If the results are not the same (especially the Local Custodian Codes in the addresses), then either the user happens to have looked up the postcode in the exact week where it has been updated by OS Places API but we haven't updated our cache yet, or - more likely - our mechanism for self-updating postcodes has broken, and requires further investigation.
3232

3333
This is how you can manually force Locations API to update its cache for a given postcode:
3434

3535
```ruby
36-
token_manager = OsPlacesApi::AccessTokenManager.new
37-
OsPlacesApi::Client.new(token_manager).update_postcode("E18QS")
36+
PostcodeManager.new.update_postcode("E18QS")
3837

3938
# check if the record was updated
4039
Postcode.find_by(postcode: "E18QS").updated_at
4140
```
41+
42+
### If OS Places API doesn't results, but there is a record
43+
44+
This is likely a case in which the returned results are from the ONS Postcode Directory. This is a lower-quality set of data for our purposes (it doesn't contain Local Custodian Codes, so can't be used for postcode matchers that need to find a council). You can check if the postcode is indeed an ONSPD record, and if so if it's retired or a Large User Postcode like this:
45+
46+
```ruby
47+
Postcode.find_by(postcode: "E18QS").onspd?
48+
Postcode.find_by(postcode: "E18QS").retired?
49+
Postcode.find_by(postcode: "E18QS").large_user_postcode?
50+
```
51+
52+
If a postcode is a Large User Postcode, it can't be used for council-type finders, and currently retired postcodes aren't detected.

0 commit comments

Comments
 (0)