geostyler / geostyler-openlayers-parser

GeoStyler Style Parser implementation for OpenLayers styles
BSD 2-Clause "Simplified" License
36 stars 26 forks source link

OL style function #672

Closed pelord closed 1 year ago

pelord commented 1 year ago

Question

It's my first attempt with geostyler. I have a project, running on ol7.2+ and I try to integrate your lib into mine. Into the project, I frequently use stylefunction... I've tried without success to read an ol stylefunction.

In your code, here what I've found

readStyle(olStyle: OlStyleLike): Promise<ReadStyleResult> {
    return new Promise<ReadStyleResult>((resolve) => {
      try {
        if (this.isOlParserStyleFct(olStyle)) {
          resolve({
            output: olStyle.__geoStylerStyle
          });

Here the question. How to convert Openlayers's style function into a geostyler style?

pelord commented 1 year ago

Here the function I try to read.

export function pointerPositionSummaryMarker(feature: olFeature<OlGeometry>, resolution: number): olstyle.Style {
  return new olstyle.Style({
    image: new olstyle.Icon({
      src: './assets/igo2/geo/icons/cross_black_18px.svg',
      imgSize: [18, 18], // for ie
    }),

    text: new olstyle.Text({
      text: feature.get('pointerSummary'),
      textAlign: 'left',
      textBaseline: 'bottom',
      font: '12px Calibri,sans-serif',
      fill: new olstyle.Fill({ color: '#000' }),
      backgroundFill: new olstyle.Fill({ color: 'rgba(255, 255, 255, 0.5)' }),
      backgroundStroke: new olstyle.Stroke({ color: 'rgba(200, 200, 200, 0.75)', width: 2 }),
      stroke: new olstyle.Stroke({ color: '#fff', width: 3 }),
      overflow: true,
      offsetX: 10,
      offsetY: -10,
      padding: [2.5, 2.5, 2.5, 2.5]
    })
  });
}
KaiVolland commented 1 year ago

Unfortunately it is not possible to translate an ol style function into an geostyler-style as these stylefunctions can contain every possible javascript code.

In your special case you don't make use of the passed in properties olFeature or resolution, so you could – if possible in your codebase – just use the returned olstyle.Style.

If your are commited to stylefunctions you could try to call your function like this and pass the result to the geostyler:

const style = pointerPositionSummaryMarker();
olStyleParser.readStyle(style);
pelord commented 1 year ago

In my case, I use text: feature.get('pointerSummary') to retrieve the feature's text.

pelord commented 1 year ago

Thanks for the quick reply. One more question, how to handle labels based on an attribute with geostyler?

pelord commented 1 year ago

@KaiVolland

How to use this example with geostyler?

https://openlayers.org/en/latest/examples/street-labels.html

KaiVolland commented 1 year ago

Oh sorry. I missed the used feature.

You can use {{propertyname}} in the textsymbolizer.

Compare: https://github.com/geostyler/geostyler-openlayers-parser/blob/master/data/styles/point_styledlabel.ts

pelord commented 1 year ago

Thanks!