geostyler-openlayers-parser
Version:
GeoStyler Style Parser implementation for OpenLayers styles
304 lines (303 loc) • 6.67 kB
JavaScript
import h from "./OlStyleUtil.js";
const d = [
"==",
"!=",
"<",
"<=",
">",
">=",
"between"
], g = [
...d,
"all",
"any",
"!"
];
function x(m) {
return Object.entries(m).reduce((s, [e, r]) => (r !== null && (s[r] = e), s), {});
}
const f = {
"==": "==",
"*=": null,
"!=": "!=",
"<": "<",
"<=": "<=",
">": ">",
">=": ">=",
"<=x<=": "between",
"&&": "all",
"||": "any",
"!": "!"
}, A = x(f), E = [
"get",
"*",
"/",
"+",
"-",
"%",
"^",
"abs",
"floor",
"round",
"ceil",
"sin",
"cos",
"atan",
"sqrt",
"case",
"interpolate",
"string",
"<",
"<=",
">",
">=",
"==",
"!=",
"!",
"all",
"any",
"between",
"in",
"to-string"
], T = {
// ---- string ----
numberFormat: null,
// numberFormat: 'number-format', // TODO: this could be done in theory but gs and mb use different format approaches
strAbbreviate: null,
strCapitalize: null,
strConcat: null,
strDefaultIfBlank: "string",
strEndsWith: null,
strEqualsIgnoreCase: null,
strIndexOf: null,
strLastIndexOf: null,
strLength: null,
strMatches: null,
strReplace: null,
strStartsWith: null,
strStripAccents: null,
strSubstring: null,
strSubstringStart: null,
strToLowerCase: null,
strToUpperCase: null,
strToString: "to-string",
strTrim: null,
// ---- number ----
add: "+",
abs: "abs",
acos: null,
asin: null,
// openlayers uses atan if only one argument is passed
// atan2 is used for two arguments
atan: "atan",
atan2: "atan",
ceil: "ceil",
cos: "cos",
div: "/",
exp: null,
floor: "floor",
interpolate: "interpolate",
log: null,
// – : 'ln2'
// – : 'log10'
// – : 'log2'
max: null,
min: null,
modulo: "%",
mul: "*",
pi: null,
// - : 'e',
pow: "^",
random: null,
rint: null,
round: "round",
sin: "sin",
sqrt: "sqrt",
sub: "-",
tan: null,
toDegrees: null,
toNumber: null,
toRadians: null,
// ---- boolean ----
all: "all",
// eslint-disable-next-line id-blacklist
any: "any",
between: "between",
double2bool: null,
equalTo: "==",
greaterThan: ">",
greaterThanOrEqualTo: ">=",
in: "in",
lessThan: "<",
lessThanOrEqualTo: "<=",
not: "!",
notEqualTo: "!=",
parseBoolean: null,
// ---- unknown ----
case: "case",
property: "get",
step: null
}, b = x(T);
class o {
static isFlatRule(s) {
return "style" in s;
}
static isFlatRuleArray(s) {
return Array.isArray(s) ? s.every((a) => o.isFlatRule(a)) : !1;
}
static isFlatStyle(s) {
return Array.isArray(s) ? !1 : !("style" in s);
}
static isFlatStyleArray(s) {
return !Array.isArray(s) || o.isFlatRuleArray(s) ? !1 : s.every(
(n) => o.isFlatStyle(n)
);
}
static isExpression(s) {
const e = s === void 0, r = Array.isArray(s);
if (e || !r)
return !1;
const a = typeof s[0] == "string", n = E.includes(s[0]);
return a && n;
}
static isFilter(s) {
const e = s === void 0, r = Array.isArray(s);
if (e || !r)
return !1;
const a = typeof s[0] == "string", n = g.includes(s[0]);
return a && n;
}
static isComparisonFilter(s) {
const e = s === void 0, r = Array.isArray(s);
if (e || !r)
return !1;
const a = typeof s[0] == "string", n = d.includes(s[0]);
return a && n;
}
static getColorAndOpacity(s) {
if (s === void 0)
return [void 0, void 0];
if (!Array.isArray(s)) {
let r = h.getHexColor(s);
if (!r)
throw new Error("Invalid color value");
r.length > 7 && (r = r.slice(0, 7));
const n = h.getOpacity(s);
return [r, n];
}
return [
h.getHexCodeFromRgbArray(s),
s[3]
];
}
static hasFlatFill(s) {
return "fill-color" in s && s["fill-color"] !== void 0;
}
static hasFlatStroke(s) {
const e = "stroke-color" in s && s["stroke-color"] !== void 0, r = "stroke-width" in s && s["stroke-width"] !== void 0;
return e || r;
}
static hasFlatText(s) {
return "text-value" in s && s["text-value"] !== void 0;
}
static hasFlatIcon(s) {
return "icon-src" in s && s["icon-src"] !== void 0;
}
static hasFlatCircle(s) {
return "circle-radius" in s && s["circle-radius"] !== void 0;
}
static olExpressionToGsExpression(s) {
if (!o.isExpression(s))
return s;
const e = s[0], r = b[e];
let a;
const n = s.slice(1);
switch (r) {
case "case": {
const i = [], l = o.olExpressionToGsExpression(n.pop());
n.forEach((t, c) => {
const u = Math.floor(c / 2);
c % 2 === 0 ? i[u] = {
case: o.olExpressionToGsExpression(t)
} : i[u] = {
...i[u],
value: o.olExpressionToGsExpression(t)
};
}), a = {
name: r,
args: [l, ...i]
};
break;
}
case "interpolate": {
const i = n.shift()[0], l = o.olExpressionToGsExpression(n.shift()), t = [];
n.forEach((c, u) => {
const p = Math.floor(u / 2);
u % 2 === 0 ? t[p] = {
stop: o.olExpressionToGsExpression(c)
} : t[p] = {
...t[p],
value: o.olExpressionToGsExpression(c)
};
}), t.unshift({ name: i }, l), a = {
name: r,
args: t
};
break;
}
case "strDefaultIfBlank": {
a = {
name: r,
// gs function only allows two args
args: n.slice(0, 2).map(o.olExpressionToGsExpression)
};
break;
}
case "in": {
const i = o.olExpressionToGsExpression(n.shift());
let l = [];
n[0] === "literal" ? l = n[0].pop() : l = n[0], a = {
name: r,
args: [i, ...l]
};
break;
}
case "atan":
case "atan2": {
const i = {
name: n.length === 1 ? "atan" : "atan2",
args: n.map(o.olExpressionToGsExpression)
};
n.length, a = i;
break;
}
default:
a = {
name: r,
args: n.map(o.olExpressionToGsExpression)
};
break;
}
return a;
}
static olFilterToGsFilter(s) {
const e = o.isExpression(s), r = o.isFilter(s), a = o.isComparisonFilter(s);
if (!r && !e)
return s;
let n;
if (r) {
const i = s[0], l = A[i], t = s.slice(1);
let c = t.shift();
a && Array.isArray(c) && c[0] === "get" && (c = c[1]), n = [
l,
o.olFilterToGsFilter(c),
...t.map(o.olFilterToGsFilter)
];
} else
n = o.olExpressionToGsExpression(s);
return n;
}
}
export {
o as default
};