NieuwlandGeo/SLDReader

How to implement SLD Reader

gHupf opened this issue · 1 comments

gHupf commented

I currently try to implement the SLD Reader into my own openlayers project. I checked the live examples as well as the API, but I am still not able to implement it.

I implemented the code into my project as follows:

html file:
<head> <script src="node_modules\@nieuwlandgeo\sldreader\docs\assets\sldreader.js"></script> </head>

javascript file:
`var linien = new ol.layer.Vector({
title: 'Linienmaßnahmen',
source: new ol.source.Vector({
url: 'data/Linienmassnahmen.geojson',
format: new ol.format.GeoJSON()
}),
style: new ol.style.Style ({
stroke: new ol.style.Stroke ({
color: [0, 0, 0, 1.0],
width: 3.5
})
})
});

function applySLD(vectorLayer, text) {
const sldObject = SLDReader.Reader(text);
window.sldObject = sldObject;
const sldLayer = SLDReader.getLayer(sldObject);
const style = SLDReader.getStyle(sldLayer, 'Maßnahmen_Linien');
const featureTypeStyle = style.featuretypestyles[0];

const viewProjection = map.getView().getProjection();
vectorLayer.setStyle(SLDReader.createOlStyleFunction(featureTypeStyle, {
convertResolution: viewResolution => {
const viewCenter = map.getView().getCenter();
return ol.proj.getPointResolution(viewProjection, viewResolution, viewCenter);
},
}));
}

applySLD(linien, 'massnahmen_linien.sld');`

"massnahmen_linien" is the name of my sld file (I also attached it as a textfile). But I'm not sure if I use the applySLD() command right. If I run the code I get the error TypeError: n.getAttribute is not a function.

massnahmen_linien.txt

<script src="node_modules\@NieuwlandGeo\sldreader\docs\assets\sldreader.js"></script>

The dist folder contains the build which you should use on your page. (but probably the one in the docs folder works too)

Can you create a demo at eg codepen (or similar) to show your problem?