1
0
Fork 0
mirror of https://github.com/sbrl/terrain50-cli.git synced 2024-11-22 06:53:01 +00:00

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

View file

@ -16,6 +16,11 @@ class Terrain50Renderer {
"#efefef",
]).mode('lrgb');
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
* containing a png-encoded image.
* @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.
*/
async do_render(terrain, classes = null) {
@ -41,6 +46,11 @@ class Terrain50Renderer {
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),
height = Math.floor(terrain.meta.nrows / this.scale_factor);
@ -65,10 +75,19 @@ class Terrain50Renderer {
if(typeof terrain.data[a_y] !== "undefined" &&
terrain.data[a_y][a_x] !== terrain.meta.NODATA_value) {
if(classes == null) {
colour = colour_domain(
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[3] = Math.floor(colour[3] * 255); // Scale the alpha value from 0-1 to 0-255
@ -105,7 +124,7 @@ class Terrain50Renderer {
* Returns a buffer containing a PNG-encoded image, which is ready to be
* written to disk for example.
* @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.
*/
async render(terrain, classes = null) {