Skip to content

Commit

Permalink
feat: read/write labelplacement
Browse files Browse the repository at this point in the history
  • Loading branch information
jansule committed Feb 12, 2024
1 parent 84d1de4 commit 260c80d
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 68 deletions.
20 changes: 20 additions & 0 deletions data/slds/1.0/text_lineplacement.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<Name>Simple Text</Name>
<UserStyle>
<Name>Simple Text</Name>
<FeatureTypeStyle>
<Rule>
<Name/>
<TextSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<Label>myText</Label>
<LabelPlacement>
<LinePlacement />
</LabelPlacement>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
20 changes: 20 additions & 0 deletions data/slds/1.0/text_pointplacement.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<Name>Simple Text</Name>
<UserStyle>
<Name>Simple Text</Name>
<FeatureTypeStyle>
<Rule>
<Name/>
<TextSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<Label>myText</Label>
<LabelPlacement>
<PointPlacement />
</LabelPlacement>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
22 changes: 22 additions & 0 deletions data/slds/1.1/text_lineplacement.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:se="http://www.opengis.net/se">
<NamedLayer>
<se:Name>Simple Text</se:Name>
<UserStyle>
<se:Name>Simple Text</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name/>
<se:TextSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<se:Label>
<ogc:Literal>myText</ogc:Literal>
</se:Label>
<se:LabelPlacement>
<se:LinePlacement />
</se:LabelPlacement>
</se:TextSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
22 changes: 22 additions & 0 deletions data/slds/1.1/text_pointplacement.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StyledLayerDescriptor version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:se="http://www.opengis.net/se">
<NamedLayer>
<se:Name>Simple Text</se:Name>
<UserStyle>
<se:Name>Simple Text</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name/>
<se:TextSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<se:Label>
<ogc:Literal>myText</ogc:Literal>
</se:Label>
<se:LabelPlacement>
<se:PointPlacement />
</se:LabelPlacement>
</se:TextSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
3 changes: 2 additions & 1 deletion data/styles/geoserver/poi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const style: Style = {
'Arial'
],
fontWeight: 'bold',
size: 14
size: 14,
placement: 'point'
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion data/styles/geoserver/poly_landmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const style: Style = {
],
fontStyle: 'normal',
fontWeight: 'bold',
size: 14
size: 14,
placement: 'point'
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion data/styles/geoserver/pophatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const style: Style = {
'Times New Roman'
],
fontStyle: 'normal',
size: 14
size: 14,
placement: 'point'
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion data/styles/geoserver/popshade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const style: Style = {
'Times New Roman'
],
fontStyle: 'normal',
size: 14
size: 14,
placement: 'point'
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion data/styles/geoserver/tiger_roads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const style: Style = {
],
fontStyle: 'normal',
fontWeight: 'bold',
size: 14
size: 14,
placement: 'line'
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion data/styles/multi_simplelineLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const multiSimplelineLabel: Style = {
size: 12,
offset: [0, 5],
fontStyle: 'normal',
fontWeight: 'bold'
fontWeight: 'bold',
placement: 'point'
}]
}]
};
Expand Down
3 changes: 2 additions & 1 deletion data/styles/point_styledLabel_elementOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const pointStyledLabel: Style = {
opacity: 1,
rotate: 45,
fontStyle: 'normal',
fontWeight: 'bold'
fontWeight: 'bold',
placement: 'point'
}]
}]
};
Expand Down
3 changes: 2 additions & 1 deletion data/styles/point_styledLabel_literalOpenCurlyBraces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const pointStyledLabel: Style = {
opacity: 1,
rotate: 45,
fontStyle: 'normal',
fontWeight: 'bold'
fontWeight: 'bold',
placement: 'point'
}]
}]
};
Expand Down
3 changes: 2 additions & 1 deletion data/styles/point_styledLabel_literalPlaceholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const pointStyledLabel: Style = {
opacity: 1,
rotate: 45,
fontStyle: 'normal',
fontWeight: 'bold'
fontWeight: 'bold',
placement: 'point'
}]
}]
};
Expand Down
3 changes: 2 additions & 1 deletion data/styles/point_styledlabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const pointStyledLabel: Style = {
haloOpacity: 1,
rotate: 45,
fontStyle: 'normal',
fontWeight: 'bold'
fontWeight: 'bold',
placement: 'point'
}]
}]
};
Expand Down
17 changes: 17 additions & 0 deletions data/styles/text_lineplacement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Style } from 'geostyler-style';

