Spaces:
Sleeping
Sleeping
File size: 6,273 Bytes
4a288a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import URL from 'url';
import {eachSource, eachLayer, eachProperty} from '../visit';
function eachLayout(layer, callback) {
for (const k in layer) {
if (k.indexOf('layout') === 0) {
callback(layer[k], k);
}
}
}
function eachPaint(layer, callback) {
for (const k in layer) {
if (k.indexOf('paint') === 0) {
callback(layer[k], k);
}
}
}
function resolveConstant(style, value) {
if (typeof value === 'string' && value[0] === '@') {
return resolveConstant(style, style.constants[value]);
} else {
return value;
}
}
function isFunction(value) {
return Array.isArray(value.stops);
}
function renameProperty(obj, from, to) {
obj[to] = obj[from]; delete obj[from];
}
export default function(style) {
style.version = 8;
// Rename properties, reverse coordinates in source and layers
eachSource(style, (source) => {
if (source.type === 'video' && source.url !== undefined) {
renameProperty(source, 'url', 'urls');
}
if (source.type === 'video') {
source.coordinates.forEach((coord) => {
return coord.reverse();
});
}
});
eachLayer(style, (layer) => {
eachLayout(layer, (layout) => {
if (layout['symbol-min-distance'] !== undefined) {
renameProperty(layout, 'symbol-min-distance', 'symbol-spacing');
}
});
eachPaint(layer, (paint) => {
if (paint['background-image'] !== undefined) {
renameProperty(paint, 'background-image', 'background-pattern');
}
if (paint['line-image'] !== undefined) {
renameProperty(paint, 'line-image', 'line-pattern');
}
if (paint['fill-image'] !== undefined) {
renameProperty(paint, 'fill-image', 'fill-pattern');
}
});
});
// Inline Constants
eachProperty(style, {paint: true, layout: true}, (property) => {
const value = resolveConstant(style, property.value);
if (isFunction(value)) {
value.stops.forEach((stop) => {
stop[1] = resolveConstant(style, stop[1]);
});
}
property.set(value);
});
delete style.constants;
eachLayer(style, (layer) => {
// get rid of text-max-size, icon-max-size
// turn text-size, icon-size into layout properties
// https://github.com/mapbox/mapbox-gl-style-spec/issues/255
eachLayout(layer, (layout) => {
delete layout['text-max-size'];
delete layout['icon-max-size'];
});
eachPaint(layer, (paint) => {
if (paint['text-size']) {
if (!layer.layout) layer.layout = {};
layer.layout['text-size'] = paint['text-size'];
delete paint['text-size'];
}
if (paint['icon-size']) {
if (!layer.layout) layer.layout = {};
layer.layout['icon-size'] = paint['icon-size'];
delete paint['icon-size'];
}
});
});
function migrateFontstackURL(input) {
const inputParsed = URL.parse(input);
const inputPathnameParts = inputParsed.pathname.split('/');
if (inputParsed.protocol !== 'mapbox:') {
return input;
} else if (inputParsed.hostname === 'fontstack') {
assert(decodeURI(inputParsed.pathname) === '/{fontstack}/{range}.pbf');
return 'mapbox://fonts/mapbox/{fontstack}/{range}.pbf';
} else if (inputParsed.hostname === 'fonts') {
assert(inputPathnameParts[1] === 'v1');
assert(decodeURI(inputPathnameParts[3]) === '{fontstack}');
assert(decodeURI(inputPathnameParts[4]) === '{range}.pbf');
return `mapbox://fonts/${inputPathnameParts[2]}/{fontstack}/{range}.pbf`;
} else {
assert(false);
}
function assert(predicate) {
if (!predicate) {
throw new Error(`Invalid font url: "${input}"`);
}
}
}
if (style.glyphs) {
style.glyphs = migrateFontstackURL(style.glyphs);
}
function migrateFontStack(font) {
function splitAndTrim(string) {
return string.split(',').map((s) => {
return s.trim();
});
}
if (Array.isArray(font)) {
// Assume it's a previously migrated font-array.
return font;
} else if (typeof font === 'string') {
return splitAndTrim(font);
} else if (typeof font === 'object') {
font.stops.forEach((stop) => {
stop[1] = splitAndTrim(stop[1]);
});
return font;
} else {
throw new Error("unexpected font value");
}
}
eachLayer(style, (layer) => {
eachLayout(layer, (layout) => {
if (layout['text-font']) {
layout['text-font'] = migrateFontStack(layout['text-font']);
}
});
});
// Reverse order of symbol layers. This is an imperfect migration.
//
// The order of a symbol layer in the layers list affects two things:
// - how it is drawn relative to other layers (like oneway arrows below bridges)
// - the placement priority compared to other layers
//
// It's impossible to reverse the placement priority without breaking the draw order
// in some cases. This migration only reverses the order of symbol layers that
// are above all other types of layers.
//
// Symbol layers that are at the top of the map preserve their priority.
// Symbol layers that are below another type (line, fill) of layer preserve their draw order.
let firstSymbolLayer = 0;
for (let i = style.layers.length - 1; i >= 0; i--) {
const layer = style.layers[i];
if (layer.type !== 'symbol') {
firstSymbolLayer = i + 1;
break;
}
}
const symbolLayers = style.layers.splice(firstSymbolLayer);
symbolLayers.reverse();
style.layers = style.layers.concat(symbolLayers);
return style;
}
|