-
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 #323 from wearepal/init-ml
Add ML output as model layer
- Loading branch information
Showing
4 changed files
with
153 additions
and
31 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,6 +286,7 @@ GEM | |
|
||
PLATFORMS | ||
arm64-darwin-21 | ||
arm64-darwin-23 | ||
x86_64-darwin-21 | ||
x86_64-linux | ||
|
||
|
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
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
119 changes: 119 additions & 0 deletions
119
app/javascript/projects/modelling/components/ml_tree_hedge_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,119 @@ | ||
import { createXYZ } from "ol/tilegrid" | ||
import { Node, Output } from "rete" | ||
import { NodeData, WorkerInputs, WorkerOutputs } from "rete/types/core/data" | ||
import { booleanDataSocket, categoricalDataSocket } from "../socket_types" | ||
import { BooleanTileGrid, CategoricalTileGrid } from "../tile_grid" | ||
import { BaseComponent } from "./base_component" | ||
import { retrieveModelDataWCS } from "../model_retrieval" | ||
import { TypedArray } from "d3" | ||
import { Extent } from "ol/extent" | ||
|
||
interface Habitat { | ||
agg: number | ||
AC: string | ||
mode: number | ||
LC: string | ||
} | ||
|
||
const habitats: Habitat[] = [ | ||
//TODO : move to a json or an alternative storage | ||
{ agg: 0, AC: "All", mode: 0, LC: "All" }, | ||
{ agg: 1, AC: "Hedge", mode: 1, LC: "Hedge" }, | ||
{ agg: 2, AC: "Tree", mode: 2, LC: "Tree" } | ||
] | ||
|
||
async function renderCategoricalData(extent: Extent, zoom: number) { | ||
// When testing locally, disable CORS in browser settings | ||
|
||
const tileGrid = createXYZ() | ||
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(extent, zoom) | ||
|
||
const geotiff = await retrieveModelDataWCS(extent, 'ml:tree_hedge_predictions', outputTileRange) | ||
|
||
const rasters = await geotiff.readRasters({ bbox: extent, width: outputTileRange.getWidth(), height: outputTileRange.getHeight() }) | ||
const image = await geotiff.getImage() | ||
|
||
|
||
const map: Map<number, string> = new Map() | ||
|
||
habitats.forEach(hab => { | ||
if (hab.mode !== 0) map.set(hab.mode, hab.LC) | ||
}) | ||
|
||
const result = new CategoricalTileGrid( | ||
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())) | ||
|
||
result.set(x, y, rasters[0][i]) | ||
|
||
} | ||
|
||
result.setLabels(map) | ||
|
||
return result | ||
} | ||
|
||
export class MlTreeHedgeComponent extends BaseComponent { | ||
categoricalData: CategoricalTileGrid | null | ||
outputCache: Map<number, BooleanTileGrid> | ||
projectExtent: Extent | ||
zoom: number | ||
|
||
constructor(projectExtent: Extent, projectZoom: number) { | ||
super("ML Model Output") | ||
this.category = "Inputs" | ||
this.categoricalData = null | ||
this.outputCache = new Map() | ||
this.projectExtent = projectExtent | ||
this.zoom = projectZoom | ||
} | ||
|
||
async builder(node: Node) { | ||
|
||
node.meta.toolTip = "Custom ML outputs." | ||
|
||
node.meta.toolTipLink = "https://www.wearepal.ai/lmt.html" | ||
|
||
habitats.forEach(hab => | ||
hab.AC === "All" ? node.addOutput(new Output(hab["mode"].toString(), hab["LC"], categoricalDataSocket)) : node.addOutput(new Output(hab["mode"].toString(), hab["LC"], booleanDataSocket)) | ||
) | ||
} | ||
|
||
async worker(node: NodeData, inputs: WorkerInputs, outputs: WorkerOutputs, ...args: unknown[]) { | ||
if (this.categoricalData === null) { | ||
this.categoricalData = await renderCategoricalData(this.projectExtent, this.zoom) | ||
} | ||
const categoricalData = this.categoricalData! | ||
|
||
habitats.filter( | ||
habitat => node.outputs[habitat.mode].connections.length > 0 | ||
).forEach(habitat => { | ||
if (habitat.mode === 0) { | ||
|
||
outputs[habitat.mode] = this.categoricalData | ||
|
||
} else { | ||
if (this.outputCache.has(habitat.mode)) { | ||
outputs[habitat.mode] = this.outputCache.get(habitat.mode) | ||
} | ||
else { | ||
const out = outputs[habitat.mode] = new BooleanTileGrid(categoricalData.zoom, categoricalData.x, categoricalData.y, categoricalData.width, categoricalData.height) | ||
out.name = habitat.LC | ||
|
||
categoricalData.iterate((x, y, value) => out.set(x, y, value === habitat.mode)) | ||
|
||
this.outputCache.set(habitat.mode, out) | ||
} | ||
} | ||
}) | ||
} | ||
} |