]> git.openstreetmap.org Git - nominatim.git/blob - data-sources/gb-postcodes/README.md
5e6cef2d3d6700e07ecb096348e38eb76c4bbba9
[nominatim.git] / data-sources / gb-postcodes / README.md
1 # GB Postcodes
2
3
4 The server [importing instructions](https://www.nominatim.org/release-docs/latest/admin/Import-and-Update/) allow optionally download [`gb_postcode_data.sql.gz`](https://www.nominatim.org/data/gb_postcode_data.sql.gz). This document explains how the file got created.
5
6 ## GB vs UK
7
8 GB (Great Britain) is more correct as the Ordnance Survey dataset doesn't contain postcodes from Northern Ireland.
9
10 ## Importing separately after the initial import
11
12 If you forgot to download the file, or have a new version, you can import it separately:
13
14 1. Import the downloaded `gb_postcode_data.sql.gz` file.
15
16 2. Run the SQL query `SELECT count(getorcreate_postcode_id(postcode)) FROM gb_postcode;`. This will update the search index.
17
18 3. Run `utils/setup.php --calculate-postcodes` from the build directory. This will copy data form the `gb_postcode` table to the `location_postcodes` table.
19
20
21
22 ## Converting Code-Point Open data
23
24 1. Download from [Code-Point® Open](https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html). It requires an email address where a download link will be send to.
25
26 2. `unzip codepo_gb.zip`
27
28    Unpacked you'll see a directory of CSV files.
29
30    ```
31    $ more codepo_gb/Data/CSV/n.csv
32    "N1 0AA",10,530626,183961,"E92000001","E19000003","E18000007","","E09000019","E05000368"
33    "N1 0AB",10,530559,183978,"E92000001","E19000003","E18000007","","E09000019","E05000368"
34    ```
35
36    The coordinates are "Northings" and "Eastings" in [OSGB 1936](http://epsg.io/1314) projection. They can be projected to WGS84 like this
37
38    ```
39    SELECT ST_AsText(ST_Transform(ST_SetSRID('POINT(530626 183961)'::geometry,27700), 4326));
40    POINT(-0.117872733220225 51.5394424719303)
41    ```
42    [-0.117872733220225 51.5394424719303 on OSM map](https://www.openstreetmap.org/?mlon=-0.117872733220225&mlat=51.5394424719303&zoom=16)
43
44
45
46 3. Create database, import CSV files, add geometry column, dump into file
47
48    ```
49    DBNAME=create_gb_postcode_file
50    
51    createdb $DBNAME
52    echo 'CREATE EXTENSION postgis' | psql $DBNAME
53    
54    cat data/gb_postcode_table.sql | psql $DBNAME
55    
56    cat codepo_gb/Data/CSV/*.csv | ./data-sources/gb-postcodes/convert_codepoint.php | psql $DBNAME
57    
58    cat codepo_gb/Doc/licence.txt | iconv -f iso-8859-1 -t utf-8 | dos2unix | sed 's/^/-- /g' > gb_postcode_data.sql
59    pg_dump -a -t gb_postcode $DBNAME | grep -v '^--' >> gb_postcode_data.sql
60    
61    gzip -9 -f gb_postcode_data.sql
62    ls -lah gb_postcode_data.*
63    
64    # dropdb $DBNAME
65    ```