geostyler / geostyler-sld-parser

GeoStyler-Style-Parser implementation for SLD
BSD 2-Clause "Simplified" License
52 stars 33 forks source link

ExternalGraphic's Format not set (geostyyler -> sld) #895

Open joserwan opened 1 year ago

joserwan commented 1 year ago

Bug

Describe the bug

The <Format> tag is missing when converting from geostyler to SLD.

To Reproduce

const { SldStyleParser } = await import("geostyler-sld-parser");
const geostyle = {
  rules: [
    {
      name: 'MissingFormat',
      symbolizers: [{kind: "Icon", image: '/path/to/icon.png'}],
    },
  ]
}
const parser = new SldStyleParser({ builderOptions: { format: true }});

parser.writeStyle(geostyle).then(({ output: sld }) => console.log(sld));

Result:

<?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" xmlns:se="http://www.opengis.net/se">
  <NamedLayer>
    <Name/>
    <UserStyle>
      <Name/>
      <Title/>
      <FeatureTypeStyle>
        <Rule>
          <Name>MissingFormat</Name>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/path/to/icon.png"/>
              </ExternalGraphic>
            </Graphic>
          </PointSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Expected behavior

<?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" xmlns:se="http://www.opengis.net/se">
  <NamedLayer>
    <Name/>
    <UserStyle>
      <Name/>
      <Title/>
      <FeatureTypeStyle>
        <Rule>
          <Name>MissingFormat</Name>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/path/to/icon.png"/>
                <Format>image/png</Format>
              </ExternalGraphic>
            </Graphic>
          </PointSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Additional context Node v20.8.0 Debian geostler-sld-parser v5.1.0

My fix

diff --git a/src/SldStyleParser.ts b/src/SldStyleParser.ts
index 0373158..eb0ccfa 100644
--- a/src/SldStyleParser.ts
+++ b/src/SldStyleParser.ts
@@ -1739,13 +1739,13 @@ export class SldStyleParser implements StyleParser<string> {
         case 'png':
         case 'jpeg':
         case 'gif':
-          graphic[0][ExternalGraphic][0][Format] = [`image/${iconExt}`];
+          graphic[0][ExternalGraphic][1] = { [Format]:  [{ '#text': `image/${iconExt}` }] };
           break;
         case 'jpg':
-          graphic[0][ExternalGraphic][0][Format] = ['image/jpeg'];
+          graphic[0][ExternalGraphic][1] = { [Format]:  [{ '#text': `image/jpeg` }] };
           break;
         case 'svg':
-          graphic[0][ExternalGraphic][0][Format] = ['image/svg+xml'];
+          graphic[0][ExternalGraphic][1] = { [Format]:  [{ '#text': `image/svg+xml` }] };
           break;
         default:
           break;

PS: Thank you for your awesome work !!

jansule commented 1 year ago

Thanks for the info @joserwan!

We actually do provide a format property in geostyler-style (see https://github.com/geostyler/geostyler-style/blob/master/style.ts#L431). However, it seems like we do not use it yet in geostyler-sld-parser.

I would suggest to adjust following things:

Also, it would be great if you could create a Pull Request for this :rocket:

joserwan commented 1 year ago

Sorry for the delay, I missed the notification.

I'll have a look on that in the next weeks, for now I'm too busy. Thank you for your feedback !

KaiVolland commented 6 months ago

@joserwan are you still planing on providing a PR here? Otherwise we might have the chance to fix this during the next codes sprint.