Comparing version
@@ -0,1 +1,5 @@ | ||
## 0.2.1 | ||
* Fix encoded output of non-string data values | ||
## 0.2.0 | ||
@@ -2,0 +6,0 @@ |
@@ -187,3 +187,3 @@ module.exports = function tokml(geojson, options) { | ||
function encode(_) { | ||
return (_ || '').replace(/&/g, '&') | ||
return (_ === null ? '' : _.toString()).replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
@@ -190,0 +190,0 @@ .replace(/>/g, '>') |
{ | ||
"name": "tokml", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "convert geojson to kml", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -69,3 +69,6 @@ [](https://travis-ci.org/mapbox/tokml) | ||
**[simplestyle-spec](https://github.com/mapbox/simplestyle-spec)** support: | ||
* `simplestyle`: set to `true` to convert simplestyle-spec styles into KML styles | ||
## Development | ||
@@ -72,0 +75,0 @@ |
@@ -61,2 +61,6 @@ var expect = require('expect.js'), | ||
}); | ||
it('non-string values', function() { | ||
expect(tokml(file('number_property.geojson'))).to.eql(output('number_property.kml')); | ||
}); | ||
}); | ||
@@ -63,0 +67,0 @@ |
54
tokml.js
@@ -10,2 +10,3 @@ (function(e){if("function"==typeof bootstrap)bootstrap("tokml",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeTokml=e}else"undefined"!=typeof window?window.tokml=e():global.tokml=e()})(function(){var define,ses,bootstrap,module,exports; | ||
description: 'description', | ||
simplestyle: false | ||
}; | ||
@@ -24,7 +25,14 @@ | ||
return function(_) { | ||
return tag('Placemark', | ||
var styleDefinition = '', | ||
styleReference = ''; | ||
if (options.simplestyle && hasStyle(_.properties)) { | ||
styleDefinition = iconstyle(_.properties); | ||
styleReference = tag('styleUrl', '#' + iconHash(_.properties)); | ||
} | ||
return styleDefinition + tag('Placemark', | ||
name(_.properties, options) + | ||
description(_.properties, options) + | ||
geometry.any(_.geometry) + | ||
extendeddata(_.properties)); | ||
extendeddata(_.properties) + | ||
styleReference); | ||
}; | ||
@@ -59,7 +67,7 @@ } | ||
function name(_, options) { | ||
return (_[options.name]) ? tag('name', encode(_[options.name])) : ''; | ||
return _[options.name] ? tag('name', encode(_[options.name])) : ''; | ||
} | ||
function description(_, options) { | ||
return (_[options.description]) ? tag('description', encode(_[options.description])) : ''; | ||
return _[options.description] ? tag('description', encode(_[options.description])) : ''; | ||
} | ||
@@ -129,2 +137,39 @@ | ||
// ## Icons | ||
function iconstyle(_) { | ||
return tag('Style', | ||
tag('IconStyle', | ||
tag('Icon', | ||
tag('href', iconUrl(_)))) + | ||
iconSize(_), [['id', iconHash(_)]]); | ||
} | ||
function iconUrl(_) { | ||
var size = _['marker-size'] || 'medium', | ||
symbol = _['marker-symbol'] ? '-' + _['marker-symbol'] : '', | ||
color = (_['marker-color'] || '7e7e7e').replace('#', ''); | ||
return 'https://api.tiles.mapbox.com/v3/marker/' + 'pin-' + size.charAt(0) + | ||
symbol + '+' + color + '.png'; | ||
} | ||
function iconSize(_) { | ||
return tag('hotSpot', '', [ | ||
['xunits', 'fraction'], | ||
['yunits', 'fraction'], | ||
['x', 0.5], | ||
['y', 0.5] | ||
]); | ||
} | ||
function hasStyle(_) { | ||
return !!(_['marker-size'] || _['marker-symbol'] || _['marker-color']); | ||
} | ||
function iconHash(_) { | ||
return (_['marker-symbol'] || '') + | ||
(_['marker-color'] || '').replace('#', '') + | ||
(_['marker-size'] || ''); | ||
} | ||
// ## Helpers | ||
@@ -147,3 +192,2 @@ function pairs(_) { | ||
function encode(_) { | ||
@@ -150,0 +194,0 @@ return (_ || '').replace(/&/g, '&') |
216821
1.07%63
3.28%948
4.64%85
3.66%