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 18 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
2 changes: 1 addition & 1 deletion 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
121 changes: 121 additions & 0 deletions migrations/v4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'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.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');

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-rotation-alignment'] = 'map';
render.placement = 'line';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the culprit I suppose

}

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];
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"coveralls": "~2.10.0"
},
"dependencies": {
"mapbox-gl-style-spec": "0.0.2",
"mapbox-gl-style-spec": "git://github.com/mapbox/mapbox-gl-style-spec.git",
"js-beautify": "^1.4.2",
"minimist": "0.0.8",
"jsonlint-lines": "~1.6.0",
Expand Down