@@ -5,6 +5,7 @@ import { getArea } from "ol/sphere"
5
5
import { fromExtent } from "ol/geom/Polygon"
6
6
import { getColorStops } from "../reify_layer/model_output"
7
7
import { re , sum } from "mathjs"
8
+ import { getMedianCellSize } from "../modelling/components/cell_area_component"
8
9
9
10
type Color = [ number , number , number , number ]
10
11
@@ -35,6 +36,14 @@ export function findColor(value: number, colorArray: any[]): Color {
35
36
return alpha
36
37
}
37
38
39
+ function unitsAdjustmentFactor ( unit : string | undefined , grid : NumericTileGrid ) : number {
40
+ const area = getMedianCellSize ( grid ) . area
41
+ if ( unit === "m²" ) return area / 1
42
+ if ( unit === "ha" ) return area / 10000
43
+ if ( unit === "km²" ) return area / ( 1000 ** 2 )
44
+ return 1
45
+ }
46
+
38
47
function medianFromMap ( arr : [ number , number ] [ ] , total : number ) : number | undefined {
39
48
let idx = total / 2 ;
40
49
@@ -162,17 +171,17 @@ export function extentToChartData(colors: Color[] | undefined, model: BooleanTil
162
171
let counts = new Map < any , number > ( )
163
172
let color = new Map < any , [ number , number , number , number ] > ( )
164
173
let numeric_stats : NumericStats | undefined
174
+ const cellSize = getMedianCellSize ( model ) . area / 1000000
165
175
166
176
for ( let x = outputTileRange . minX ; x <= outputTileRange . maxX ; x ++ ) {
167
177
for ( let y = outputTileRange . minY ; y <= outputTileRange . maxY ; y ++ ) {
168
178
169
179
if ( model instanceof CategoricalTileGrid ) {
170
180
171
- const area = getArea ( fromExtent ( tileGrid . getTileCoordExtent ( [ model . zoom , x , y ] ) ) ) / 1000000
172
181
173
182
const value = model . labels . get ( model . get ( x , y ) ) ? model . labels . get ( model . get ( x , y ) ) : "No Data"
174
183
const count = counts . get ( value ) || 0
175
- counts . set ( value , count + area )
184
+ counts . set ( value , count + cellSize )
176
185
177
186
178
187
if ( colors ) {
@@ -182,12 +191,11 @@ export function extentToChartData(colors: Color[] | undefined, model: BooleanTil
182
191
183
192
} else {
184
193
185
- const area = model instanceof NumericTileGrid ? 1 : getArea ( fromExtent ( tileGrid . getTileCoordExtent ( [ model . zoom , x , y ] ) ) ) / 1000000
186
194
const value = model . get ( x , y )
187
195
188
196
const count = counts . get ( value ) || 0
189
197
190
- counts . set ( value , count + area )
198
+ counts . set ( value , count + cellSize )
191
199
192
200
if ( colors && model instanceof BooleanTileGrid ) {
193
201
const col_value = colors [ value ? 1 : 0 ]
@@ -219,7 +227,7 @@ export function extentToChartData(colors: Color[] | undefined, model: BooleanTil
219
227
const range = max - min
220
228
const step = range / bins
221
229
222
- const _sum = sum ( mapEntries . map ( ( x ) => x [ 1 ] * x [ 0 ] ) )
230
+ const _sum = sum ( mapEntries . map ( ( x ) => x [ 1 ] * x [ 0 ] ) ) * unitsAdjustmentFactor ( model . properties . area , model )
223
231
const total_entries = mapEntries . reduce ( ( acc , cur ) => acc + cur [ 1 ] , 0 )
224
232
225
233
const _mean = _sum / total_entries
0 commit comments