Skip to content

Commit

Permalink
add support for simple bbox obj resampling
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jun 8, 2024
1 parent ae55519 commit bf45190
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/stats/stats.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
3 changes: 2 additions & 1 deletion src/sum/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/utils.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bf45190

Please sign in to comment.