Skip to content

Commit e45b05b

Browse files
author
Kai Volland
committed
add more tests and update unsupportedProperties
1 parent 20084e7 commit e45b05b

6 files changed

+168
-22
lines changed

data/olStyles/function_boolean.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import OlStyle from 'ol/style/Style';
2+
import OlStyleCircle from 'ol/style/Circle';
3+
import OlStyleFill from 'ol/style/Fill';
4+
5+
export const olBoolean1 = new OlStyle({
6+
image: new OlStyleCircle({
7+
radius: 10,
8+
fill: new OlStyleFill({
9+
color: '#FF0000'
10+
})
11+
})
12+
});
13+
14+
export const olBoolean2 = new OlStyle({
15+
image: new OlStyleCircle({
16+
radius: 6,
17+
fill: new OlStyleFill({
18+
color: '#FF0000'
19+
})
20+
})
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import OlStyle from 'ol/style/Style';
2+
import OlStyleFill from 'ol/style/Fill';
3+
import OlStyleStroke from 'ol/style/Stroke';
4+
5+
const olPolygonSimple = new OlStyle({
6+
fill: new OlStyleFill({
7+
color: '#F1337F',
8+
}),
9+
stroke: new OlStyleStroke({
10+
width: 5
11+
})
12+
});
13+
14+
export default olPolygonSimple;

data/styles/function_boolean.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Style } from 'geostyler-style';
2+
3+
const functionBoolean: Style = {
4+
name: 'OL Style',
5+
rules: [
6+
{
7+
name: 'OL Style Rule 0',
8+
filter: [
9+
'==',
10+
{
11+
name: 'between',
12+
args: [{
13+
name: 'property',
14+
args: ['testprop']
15+
}, 0, 1]
16+
},
17+
true
18+
],
19+
symbolizers: [{
20+
kind: 'Mark',
21+
wellKnownName: 'circle',
22+
color: '#FF0000',
23+
radius: 10
24+
}]
25+
}, {
26+
name: 'OL Style Rule 1',
27+
symbolizers: [{
28+
kind: 'Mark',
29+
wellKnownName: 'circle',
30+
color: '#FF0000',
31+
radius: 6
32+
}]
33+
}
34+
]
35+
};
36+
37+
export default functionBoolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Style } from 'geostyler-style';
2+
3+
const functionNestedFillSymbolizer: Style = {
4+
name: 'OL Style',
5+
rules: [
6+
{
7+
name: 'OL Style Rule 0',
8+
symbolizers: [{
9+
kind: 'Fill',
10+
color: '#F1337F',
11+
outlineWidth: {
12+
name: 'max',
13+
args: [{
14+
name: 'pi'
15+
}, {
16+
name: 'strLength',
17+
args: ['Peter']
18+
}]
19+
}
20+
}]
21+
}
22+
]
23+
};
24+
25+
export default functionNestedFillSymbolizer;

src/OlStyleParser.spec.ts

+36-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import filter_simplefilter from '../data/styles/filter_simpleFilter';
3535
import filter_nestedfilter from '../data/styles/filter_nestedFilter';
3636
import filter_invalidfilter from '../data/styles/filter_invalidFilter';
3737
import function_marksymbolizer from '../data/styles/function_markSymbolizer';
38+
import function_nested_fillsymbolizer from '../data/styles/function_nested_fillSymbolizer';
3839
import point_styledLabel_static from '../data/styles/point_styledLabel_static';
3940
import multi_twoRulesSimplepoint from '../data/styles/multi_twoRulesSimplepoint';
4041
import multi_simplefillSimpleline from '../data/styles/multi_simplefillSimpleline';
@@ -48,6 +49,8 @@ import point_styledlabel from '../data/styles/point_styledlabel';
4849
import point_fontglyph from '../data/styles/point_fontglyph';
4950
import unsupported_properties from '../data/styles/unsupported_properties';
5051

