-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from wearepal/kew-soil-preds
Added Kew Soil Carbon Component, improved error handling on AI Model Component
- Loading branch information
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
app/javascript/projects/modelling/components/raster_component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { BaseComponent } from "./base_component" | ||
import { NodeData, WorkerInputs, WorkerOutputs } from 'rete/types/core/data' | ||
import { Input, Node, Output, Socket } from 'rete' | ||
import { booleanDataSocket, numericDataSocket } from "../socket_types" | ||
import { ProjectProperties } from "." | ||
|
||
import { maskFromExtentAndShape } from "../bounding_box" | ||
import { createXYZ } from "ol/tilegrid" | ||
import { retrieveModelDataWCS } from "../model_retrieval" | ||
import { NumericTileGrid } from "../tile_grid" | ||
import { TypedArray } from "d3" | ||
|
||
// Generic component class for handling raster datasets | ||
|
||
interface RasterComponentOption { | ||
name: string | ||
source: string | ||
} | ||
|
||
export class RasterComponent extends BaseComponent { | ||
projectProps: ProjectProperties | ||
options: RasterComponentOption[] | ||
cache: Map<string, NumericTileGrid> | ||
|
||
constructor(projectProps: ProjectProperties, name: string, category: string, options: RasterComponentOption[]) { | ||
super(name) | ||
this.category = category | ||
this.projectProps = projectProps | ||
this.options = options | ||
this.cache = new Map() | ||
} | ||
|
||
async builder(node: Node) { | ||
this.options.forEach((option) => { | ||
node.addOutput(new Output(option.name, option.name, numericDataSocket)) | ||
}) | ||
} | ||
|
||
async worker(node: NodeData, inputs: WorkerInputs, outputs: WorkerOutputs, ...args: unknown[]) { | ||
const editorNode = this.editor?.nodes.find(n => n.id === node.id) | ||
if (editorNode === undefined) { return } | ||
|
||
const mask = await maskFromExtentAndShape( | ||
this.projectProps.extent, | ||
this.projectProps.zoom, | ||
this.projectProps.maskLayer, | ||
this.projectProps.maskCQL, | ||
this.projectProps.mask | ||
) | ||
|
||
|
||
const tileGrid = createXYZ() | ||
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(this.projectProps.extent, this.projectProps.zoom) | ||
|
||
const promises = this.options.filter( | ||
opt => node.outputs[opt.name].connections.length > 0 | ||
).map( | ||
async opt => { | ||
if (this.cache.has(opt.name)) { | ||
|
||
outputs[opt.name] = this.cache.get(opt.name) | ||
|
||
}else{ | ||
|
||
const geotiff = await retrieveModelDataWCS(this.projectProps.extent, opt.source, outputTileRange) | ||
|
||
const image = await geotiff.getImage() | ||
const rasters = await geotiff.readRasters({ bbox: this.projectProps.extent, width: outputTileRange.getWidth(), height: outputTileRange.getHeight() }) | ||
|
||
const out = new NumericTileGrid(this.projectProps.zoom, outputTileRange.minX, outputTileRange.minY, outputTileRange.getWidth(), outputTileRange.getHeight()) | ||
|
||
for (let i = 0; i < (rasters[0] as TypedArray).length; i++) { | ||
let x = (outputTileRange.minX + i % image.getWidth()) | ||
let y = (outputTileRange.minY + Math.floor(i / image.getWidth())) | ||
|
||
out.set(x, y, mask.get(x, y) === true ? (rasters[0][i]) : NaN) | ||
} | ||
|
||
outputs[opt.name] = out | ||
this.cache.set(opt.name, out) | ||
|
||
} | ||
|
||
} | ||
) | ||
|
||
await Promise.all(promises) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters