Terrain50Renderer: update to add class binning support

This commit is contained in:
Starbeamrainbowlabs 2021-01-18 19:57:03 +00:00
parent 6ec176d895
commit e11c2e0f3d
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
1 changed files with 25 additions and 6 deletions

View File

@ -16,6 +16,11 @@ class Terrain50Renderer {
"#efefef", "#efefef",
]).mode('lrgb'); ]).mode('lrgb');
this.colour_nodata = chroma("#f97153").rgba(); this.colour_nodata = chroma("#f97153").rgba();
thiis.colour_scale_classes = chroma.scale([
"#00ff00",
"#ff0000"
]);
} }
/** /**
@ -24,7 +29,7 @@ class Terrain50Renderer {
* You probably want the .render() method, which returns a buffer * You probably want the .render() method, which returns a buffer
* containing a png-encoded image. * containing a png-encoded image.
* @param {Terrain50} terrain The Terrain50 object instance to render. * @param {Terrain50} terrain The Terrain50 object instance to render.
* @param {[number, number][]} classes The classes to bin the values into. If not specified, values are not binned into classes. Warning: Values *must* fit into a bin. It is recommended to use -Infinity and Infinity in the first and last bins. * @param {{min:number,max:number}[]} classes The classes to bin the values into. If not specified, values are not binned into classes. Warning: Values *must* fit into a bin. It is recommended to use -Infinity and Infinity in the first and last bins.
* @return {ArrayBuffer} A canvas with the image rendered on it. * @return {ArrayBuffer} A canvas with the image rendered on it.
*/ */
async do_render(terrain, classes = null) { async do_render(terrain, classes = null) {
@ -40,6 +45,11 @@ class Terrain50Renderer {
colour_domain = this.colour_scale.domain(this.colour_domain); colour_domain = this.colour_scale.domain(this.colour_domain);
l.log(`[Terrain50Renderer] Static colour domain: ${this.colour_domain[0]} - ${this.colour_domain[1]}`); l.log(`[Terrain50Renderer] Static colour domain: ${this.colour_domain[0]} - ${this.colour_domain[1]}`);
} }
if(classes != null)
colour_domain = this.colour_scale_classes.domain([
0, classes.length
]);
let width = Math.floor(terrain.meta.ncols / this.scale_factor), let width = Math.floor(terrain.meta.ncols / this.scale_factor),
height = Math.floor(terrain.meta.nrows / this.scale_factor); height = Math.floor(terrain.meta.nrows / this.scale_factor);
@ -65,9 +75,18 @@ class Terrain50Renderer {
if(typeof terrain.data[a_y] !== "undefined" && if(typeof terrain.data[a_y] !== "undefined" &&
terrain.data[a_y][a_x] !== terrain.meta.NODATA_value) { terrain.data[a_y][a_x] !== terrain.meta.NODATA_value) {
colour = colour_domain( if(classes == null) {
terrain.data[a_y][a_x] colour = colour_domain(
).rgba(); // 0: r, 1: g, 2: b, a: 3 terrain.data[a_y][a_x]
).rgba(); // 0: r, 1: g, 2: b, a: 3
}
else {
for(let i in classes) {
if(terrain.data[a_y][a_x] >= classes[i].min && terrain.data[a_y][a_x] < classes[i].max) {
colour = colour_domain(i).rgba(); // 0: r, 1: g, 2: b, a: 3
}
}
}
} }
// colour = chroma("red").rgba(); // colour = chroma("red").rgba();
@ -104,8 +123,8 @@ class Terrain50Renderer {
* Renders the given Terrain50 object to an image. * Renders the given Terrain50 object to an image.
* Returns a buffer containing a PNG-encoded image, which is ready to be * Returns a buffer containing a PNG-encoded image, which is ready to be
* written to disk for example. * written to disk for example.
* @param {Terrain50} terrain The terrain object to render. * @param {Terrain50} terrain The terrain object to render.
* @param {[number, number][]} classes The classes to bin the values into. If not specified, values are not binned into classes. Warning: Values *must* fit into a bin. It is recommended to use -Infinity and Infinity in the first and last bins. * @param {{min:number,max:number}[]} classes The classes to bin the values into. If not specified, values are not binned into classes. Warning: Values *must* fit into a bin. It is recommended to use -Infinity and Infinity in the first and last bins.
* @return {Buffer} The terrain object as a png, represented as a buffer. * @return {Buffer} The terrain object as a png, represented as a buffer.
*/ */
async render(terrain, classes = null) { async render(terrain, classes = null) {