-
Notifications
You must be signed in to change notification settings - Fork 5
/
split-layers.js
executable file
·130 lines (119 loc) · 3.07 KB
/
split-layers.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
module.exports = {
splitLayers : function(xmlData, outputDir, outputPrefix, callback) {
splitLayers2(xmlData, outputDir, outputPrefix, callback);
}
};
var tilelive = require('tilelive');
var path = require('path');
var fs = require("fs");
var zlib = require("zlib");
var xml2js = require('xml2js');
require('tilelive-bridge').registerProtocols(tilelive);
var XMLWriter = require('xml-writer');
if (require.main == module) {
main();
}
function main() {
var args = process.argv;
args.splice(0, 2); // Remove 'node' and the name of the script
if (args.length < 1) {
console.error('Usage: tile-layer-conv.js <Mapnik xml file>');
process.exit(1);
}
var mapnikFile = path.resolve(__dirname, args[0]);
var outputPrefix = path.parse(mapnikFile).name;
fs.readFile(mapnikFile, 'utf8', function(err, data) {
splitLayers2(data, 'out/', outputPrefix, function() {
process.exit(0);
});
});
}
function splitLayers2(mapnikXmlData, outputDir, outputPrefix, callback) {
xml2js.parseString(mapnikXmlData, function(err, json) {
output = [];
json.Map.Layer.forEach(function(layer) {
var layerName = layer.$.name;
var layerXml = createLayerXml(json, layer);
output.push({
name : layerName,
xml : layerXml
});
});
var counter = output.length;
output.forEach(function(layer) {
var outputFile = path.resolve(__dirname, outputDir + outputPrefix + '-' + layer.name + '.xml');
fs.writeFile(outputFile, layer.xml, 'utf8', function() {
counter--;
if (counter == 0) {
callback();
}
});
});
});
}
function createLayerXml(fullJson, layerJson) {
var xml = new XMLWriter(true);
xml.startDocument();
xml.startElement('Map');
forEachAttribute(fullJson.Map.$, function(key, value) {
xml.writeAttribute(key, value);
});
// Map parameters
fullJson.Map.Parameters.forEach(function(param) {
json2xml(xml, param, 'Parameters');
});
// The layer itself
json2xml(xml, layerJson, 'Layer');
xml.endDocument();
return xml.toString();
}
function findLayer(layerName, layers) {
var foundLayer = null;
layers.some(function(layer) {
if (layer.$.name == layerName) {
foundLayer = layer;
return true;
}
});
if (foundLayer == null) {
throw new Error('Could not find layer: ' + layerName);
}
return foundLayer;
}
function json2xml(writer, object, rootName) {
// Simple object
if (typeof object == 'string') {
writer.writeElement(rootName, object);
return;
}
// Json object
writer.startElement(rootName);
if (object.hasOwnProperty('$')) {
forEachAttribute(object.$, function(key, value) {
writer.writeAttribute(key, value);
});
}
if (object.hasOwnProperty('_')) {
writer.text(object._);
}
forEachAttribute(object, function(key, value) {
if (key != '_' && key != '$') {
if (value.constructor === Array) {
value.forEach(function(repeatedVal) {
json2xml(writer, repeatedVal, key);
});
} else {
json2xml(writer, value, key);
}
}
});
writer.endElement();
}
function forEachAttribute(object, callback) {
for ( var key in object) {
if (object.hasOwnProperty(key)) {
callback(key, object[key]);
}
}
}