forked from mapbox/geojson-mapnikify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (93 loc) · 3.07 KB
/
index.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
var normalize = require('geojson-normalize'),
makizushi = require('makizushi'),
queue = require('queue-async'),
path = require('path'),
fs = require('fs'),
sigmund = require('sigmund'),
enforceDefaults = require('./lib/defaults.js'),
normalizeStyle = require('./lib/normalizestyle.js'),
cachepath = require('./lib/cachepath.js'),
loadURL = require('./lib/urlmarker.js');
var template = fs.readFileSync(__dirname + '/lib/template.xml', 'utf8');
module.exports = generateXML;
function generateXML(data, retina, callback) {
var gj = normalize(data),
q = queue(1);
if (!gj) return callback(new Error('invalid GeoJSON'));
for (var i = 0; i < gj.features.length; i++) {
gj.features[i] = !markerURL(gj.features[i]) ? enforceDefaults(normalizeStyle(gj.features[i])) : normalizeStyle(gj.features[i]);
}
gj.features.filter(isPoint).forEach(function(feat, i) {
if (markerURL(feat)) {
q.defer(getRemote, feat, retina);
} else {
q.defer(getMarker, feat, retina);
}
});
q.awaitAll(done);
function done(err, ls) {
if (err) return callback(err);
return callback(null,
template.replace('{{geojson}}', JSON.stringify(gj)));
}
}
function getRemote(feature, retina, callback) {
var path = cachepath(markerURL(feature) + feature.properties['marker-color']) + '.png';
var written = function(err) {
if (err) return callback(err);
feature.properties['marker-path'] = path;
callback(null, path);
};
fs.exists(path, function(exists) {
if (exists) {
return written(null);
} else {
loadURL(feature, function urlLoaded(err, data) {
if (err) return callback(err);
fs.writeFile(path, data, written);
});
}
});
}
function getMarker(feature, retina, callback) {
var fp = feature.properties || {},
size = fp['marker-size'][0],
symbol = fp['marker-symbol'] ? fp['marker-symbol'] : '',
color = fp['marker-color'].replace('#', '');
var options = {
tint: color,
base: 'pin',
symbol: symbol,
retina: retina,
size: size
};
var path = cachepath(JSON.stringify(options)) + '.png';
fs.exists(path, function(exists) {
if (exists) {
feature.properties['marker-path'] = path;
return callback(null, path);
} else {
makizushi(options, rendered);
}
});
function rendered(err, data) {
if (err) return callback(err);
fs.writeFile(path, data, written);
}
function written(err) {
if (err) {
return callback(err);
} else {
feature.properties['marker-path'] = path;
callback(null, path);
}
}
}
function isPoint(feature) {
return feature.geometry &&
(feature.geometry.type === 'Point' ||
feature.geometry.type === 'MultiPoint');
}
function markerURL(feature) {
return (feature.properties || {})['marker-url'];
}