Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to set colorMap for IDW layers #859

Closed
raitisbe opened this issue Sep 23, 2022 · 6 comments
Closed

Not possible to set colorMap for IDW layers #859

raitisbe opened this issue Sep 23, 2022 · 6 comments
Labels

Comments

@raitisbe
Copy link

Colormap seems to be always hardcoded to jet and changing the getColor function on IDW source class object doesn't seem to have any effect since moving to ES6, since it's a local variable in olsourceIDW function.

Would be good if var getColor = function (v) { could be exposed as a public variable in the olsourceIDW class.

@raitisbe
Copy link
Author

raitisbe commented Sep 23, 2022

Currently we managed to set the custom color map by overriding the whole computeImage function. Not sure its thee right way to go though.

  const clrMap = this.generateColormap('copper')
  const getColor = (v) => {
      const black = [0, 0, 0, 255];
      if (isNaN(v)) {
        return black;
      }
      if (v > 99) {
        v = 99;
      }
      if (v < 0) {
        v = 0;
      }
      v = Math.floor(v);
      return clrMap[v];
    };
  super.computeImage = (e) => {
      const pts = e.data.pts;
      const width = e.data.width;
      const height = e.data.height;
      const imageData = new Uint8ClampedArray(width * height * 4);
      // Compute image
      let x, y;
      for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
          let t = 0,
            b = 0;
          for (let i = 0; i < pts.length; ++i) {
            const dx = x - pts[i][0];
            const dy = y - pts[i][1];
            const d = dx * dx + dy * dy;

            // Inverse distance weighting - Shepard's method
            if (d === 0) {
              b = 1;
              t = pts[i][2];
              break;
            }
            const inv = 1 / (d * d);
            t += inv * pts[i][2];
            b += inv;
          }
          // Set color
          const color = getColor(t / b);
          // Convert to RGB
          const pos = (y * width + x) * 4;
          imageData[pos] = color[0];
          imageData[pos + 1] = color[1];
          imageData[pos + 2] = color[2];
          imageData[pos + 3] = color[3];
        }
      }
      return {type: 'image', data: imageData, width: width, height: height};
    }

@Viglino
Copy link
Owner

Viglino commented Sep 23, 2022

You're right... I've encapsulated the getColor method to make it accessible with worker...
I'll have to find a trick to handle it as a parameter...

@Viglino Viglino added the bug label Sep 23, 2022
Viglino added a commit that referenced this issue Sep 23, 2022
@Viglino
Copy link
Owner

Viglino commented Sep 23, 2022

I added a getColor parameter to let you override the default color function when creating IDW.
You can also add lib functions when using workers.
See doc http://viglino.github.io/ol-ext/doc/doc-pages/ol.source.IDW.html
and example source : https://github.com/Viglino/ol-ext/blob/master/examples/layer/map.layer.idw.html#L113

@raitisbe
Copy link
Author

How about possibility to change the color map for an existing ol.source.IDW object instance?

@Viglino
Copy link
Owner

Viglino commented Sep 27, 2022

If you're not using worker, you just have to set the getColor method.

idwSource.getColor = function(v) { return [r,g,b,a] } 

If you're using worker it's a little bit more difficult...

@raitisbe
Copy link
Author

Thanks, works great. I'm not using worker.

@Viglino Viglino closed this as completed Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants