From 3b7d6c8ea5a0a76904c9c5f738e3047043a20ae0 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 7 Aug 2014 19:21:35 +0100 Subject: Change format from YAML to OSM XML The yaml file was hard to edit, using OSM XML allows the use of JOSM as an editor. --- process.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 process.js (limited to 'process.js') diff --git a/process.js b/process.js new file mode 100755 index 0000000..4054106 --- /dev/null +++ b/process.js @@ -0,0 +1,66 @@ +#!/usr/bin/env nodejs + +fs = require('fs') +osmtogeojson = require('osmtogeojson'); + +DOMParser = require('xmldom').DOMParser; + +if (!String.prototype.endsWith) { + Object.defineProperty(String.prototype, 'endsWith', { + value: function (searchString, position) { + var subjectString = this.toString(); + if (position === undefined || position > subjectString.length) { + position = subjectString.length; + } + position -= searchString.length; + var lastIndex = subjectString.indexOf(searchString, position); + return lastIndex !== -1 && lastIndex === position; + } + }); +} + +fs.readdir('.', function(err, files) { + var features = []; + + files.forEach(function(name) { + if (name.endsWith(".osm")) { + buildingId = name.slice(0, -4); + + data = fs.readFileSync(name, 'utf8'); + + var doc = new DOMParser().parseFromString(data); + + var geojson = osmtogeojson(doc, { + flatProperties: true + }); + + geojson.features.forEach(function(feature) { + delete feature.id + delete feature.properties.id + + features.push(feature); + }); + } + }); + + var byURI = {}; + + features.forEach(function(feature) { + if (feature.properties.uri in byURI) { + console.error("duplicate uri " + feature.properties.uri); + } else { + byURI[feature.properties.uri] = feature; + } + }); + + featureCollection = { + type: "FeatureCollection", + features: features + }; + + var stream = fs.createWriteStream("./data.json"); + stream.once('open', function(fd) { + stream.write(JSON.stringify(featureCollection, null, 4)); + stream.end(); + }); +}); -- cgit v1.2.3