52+
import ol_function_marksymbolizer from '../data/olStyles/function_markSymbolizer';
53+
import ol_function_nested_fillsymbolizer from '../data/olStyles/function_nested_fillSymbolizer';
5154
import ol_point_simplepoint from '../data/olStyles/point_simplepoint';
5255
import ol_point_icon from '../data/olStyles/point_icon';
5356
import ol_point_simplesquare from '../data/olStyles/point_simplesquare';
@@ -82,8 +85,8 @@ import {
8285
} from 'geostyler-style';
8386

8487
import OlStyleUtil from './Util/OlStyleUtil';
85-
import olSimpleCross from '../data/olStyles/point_simplecross';
86-
import olFunctionMark from '../data/olStyles/function_markSymbolizer';
88+
import function_boolean from '../data/styles/function_boolean';
89+
import { olBoolean1 as ol_function_boolean_fillsymbolizer1, olBoolean2 as ol_function_boolean_fillsymbolizer2 } from '../data/olStyles/function_boolean';
8790

8891
// reverse calculation of resolution for scale (from ol-util MapUtil)
8992
function getResolutionForScale (scale, units) {
@@ -1121,12 +1124,39 @@ describe('OlStyleParser implements StyleParser', () => {
11211124
expect(geoStylerStyle).toBeDefined();
11221125
expect(typeof geoStylerStyle === 'function').toBe(true);
11231126
geoStylerStyle = geoStylerStyle as OlParserStyleFct;
1124-
const dummyFeat = new OlFeature({
1125-
path: 'image.jpg'
1126-
});
1127+
const dummyFeat = new OlFeature();
11271128
const targetStyle = geoStylerStyle(dummyFeat);
11281129
expect(geoStylerStyle.__geoStylerStyle).toEqual(function_marksymbolizer);
1129-
expect(targetStyle[0]).toEqual(olFunctionMark);
1130+
expect(targetStyle[0]).toEqual(ol_function_marksymbolizer);
1131+
});
1132+
1133+
it('can write a FillSymbolizer with a nested GeoStylerFunction', async () => {
1134+
let { output: geoStylerStyle } = await styleParser.writeStyle(function_nested_fillsymbolizer);
1135+
expect(geoStylerStyle).toBeDefined();
1136+
expect(typeof geoStylerStyle === 'function').toBe(true);
1137+
geoStylerStyle = geoStylerStyle as OlParserStyleFct;
1138+
const dummyFeat = new OlFeature();
1139+
const targetStyle = geoStylerStyle(dummyFeat);
1140+
expect(geoStylerStyle.__geoStylerStyle).toEqual(function_nested_fillsymbolizer);
1141+
expect(targetStyle[0]).toEqual(ol_function_nested_fillsymbolizer);
1142+
});
1143+
1144+
it('can write a Filter with a GeoStylerBooleanFunction', async () => {
1145+
let { output: geoStylerStyle } = await styleParser.writeStyle(function_boolean);
1146+
expect(geoStylerStyle).toBeDefined();
1147+
expect(typeof geoStylerStyle === 'function').toBe(true);
1148+
geoStylerStyle = geoStylerStyle as OlParserStyleFct;
1149+
const dummyFeat1 = new OlFeature({
1150+
testprop: 0.8
1151+
});
1152+
const dummyFeat2 = new OlFeature({
1153+
testprop: 2
1154+
});
1155+
const targetStyle1 = geoStylerStyle(dummyFeat1);
1156+
const targetStyle2 = geoStylerStyle(dummyFeat2);
1157+
expect(geoStylerStyle.__geoStylerStyle).toEqual(function_boolean);
1158+
expect(targetStyle1[0]).toEqual(ol_function_boolean_fillsymbolizer1);
1159+
expect(targetStyle2[0]).toEqual(ol_function_boolean_fillsymbolizer2);
11301160
});
11311161

11321162
it('adds unsupportedProperties to the write output', async () => {

src/OlStyleParser.ts

+35-16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
109109
perpendicularOffset: 'none'
110110
},
111111
RasterSymbolizer: 'none'
112+
},
113+
Function: {
114+
atan2: {
115+
support: 'none',
116+
info: 'Currently returns the first argument'
117+
},
118+
rint: {
119+
support: 'none',
120+
info: 'Currently returns the first argument'
121+
},
122+
numberFormat: {
123+
support: 'none',
124+
info: 'Currently returns the first argument'
125+
},
126+
strAbbreviate: {
127+
support: 'none',
128+
info: 'Currently returns the first argument'
129+
}
112130
}
113131
};
114132

