Skip to content

Commit 9a79c9b

Browse files
authored
Merge pull request #1079 from geostyler/1074-text-displacement
Fix displacement-offset conversion and update geostyler-style
2 parents ff7ccb5 + 48fbc28 commit 9a79c9b

File tree

7 files changed

+271
-187
lines changed

7 files changed

+271
-187
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<StyledLayerDescriptor
3+
version="1.0.0"
4+
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
5+
xmlns="http://www.opengis.net/sld"
6+
xmlns:ogc="http://www.opengis.net/ogc"
7+
xmlns:xlink="http://www.w3.org/1999/xlink"
8+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
>
10+
<NamedLayer>
11+
<Name>Displaced Text</Name>
12+
<UserStyle>
13+
<Name>Displaced Text</Name>
14+
<FeatureTypeStyle>
15+
<Rule>
16+
<Name/>
17+
<TextSymbolizer>
18+
<Label>Displaced</Label>
19+
<LabelPlacement>
20+
<PointPlacement>
21+
<Displacement>
22+
<DisplacementX>-20</DisplacementX>
23+
<DisplacementY>10</DisplacementY>
24+
</Displacement>
25+
</PointPlacement>
26+
</LabelPlacement>
27+
</TextSymbolizer>
28+
</Rule>
29+
</FeatureTypeStyle>
30+
</UserStyle>
31+
</NamedLayer>
32+
</StyledLayerDescriptor>

data/styles/geoserver/poi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const style: Style = {
2121
radius: 3.5,
2222
offset: [
2323
5,
24-
10
24+
-10
2525
],
2626
}
2727
]

data/styles/point_simplepoint_displacement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const pointSimplePointDisplacement: Style = {
1212
fillOpacity: 0.5,
1313
strokeColor: '#0000FF',
1414
strokeOpacity: 0.7,
15-
offset: [13, 37]
15+
offset: [13, -37]
1616
}]
1717
}]
1818
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"fast-xml-parser": "^5.2.3",
39-
"geostyler-style": "^11.0.1",
39+
"geostyler-style": "^11.0.2",
4040
"lodash": "^4.17.21"
4141
},
4242
"devDependencies": {

src/SldParser.spec.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { expect, it, describe } from 'vitest';
88
import point_simplepoint from '../data/styles/point_simplepoint';
99
// @ts-ignore
1010
import cdata from '../data/styles/cdata';
11+
import { TextSymbolizer } from 'geostyler-style';
1112

1213
describe('SldStyleParser implements StyleParser (reading from one version and writing to another version)', () => {
1314
it('can read and write a SLD PointSymbolizer (from 1.0.0 version to 1.1.0 version)', async () => {
14-
const styleParser = new SldStyleParser({sldVersion: '1.1.0'});
15+
const styleParser = new SldStyleParser({ sldVersion: '1.1.0' });
1516

1617
const sld = fs.readFileSync('./data/slds/1.0/point_simplepoint.sld', 'utf8');
1718
const { output: geoStylerStyle } = await styleParser.readStyle(sld);
@@ -32,7 +33,7 @@ describe('SldStyleParser implements StyleParser (reading from one version and wr
3233
expect(readStyle).toEqual(point_simplepoint);
3334
});
3435
it('can read and write a SLD PointSymbolizer (from 1.1.0 version to 1.0.0 version)', async () => {
35-
const styleParser = new SldStyleParser({sldVersion: '1.0.0'});
36+
const styleParser = new SldStyleParser({ sldVersion: '1.0.0' });
3637

3738
const sld = fs.readFileSync('./data/slds/1.1/point_simplepoint.sld', 'utf8');
3839
const { output: geoStylerStyle } = await styleParser.readStyle(sld);
@@ -67,33 +68,54 @@ describe('SldStyleParser implements StyleParser (reading from one version and wr
6768

6869
describe('Test Anchor-Point-Conversions', () => {
6970
it('transforms the sld-Anchorpoint-values to geostyler-anchors', async () => {
70-
const test1 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.0,0.0);
71+
const test1 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.0, 0.0);
7172
expect(test1).toEqual('bottom-left');
72-
const test2 = new SldStyleParser().getAnchorFromSldAnchorPoint(1.0,1.0);
73+
const test2 = new SldStyleParser().getAnchorFromSldAnchorPoint(1.0, 1.0);
7374
expect(test2).toEqual('top-right');
74-
const test3 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.5,1.0);
75+
const test3 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.5, 1.0);
7576
expect(test3).toEqual('top');
76-
const test4 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.45,0.9);
77+
const test4 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.45, 0.9);
7778
expect(test4).toEqual('top');
78-
const test5 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.5,0.5);
79+
const test5 = new SldStyleParser().getAnchorFromSldAnchorPoint(0.5, 0.5);
7980
expect(test5).toBeUndefined();
80-
const test6 = new SldStyleParser().getAnchorFromSldAnchorPoint(undefined,undefined);
81+
const test6 = new SldStyleParser().getAnchorFromSldAnchorPoint(undefined, undefined);
8182
expect(test6).toBeUndefined();
8283
});
8384
it('transformes the geostyler-anchors to sld-Anchopoint-values', async () => {
84-
const test1 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom-left','x');
85+
const test1 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom-left', 'x');
8586
expect(test1).toEqual(0.0);
86-
const test2 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom-left','y');
87+
const test2 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom-left', 'y');
8788
expect(test2).toEqual(0.0);
88-
const test3 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom','x');
89+
const test3 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom', 'x');
8990
expect(test3).toEqual(0.5);
90-
const test4 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom','y');
91+
const test4 = new SldStyleParser().getSldAnchorPointFromAnchor('bottom', 'y');
9192
expect(test4).toEqual(0.0);
92-
const test5 = new SldStyleParser().getSldAnchorPointFromAnchor('center','x');
93+
const test5 = new SldStyleParser().getSldAnchorPointFromAnchor('center', 'x');
9394
expect(test5).toEqual(0.5);
94-
const test6 = new SldStyleParser().getSldAnchorPointFromAnchor('center','y');
95+
const test6 = new SldStyleParser().getSldAnchorPointFromAnchor('center', 'y');
9596
expect(test6).toEqual(0.5);
96-
const test7 = new SldStyleParser().getAnchorFromSldAnchorPoint(undefined,'x');
97+
const test7 = new SldStyleParser().getAnchorFromSldAnchorPoint(undefined, 'x');
9798
expect(test7).toBeUndefined();
9899
});
99100
});
101+
102+
describe('Diplacement parsing works with `pareTagValue: false` option', async () => {
103+
let styleParser: SldStyleParser;
104+
it('parses the displacement values as numbers', async () => {
105+
const sld = fs.readFileSync('./data/slds/1.0/text_displacement.sld', 'utf8');
106+
styleParser = new SldStyleParser({
107+
sldVersion: '1.0.0',
108+
parserOptions: {
109+
parseTagValue: false
110+
}
111+
});
112+
const { output: geoStylerStyle } = await styleParser.readStyle(sld);
113+
expect(geoStylerStyle).toBeDefined();
114+
const symbolizer = geoStylerStyle?.rules?.[0]?.symbolizers?.[0] as TextSymbolizer;
115+
expect(symbolizer).toBeDefined();
116+
// beware that in SLD positive y values mean a displacement to the top,
117+
// whereas in geostyler-style positive y values mean a displacement to the bottom,
118+
// thus the y value is inverted
119+
expect(symbolizer.offset).toEqual([-20, -10]);
120+
});
121+
});

0 commit comments

Comments
 (0)