TransWikia.com

Openlayers: Update scale from resolution in cached features?

Geographic Information Systems Asked by Ben Upham on March 8, 2021

I have a large number of cached vector features which I am caching for speed. However, as they need to change size depending on view resolution, I have to do various updates on them with each increment, which sort of defeats the purpose of caching.

Is there a way for cached styles to update their scale (and other options, like offSet) when resolution changes?

Here’s the basic code:


const imageVectorStyle = function(image, resolution) { 
  // Caching both the style and the icon, fyi. 
  let style = styleCache[image.getProperties().name];
  if (style) {
    //The style exists in the cache, but since the resolution has changed
    //since caching, I have to update the scale. 
    style.getImage().setScale(1 / resolution); 
    return style
  }
  else  {
    const imageProperties = image.getProperties();
    let imageIcon = iconcache[imageProperties.src];
    if (!imageIcon) {
      imageIcon = new Icon({
        offset: imageProperties.offset,
        size: imageProperties.size,
        stroke: new Stroke({color: '#dee2e6', width: 1 }),
        // Here scale is set based on resolution when the style is first created. 
        scale: 1 / resolution,
        crossOrigin: 'anonymous',
        src: imageProperties.src
      });
    }
  }
}

One Answer

If you want to style features based on resolution, you can use style function. It is used in this example. It is a function which takes feature and resolution as parameters and has to return instance of ol.Style, or array of styles.

Correct answer by Catch on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP