Skip to content

Commit 49dd02d

Browse files
authored
Merge pull request #199 from NieuwlandGeo/externalgraphic-inlinecontent
Externalgraphic inlinecontent
2 parents df30897 + e9c08f1 commit 49dd02d

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

docs/assets/sldreader-standalone.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Version: 0.6.1 - May 14, 2025 12:07:44 */
1+
/* Version: 0.6.1 - May 19, 2025 12:07:05 */
22
var SLDReader = (function (exports, RenderFeature, Style, Icon, Fill, Stroke, Circle, RegularShape, render, Point, LineString, extent, has, Polygon, MultiPolygon, Text, MultiPoint) {
33
'use strict';
44

@@ -413,6 +413,15 @@ var SLDReader = (function (exports, RenderFeature, Style, Icon, Fill, Stroke, Ci
413413
console.error('Error converting parametric SVG: ', e);
414414
}
415415
}
416+
} else if (externalgraphic.inlinecontent) {
417+
if (externalgraphic.encoding?.indexOf('base64') > -1) {
418+
externalgraphic.onlineresource = `data:${externalgraphic.format || ''};base64,${externalgraphic.inlinecontent}`;
419+
delete externalgraphic.inlinecontent;
420+
} else if (externalgraphic.encoding?.indexOf('xml') > -1) {
421+
const encodedXml = window.encodeURIComponent(externalgraphic.inlinecontent);
422+
externalgraphic.onlineresource = `data:image/svg+xml;utf8,${encodedXml}`;
423+
delete externalgraphic.inlinecontent;
424+
}
416425
}
417426
}
418427

@@ -751,6 +760,14 @@ var SLDReader = (function (exports, RenderFeature, Style, Icon, Fill, Stroke, Ci
751760
OnlineResource: (element, obj) => {
752761
obj.onlineresource = element.getAttribute('xlink:href');
753762
},
763+
InlineContent: (element, obj) => {
764+
obj.encoding = element.getAttribute('encoding');
765+
if (obj.encoding?.indexOf('base64') > -1) {
766+
obj.inlinecontent = element.textContent?.trim();
767+
} else if (obj.encoding?.indexOf('xml') > -1) {
768+
obj.inlinecontent = element.innerHTML?.trim();
769+
}
770+
},
754771
CssParameter: (element, obj, prop, options) => addParameterValue(element, obj, prop, 'styling', options),
755772
SvgParameter: (element, obj, prop, options) => addParameterValue(element, obj, prop, 'styling', options)
756773
};

src/Reader/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ function addExternalGraphicProp(node, obj, prop, options) {
143143
console.error('Error converting parametric SVG: ', e);
144144
}
145145
}
146+
} else if (externalgraphic.inlinecontent) {
147+
if (externalgraphic.encoding?.indexOf('base64') > -1) {
148+
externalgraphic.onlineresource = `data:${externalgraphic.format || ''};base64,${externalgraphic.inlinecontent}`;
149+
delete externalgraphic.inlinecontent;
150+
} else if (externalgraphic.encoding?.indexOf('xml') > -1) {
151+
const encodedXml = window.encodeURIComponent(externalgraphic.inlinecontent);
152+
externalgraphic.onlineresource = `data:image/svg+xml;utf8,${encodedXml}`;
153+
delete externalgraphic.inlinecontent;
154+
}
146155
}
147156
}
148157

@@ -532,6 +541,14 @@ const SymbParsers = {
532541
OnlineResource: (element, obj) => {
533542
obj.onlineresource = element.getAttribute('xlink:href');
534543
},
544+
InlineContent: (element, obj) => {
545+
obj.encoding = element.getAttribute('encoding');
546+
if (obj.encoding?.indexOf('base64') > -1) {
547+
obj.inlinecontent = element.textContent?.trim();
548+
} else if (obj.encoding?.indexOf('xml') > -1) {
549+
obj.inlinecontent = element.innerHTML?.trim();
550+
}
551+
},
535552
CssParameter: (element, obj, prop, options) =>
536553
addParameterValue(element, obj, prop, 'styling', options),
537554
SvgParameter: (element, obj, prop, options) =>

test/Reader.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { sld } from './data/test.sld';
33
import { sld11 } from './data/test11.sld';
44
import { dynamicSld } from './data/dynamic.sld';
5+
import { externalGraphicInlineContentSld } from './data/externalgraphic-inlinecontent.sld';
56
import { graphicstrokeSymbolizerSld } from './data/graphicstrokeSymbolizer.sld';
67
import { graphicStrokeWithGap } from './data/graphicstroke-with-gap.sld';
78
import { graphicStrokeWithComments } from './data/graphicstroke-with-comments.sld';
@@ -582,6 +583,31 @@ describe('SVG style parameters', () => {
582583
});
583584
});
584585

