#!/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(); }); });