colouring-montreal/etl
2022-04-14 09:28:34 +01:00
..
join_building_data Add debug, no overwrite flags 2020-06-16 16:16:46 +01:00
screenshot add OS TOID download info 2022-04-14 09:28:34 +01:00
__init__.py add test for filtering mastermap (not working) 2022-03-18 14:34:06 +00:00
convert_opentoid_bng_latlon.py remove index not needed 2022-04-14 09:28:34 +01:00
create_building_records.sh Update etl to load UPRNs to table 2018-10-02 21:12:46 +01:00
drop_outside_limit.sh Skip altering foreign key restrictions 2018-10-04 19:00:56 +01:00
extract_mastermap.sh add echos to extraction step 2022-03-29 14:46:18 +01:00
filter_mastermap.py remove print statement 2022-03-29 14:37:42 +01:00
filter_transform_mastermap_for_loading.sh fix filtering script 2022-03-29 15:41:00 +01:00
get_test_polygons.py flake8 2022-03-18 16:18:44 +00:00
load_coordinates.sh dont delete open_toid table 2022-04-14 09:28:34 +01:00
load_geometries.sh alt way to load geometries (untested) 2022-04-14 09:28:34 +01:00
load_postcodes.sh Set postcode zoom and class 2019-02-11 09:07:26 +00:00
README.md add hypothetical next scripts 2022-04-14 09:28:34 +01:00
requirements.txt update instructions for python 2022-04-14 09:28:34 +01:00
run_all.sh remove addressbase from all steps and reorder readme 2022-03-18 11:07:15 +00:00
run_clean.sh remove addressbase from all steps and reorder readme 2022-03-18 11:07:15 +00:00
update_building_records.sh update pseudocode 2022-04-14 09:28:34 +01:00

Extract, transform and load

The scripts in this directory are used to extract, transform and load (ETL) the core datasets for Colouring London. This README acts as a guide for setting up the Colouring London database with these datasets and updating it.

Contents

⬇️ Downloading Ordnance Survey data

The building geometries are sourced from Ordnance Survey (OS) MasterMap (Topography Layer). We also make use of OS Open TOID data which proves access to a generalised location for those geometries.

Downloading MasterMap data

  1. Sign up for the Ordnance Survey Data Exploration License. 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:

  1. 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.
  2. 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.

Downloading OS Open TOID data

  1. Navigate to the download page at https://osdatahub.os.uk/downloads/open/OpenTOID
  2. Select the area of the map you require location data for (e.g. the squares covering London) and download the data in CSV format:

  1. Unzip the .zip file(s) to get the CSV files and move them to a single folder in a convenient location. We will use this folder in later steps.

🐧 Making data available to Ubuntu

Before creating or updating a Colouring London database, you'll need to make sure the downloaded OS files are available to the Ubuntu machine where the database is hosted. If you are using Virtualbox, you could host share folder(s) containing the OS files with the VM (e.g. see these instructions for Mac).

🌑 Creating a Colouring London database from scratch

Prerequisites

You should already have set up PostgreSQL and created a database in an Ubuntu environment. Make sure to create environment variables to use psql if you haven't already:

export PGPASSWORD=<pgpassword>
export PGUSER=<username>
export PGHOST=localhost
export PGDATABASE=<colouringlondondb>

Create the core database tables:

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.

You should already have installed GNU parallel, which is used to speed up loading bulk data.

Processing and loading Ordnance Survey data

Move into the etl directory and set execute permission on all scripts.

cd ~/colouring-london/etl
chmod +x *.sh

Extract the MasterMap data (this step could take a while).

sudo ./extract_mastermap.sh /path/to/mastermap_dir

Filter MasterMap 'building' polygons.

sudo ./filter_transform_mastermap_for_loading.sh /path/to/mastermap_dir

Load all building outlines. Note: you should ensure that mastermap_dir has permissions that will allow the linux find command to work without using sudo.

./load_geometries.sh /path/to/mastermap_dir

Index geometries.

psql < ../migrations/002.index-geometries.up.sql

Create a building record per outline.

./create_building_records.sh

Run the remaining migrations in ../migrations to create the rest of the database structure.

ls ~/colouring-london/migrations/*.up.sql 2>/dev/null | while read -r migration; do psql < $migration; done;

🌕 Updating the Colouring London database with new OS data

In the Ubuntu environment where the database exists, set up environment variable to make the following steps simpler.

export PGPASSWORD=<pgpassword>
export PGUSER=<username>
export PGHOST=localhost
export PGDATABASE=<colouringlondondb>

Move into the etl directory and set execute permission on all scripts.

cd ~/colouring-london/etl
chmod +x *.sh

Extract the new MasterMap data (this step could take a while).

sudo ./extract_mastermap.sh /path/to/mastermap_dir

Filter MasterMap 'building' polygons.

sudo ./filter_transform_mastermap_for_loading.sh /path/to/mastermap_dir

Load all new building outlines. This step will only add geometries that are not already present (based on the TOID). Note: you should ensure that mastermap_dir has permissions that will allow the linux find command to work without using sudo.

./load_geometries.sh /path/to/mastermap_dir

Create a virtual environment for python in the etl folder of your repository. In the following example we have name the virtual environment colouringlondon but it can have any name.

pyvenv colouringlondon

Activate the virtual environment so we can install python packages into it.

source colouringlondon/bin/activate

Install python pip package manager and related tools.

pip install --upgrade pip
pip install --upgrade setuptools wheel

Install the required python packages.

pip install -r requirements.txt

Converting the OS Open TOID data OSGB36 Eastings and Northings to WGS84 longitude and latitude coordinates.

WARNING EC note: When testing this in my developemt setup, due to memory constraints I actually ended up running this Python step on my Mac directly, rather than in my Ubuntu Virtualbox environment.

python convert_opentoid_bng_latlon.py /path/to/opentoids_dir

Assign latitude and longitude to buildings with the converted OS Open TOID data.

./load_coordinates.sh /path/to/opentoids_dir

Create building record to match each new geometry (TOID) that doesn't already have a linked building. Ignores geometries in the same location as pre-existing ones based on lat/lon coordinates, in which case marking the old one as valid up to the current date and adding the new one as valid from today, keeping both linked to the existing building record.

./update_building_records.sh 

Mark TOIDs not present in the update as demolished.

./mark_demolitions.sh