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
131
132
133
134
135
136 | 1
15
15
1
13
13
1
15
14
11
1
2
1
1
1
13
1
13
1
9
4
3
1
3
1
2
1
2
1
2
1
15
14
1
1
28
1
13
1
23
1
13
23
13
1
126
38
1
126
1
48
| module.exports = function tokml(geojson, options) {
options = options || {
name: 'name',
description: 'description',
};
return '<?xml version="1.0" encoding="UTF-8"?>' +
tag('kml',
tag('Document',
root(geojson, options)
), [['xmlns', '//www.opengis.net/kml/2.2']]);
};
function feature(options) {
return function(_) {
return tag('Placemark',
name(_.properties, options) +
description(_.properties, options) +
geometry.any(_.geometry) +
extendeddata(_.properties));
};
}
function root(_, options) {
if (!_.type) return '';
switch (_.type) {
case 'FeatureCollection': return _.features.map(feature(options)).join('');
case 'Feature': return feature(options)(_);
default:
if (_.type in geometry) {
return feature(options)({
type: 'Feature',
geometry: _,
properties: {}
});
}
}
return '';
}
function name(_, options) {
return (_[options.name]) ? tag('name', encode(_[options.name])) : '';
}
function description(_, options) {
return (_[options.description]) ? tag('description', encode(_[options.description])) : '';
}
// ## Geometry Types
//
// https://developers.google.com/kml/documentation/kmlreference#geometry
var geometry = {
Point: function(_) {
return tag('Point', tag('coordinates', _.coordinates.join(',')));
},
LineString: function(_) {
return tag('LineString', tag('coordinates', linearring(_.coordinates)));
},
Polygon: function(_) {
var outer = _.coordinates[0],
inner = _.coordinates.slice(1),
outerRing = tag('outerBoundaryIs',
tag('LinearRing', tag('coordinates', linearring(outer)))),
innerRings = inner.map(function(i) {
return tag('innerBoundaryIs',
tag('LinearRing', tag('coordinates', linearring(i))));
}).join('');
return tag('Polygon', outerRing + innerRings);
},
MultiPoint: function(_) {
return tag('MultiGeometry', _.coordinates.map(function(c) {
return geometry.Point({ coordinates: c });
}).join(''));
},
MultiPolygon: function(_) {
return tag('MultiGeometry', _.coordinates.map(function(c) {
return geometry.Polygon({ coordinates: c });
}).join(''));
},
MultiLineString: function(_) {
return tag('MultiGeometry', _.coordinates.map(function(c) {
return geometry.LineString({ coordinates: c });
}).join(''));
},
GeometryCollection: function(_) {
return tag('MultiGeometry',
_.geometries.map(geometry.any).join(''));
},
any: function(_) {
if (geometry[_.type]) {
return geometry[_.type](_);
} else {
return '';
}
}
};
function linearring(_) {
return _.map(function(cds) { return cds.join(','); }).join(' ');
}
// ## Data
function extendeddata(_) {
return tag('ExtendedData', pairs(_).map(data).join(''));
}
function data(_) {
return tag('Data', encode(_[1]), [['name', encode(_[0])]]);
}
// ## Helpers
function pairs(_) {
var o = [];
for (var i in _) o.push([i, _[i]]);
return o;
}
function attr(_) {
return _ ? (' ' + _.map(function(a) {
return a[0] + '="' + a[1] + '"';
}).join(' ')) : '';
}
function tag(el, contents, attributes) {
return '<' + el + attr(attributes) + '>' + contents + '</' + el + '>';
}
function encode(_) {
return (_ || '').replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
|