colouring-montreal/etl/README.md

131 lines
4.4 KiB
Markdown
Raw Normal View History

2022-03-09 06:48:45 -05:00
# Creating a Colouring London database from scratch
2022-03-09 08:24:27 -05:00
## Data downloading
2018-09-25 15:46:16 -04:00
The scripts in this directory are used to extract, transform and load (ETL) the core datasets
for Colouring London:
2022-03-17 10:47:08 -04:00
Building geometries, sourced from Ordnance Survey (OS) MasterMap (Topography Layer)
<!-- 1. Unique Property Reference Numbers (UPRNs), sourced from Ordnance Survey AddressBase -->
2018-09-25 15:46:16 -04:00
2022-03-09 08:24:27 -05:00
To get the required datasets, you'll need to complete the following steps:
1. Sign up for the Ordnance Survey [Data Exploration License](https://www.ordnancesurvey.co.uk/business-government/licensing-agreements/data-exploration-sign-up). You should receive an e-mail with a link to log in to the platform (this could take up to a week).
2. Navigate to https://orders.ordnancesurvey.co.uk/orders and click the button for: ✏️ Order. From here you should be able to click another button to add a product.
3. Drop a rectangle or Polygon over London and make the following selections, clicking the "Add to basket" button for each:
![](screenshot/MasterMap.png)
<p></p>
2022-03-17 10:47:08 -04:00
<!-- ![](screenshot/AddressBase.png) -->
2022-03-09 08:24:27 -05:00
2022-03-10 06:46:34 -05:00
4. You should be then able to check out your basket and download the files. Note: there may be multiple `.zip` files to download for MasterMap due to the size of the dataset.
2022-03-17 10:47:08 -04:00
<!-- 5. Unzip the AddressBase `.zip` in a convenient location. We will use the unzipped folder in later steps. Rename the folder as appropriate (make sure this folder doesn't contain the original `.zip` file). Note: this folder also contains `.zip` files, do not unzip at this stage as a script will do this later. -->
2022-03-10 07:01:41 -05:00
6. Unzip the MasterMap `.zip` files and move all the `.gz` files from each to a single folder in a convenient location. We will use this folder in later steps.
2022-03-09 08:24:27 -05:00
2018-09-25 15:46:16 -04:00
## Prerequisites
2022-03-09 06:48:45 -05:00
You should already have set up PostgreSQL and created a database. Make sure to create environment variables to use `psql` if you haven't already:
2022-03-09 06:48:45 -05:00
```bash
export PGPASSWORD=<pgpassword>
export PGUSER=<username>
export PGHOST=localhost
export PGDATABASE=<colouringlondondb>
```
Create the core database tables:
```bash
2022-03-09 08:33:26 -05:00
cd ~/colouring-london
psql < migrations/001.core.up.sql
```
There is some performance benefit to creating indexes after bulk loading data.
Otherwise, it's fine to run all the migrations at this point and skip the index
creation steps below.
Install GNU parallel, this is used to speed up loading bulk data.
2022-03-09 08:38:57 -05:00
## Make data available to Ubuntu
2022-03-17 11:02:51 -04:00
If you didn't download the OS files to the Ubuntu machine where you are setting up your Colouring London application, you will need to make them available there. If you are using Virtualbox, you could host share a the folder containing the MasteerMap files with the VM via a shared folder (e.g. [see these instructions for Mac](https://medium.com/macoclock/share-folder-between-macos-and-ubuntu-4ce84fb5c1ad)).
2022-03-10 04:35:04 -05:00
## Process and load Ordnance Survey data
2018-09-25 15:46:16 -04:00
2022-03-11 08:28:23 -05:00
Move into the `etl` directory and set execute permission on all scripts.
2018-09-25 15:46:16 -04:00
2018-09-29 13:29:57 -04:00
```bash
2022-03-10 04:53:12 -05:00
cd ~/colouring-london/etl
2022-03-11 08:28:23 -05:00
chmod +x *.sh
2022-03-10 05:39:06 -05:00
```
2022-03-11 06:46:00 -05:00
<!-- Extract the addressBase dataset.
2022-03-10 05:39:06 -05:00
```bash
2022-03-11 08:28:23 -05:00
./extract_addressbase.sh ./addressbase_dir
2022-03-11 06:46:00 -05:00
``` -->
2022-03-10 05:39:06 -05:00
2022-03-10 07:02:26 -05:00
<!-- ERROR 1: Couldn't fetch requested layer 'BasicLandPropertyUnit'! -->
2022-03-10 05:39:06 -05:00
Extract the MasterMap data (this step could take a while).
```bash
2022-03-11 08:28:23 -05:00
./extract_mastermap.sh ./mastermap_dir
2022-03-10 05:39:06 -05:00
```
2022-03-10 08:40:30 -05:00
<!-- Didn't throw an error - did Ctrl-C after an hour -->
Ensure you have the `colouringlondon` environment activated.
```bash
source colouringlondon/bin/activate
```
2022-03-10 05:39:06 -05:00
Filter MasterMap 'building' polygons and any others referenced by addressbase.
```bash
2022-03-11 08:28:23 -05:00
./filter_transform_mastermap_for_loading.sh ./mastermap_dir
2022-03-10 05:39:06 -05:00
```
Load all building outlines.
2022-03-10 09:02:00 -05:00
<!-- I had to edit the below file to set the psql vars before running -->
2022-03-10 05:39:06 -05:00
```bash
2022-03-11 08:28:23 -05:00
./load_geometries.sh ./mastermap_dir
2022-03-10 05:39:06 -05:00
```
Index geometries.
```bash
2022-03-10 05:44:31 -05:00
psql < ../migrations/002.index-geometries.up.sql
2022-03-10 05:39:06 -05:00
```
Create a building record per outline.
2022-03-10 09:02:00 -05:00
<!-- I had to edit the below file to set the psql vars before running -->
2022-03-10 05:39:06 -05:00
```bash
2022-03-11 08:28:23 -05:00
./create_building_records.sh
2022-03-10 05:39:06 -05:00
```
2022-03-10 09:02:00 -05:00
<!-- Insert 0.... -->
2022-03-10 08:40:30 -05:00
Add UPRNs where they match.
2022-03-10 05:39:06 -05:00
2022-03-10 09:02:00 -05:00
<!-- I had to edit the below file to set the psql vars before running -->
2022-03-10 05:39:06 -05:00
```bash
2022-03-11 08:28:23 -05:00
./load_uprns.sh ./addressbase_dir
2022-03-10 05:39:06 -05:00
````
2022-03-10 05:44:31 -05:00
Run the remaining migrations in `../migrations` to create the rest of the database structure.
2022-03-10 05:39:06 -05:00
```bash
2022-03-10 05:44:31 -05:00
ls ~/colouring-london/migrations/*.up.sql 2>/dev/null | while read -r migration; do psql < $migration; done;
2018-09-29 13:29:57 -04:00
```
2022-03-10 05:06:44 -05:00
# [WIP] Updating the Colouring London database with new OS data
TODO: this section should instruct how to update and existing db