Skip to content

Commit

Permalink
Ran build to update the tokml.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsels committed Jan 28, 2016
1 parent af620c9 commit bd98c75
Showing 1 changed file with 107 additions and 37 deletions.
144 changes: 107 additions & 37 deletions tokml.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,35 @@ module.exports = function tokml(geojson, options) {
), [['xmlns', 'http://www.opengis.net/kml/2.2']]);
};

function feature(options) {
function feature(options, styleHashesArray) {
return function(_) {
if (!_.properties || !geometry.valid(_.geometry)) return '';
var geometryString = geometry.any(_.geometry);
if (!geometryString) return '';

var styleDefinition = '',
styleReference = '';
if (options.simplestyle) {
if (hasMarkerStyle(_.properties)) {
styleDefinition = iconStyle(_.properties);
styleReference = tag('styleUrl', '#' + iconHash(_.properties));
} else if (hasPolyStyle(_.properties)) {
styleDefinition = shapeStyle(_.properties);
styleReference = tag('styleUrl', '#' + iconHash(_.properties));
var styleHash = hashStyle(_.properties);
if (styleHash) {
if (geometry.isPoint(_.geometry) && hasMarkerStyle(_.properties)) {
if (styleHashesArray.indexOf(styleHash) === -1) {
styleDefinition = markerStyle(_.properties, styleHash);
styleHashesArray.push(styleHash);
}
styleReference = tag('styleUrl', '#' + styleHash);
} else if ((geometry.isPolygon(_.geometry) || geometry.isLine(_.geometry)) &&
hasPolygonAndLineStyle(_.properties)) {
if (styleHashesArray.indexOf(styleHash) === -1) {
styleDefinition = polygonAndLineStyle(_.properties, styleHash);
styleHashesArray.push(styleHash);
}
styleReference = tag('styleUrl', '#' + styleHash);
}
// Note that style of GeometryCollection / MultiGeometry is not supported
}
}
if (!_.properties || !geometry.valid(_.geometry)) return '';
var geometryString = geometry.any(_.geometry);
if (!geometryString) return '';

return styleDefinition + tag('Placemark',
name(_.properties, options) +
description(_.properties, options) +
Expand All @@ -51,14 +64,16 @@ function feature(options) {

function root(_, options) {
if (!_.type) return '';
var styleHashesArray = [];

switch (_.type) {
case 'FeatureCollection':
if (!_.features) return '';
return _.features.map(feature(options)).join('');
return _.features.map(feature(options, styleHashesArray)).join('');
case 'Feature':
return feature(options)(_);
return feature(options, styleHashesArray)(_);
default:
return feature(options)({
return feature(options, styleHashesArray)({
type: 'Feature',
geometry: _,
properties: {}
Expand Down Expand Up @@ -132,14 +147,26 @@ var geometry = {
},
valid: function(_) {
return _ && _.type && (_.coordinates ||
_.type === 'GeometryCollection' && _.geometries.every(geometry.valid));
_.type === 'GeometryCollection' && _.geometries && _.geometries.every(geometry.valid));
},
any: function(_) {
if (geometry[_.type]) {
return geometry[_.type](_);
} else {
return '';
}
},
isPoint: function(_) {
return _.type === 'Point' ||
_.type === 'MultiPoint';
},
isPolygon: function(_) {
return _.type === 'Polygon' ||
_.type === 'MultiPolygon';
},
isLine: function(_) {
return _.type === 'LineString' ||
_.type === 'MultiLineString';
}
};

Expand All @@ -156,13 +183,17 @@ function data(_) {
return tag('Data', tag('value', encode(_[1])), [['name', encode(_[0])]]);
}

// ## Icons
function iconStyle(_) {
// ## Marker style
function hasMarkerStyle(_) {
return !!(_['marker-size'] || _['marker-symbol'] || _['marker-color']);
}

function markerStyle(_, styleHash) {
return tag('Style',
tag('IconStyle',
tag('Icon',
tag('href', iconUrl(_)))) +
iconSize(_), [['id', iconHash(_)]]);
iconSize(_), [['id', styleHash]]);
}

function iconUrl(_) {
Expand All @@ -183,11 +214,8 @@ function iconSize(_) {
]);
}

function hasMarkerStyle(_) {
return !!(_['marker-size'] || _['marker-symbol'] || _['marker-color']);
}

function hasPolyStyle(_) {
// ## Polygon and Line style
function hasPolygonAndLineStyle(_) {
for (var key in _) {
if ({
"stroke": true,
Expand All @@ -199,30 +227,72 @@ function hasPolyStyle(_) {
}
}

function iconHash(_) {
return (_['marker-symbol'] || '') +
(_['marker-color'] || '').replace('#', '') +
(_['marker-size'] || '');
function polygonAndLineStyle(_, styleHash) {
var lineStyle = tag('LineStyle', [
tag('color', hexToKmlColor(_['stroke'], _['stroke-opacity']) || 'ff555555') +
tag('width', _['stroke-width'] === undefined ? 2 : _['stroke-width'])
]);

var polyStyle = '';

if (_['fill'] || _['fill-opacity']) {
polyStyle = tag('PolyStyle', [
tag('color', hexToKmlColor(_['fill'], _['fill-opacity']) || '88555555')
]);
}

return tag('Style', lineStyle + polyStyle, [['id', styleHash]]);
}

function shapeStyle(_) {
var lineStyle = tag('LineStyle', '', [
['color', _.stroke || '555555'],
['width', _['stroke-width'] === undefined ? 2 : _['stroke-width']]
]);
var polyStyle = tag('PolyStyle', '', [
['color', _.fill || '555555']
]);
return tag('Style', lineStyle + polyStyle);
// ## Style helpers
function hashStyle(_) {
var hash = '';

if (_['marker-symbol']) hash = hash + 'ms' + _['marker-symbol'];
if (_['marker-color']) hash = hash + 'mc' + _['marker-color'].replace('#', '');
if (_['marker-size']) hash = hash + 'ms' + _['marker-size'];
if (_['stroke']) hash = hash + 's' + _['stroke'].replace('#', '');
if (_['stroke-width']) hash = hash + 'sw' + _['stroke-width'].toString().replace('.', '');
if (_['stroke-opacity']) hash = hash + 'mo' + _['stroke-opacity'].toString().replace('.', '');
if (_['fill']) hash = hash + 'f' + _['fill'].replace('#', '');
if (_['fill-opacity']) hash = hash + 'fo' + _['fill-opacity'].toString().replace('.', '');

return hash;
}

function hexToKmlColor(hexColor, opacity) {
if (typeof hexColor !== 'string') return '';

hexColor = hexColor.replace('#', '').toLowerCase();

if (hexColor.length === 3) {
hexColor = hexColor[0] + hexColor[0] +
hexColor[1] + hexColor[1] +
hexColor[2] + hexColor[2];
} else if (hexColor.length !== 6) {
return '';
}

var r = hexColor[0] + hexColor[1];
var g = hexColor[2] + hexColor[3];
var b = hexColor[4] + hexColor[5];

var o = 'ff';
if (typeof opacity === 'number' && opacity >= 0.0 && opacity <= 1.0) {
o = (opacity * 255).toString(16);
if (o.indexOf('.') > -1) o = o.substr(0, o.indexOf('.'));
if (o.length < 2) o = '0' + o;
}

return o + b + g + r;
}

// ## Helpers
// ## General helpers
function pairs(_) {
var o = [];
for (var i in _) o.push([i, _[i]]);
return o;
}

},{"strxml":2}],2:[function(require,module,exports){
module.exports.attr = attr;
module.exports.tagClose = tagClose;
Expand Down

0 comments on commit bd98c75

Please sign in to comment.