Last active
August 29, 2015 14:05
-
-
Save MadeByPi/e4ca09d123ef25888427 to your computer and use it in GitHub Desktop.
Process the OS code-point-open data and generate JSON containing Lat/Lon coordinates for every UK postcode. An example use of MadeByPi/cartography-coordinate-tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
// https://github.com/MadeByPi/cartography-coordinate-tools | |
import cartography.CoordTransform; | |
import cartography.LatLon; | |
import cartography.OSGridRef; | |
import neko.Lib; | |
import sys.io.File; | |
import sys.FileSystem; | |
using StringTools; | |
/** | |
* @author Mike Almond - https://github.com/mikedotalmond - https://github.com/MadeByPi | |
* | |
* Process the OS code-point-open data and generate JSON containing Lat/Lon coordinates for every UK postcode. | |
* OS data provides positional data for each point in the OS Grid format (Easting,Northing), but lat-lon cooridnates are generally more useful in mapping applications. | |
* | |
* Uses cartography-tools - https://github.com/MadeByPi/cartography-coordinate-tools | |
* With data from - http://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html | |
*/ | |
class Main { | |
static function main() { | |
/** | |
* You'll need to download + extract the open dataset of all UK postcode-points -- a zip containing CSV files for regions | |
* http://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html | |
*/ | |
var dataPath = 'codepoints_gb/Data/CSV/'; | |
var files = FileSystem.readDirectory(dataPath); | |
trace('Processing ${files.length} codepoint csv files...'); | |
var outpath = 'processed'; | |
if (!FileSystem.exists(outpath)) FileSystem.createDirectory(outpath); | |
for (file in files) { | |
if (file.toLowerCase().endsWith('csv')) { | |
var fileName = file.substr(0, file.length - 4); | |
var output = processFile('$dataPath/$file'); | |
Lib.println('Saving as JSON to $outpath/$fileName.json'); | |
File.saveContent('$outpath/$fileName.json', output); | |
} | |
} | |
} | |
static function processFile(file:String):String { | |
var fIn = File.read(file, false); | |
var lineNum = 0; | |
/** CSV headers... | |
* PC,PQ,EA,NO,CY,RH,LH,CC,DC,WC | |
* Postcode, Positional_quality_indicator, Eastings, Northings, Country_code, NHS_regional_HA_code, NHS_HA_code, Admin_county_code, Admin_district_code, Admin_ward_code | |
*/ | |
var out:StringBuf = new StringBuf(); | |
out.add('['); | |
try { | |
while ( true ) { | |
var fields = fIn.readLine().split(","); | |
var gridref = new OSGridRef(Std.parseInt(fields[2]), Std.parseInt(fields[3])); | |
var latlon = OSGridRef.osGridToLatLong(gridref); | |
latlon = CoordTransform.convertOSGB36toWGS84(latlon); | |
var pcode = fields[0].substr(1,7).toUpperCase(); // remove "" around the post code | |
if (lineNum > 0) out.add(','); | |
out.add('{"c":"${pcode}","la":${latlon.lat},"lo":${latlon.lon}}'); | |
lineNum++; | |
} | |
} catch ( ex:haxe.io.Eof ) { } | |
fIn.close(); | |
Lib.println('$file - processed $lineNum postcodes'); | |
out.add(']'); | |
return out.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment