import {AreaQueryData} from "./area_results"; import {GroupableTabularData, TabularData} from "../visualisation/tabular-data"; export module AreaQuery { function columnsToObj(columns: AreaQueryData.Column[]) : {[name: string] : number|string|object} { let result: {[name: string] : number|string|object} = {}; columns.forEach((c) => result[c.name] = c.value); return result; } function convertRow(row: AreaQueryData.Row, extras: T|undefined = undefined) : TabularData & T { let columnObj = columnsToObj(row.columns); let d = { name: `${columnObj['Class Description']}`, value: columnObj['Class Area'], color: columnObj['Class Color'] }; return Object.assign(d, extras, columnObj); } export function layerToTabular(layer: AreaQueryData.Layer) : TabularData[] { return layer.rows.map(r => convertRow(r)); } const unclassifiedColor = '#F4F4F4'; const unclassifiedName = 'Unclassified/other'; const unclassifiedAbbr = 'unc'; const uncStroke = '#484848'; const waterName = 'Water'; const waterColor = '#74ADCD'; const waterAbbr = 'water'; export function smapResponseToGroupableTabularData(data: AreaQueryData.ISMapQueryResult, includeUnclassified: boolean = false, unclassifiedTolerance: number = 0, colors: {[key: string]: string} = {}, includeWater: boolean = false) : TabularData[] { const response = data.response; const tableData: TabularData[] = response.soils.map(soil => { return { name: soil.description, value: soil.area, color: colors[soil.classNo] } }); if (includeUnclassified) { tableData.push({ name: unclassifiedName, value: response.unclassified, color: unclassifiedColor, strokeColor: uncStroke }); } if (includeWater) { tableData.push({ name: waterName, value: response.totalArea - (response.totalSmapArea + response.unclassified), color: waterColor }); } return tableData; } export function responseToGroupableTabularData(data: AreaQueryData.OEResponse, includeUnclassified: boolean = false, unclassifiedTolerance: number = 0) : GroupableTabularData[] { return data.response.layers.reduce((acc: GroupableTabularData[], layer) => { let rowData: GroupableTabularData[] = layer.rows .map(r => convertRow<{ group: string }>(r, {group: layer.name})); if (includeUnclassified && data.response.area) { const totalArea = data.response.area; const unclassified = unclassifiedEntry( rowData, totalArea, { group: layer.name, abbr: unclassifiedAbbr, name: unclassifiedName, color: unclassifiedColor, strokeColor: uncStroke, value: 0 }, unclassifiedTolerance); if (unclassified) rowData.push(unclassified); } return acc.concat(rowData); }, []); } export function unclassifiedEntry(data: GroupableTabularData[], totalArea: number, opts: GroupableTabularData, tolerance = 0) : GroupableTabularData|null { const sum = data.reduce((theSum, d) => theSum + d.value, 0); const unclassifiedArea = totalArea - sum; if (unclassifiedArea === 0 || unclassifiedArea < tolerance) { return null; } return Object.assign({}, opts, { value: unclassifiedArea }); } export function m2ToHectaresRounded(sqaureMetres : number) : number { let ha = sqaureMetres / 10000; let rounded = Math.round(ha), figures = ("" + rounded).length; if (figures < 4) { return rounded; } if (figures < 6) { return Math.round(rounded / 10) * 10; } return Math.round(rounded / 100) * 100; } }