Skip to content

Commit

Permalink
Updates overlapFeatures and overlapRaster to use roundDecimal and add…
Browse files Browse the repository at this point in the history
…s test for roundDecimal
  • Loading branch information
avmey committed Sep 19, 2023
1 parent 067df0b commit 3ad286f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
15 changes: 15 additions & 0 deletions packages/geoprocessing/src/helpers/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @jest-environment node
* @group unit
*/

import { roundDecimal } from "./number";

describe("number", () => {
test("number - roundDecimal with 6 digit precision", () => {
expect(roundDecimal(1, 6)).toBe(1);
expect(roundDecimal(0.000000858374, 6)).toBe(0);
expect(roundDecimal(8.58e-7, 6)).toBe(0);
expect(roundDecimal(1111.1111119, 6)).toBe(1111.111112);
});
});
5 changes: 3 additions & 2 deletions packages/geoprocessing/src/toolbox/overlapFeatures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import area from "@turf/area";
import fix from "../testing/fixtures/squareSketches";
import { firstMatchingMetric } from "../metrics";
import { testWithinPerc } from "../testing";
import { roundDecimal } from "../helpers";

describe("overlapFeatures", () => {
test("function is present", () => {
Expand All @@ -27,9 +28,9 @@ describe("overlapFeatures", () => {

test("overlapFeatures - sketch polygon fully inside - simplified precision", async () => {
const metrics = await overlapFeatures("test", [fix.outer], fix.sketch1, {
simplifyPrecision: true,
truncate: true,
});
expect(metrics[0].value).toBe(parseFloat(area(fix.sketch1).toPrecision(6)));
expect(metrics[0].value).toBe(roundDecimal(area(fix.sketch1), 6));
});

test("overlapFeatures - sketch multipolygon fully inside", async () => {
Expand Down
13 changes: 5 additions & 8 deletions packages/geoprocessing/src/toolbox/overlapFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
chunk,
clip,
clipMultiMerge,
roundDecimal,
} from "../helpers";
import { createMetric } from "../metrics";
import { featureCollection, MultiPolygon } from "@turf/helpers";
Expand All @@ -20,8 +21,8 @@ interface OverlapFeatureOptions {
/** If sketch collection, will include its child sketch metrics in addition to collection metrics, defaults to true */
includeChildMetrics?: boolean;
sumProperty?: string;
/** Simplifies results to 6 significant digits to avoid floating pt arithmetric differences, defaults to false */
simplifyPrecision?: boolean;
/** Truncates results to 6 digits, defaults to false */
truncate?: boolean;
}

// ToDo: support
Expand Down Expand Up @@ -90,9 +91,7 @@ export async function overlapFeatures(
return createMetric({
metricId,
sketchId: curSketch.properties.id,
value: newOptions.simplifyPrecision
? parseFloat(sketchValue.toPrecision(6))
: sketchValue,
value: newOptions.truncate ? roundDecimal(sketchValue, 6) : sketchValue,
extra: {
sketchName: curSketch.properties.name,
},
Expand All @@ -110,9 +109,7 @@ export async function overlapFeatures(
createMetric({
metricId,
sketchId: sketch.properties.id,
value: newOptions.simplifyPrecision
? parseFloat(sumValue.toPrecision(6))
: sumValue,
value: newOptions.truncate ? roundDecimal(sumValue, 6) : sumValue,
extra: {
sketchName: sketch.properties.name,
isCollection: true,
Expand Down
8 changes: 5 additions & 3 deletions packages/geoprocessing/src/toolbox/overlapRaster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("overlapRaster test", () => {
const raster = await parseGeoraster(
[
[
[1, 2323232],
[1, 0.0023456],
[1, 1],
],
],
Expand All @@ -71,9 +71,11 @@ describe("overlapRaster test", () => {
pixelHeight: 10,
}
);
const metrics = await overlapRaster("test", raster, fix.topRightPoly, true);
const metrics = await overlapRaster("test", raster, fix.topRightPoly, {
truncate: true,
});
expect(metrics.length).toBe(1);
expect(metrics[0].value).toBe(2323230); // Not 2323232
expect(metrics[0].value).toBe(0.002346);
});

test("overlapRaster - whole raster sum should be 5", async () => {
Expand Down
18 changes: 12 additions & 6 deletions packages/geoprocessing/src/toolbox/overlapRaster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Sketch, SketchCollection, Polygon, Metric } from "../types";
import { isSketchCollection } from "../helpers";
import { isSketchCollection, roundDecimal } from "../helpers";
import { createMetric } from "../metrics";
import { featureEach } from "@turf/meta";
import { MultiPolygon } from "@turf/helpers";
Expand All @@ -8,6 +8,11 @@ import { getSum } from "./geoblaze";
// @ts-ignore
import { Georaster } from "geoblaze";

interface OverlapRasterOptions {
/** Truncates results to 6 digits, defaults to false */
truncate?: boolean;
}

/**
* Returns metrics representing sketch overlap with raster.
* If sketch collection, then calculate overlap for all child sketches also
Expand All @@ -21,9 +26,10 @@ export async function overlapRaster(
sketch:
| Sketch<Polygon | MultiPolygon>
| SketchCollection<Polygon | MultiPolygon>,
/** Simplifies results to 6 significant digits to avoid floating pt arithmetric differences, defaults to false */
simplifyPrecision?: boolean
options?: Partial<OverlapRasterOptions>
): Promise<Metric[]> {
const newOptions: OverlapRasterOptions = options || {};

// Get raster sum for each feature
const sumPromises: Promise<number>[] = [];
const sumFeatures: Sketch[] = [];
Expand All @@ -41,7 +47,7 @@ export async function overlapRaster(
createMetric({
metricId,
sketchId: sumFeatures[index].properties.id,
value: simplifyPrecision ? parseFloat(curSum.toPrecision(6)) : curSum,
value: newOptions.truncate ? roundDecimal(curSum, 6) : curSum,
extra: {
sketchName: sumFeatures[index].properties.name,
},
Expand All @@ -56,8 +62,8 @@ export async function overlapRaster(
createMetric({
metricId,
sketchId: sketch.properties.id,
value: simplifyPrecision
? parseFloat(collSumValue.toPrecision(6))
value: newOptions.truncate
? roundDecimal(collSumValue, 6)
: collSumValue,
extra: {
sketchName: sketch.properties.name,
Expand Down

0 comments on commit 3ad286f

Please sign in to comment.