586+
describe('ExternalGraphic with InlineContent', () => {
587+
let style;
588+
beforeEach(() => {
589+
const parsedSld = Reader(externalGraphicInlineContentSld);
590+
[style] = parsedSld.layers[0].styles[0].featuretypestyles;
591+
});
592+
593+
it('Base64 inline content is converted to base64 onlineresource', () => {
594+
const externalgraphic =
595+
style.rules[0].pointsymbolizer[0].graphic.externalgraphic;
596+
expect(externalgraphic.onlineresource).to.equal(
597+
'data:image/png;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
598+
);
599+
});
600+
601+
it('SVG xml inline content is converted to base64 onlineresource', () => {
602+
const externalgraphic =
603+
style.rules[1].pointsymbolizer[0].graphic.externalgraphic;
604+
console.log(externalgraphic.onlineresource);
605+
expect(externalgraphic.onlineresource).to.equal(
606+
'data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20100%20100%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Cpath%20d%3D%22M50%2C3l12%2C36h38l-30%2C22l11%2C36l-31-21l-31%2C21l11-36l-30-22h38z%22%20fill%3D%22%23FF0%22%20stroke%3D%22%23FC0%22%20stroke-width%3D%222%22%2F%3E%3C%2Fsvg%3E'
607+
);
608+
});
609+
});
610+
585611
describe('Parse units of measure', () => {
586612
let parsedSld;
587613
let pointSymbolizer;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const externalGraphicInlineContentSld = `<?xml version="1.0" encoding="UTF-8"?>
2+
<sld:StyledLayerDescriptor xmlns="http://www.opengis.net/sld"
3+
xmlns:sld="http://www.opengis.net/sld"
4+
xmlns:ogc="http://www.opengis.net/ogc"
5+
xmlns:gml="http://www.opengis.net/gml"
6+
xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0.0">
7+
<NamedLayer>
8+
<sld:Name>icons_puntenlaag</sld:Name>
9+
<UserStyle>
10+
<sld:Name>icons_puntenlaag</sld:Name>
11+
<sld:FeatureTypeStyle>
12+
<sld:Rule>
13+
<sld:Name>Inline base64</sld:Name>
14+
<sld:PointSymbolizer>
15+
<sld:Graphic>
16+
<sld:ExternalGraphic>
17+
<sld:InlineContent encoding="base64">R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==</sld:InlineContent>
18+
<sld:Format>image/png</sld:Format>
19+
</sld:ExternalGraphic>
20+
<sld:Size>24</sld:Size>
21+
</sld:Graphic>
22+
</sld:PointSymbolizer>
23+
</sld:Rule>
24+
<sld:Rule>
25+
<sld:Name>Inline svg</sld:Name>
26+
<sld:PointSymbolizer>
27+
<sld:Graphic>
28+
<sld:ExternalGraphic>
29+
<sld:InlineContent encoding="xml">
30+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100"><path d="M50,3l12,36h38l-30,22l11,36l-31-21l-31,21l11-36l-30-22h38z" fill="#FF0" stroke="#FC0" stroke-width="2"/></svg>
31+
</sld:InlineContent>
32+
<sld:Format>image/svg+xml</sld:Format>
33+
</sld:ExternalGraphic>
34+
<sld:Size>24</sld:Size>
35+
</sld:Graphic>
36+
</sld:PointSymbolizer>
37+
</sld:Rule>
38+
</sld:FeatureTypeStyle>
39+
</UserStyle>
40+
</NamedLayer>
41+
</sld:StyledLayerDescriptor>`;
42+
43+
export default externalGraphicInlineContentSld;

0 commit comments

Comments
 (0)