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

Refactor code to use iterate method for iterating over tilegrids #214

Merged
merged 1 commit into from
Nov 27, 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
Expand Up @@ -40,11 +40,8 @@ export class AreaComponent extends BaseComponent {

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

for (let x = input.x; x < input.x + input.width; ++x) {
for (let y = input.y; y < input.y + input.height; ++y) {
totalArea += input.get(x, y) ? getArea(fromExtent(tileGrid.getTileCoordExtent([input.zoom, x, y]))) : 0
}
}
input.iterate((x, y, value) => totalArea += value ? getArea(fromExtent(tileGrid.getTileCoordExtent([input.zoom, x, y]))) : 0)

totalArea /= 1000000
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ export class MaskNumericDataComponent extends BaseComponent {

const out = editorNode.meta.output = outputs['out'] = new NumericTileGrid(mask.zoom, mask.x, mask.y, mask.width, mask.height)

for (let x = mask.x; x < mask.x + mask.width; ++x) {
for (let y = mask.y; y < mask.y + mask.height; ++y) {
out.set(x, y, mask.get(x, y) ? num.get(x, y, mask.zoom) : 0)
}
}
mask.iterate((x, y, value) => out.set(x, y, value ? num.get(x, y, mask.zoom) : 0))

}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,8 @@ export class UkcehLandCoverComponent extends BaseComponent {
const out = outputs[habitat.mode] = new BooleanTileGrid(categoricalData.zoom, categoricalData.x, categoricalData.y, categoricalData.width, categoricalData.height)
out.name = habitat.LC

for (let x = categoricalData.x; x < categoricalData.x + categoricalData.width; ++x) {
for (let y = categoricalData.y; y < categoricalData.y + categoricalData.height; ++y) {
out.set(x, y, categoricalData.get(x, y) === habitat.mode)
}
}
categoricalData.iterate((x, y, value) => out.set(x, y, value === habitat.mode))

this.outputCache.set(habitat.mode, out)
}
}
Expand Down
28 changes: 27 additions & 1 deletion app/javascript/projects/modelling/tile_grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ export class BooleanTileGrid extends TileGrid {
}
}

iterate(callback: (x: number, y: number, value: boolean) => void) {
const { x, y, width, height } = this
for (let i = x; i < x + width; i++) {
for (let j = y; j < y + height; j++) {
callback(i, j, this.get(i, j))
}
}
}

get(x: number, y: number, zoom = this.zoom): boolean {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
Expand Down Expand Up @@ -205,6 +214,15 @@ export class NumericTileGrid extends TileGrid {
this.minMax = null
}

iterate(callback: (x: number, y: number, value: number) => void) {
const { x, y, width, height } = this
for (let i = x; i < x + width; i++) {
for (let j = y; j < y + height; j++) {
callback(i, j, this.get(i, j))
}
}
}

get(x: number, y: number, zoom = this.zoom): number {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
Expand Down Expand Up @@ -236,7 +254,6 @@ export class NumericTileGrid extends TileGrid {
}
return this.minMax
}

getStats(): tileGridStats {

const [min, max] = this.getMinMax()
Expand Down Expand Up @@ -276,6 +293,15 @@ export class CategoricalTileGrid extends TileGrid {
if (labels) this.setLabels(labels)
}

iterate(callback: (x: number, y: number, value: number) => void) {
const { x, y, width, height } = this
for (let i = x; i < x + width; i++) {
for (let j = y; j < y + height; j++) {
callback(i, j, this.get(i, j))
}
}
}

get(x: number, y: number, zoom = this.zoom): number {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
Expand Down