Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Extract SLD version from SLD #696 and #925 #926

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/SldStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ import {
numberExpression
} from './Util/SldUtil';

export type SldVersion = '1.0.0' | '1.1.0';
const SLD_VERSIONS = ['1.0.0','1.1.0'] as const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const SLD_VERSIONS = ['1.0.0','1.1.0'] as const;
const SLD_VERSIONS = ['1.0.0', '1.1.0'] as const;


export type SldVersion = (typeof SLD_VERSIONS)[number];

export type ConstructorParams = {
numericFilterFields?: string[];
boolFilterFields?: string[];
/* optional for reading style (it will be guessed from sld style) and mandatory for writing */
sldVersion?: SldVersion;
symbolizerUnits?: string;
parserOptions?: X2jOptionsOptional;
Expand Down Expand Up @@ -271,9 +274,9 @@ export class SldStyleParser implements StyleParser<string> {

/**
* String indicating the SLD version to use. 1.1.0 will make use of
* Symbology Encoding. Default ist to use SLD 1.0.0
* Symbology Encoding.
*/
private _sldVersion: SldVersion = '1.0.0';
private _sldVersion: SldVersion;

/**
* Getter for _sldVersion
Expand Down Expand Up @@ -325,6 +328,15 @@ export class SldStyleParser implements StyleParser<string> {
return new Promise<ReadStyleResult>((resolve) => {
try {
const sldObject = this.parser.parse(sldString);

if (this._sldVersion === undefined) {
const version = getAttribute(sldObject[0], 'version');
if (! SLD_VERSIONS.includes(version)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (! SLD_VERSIONS.includes(version)) {
if (!SLD_VERSIONS.includes(version)) {

throw new Error(`SLD version must be ${SLD_VERSIONS.toString()}`);
}
this._sldVersion = version;
}

const geoStylerStyle: Style = this.sldObjectToGeoStylerStyle(sldObject);
resolve({
output: geoStylerStyle
Expand Down Expand Up @@ -1165,6 +1177,10 @@ export class SldStyleParser implements StyleParser<string> {
* @return The Promise resolving with the SLD as a string.
*/
writeStyle(geoStylerStyle: Style): Promise<WriteStyleResult<string>> {
if (this._sldVersion === undefined) {
throw new Error('sldVersion is mandatory');
}

return new Promise<WriteStyleResult<string>>(resolve => {
const unsupportedProperties = this.checkForUnsupportedProperties(geoStylerStyle);
try {
Expand Down
12 changes: 11 additions & 1 deletion src/SldStyleParser.v1.0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ it('SldStyleParser is defined', () => {
expect(SldStyleParser).toBeDefined();
});

describe('SldStyleParser implements StyleParser', () => {
describe('SldStyleParser implements StyleParser (reading)', () => {
let styleParser: SldStyleParser;

beforeEach(() => {
Expand Down Expand Up @@ -361,6 +361,14 @@ describe('SldStyleParser implements StyleParser', () => {
});
});
});
});

describe('SldStyleParser implements StyleParser (writing)', () => {
let styleParser: SldStyleParser;

beforeEach(() => {
styleParser = new SldStyleParser({sldVersion: '1.0.0'});
});

describe('#writeStyle', () => {
it('is defined', () => {
Expand Down Expand Up @@ -924,6 +932,7 @@ describe('SldStyleParser implements StyleParser', () => {

it('creates the correct order in a text symbolizer', async () => {
const styleParserOrder = new SldStyleParser({
sldVersion: '1.0.0',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can probably be omitted, as version 1.0.0 is set as default.

builderOptions: {
format: true
}
Expand All @@ -935,6 +944,7 @@ describe('SldStyleParser implements StyleParser', () => {
});
it('adds unsupportedProperties to the write output', async () => {
const styleParserOrder = new SldStyleParser({
sldVersion: '1.0.0',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above

builderOptions: {
format: true
}
Expand Down
15 changes: 11 additions & 4 deletions src/SldStyleParser.v1.1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ it('SldStyleParser is defined', () => {
expect(SldStyleParser).toBeDefined();
});

describe('SldStyleParser with Symbology Encoding implements StyleParser', () => {
describe('SldStyleParser with Symbology Encoding implements StyleParser (reading)', () => {
let styleParser: SldStyleParser;

beforeEach(() => {
styleParser = new SldStyleParser({
sldVersion: '1.1.0'
});
styleParser = new SldStyleParser();
});

describe('#readStyle', () => {
Expand Down Expand Up @@ -370,11 +368,20 @@ describe('SldStyleParser with Symbology Encoding implements StyleParser', () =>
});
});
});
});

describe('SldStyleParser with Symbology Encoding implements StyleParser (writing)', () => {
let styleParser: SldStyleParser;

beforeEach(() => {
styleParser = new SldStyleParser({sldVersion: '1.1.0'});
});

describe('#writeStyle', () => {
it('is defined', () => {
expect(styleParser.writeStyle).toBeDefined();
});

it('can write a SLD 1.1 PointSymbolizer', async () => {
const { output: sldString } = await styleParser.writeStyle(point_simplepoint);
expect(sldString).toBeDefined();
Expand Down
Loading