@@ -793,11 +811,11 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
793811
throw new Error('Cannot parse Filter. Unknown combination or negation operator.');
794812
}
795813
} else {
796-
let prop: any;
797-
if (isGeoStylerStringFunction(filter[1])) {
798-
prop = feature.get(OlStyleUtil.evaluateStringFunction(filter[1], feature));
814+
let arg1: any;
815+
if (isGeoStylerFunction(filter[1])) {
816+
arg1 = OlStyleUtil.evaluateFunction(filter[1], feature);
799817
} else {
800-
prop = feature.get(filter[1]);
818+
arg1 = feature.get(filter[1]);
801819
}
802820
let arg2: any;
803821
if (isGeoStylerFunction(filter[2])) {
@@ -807,33 +825,33 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
807825
}
808826
switch (filter[0]) {
809827
case '==':
810-
matchesFilter = ('' + prop) === ('' + arg2);
828+
matchesFilter = ('' + arg1) === ('' + arg2);
811829
break;
812830
case '*=':
813831
// inspired by
814832
// https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
815-
if (typeof arg2 === 'string' && typeof prop === 'string') {
816-
if (arg2.length > prop.length) {
833+
if (typeof arg2 === 'string' && typeof arg1 === 'string') {
834+
if (arg2.length > arg1.length) {
817835
matchesFilter = false;
818836
} else {
819-
matchesFilter = prop.indexOf(arg2) !== -1;
837+
matchesFilter = arg1.indexOf(arg2) !== -1;
820838
}
821839
}
822840
break;
823841
case '!=':
824-
matchesFilter = ('' + prop) !== ('' + arg2);
842+
matchesFilter = ('' + arg1) !== ('' + arg2);
825843
break;
826844
case '<':
827-
matchesFilter = parseFloat(prop) < Number(arg2);
845+
matchesFilter = Number(arg1) < Number(arg2);
828846
break;
829847
case '<=':
830-
matchesFilter = parseFloat(prop) <= Number(arg2);
848+
matchesFilter = Number(arg1) <= Number(arg2);
831849
break;
832850
case '>':
833-
matchesFilter = parseFloat(prop) > Number(arg2);
851+
matchesFilter = Number(arg1) > Number(arg2);
834852
break;
835853
case '>=':
836-
matchesFilter = parseFloat(prop) >= Number(arg2);
854+
matchesFilter = Number(arg1) >= Number(arg2);
837855
break;
838856
default:
839857
throw new Error('Cannot parse Filter. Unknown comparison operator.');
@@ -1193,9 +1211,10 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
11931211
OlStyleUtil.getRgbaColor(symbolizer.color, symbolizer.opacity) : symbolizer.color
11941212
}) : null;
11951213

1196-
const stroke = symbolizer.outlineColor ? new this.OlStyleStrokeConstructor({
1197-
color: (symbolizer.outlineOpacity !== null && symbolizer.outlineOpacity !== undefined) ?
1198-
OlStyleUtil.getRgbaColor(symbolizer.outlineColor, symbolizer.outlineOpacity) : symbolizer.outlineColor,
1214+
const stroke = symbolizer.outlineColor || symbolizer.outlineWidth ? new this.OlStyleStrokeConstructor({
1215+
color: (symbolizer.outlineColor && Number.isFinite(symbolizer.outlineOpacity))
1216+
? OlStyleUtil.getRgbaColor(symbolizer.outlineColor, symbolizer.outlineOpacity as number)
1217+
: symbolizer.outlineColor,
11991218
width: symbolizer.outlineWidth,
12001219
lineDash: symbolizer.outlineDasharray,
12011220
}) : null;

0 commit comments

Comments
 (0)