Skip to content

Commit

Permalink
Merge pull request #623 from KaiVolland/unsupported-properties-feedback
Browse files Browse the repository at this point in the history
Adds unsupportedProperties feedback to WriteStyleResult
  • Loading branch information
KaiVolland authored Jun 9, 2022
2 parents 3fd589f + 37a8fb2 commit 9e9285a
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
20 changes: 20 additions & 0 deletions data/slds/1.0/unsupported_properties.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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>OL Style</Name>
<UserStyle>
<Name>OL Style</Name>
<Title>OL Style</Title>
<FeatureTypeStyle>
<Rule>
<Name>OL Style Rule 0</Name>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#F1337F</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
19 changes: 19 additions & 0 deletions data/slds/1.1/unsupported_properties.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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>OL Style</se:Name>
<UserStyle>
<se:Name>OL Style</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name>OL Style Rule 0</se:Name>
<se:PolygonSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<se:Fill>
<se:SvgParameter name="fill">#F1337F</se:SvgParameter>
</se:Fill>
</se:PolygonSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
17 changes: 17 additions & 0 deletions data/styles/unsupported_properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Style } from 'geostyler-style';

const unsupportedProperties: Style = {
name: 'OL Style',
rules: [
{
name: 'OL Style Rule 0',
symbolizers: [{
kind: 'Fill',
color: '#F1337F',
opacity: 0.5
}]
}
]
};

export default unsupportedProperties;
41 changes: 40 additions & 1 deletion src/SldStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ export class SldStyleParser implements StyleParser<string> {
*/
writeStyle(geoStylerStyle: Style): Promise<WriteStyleResult<string>> {
return new Promise<WriteStyleResult<string>>(resolve => {
const unsupportedProperties = this.checkForUnsupportedProperites(geoStylerStyle);
try {
const builderOpts = {
renderOpts: {pretty: this.prettyOutput}
Expand All @@ -1307,7 +1308,9 @@ export class SldStyleParser implements StyleParser<string> {
const sldObject = this.geoStylerStyleToSldObject(geoStylerStyle);
const sldString = builder.buildObject(sldObject);
resolve({
output: sldString
output: sldString,
unsupportedProperties,
warnings: unsupportedProperties && ['Your style contains unsupportedProperties!']
});
} catch (error) {
resolve({
Expand All @@ -1317,6 +1320,42 @@ export class SldStyleParser implements StyleParser<string> {
});
}

checkForUnsupportedProperites(geoStylerStyle: Style): UnsupportedProperties | undefined {
const capitalizeFirstLetter = (a: string) => a[0].toUpperCase() + a.slice(1);
const unsupportedProperties: UnsupportedProperties = {};
geoStylerStyle.rules.forEach(rule => {
// ScaleDenominator and Filters are completly supported so we just check for symbolizers
rule.symbolizers.forEach(symbolizer => {
const key = capitalizeFirstLetter(`${symbolizer.kind}Symbolizer`);
const value = this.unsupportedProperties?.Symbolizer?.[key];
if (value) {
if (!unsupportedProperties.Symbolizer) {
unsupportedProperties.Symbolizer = {};
}
if (typeof value === 'string' || value instanceof String ) {
unsupportedProperties.Symbolizer[key] = value;
} else {
if (!unsupportedProperties.Symbolizer[key]) {
unsupportedProperties.Symbolizer[key] = {};
}
Object.keys(symbolizer).forEach(property => {
if (value[property]) {
if (!unsupportedProperties.Symbolizer) {
unsupportedProperties.Symbolizer = {};
}
unsupportedProperties.Symbolizer[key][property] = value[property];
}
});
}
}
});
});
if (Object.keys(unsupportedProperties).length > 0) {
return unsupportedProperties;
}
return undefined;
}

/**
* Get the SLD Object (readable with xml2js) from an GeoStyler-Style Style
*
Expand Down
25 changes: 25 additions & 0 deletions src/SldStyleParser.v1.0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import point_styledLabel_literalPlaceholder from '../data/styles/point_styledLab
import point_styledLabel_elementOrder from '../data/styles/point_styledLabel_elementOrder';
import raster_simpleraster from '../data/styles/raster_simpleRaster';
import raster_complexraster from '../data/styles/raster_complexRaster';
import unsupported_properties from '../data/styles/unsupported_properties';

it('SldStyleParser is defined', () => {
expect(SldStyleParser).toBeDefined();
Expand Down Expand Up @@ -608,6 +609,30 @@ describe('SldStyleParser implements StyleParser', () => {
const sld = fs.readFileSync('./data/slds/1.0/point_styledLabel_elementOrder.sld', 'utf8');
expect(sldString).toEqual(sld.trim());
});
it('adds unsupportedProperties to the write output', async () => {
const styleParserOrder = new SldStyleParser();
const {
output: sldString,
unsupportedProperties,
warnings
} = await styleParserOrder.writeStyle(unsupported_properties);
expect(sldString).toBeDefined();
const unsupportedGot = {
Symbolizer: {
FillSymbolizer: {
opacity: {
info: 'General opacity is not supported. Use fillOpacity and strokeOpacity instead.',
support: 'none'
}
}
}
};
const warningsGot = ['Your style contains unsupportedProperties!'];
expect(unsupportedProperties).toEqual(unsupportedGot);
expect(warnings).toEqual(warningsGot);
const sld = fs.readFileSync('./data/slds/1.0/unsupported_properties.sld', 'utf8');
expect(sldString).toEqual(sld.trim());
});

describe('#geoStylerStyleToSldObject', () => {
it('is defined', () => {
Expand Down
25 changes: 25 additions & 0 deletions src/SldStyleParser.v1.1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import point_styledLabel_literalPlaceholder from '../data/styles/point_styledLab
import point_styledLabel_elementOrder from '../data/styles/point_styledLabel_elementOrder';
import raster_simpleraster from '../data/styles/raster_simpleRaster';
import raster_complexraster from '../data/styles/raster_complexRaster';
import unsupported_properties from '../data/styles/unsupported_properties';

it('SldStyleParser is defined', () => {
expect(SldStyleParser).toBeDefined();
Expand Down Expand Up @@ -600,6 +601,30 @@ describe('SldStyleParser with Symbology Encoding implements StyleParser', () =>
const sld = fs.readFileSync('./data/slds/1.1/point_styledLabel_elementOrder.sld', 'utf8');
expect(sldString).toEqual(sld.trim());
});
it('adds unsupportedProperties to the write output', async () => {
const styleParserOrder = new SldStyleParser({ sldVersion: '1.1.0' });
const {
output: sldString,
unsupportedProperties,
warnings
} = await styleParserOrder.writeStyle(unsupported_properties);
expect(sldString).toBeDefined();
const unsupportedGot = {
Symbolizer: {
FillSymbolizer: {
opacity: {
info: 'General opacity is not supported. Use fillOpacity and strokeOpacity instead.',
support: 'none'
}
}
}
};
const warningsGot = ['Your style contains unsupportedProperties!'];
expect(unsupportedProperties).toEqual(unsupportedGot);
expect(warnings).toEqual(warningsGot);
const sld = fs.readFileSync('./data/slds/1.1/unsupported_properties.sld', 'utf8');
expect(sldString).toEqual(sld.trim());
});

describe('#geoStylerStyleToSldObject', () => {
it('is defined', () => {
Expand Down

0 comments on commit 9e9285a

Please sign in to comment.