Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 migration #16

Merged
merged 22 commits into from
Jul 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions bin/gl-style-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ var beautify = require('js-beautify').js_beautify,

var data = JSON.parse(fs.readFileSync(argv._[0]));

if (data.version !== 2) {
console.warn('style version !== 2');
process.exit(1);

var migrated = false;

if (data.version === 2) {
// Migrate to v3
data = require('../migrations/v3')(data);
data = require('../migrations/v4')(data);
migrated = true;
}

// Migrate to v3
data = require('../migrations/v3')(data);
if (data.version === 3) {
// Migrate to v4
data = require('../migrations/v4')(data);
migrated = true;
}

if (!migrated) {
console.warn('cannot migrate from', data.version);
process.exit(1);
}

function format(json) {
var str = beautify(JSON.stringify(json), {
Expand Down
13 changes: 8 additions & 5 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var parseCSSColor = require('csscolorparser').parseCSSColor;
module.exports = {};
module.exports.value = value;

['v2','v3'].forEach(function(version) {
['v2','v3','v4'].forEach(function(version) {
var ref = reference(version);
// Create validator for version
module.exports[version] = validator(ref);
Expand Down Expand Up @@ -98,7 +98,11 @@ function value(key, val, constants, ref, spec, errors) {
for (var i = 0; i < val.length; i++) {
var valspec = ref[spec.value]||spec.value;
if (typeof valspec === 'string') {
pass = validateNative(key + '[' + i + ']', val[i], valspec, errors) && pass;
if (spec.function && typeof val[i] === 'object') {
pass = value(key, val[i], constants, ref, ref.function, errors);
} else {
pass = validateNative(key + '[' + i + ']', val[i], valspec, errors) && pass;
}
} else {
pass = value(key + '[' + i + ']', val[i], constants, ref, valspec, errors) && pass;
}
Expand All @@ -107,7 +111,7 @@ function value(key, val, constants, ref, spec, errors) {
} else {
errors.push({
message: key + ': array expected, ' + typeof val + ' found',
line: val.__line__
line: val ? val.__line__ : null
});
return false;
}
Expand Down Expand Up @@ -189,7 +193,7 @@ function validateNative(key, val, spec, errors) {
if (type !== spec) {
errors.push({
message: key + ': ' + spec + ' expected, ' + (typeof val) + ' found',
line: val.__line__
line: val ? val.__line__ : null
});
return false;
} else {
Expand All @@ -208,4 +212,3 @@ function validateEnum(key, val, spec, errors) {
return true;
}
}

130 changes: 130 additions & 0 deletions migrations/v4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
'use strict';

//var ref = require('../lib/reference')('v4');

var vc;

module.exports = function(v3) {
v3.version = 4;
vc = v3.constants;
for (var id in v3.sources) {
var source = v3.sources[id];
if (source.glyphs) {
v3.glyphs = source.glyphs;
delete source.glyphs;
}
}
v3.layers.forEach(convertLayer);
return v3;
};

var newTypes = {
point: 'Point',
line: 'LineString',
polygon: 'Polygon'
};

function convertLayer(layer) {
var render = layer.render;

if (!render) return;

layer.type = render.type;
delete render.type;


if (Object.keys(render).length === 0) { // was just type
delete layer.render;
}

if (layer.filter && layer.filter.$type) {
layer.filter.$type = newTypes[layer.filter.$type];
}

if (layer.filter && layer.filter['!'||'|'||'&'||'^'] && layer.filter['!'||'|'||'&'||'^'].$type) {
layer.filter['!'||'|'||'&'||'^'].$type = newTypes[layer.filter['!'||'|'||'&'||'^'].$type];
}

if (layer.type === 'text' || layer.type === 'icon') {
layer.type = 'symbol';

var convertHalo = function(haloWidth, textSize) {
return Number(((6 - haloWidth * 8) * textSize / 24).toFixed(2));
};

// convert text-halo-width to pixels
for (var classname in layer) {
if (classname.indexOf('style') === 0) {
var style = layer[classname];
if (style['text-halo-width']) {
if (typeof(style['text-halo-width']) == 'string' && style['text-halo-width'].indexOf('@') != -1) {
style['text-halo-width'] = vc[style['text-halo-width']];
}
// handle 3 cases: text-size as constant, text-size as #, no text-size but max-text-size
var textSize = (typeof(style['text-size']) == 'string' &&
style['text-size'].indexOf('@') != -1) ?
vc[style['text-size']] :
(style['text-size'] ?
style['text-size'] :
layer.render['text-max-size']);

// handle text-size numbers and functions
if (typeof(textSize) == 'number') {
style['text-halo-width'] = convertHalo(style['text-halo-width'], textSize);
} else if (textSize && textSize.fn && textSize.fn == 'stops') {
var stops = [];
for (var stop in textSize.stops) {
stops.push(
[textSize.stops[stop][0],
convertHalo(style['text-halo-width'], textSize.stops[stop][1])]
);
}
style['text-halo-width'] = {
"fn": "stops",
"stops": stops
};
} else if (textSize && textSize.fn && textSize.fn == ('exponential' || 'linear')) {
var val; var max; var min;
if (textSize.val) val = convertHalo(style['text-halo-width'], textSize.val);
if (textSize.max) max = convertHalo(style['text-halo-width'], textSize.max);
if (textSize.min) min = convertHalo(style['text-halo-width'], textSize.min);
style['text-halo-width'] = textSize;
style['text-halo-width'].val = val;
style['text-halo-width'].max = max;
style['text-halo-width'].min = min;
}
}
}
}

if (!layer.render) return;

rename(render, 'icon-spacing', 'symbol-min-distance');
rename(render, 'text-min-distance', 'symbol-min-distance');
rename(render, 'text-alignment', 'text-horizontal-align');
rename(render, 'text-vertical-alignment', 'text-vertical-align');

if (layer.style && layer.style['icon-rotate-anchor']) {
render['symbol-rotation-alignment'] = layer.style['icon-rotate-anchor'];
delete layer.style['icon-rotate-anchor'];
}

if (render['text-path'] === 'curve') {
render['symbol-placement'] = 'line';
}

if (render['icon-size']) {
delete render['icon-size'];
}

delete render['text-path'];
}
if (layer.layers) layer.layers.forEach(convertLayer);
}

function rename(render, from, to) {
if (render[from]) {
render[to] = render[from];
delete render[from];
}
}