aboutsummaryrefslogtreecommitdiff
path: root/process.js
blob: 4054106a06f86091673d53c3291b64936609f288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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();
    });
});