Skip to content

Commit ba6927c

Browse files
committed
Support everything, tests
1 parent 5c54581 commit ba6927c

20 files changed

+283
-44
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tokml.js:
2+
browserify -s tokml index.js > tokml.js

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,38 @@
44

55
Convert [GeoJSON](http://geojson.org/) to [KML](https://developers.google.com/kml/documentation/).
66

7-
## usage
7+
## Usage
88

99
npm install --save tokml
1010

11-
## api
11+
as a binary:
12+
13+
npm install -g tokml
14+
tokml file.geojson > file.kml
15+
tokml < file.geojson > file.kml
16+
17+
## Example
1218

1319
```js
1420
var kml = tokml(geojsonObject);
1521
```
22+
23+
## API
24+
25+
### `tokml(geojsonObject)`
26+
27+
Given [GeoJSON](http://geojson.org/) data as an object, return KML data as a
28+
string of XML.
29+
30+
## Development
31+
32+
Requires [node.js](http://nodejs.org/) and [browserify](https://github.com/substack/node-browserify):
33+
34+
To build `tokml.js`:
35+
36+
make
37+
38+
To run tests:
39+
40+
npm install
41+
npm test

index.js

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
1-
module.exports = tokml;
2-
3-
var header = '<?xml version="1.0" encoding="UTF-8"?>' +
4-
'<kml xmlns="http://www.opengis.net/kml/2.2">',
5-
footer = '</kml>';
6-
1+
module.exports = function tokml(geojson) {
2+
return '<?xml version="1.0" encoding="UTF-8"?>' +
3+
tag('kml',
4+
tag('Document',
5+
geojson.features.map(feature).join('')
6+
), [['xmlns', 'http://www.opengis.net/kml/2.2']]);
7+
};
78

89
// ## Geometry Types
910
//
1011
// https://developers.google.com/kml/documentation/kmlreference#geometry
11-
function point(_) {
12-
return tag('Point', tag('coordinates', _.coordinates.join(',')));
13-
}
14-
15-
function linestring(_) {
16-
return tag('LineString', tag('coordinates', linearring(_.coordinates)));
17-
}
12+
var geometry = {
13+
Point: function(_) {
14+
return tag('Point', tag('coordinates', _.coordinates.join(',')));
15+
},
16+
LineString: function(_) {
17+
return tag('LineString', tag('coordinates', linearring(_.coordinates)));
18+
},
19+
Polygon: function(_) {
20+
var outer = _.coordinates[0],
21+
inner = _.coordinates.slice(1),
22+
outerRing = tag('outerBoundaryIs',
23+
tag('LinearRing', tag('coordinates', linearring(outer)))),
24+
innerRings = inner.map(function(i) {
25+
return tag('innerBoundaryIs',
26+
tag('LinearRing', tag('coordinates', linearring(i))));
27+
}).join('');
28+
return tag('Polygon', outerRing + innerRings);
29+
},
30+
MultiPoint: function(_) {
31+
return tag('MultiGeometry', _.coordinates.map(function(c) {
32+
return geometry.Point({ coordinates: c });
33+
}).join(''));
34+
},
35+
MultiPolygon: function(_) {
36+
return tag('MultiGeometry', _.coordinates.map(function(c) {
37+
return geometry.Polygon({ coordinates: c });
38+
}).join(''));
39+
},
40+
MultiLineString: function(_) {
41+
return tag('MultiGeometry', _.coordinates.map(function(c) {
42+
return geometry.LineString({ coordinates: c });
43+
}).join(''));
44+
},
45+
GeometryCollection: function(_) {
46+
return tag('MultiGeometry',
47+
_.geometries.map(geometry.any).join(''));
48+
},
49+
any: function(_) {
50+
if (geometry[_.type]) {
51+
return geometry[_.type](_);
52+
} else { }
53+
}
54+
};
1855

1956
function linearring(_) {
2057
return _.map(function(cds) { return cds.join(','); }).join(' ');
2158
}
2259

23-
function polygon(_) {
24-
return tag('Polygon',
25-
tag('outerBoundaryIs',
26-
tag('LinearRing',
27-
tag('coordinates', linearring(_.coordinates[0])))));
28-
}
29-
3060
// ## Data
3161
function extendeddata(_) {
3262
return tag('ExtendedData', pairs(_).map(data).join(''));
@@ -36,25 +66,12 @@ function data(_) {
3666
return tag('Data', _[1], [['name', _[0]]]);
3767
}
3868

39-
var geometry = {
40-
Point: point,
41-
LineString: linestring,
42-
Polygon: polygon
43-
};
44-
4569
function feature(_) {
4670
return tag('Placemark',
47-
geometry[_.geometry.type](_.geometry) +
71+
geometry.any(_.geometry) +
4872
extendeddata(_.properties));
4973
}
5074

51-
// # tokml
52-
function tokml(geojson) {
53-
return header +
54-
geojson.features.map(feature).join('') +
55-
footer;
56-
}
57-
5875
// ## Helpers
5976
function pairs(_) {
6077
var o = [];

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
"license": "BSD-2-Clause",
2020
"devDependencies": {
2121
"expect.js": "~0.2.0",
22-
"mocha": "~1.13.0"
22+
"mocha": "~1.13.0",
23+
"glob": "~3.2.6"
2324
},
2425
"dependencies": {
25-
"minimist": "0.0.5"
26+
"minimist": "0.0.5",
27+
"concat-stream": "~1.0.1",
28+
"sharkdown": "0.0.1"
2629
}
2730
}

scripts/regenerate.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var tokml = require('../'),
2+
fs = require('fs'),
3+
glob = require('glob');
4+
5+
glob.sync('test/data/*.geojson').forEach(function(g) {
6+
fs.writeFileSync(g.replace('.geojson', '.kml'), tokml(JSON.parse(fs.readFileSync(g))));
7+
});

test/data/featurecollection.kml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Placemark><Point><coordinates>102,0.5</coordinates></Point><ExtendedData><Data name="prop0">value0</Data></ExtendedData></Placemark><Placemark><LineString><coordinates>102,0 103,1 104,0 105,1</coordinates></LineString><ExtendedData><Data name="prop0">value0</Data><Data name="prop1">0</Data></ExtendedData></Placemark><Placemark><Polygon><outerBoundaryIs><LinearRing><coordinates>100,0 101,0 101,1 100,1 100,0</coordinates></LinearRing></outerBoundaryIs></Polygon><ExtendedData><Data name="prop0">value0</Data><Data name="prop1">val2</Data></ExtendedData></Placemark></Document></kml>

test/data/geometrycollection.geojson

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{ "type": "FeatureCollection",
2+
"features": [{
3+
"type": "Feature",
4+
"geometry": { "type": "GeometryCollection",
5+
"geometries": [
6+
{ "type": "Point",
7+
"coordinates": [100.0, 0.0]
8+
},
9+
{ "type": "LineString",
10+
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
11+
}
12+
]
13+
},
14+
"properties": {
15+
"prop0": "value0",
16+
"prop1": "val2"
17+
}
18+
}
19+
]
20+
}

test/data/geometrycollection.kml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Placemark><MultiGeometry><Point><coordinates>100,0</coordinates></Point><LineString><coordinates>101,0 102,1</coordinates></LineString></MultiGeometry><ExtendedData><Data name="prop0">value0</Data><Data name="prop1">val2</Data></ExtendedData></Placemark></Document></kml>

test/data/linestring.kml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Placemark><LineString><coordinates>100,0 101,1</coordinates></LineString><ExtendedData><Data name="prop0">value0</Data><Data name="prop1">val2</Data></ExtendedData></Placemark></kml>
1+
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Placemark><LineString><coordinates>100,0 101,1</coordinates></LineString><ExtendedData><Data name="prop0">value0</Data><Data name="prop1">val2</Data></ExtendedData></Placemark></Document></kml>

test/data/multilinestring.geojson

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ "type": "FeatureCollection",
2+
"features": [{
3+
"type": "Feature",
4+
"geometry": { "type": "MultiLineString",
5+
"coordinates": [
6+
[ [100.0, 0.0], [101.0, 1.0] ],
7+
[ [102.0, 2.0], [103.0, 3.0] ]
8+
]
9+
},
10+
"properties": {
11+
"prop0": "value0",
12+
"prop1": "val2"
13+
}
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)