Skip to content

Commit

Permalink
Only one style def per kml, fix hash for polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsels committed Jan 27, 2016
1 parent 7efdf3e commit a0a3d85
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions index.js
Expand Up @@ -22,22 +22,33 @@ 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 (hasMarkerStyle(_.properties)) {
if (styleHashesArray.indexOf(styleHash) === -1) {
styleDefinition = markerStyle(_.properties, styleHash);
styleHashesArray.push(styleHash);
}
styleReference = tag('styleUrl', '#' + styleHash);
} else if (hasPolygonStyle(_.properties)) {
if (styleHashesArray.indexOf(styleHash) === -1) {
styleDefinition = polygonStyle(_.properties, styleHash);
styleHashesArray.push(styleHash);
}
styleReference = tag('styleUrl', '#' + styleHash);
}
}
}
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 @@ -50,14 +61,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 @@ -155,13 +168,13 @@ function data(_) {
return tag('Data', tag('value', encode(_[1])), [['name', encode(_[0])]]);
}

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

function iconUrl(_) {
Expand All @@ -186,7 +199,7 @@ function hasMarkerStyle(_) {
return !!(_['marker-size'] || _['marker-symbol'] || _['marker-color']);
}

function hasPolyStyle(_) {
function hasPolygonStyle(_) {
for (var key in _) {
if ({
"stroke": true,
Expand All @@ -198,21 +211,26 @@ function hasPolyStyle(_) {
}
}

function iconHash(_) {
function hashStyle(_) {
return (_['marker-symbol'] || '') +
(_['marker-color'] || '').replace('#', '') +
(_['marker-size'] || '');
(_['marker-size'] || '') +
(_['stroke'] || '').replace('#', '') +
(_['stroke-width'] || '').replace('.', '') +
(_['stroke-opacity'] || '').replace('.', '') +
(_['fill'] || '').replace('#', '') +
(_['fill-opacity'] || '').replace('.', '');
}

function shapeStyle(_) {
function polygonStyle(_, styleHash) {
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);
return tag('Style', lineStyle + polyStyle, [['id', styleHash]]);
}

// ## Helpers
Expand Down

0 comments on commit a0a3d85

Please sign in to comment.