Automating OS OpenData downloads

This guide takes you though the process of automating the download of OS OpenData downloads.

What you need

  • A basic understanding of automatic processing of JSON data and basic procedural (if-then-else) programming.

  • A text editor like Visual Studio Code

  • A working installation of Node.js and the popular axios module.

Please read the download support documentation before working with data packages.

Downloading OS OpenData

These are generic instructions intended to broadly outline the necessary steps. Please see Sample implementation using Node.js below for one possible implementation.

  1. Call the /products endpoint to obtain a list of products and their associated IDs.

    • This is similar to a manual download in that you first need to discover what is available. Be mindful that list items can change.

  2. Interrogate the JSON response to identify the dataset you are interested in.

    • Each entry in the JSON response provides the Name, Description, Release Version, Download URL and Product ID.

    • You may at this point wish to create a loop that iterates through all available datasets or a subset that you are interested in based on common attributes (for example, vector data in a certain format).

    • You could store the product id and version. That way you can easily check periodically for new versions of the data being available, simply by comparing the version we provide with the one you hold.

  3. Using the product id from the previous response make a call to the /products/{productId}/downloads endpoint.

    • This will result in a list of all available download links. This may be a single link for some datasets or a set of links split by area covered and/or formats supplied.

    • Once you obtain the download link for the area of the dataset in the format you require you are ready to download the data, ready for automated processing by whichever means your system requires.

Sample implementation using Node.js

This example relies on a working installation of Node.js and the popular axios module.

  1. Get the list of products available including their basic information, such as their product id and version.

    const axios = require('axios');
    
    async function getList() {
        const productList = await axios('https://api.os.uk/downloads/v1/products');
        /* For explanation and debugging purposes we display the full response from the API in the console */
        console.log(productList.data)
        for (const product of productList.data) {
            /* This will print out the product ID which can be used in another request. This would be the ideal place
               to call another function which continues the processing or filters down the results to the set required. */
            console.log(product.id)
        }
    }
    getList() 

  2. Obtain information for a specific product. While the key parts of this are already provided in step 1, this shows another way of getting to specific detail.

    const axios = require('axios');
    
    /* This function will return the specific information on OS Open Greenspace */
    async function getProductDetails() {
        const greenspaceDetails = await axios('https://api.os.uk/downloads/v1/products/OpenGreenspace');
        console.log(greenspaceDetails.data)
        /* At this point we could insert another function to process the results or act on them/download them */
    }
    getProductDetails()

  3. Download the data. In this example we hard code the product and format we are interested in (OpenGreenspace in ESRI® Shapefile). This can also be provided dynamically into the function allowing the same function to be used for multiple products and formats.

    const fs = require('fs');
    const axios = require('axios');
    
    /* ============================================================
    Function: Uses Axios to download file as stream using Promise
    ============================================================ */
    const download_file = (url, filename) =>
        axios({
            url,
            responseType: 'stream'
        }).then(
            response =>
                new Promise((resolve, reject) => {
                    response.data
                        .pipe(fs.createWriteStream(filename))
                        .on('finish', () => resolve())
                        .on('error', e => reject(e));
                }
        )
    );
    
    /* ============================================================
    Download Files in Order
    ============================================================ */
    async function downloadFiles() {
        try {
            const downloadInfo = await axios.get('https://api.os.uk/downloads/v1/products/OpenGreenspace/downloads')
            for (const download of downloadInfo.data) {
                if(download.area !== 'GB' && download.format === 'ESRI® Shapefile') {
                    let downloadFile = await download_file(download.url, `${download.area}.zip`);
                    console.log(`Downloaded file ${download.area}`)
                }
            }
            console.log('Completed downloading files')
        } catch (error) {
            console.error(error);
        }
    }
    
    downloadFiles()

Last updated