const pointStyledLabel: Style = {
name: 'Simple Text',
rules: [{
name: '',
symbolizers: [{
color: '#000000',
opacity: 1,
kind: 'Text',
label: 'myText',
placement: 'line'
}]
}]
};

export default pointStyledLabel;
17 changes: 17 additions & 0 deletions data/styles/text_pointplacement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Style } from 'geostyler-style';

const pointStyledLabel: Style = {
name: 'Simple Text',
rules: [{
name: '',
symbolizers: [{
color: '#000000',
opacity: 1,
kind: 'Text',
label: 'myText',
placement: 'point'
}]
}]
};

export default pointStyledLabel;
30 changes: 5 additions & 25 deletions src/SldStyleParser.geoserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,10 @@ describe('SldStyleParser implements StyleParser', () => {
it('can write the geoserver poi.sld', async () => {
const {
output: sldString,
errors,
warnings,
unsupportedProperties
errors
} = await styleParser.writeStyle(poi);
expect(sldString).toBeDefined();
expect(errors).toBeUndefined();
expect(warnings).toBeUndefined();
expect(unsupportedProperties).toBeUndefined();
// As string comparison between two XML-Strings is awkward and nonsens
// we read it again and compare the json input with the parser output
const { output: readStyle} = await styleParser.readStyle(sldString!);
Expand All @@ -415,14 +411,10 @@ describe('SldStyleParser implements StyleParser', () => {
it('can write the geoserver poly_landmarks.sld', async () => {
const {
output: sldString,
errors,
warnings,
unsupportedProperties
errors
} = await styleParser.writeStyle(poly_landmarks);
expect(sldString).toBeDefined();
expect(errors).toBeUndefined();
expect(warnings).toBeUndefined();
expect(unsupportedProperties).toBeUndefined();
// As string comparison between two XML-Strings is awkward and nonsens
// we read it again and compare the json input with the parser output
const { output: readStyle} = await styleParser.readStyle(sldString!);
Expand All @@ -431,14 +423,10 @@ describe('SldStyleParser implements StyleParser', () => {
it('can write the geoserver pophatch.sld', async () => {
const {
output: sldString,
errors,
warnings,
unsupportedProperties
errors
} = await styleParser.writeStyle(pophatch);
expect(sldString).toBeDefined();
expect(errors).toBeUndefined();
expect(warnings).toBeUndefined();
expect(unsupportedProperties).toBeUndefined();
// As string comparison between two XML-Strings is awkward and nonsens
// we read it again and compare the json input with the parser output
const { output: readStyle} = await styleParser.readStyle(sldString!);
Expand All @@ -447,14 +435,10 @@ describe('SldStyleParser implements StyleParser', () => {
it('can write the geoserver popshade.sld', async () => {
const {
output: sldString,
errors,
warnings,
unsupportedProperties
errors
} = await styleParser.writeStyle(popshade);
expect(sldString).toBeDefined();
expect(errors).toBeUndefined();
expect(warnings).toBeUndefined();
expect(unsupportedProperties).toBeUndefined();
// As string comparison between two XML-Strings is awkward and nonsens
// we read it again and compare the json input with the parser output
const { output: readStyle} = await styleParser.readStyle(sldString!);
Expand Down Expand Up @@ -543,14 +527,10 @@ describe('SldStyleParser implements StyleParser', () => {
it('can write the geoserver tiger_roads.sld', async () => {
const {
output: sldString,
errors,
warnings,
unsupportedProperties
errors
} = await styleParser.writeStyle(tiger_roads);
expect(sldString).toBeDefined();
expect(errors).toBeUndefined();
expect(warnings).toBeUndefined();
expect(unsupportedProperties).toBeUndefined();
// As string comparison between two XML-Strings is awkward and nonsens
// we read it again and compare the json input with the parser output
const { output: readStyle} = await styleParser.readStyle(sldString!);
Expand Down
49 changes: 36 additions & 13 deletions src/SldStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ export class SldStyleParser implements StyleParser<string> {
resampling: 'none',
saturation: 'none',
visibility: 'none'
},
TextSymbolizer: {
placement: {
support: 'partial',
info: 'Only "line" and "point" are currently supported'
}
}
}
};
Expand Down Expand Up @@ -695,18 +701,28 @@ export class SldStyleParser implements StyleParser<string> {
if (!isNil(haloColor)) {
textSymbolizer.haloColor = haloColor;
}
const displacement = get(sldSymbolizer, 'LabelPlacement.PointPlacement.Displacement');
if (!isNil(displacement)) {
const x = get(displacement, 'DisplacementX.#text');
const y = get(displacement, 'DisplacementY.#text');
textSymbolizer.offset = [
Number.isFinite(x) ? numberExpression(x) : 0,
Number.isFinite(y) ? numberExpression(y) : 0,
];
}
const rotation = get(sldSymbolizer, 'LabelPlacement.PointPlacement.Rotation.#text');
if (!isNil(rotation)) {
textSymbolizer.rotate = numberExpression(rotation);
const placement = get(sldSymbolizer, 'LabelPlacement');
if (!isNil(placement)) {
const pointPlacement = get(placement, 'PointPlacement');
const linePlacement = get(placement, 'LinePlacement');
if (!isNil(pointPlacement)) {
textSymbolizer.placement = 'point';
const displacement = get(placement, 'PointPlacement.Displacement');
if (!isNil(displacement)) {
const x = get(displacement, 'DisplacementX.#text');
const y = get(displacement, 'DisplacementY.#text');
textSymbolizer.offset = [
Number.isFinite(x) ? numberExpression(x) : 0,
Number.isFinite(y) ? numberExpression(y) : 0,
];
}
const rotation = get(placement, 'PointPlacement.Rotation.#text');
if (!isNil(rotation)) {
textSymbolizer.rotate = numberExpression(rotation);
}
} else if (!isNil(linePlacement)) {
textSymbolizer.placement = 'line';
}
}
if (!isNil(fontFamily)) {
textSymbolizer.font = [fontFamily];
Expand Down Expand Up @@ -1807,6 +1823,7 @@ export class SldStyleParser implements StyleParser<string> {
const DisplacementY = this.getTagName('DisplacementY');
const LabelPlacement = this.getTagName('LabelPlacement');
const PointPlacement = this.getTagName('PointPlacement');
const LinePlacement = this.getTagName('LinePlacement');
const Rotation = this.getTagName('Rotation');
const Radius = this.getTagName('Radius');
const Label = this.getTagName('Label');
Expand Down Expand Up @@ -1854,7 +1871,13 @@ export class SldStyleParser implements StyleParser<string> {
});
}

if (textSymbolizer.offset || textSymbolizer.rotate !== undefined) {
if (textSymbolizer.placement === 'line') {
sldTextSymbolizer.push({
[LabelPlacement]: [{
[LinePlacement]: []
}]
});
} else if (textSymbolizer.offset || textSymbolizer.rotate !== undefined || textSymbolizer.placement === 'point') {
const pointPlacement: any = [];
if (textSymbolizer.offset) {
pointPlacement.push({
Expand Down
Loading

0 comments on commit 260c80d

Please sign in to comment.