Skip to content

Commit

Permalink
Merge pull request #218 from wearepal/load-datasets-NaN-values
Browse files Browse the repository at this point in the history
improved tilegrid.get to automatically downscale.
  • Loading branch information
paulthatjazz authored Nov 30, 2023
2 parents 8b50fb3 + de1fb9b commit e768c49
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions app/javascript/projects/modelling/tile_grid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mean, mode } from "mathjs"
import { Extent } from "ol/extent"
import { createXYZ } from "ol/tilegrid"
import { registerSerializer } from "threads"
Expand Down Expand Up @@ -119,8 +120,23 @@ export class BooleanTileGrid extends TileGrid {

get(x: number, y: number, zoom = this.zoom): boolean {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
const x1 = x * Math.pow(2, this.zoom - zoom)
const y1 = y * Math.pow(2, this.zoom - zoom)
const x2 = x1 + Math.pow(2, this.zoom - zoom)
const y2 = y1 + Math.pow(2, this.zoom - zoom)

for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
const index = toIndex(this, Math.floor(x), Math.floor(y))
if(typeof index === 'undefined') return false
if (this.data[index] === 1) {
return true;
}
}
}
return false;
}

const scale = Math.pow(2, zoom - this.zoom)
const index = toIndex(this, Math.floor(x / scale), Math.floor(y / scale))
return (typeof index === 'undefined') ? false : this.data[index] === 1
Expand Down Expand Up @@ -225,11 +241,25 @@ export class NumericTileGrid extends TileGrid {

get(x: number, y: number, zoom = this.zoom): number {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
const x1 = x * Math.pow(2, this.zoom - zoom)
const y1 = y * Math.pow(2, this.zoom - zoom)
const x2 = x1 + Math.pow(2, this.zoom - zoom)
const y2 = y1 + Math.pow(2, this.zoom - zoom)

let nums: number[] = []

for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
const index = toIndex(this, Math.floor(x), Math.floor(y))
if(typeof index === 'undefined') return NaN
nums.push(this.data[index])
}
}
return nums.length > 0 ? mean(...nums) : NaN
}
const scale = Math.pow(2, zoom - this.zoom)
const index = toIndex(this, Math.floor(x / scale), Math.floor(y / scale))
return (typeof index === 'undefined') ? 0 : this.data[index]
return (typeof index === 'undefined') ? NaN : this.data[index]
}

set(x: number, y: number, value: number) {
Expand Down Expand Up @@ -304,8 +334,30 @@ export class CategoricalTileGrid extends TileGrid {

get(x: number, y: number, zoom = this.zoom): number {
if (zoom < this.zoom) {
throw new TypeError("invalid zoom level")
const x1 = x * Math.pow(2, this.zoom - zoom)
const y1 = y * Math.pow(2, this.zoom - zoom)
const x2 = x1 + Math.pow(2, this.zoom - zoom)
const y2 = y1 + Math.pow(2, this.zoom - zoom)

let nums: number[] = []

for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
const index = toIndex(this, Math.floor(x), Math.floor(y))
if(typeof index === 'undefined') return 255
nums.push(this.data[index])
}
}

if(nums.length == 0) return 255
else{
let r = mode(...nums)
if (r instanceof Array) return r[0]
else return r
}
}


const scale = Math.pow(2, zoom - this.zoom)
const index = toIndex(this, Math.floor(x / scale), Math.floor(y / scale))
return (typeof index === 'undefined') ? 255 : this.data[index]
Expand Down

0 comments on commit e768c49

Please sign in to comment.