Skip to content

Commit

Permalink
report no data values for polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Oct 7, 2023
1 parent 2b41bad commit c9b54e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/intersect-polygon/intersect-polygon.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const intersectPolygon = (georaster, geometry, perPixelFunction, { debug_level =
samples = [{ intersections, sample, offset: [0, 0] }];
} else if (georaster.getValues) {
const geometry_bboxes = calcAll(geometry);
if (debug_level >= 2) console.log("[geoblaze] geometry_bboxes:", geometry_bboxes);

if (!geometry_bboxes.some(bbox => booleanIntersects(bbox, georaster_bbox))) return;

Expand Down Expand Up @@ -121,9 +122,7 @@ const intersectPolygon = (georaster, geometry, perPixelFunction, { debug_level =
const row = band[irow];
if (row) {
const value = row[icol];
if (value != undefined && value !== noDataValue) {
perPixelFunction(value, iband, yoff + irow, xoff + icol);
}
perPixelFunction(value, iband, yoff + irow, xoff + icol);
}
});
}
Expand Down
19 changes: 12 additions & 7 deletions src/stats/stats.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ const stats = (georaster, geometry, calcStatsOptions, test, { debug_level = 0 }
// the third argument of this function is a function which
// runs for every pixel in the polygon. Here we add them to
// an array, so we can later on calculate stats for each band separately
const done = intersectPolygon(georaster, geometry, (value, bandIndex) => {
if (values[bandIndex]) {
values[bandIndex].push(value);
} else {
values[bandIndex] = [value];
}
});
const done = intersectPolygon(
georaster,
geometry,
(value, bandIndex) => {
if (values[bandIndex]) {
values[bandIndex].push(value);
} else {
values[bandIndex] = [value];
}
},
{ debug_level }
);

return QuickPromise.resolve(done).then(() => {
const bands = values.filter(band => band.length !== 0);
Expand Down
17 changes: 17 additions & 0 deletions src/stats/stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,20 @@ test("edge", async ({ eq }) => {
const results = await stats(georaster, geojson, { stats: ["count", "min", "max", "sum"] });
eq(results, [{ count: 2, min: 9.936111450195312, max: 19.24805450439453, sum: 29.184165954589844 }]);
});

test("multipolygon vs 2 polygons", async ({ eq }) => {
const geojson = JSON.parse(readFileSync("./data/antimeridian/split.geojson", "utf-8"));
const georaster = await parse("http://localhost:3000/data/geotiff-test-data/spam2005v3r2_harvested-area_wheat_total.tiff");
const _stats = ["count", "invalid", "min", "max", "sum", "valid"];

const expected = [{ count: 1152, valid: 4, invalid: 1148, min: 0, max: 0, sum: 0 }];
eq(await stats(georaster, geojson, { stats: _stats }, undefined, { debug_level: 5 }), expected);
eq(await stats(georaster, geojson.features[0], { stats: _stats }), expected);

const [poly1, poly2] = geojson.features[0].geometry.coordinates;
const results1 = await stats(georaster, poly1, { debug_level: 5, stats: _stats });
eq(results1, [{ count: 576, valid: 0, invalid: 576, sum: 0, min: undefined, max: undefined }]);

const results2 = await stats(georaster, poly2, { debug_level: 5, stats: _stats });
eq(results2, [{ count: 576, valid: 4, invalid: 572, min: 0, max: 0, sum: 0 }]);
});

0 comments on commit c9b54e7

Please sign in to comment.