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

Add Fill Component to model view #227

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/javascript/projects/modelling/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ScaleFactorComponent } from "./scale_factor_component"
import { CROMEComponent } from "./crome_component"
import { RescaleComponent } from "./rescale_component"
import { Extent } from "ol/extent"
import { ReplaceNaNComponent } from "./replace_nan_component"

export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: SaveModel, getDatasets: getDatasets, extent: Extent, zoom: number): BaseComponent[] {
return [
Expand Down Expand Up @@ -80,6 +81,7 @@ export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: S
new UnaryOpComponent('Reciprocal', '⁻¹', 'postfix', numericDataSocket, numericDataSocket, 'Arithmetic'),
new BinaryOpComponent('Less', '<', numericDataSocket, booleanDataSocket, 'Arithmetic'),
new BinaryOpComponent('Greater', '>', numericDataSocket, booleanDataSocket, 'Arithmetic'),
new ReplaceNaNComponent(),

// DEBUG TOOLS
new CellAreaComponent(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { BaseComponent } from "./base_component"
import { NodeData, WorkerInputs, WorkerOutputs } from 'rete/types/core/data'
import { Input, Node, Output } from 'rete'
import { numericDataSocket, numericNumberDataSocket } from "../socket_types"
import { NumericTileGrid } from "../tile_grid"
import { NumericConstant } from "../numeric_constant"
import { isEqual } from "lodash"

export class ReplaceNaNComponent extends BaseComponent {

constructor() {
super("Fill")
this.category = "Arithmetic"
}

async builder(node: Node) {
node.meta.toolTip = "Replace missing values with a constant"

node.addInput(new Input('in', 'Input', numericDataSocket))
node.addInput(new Input('replace', 'Replacement', numericNumberDataSocket))
node.addOutput(new Output('out', 'Output', 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 input = inputs['in'][0] as unknown as NumericTileGrid
const replace = inputs['replace'][0] as unknown as NumericConstant

if(isEqual([input, replace], editorNode.meta.previousInputs)) {
outputs['out'] = editorNode.meta.output
}else{
const output = outputs['out'] = new NumericTileGrid(
input.zoom,
input.x,
input.y,
input.width,
input.height
)

input.iterate((x, y, value) => output.set(x, y, isNaN(value) ? replace.value : value))
editorNode.meta.previousInputs = [input, replace]
editorNode.meta.output = output
}

}
}