From bf45190f55e44e6c287f80a3f5a64821a4de9cbf Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Sat, 8 Jun 2024 11:41:25 -0400 Subject: [PATCH] add support for simple bbox obj resampling --- src/stats/stats.core.js | 3 +++ src/sum/index.js | 2 +- src/sum/sum.test.js | 3 ++- src/utils/utils.module.js | 12 ++++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/stats/stats.core.js b/src/stats/stats.core.js index d96b11f..f5343de 100644 --- a/src/stats/stats.core.js +++ b/src/stats/stats.core.js @@ -49,6 +49,9 @@ const stats = (georaster, geometry, calcStatsOptions, test, { debug_level = 0, i geometry = polygon([georaster.xmin, georaster.ymin, georaster.xmax, georaster.ymax]); } else if (validate(geometry)) { geometry = polygon(geometry); + } else if (utils.isBboxObj(geometry)) { + // convert { xmin: 20, xmax: 32, ymin: -3, ymax: 0 } to geojson polygon + geometry = polygon([geometry.xmin, geometry.ymin, geometry.xmax, geometry.ymax]); } } diff --git a/src/sum/index.js b/src/sum/index.js index 75659af..0ced398 100644 --- a/src/sum/index.js +++ b/src/sum/index.js @@ -19,5 +19,5 @@ import stat from "../stat"; * [217461, 21375, 57312, 457125] */ export default function sum(georaster, geometry, test) { - return QuickPromise.resolve(stat(georaster, geometry, "sum", test, { rescale: true, vrm: "minimal" })); + return stat(georaster, geometry, "sum", test, { rescale: true, vrm: "minimal" }); } diff --git a/src/sum/sum.test.js b/src/sum/sum.test.js index da5639d..7b17461 100644 --- a/src/sum/sum.test.js +++ b/src/sum/sum.test.js @@ -13,7 +13,7 @@ import sum from "."; const { fetchJson, fetchJsons } = utils; -const { port } = serve({ debug: true, max: 100, port: 8888, wait: 120 }); +const { port } = serve({ debug: true, max: 31, port: 8888, wait: 120 }); const urlRwanda = `http://localhost:${port}/data/RWA_MNH_ANC.tif`; const bboxRwanda = require("../../data/RwandaBufferedBoundingBox.json"); @@ -59,6 +59,7 @@ test("(Legacy) Get Sum from Veneto Geonode", async ({ eq }) => { ]; const [georaster, geojson] = values; const results = sum(georaster, geojson); + eq(Array.isArray(results), true); const actualValue = Number(results[0].toFixed(2)); const expectedValue = 24_943.31; // rasterstats says 24,963.465454101562 eq(actualValue, expectedValue); diff --git a/src/utils/utils.module.js b/src/utils/utils.module.js index 91255e6..5e5034b 100644 --- a/src/utils/utils.module.js +++ b/src/utils/utils.module.js @@ -122,6 +122,18 @@ const utils = { return Array.isArray(parsed) ? parsed[0] : parsed; }, + // checks if bbox in form { xmin: 20, xmax: 32, ymin: -3, ymax: 0 } + isBboxObj(geometry) { + return ( + typeof geometry === "object" && + Array.isArray(geometry) === false && + typeof geometry.xmin === "number" && + typeof geometry.xmax === "number" && + typeof geometry.ymin === "number" && + typeof geometry.ymax === "number" + ); + }, + isPolygonal(geometry) { const polys = mpoly.get(geometry);