A Dockerized Flask-restful application to serve postcode GeoJson from a MongoDB backend.
Endpoint:
returns:
{
"crs": {
"properties": {
"name": "urn:ogc:def:crs:EPSG::27700"
},
"type": "name"
},
"properties": {
"postcode": "EH111AT"
},
"geometry": {
"coordinates": [
324079,
672543
],
"type": "Point"
},
"type": "Feature"
}
To run the application locally you will need to have installed both Docker and Docker Compose. Compose is a tool for defining and running multi-container Docker applications. This is used to get the Python application, database and HTTP server configured and all talking to each other.
Python scripts have been written in Python 3.5.
-
Code-Point Open CSV to GeoJson
To load this data into MongoDB it must first be convert from CSV to GeoJson format.
- Download the OS Code-Point Open zipfile and extract the CSV to a sensible location on your machine.
- Place the script
mongo/csv_to_geojson.py
in the same folder as the extracted CSV files and runpython csv_to_gejson.py
. This script requires the python-geojson module installed. Once complete there should be a new folder within the CSV directory which contains a GeoJson file for each CSV that exists. e.g. tr.json.
-
Load GeoJson files to MongoDB
Now we have GeoJson representations for each postcode we are ready to load this data into MongoDB, a NoSQL database. Each file will be loaded as a seperate collection containing a document for each postcode.
- Clone this repository on your machine.
- In the file docker-compose.yaml, change the path of the mapped volume to a folder on your machine. This allows us to access and run
- From a terminal CD into the repo directory and run
docker-compose build
. This should pull down the various docker images required by the application. - Right now we are only interested in the container named
postcodesearch_db
. Check this exists withdocker ps -a
. This command lists all docker containers. - Enter the container with the command
docker exec -i -t <container name> /bin/bash
and use mongoshell to create a postcode database. - Run the script
mongo/file-loop.sh
in the same folder as the GeoJson files. The script iterates over all files with a *.json extension and loads them into a collection of the same filename.