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

Scale factor and Cell Area components #182

Merged
merged 1 commit into from
Oct 28, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { BaseComponent } from "./base_component"
import { NodeData, WorkerInputs, WorkerOutputs } from 'rete/types/core/data'
import { Input, Node, Output } from 'rete'
import { dataSocket, numberSocket } from "../socket_types"
import { BooleanTileGrid, CategoricalTileGrid, NumericTileGrid } from "../tile_grid"
import { createXYZ } from "ol/tilegrid"
import { fromExtent } from "ol/geom/Polygon"
import { getArea } from "ol/sphere"
import { NumericConstant } from "../numeric_constant"
import { LabelControl } from "../controls/label"


export class CellAreaComponent extends BaseComponent {

cache: Map<number, NumericConstant>

constructor() {
super("Cell area")
this.category = "Debug tools"
}

async builder(node: Node) {
node.addInput(new Input('in', 'Input', dataSocket))
node.addOutput(new Output('out', 'Output', numberSocket))
node.addControl(new LabelControl('summary'))
this.cache = new Map()
}

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 }

if (inputs['in'].length > 0) {
delete editorNode.meta.errorMessage

const input = inputs['in'][0]

if (input instanceof BooleanTileGrid || input instanceof NumericTileGrid || input instanceof CategoricalTileGrid) {

if (this.cache.has(input.zoom)) {
outputs['out'] = this.cache.get(input.zoom)

} else {
const tileGrid = createXYZ()
const output = outputs['out'] = new NumericTileGrid(
input.zoom,
input.x,
input.y,
input.width,
input.height
)

const totalTiles = output.width * output.height

const middleIndex = Math.floor(totalTiles / 2)

let currentIndex = 0
let r

for (let x = output.x; x < output.x + output.width; ++x) {
for (let y = output.y; y < output.y + output.height; ++y) {
if (currentIndex === middleIndex) {
r = getArea(fromExtent(tileGrid.getTileCoordExtent([input.zoom, x, y])))
}

currentIndex++;
}
}

const out = outputs['out'] = new NumericConstant(r, editorNode.data.name as string)

this.cache.set(input.zoom, out)

node.data.summary = `${r.toLocaleString()} m²`

const summaryControl: any = editorNode.controls.get('summary')

summaryControl.update()
}



} else {

editorNode.meta.errorMessage = 'Numeric constants cannot be assigned to this node'
}

} else {
editorNode.meta.errorMessage = 'No input'
}



}


}
9 changes: 8 additions & 1 deletion app/javascript/projects/modelling/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { PrecompiledModelComponent, getDatasets } from "./dataset_component"
import { UkcehLandCoverComponent } from "./ukceh_land_cover_component"
import { CensusComponent } from "./census_component"
import { OSGreenSpacesComponent } from "./os_greenspaces_component"
import { CellAreaComponent } from "./cell_area_component"
import { ScaleFactorComponent } from "./scale_factor_component"

export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: SaveModel, getDatasets: getDatasets): BaseComponent[] {
return [
Expand All @@ -33,7 +35,7 @@ export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: S
new PrecompiledModelComponent(getDatasets),
new CensusComponent(),
new OSGreenSpacesComponent(),

// Outputs
new MapLayerComponent(saveMapLayer),
new SaveModelOutputComponent(saveModel),
Expand All @@ -46,6 +48,7 @@ export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: S
// Calculations
new AreaComponent(),
new DistanceMapComponent(),
new ScaleFactorComponent(),

// Charts
new BarChartComponent(),
Expand Down Expand Up @@ -73,5 +76,9 @@ 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'),

// DEBUG TOOLS
new CellAreaComponent(),

]
}
130 changes: 130 additions & 0 deletions app/javascript/projects/modelling/components/scale_factor_component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { BaseComponent } from "./base_component"
import { NodeData, WorkerInputs, WorkerOutputs } from 'rete/types/core/data'
import { Input, Node, Output } from 'rete'
import { dataSocket, numberSocket, numericDataSocket } from "../socket_types"
import { BooleanTileGrid, CategoricalTileGrid, NumericTileGrid } from "../tile_grid"
import { createXYZ } from "ol/tilegrid"
import { fromExtent } from "ol/geom/Polygon"
import { getArea } from "ol/sphere"
import { SelectControl } from "../controls/select"
import { LabelControl } from "../controls/label"
import { NumericConstant } from "../numeric_constant"


interface ScaleFactorType {
name: string
id: number
metersPerUnit: number
}

const ScaleFactorTypes: ScaleFactorType[] = [
{
name: 'ha',
id: 0,
metersPerUnit: 10000
},
{
name: 'km',
id: 1,
metersPerUnit: 1000
}
]

export class ScaleFactorComponent extends BaseComponent {

cache: Map<[number, number], NumericConstant>

constructor() {
super("Scale factor")
this.category = "Calculations"
}

async builder(node: Node) {
node.addInput(new Input('in', 'Input', dataSocket))
node.addOutput(new Output('out', 'Output', numberSocket))
node.addControl(new SelectControl(
this.editor,
'scaleFactorId',
() => ScaleFactorTypes,
() => [],
'Unit'
))
node.addControl(new LabelControl('summary'))
this.cache = new Map()
}

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 }

let index = node.data.scaleFactorId as number
if (!index) index = 0

const unitPerM = ScaleFactorTypes[index].metersPerUnit

if (inputs['in'].length > 0) {
delete editorNode.meta.errorMessage

const input = inputs['in'][0]

if (input instanceof BooleanTileGrid || input instanceof NumericTileGrid || input instanceof CategoricalTileGrid) {

if (this.cache.has([input.zoom, index])) {
outputs['out'] = this.cache.get([input.zoom, index])

} else {
const tileGrid = createXYZ()
const output = outputs['out'] = new NumericTileGrid(
input.zoom,
input.x,
input.y,
input.width,
input.height
)

const totalTiles = output.width * output.height

const middleIndex = Math.floor(totalTiles / 2)

let currentIndex = 0
let r

for (let x = output.x; x < output.x + output.width; ++x) {
for (let y = output.y; y < output.y + output.height; ++y) {
if (currentIndex === middleIndex) {
r = getArea(fromExtent(tileGrid.getTileCoordExtent([input.zoom, x, y]))) / unitPerM
}

currentIndex++;
}
}

const out = outputs['out'] = new NumericConstant(r, editorNode.data.name as string)

this.cache.set([input.zoom, index], out)

node.data.summary = `${r.toLocaleString()}`

const summaryControl: any = editorNode.controls.get('summary')

summaryControl.update()
}



} else {

editorNode.meta.errorMessage = 'Numeric constants cannot be assigned to this node'
}

} else {
editorNode.meta.errorMessage = 'No input'
}



}


}