Skip to content

Commit

Permalink
cleaned up more virtual resampling code and added resampling support …
Browse files Browse the repository at this point in the history
…to georasters fully loaded into memory
  • Loading branch information
DanielJDufour committed May 11, 2024
1 parent 4fcae79 commit d4a1d39
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 81 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"test:mode": "node -r esm ./src/mode/mode.test.js",
"test:modes": "node -r esm ./src/modes/modes.test.js",
"test:range": "node -r esm ./src/range/range.test.js",
"test:stats": "node -r esm ./src/stats/stats.test.js",
"test:sum": "node -r esm ./src/sum/sum.test.js",
"test-loading-builds": "node test-loading-builds.js",
"setup": "bash setup.sh"
Expand Down
13 changes: 7 additions & 6 deletions src/intersect-polygon/intersect-polygon.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ const intersectPolygon = (georaster, geometry, perPixelFunction, { debug_level =
if (geometry_bbox_size_ratios.some(([xratio, yratio]) => xratio <= 1 || yratio <= 1)) {
const geometry_bboxes_multipliers = geometry_bbox_size_ratios.map(([xratio, yratio]) => [2 / xratio, 2 / yratio]);
vrm = [
Math.ceil(fastMax(geometry_bboxes_multipliers.map(([xmul, ymul]) => xmul))),
Math.ceil(fastMax(geometry_bboxes_multipliers.map(([xmul, ymul]) => ymul)))
// don't drop more than 10,000 sample lines per pixel
Math.min(10000, Math.ceil(fastMax(geometry_bboxes_multipliers.map(([xmul, ymul]) => xmul)))),
Math.min(10000, Math.ceil(fastMax(geometry_bboxes_multipliers.map(([xmul, ymul]) => ymul))))
];
} else {
vrm = VRM_NO_RESAMPLING;
Expand All @@ -79,10 +80,10 @@ const intersectPolygon = (georaster, geometry, perPixelFunction, { debug_level =
const intersections = dufour_peyton_intersection.calculate({
debug: false,
raster_bbox: georaster_bbox,
raster_height: georaster.height,
raster_width: georaster.width,
pixel_height: georaster.pixelHeight,
pixel_width: georaster.pixelWidth,
raster_height: georaster.height * yvrm,
raster_width: georaster.width * xvrm,
pixel_height: georaster.pixelHeight / yvrm,
pixel_width: georaster.pixelWidth / xvrm,
geometry
});

Expand Down
2 changes: 1 addition & 1 deletion src/max/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import stat from "../stat";
*/

export default function max(georaster, geometry, test) {
return stat(georaster, geometry, "max", test, { vrm: 100 });
return stat(georaster, geometry, "max", test, { vrm: "minimal" });
}
14 changes: 7 additions & 7 deletions src/max/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { serve } from "srvd";
import load from "../load";
import max from ".";

serve({ debug: true, max: 5, port: 3000 });
serve({ debug: true, max: 7, port: 3000, wait: 240 });

const url = "http://localhost:3000/data/test.tiff";

Expand Down Expand Up @@ -65,13 +65,13 @@ test("Max with Web Mercator Bounding Box and GeoRaster URL", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await max(url, geojson)
eq(result, [38]);
})
const result = await max(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await max(url, geojson)
eq(result, [38]);
})
const result = await max(url, geojson);
eq(result, [38]);
});
2 changes: 1 addition & 1 deletion src/mean/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import stat from "../stat";
*/

export default function mean(georaster, geometry, test) {
return stat(georaster, geometry, "mean", test, { vrm: 100 });
return stat(georaster, geometry, "mean", test, { vrm: "minimal" });
}
27 changes: 21 additions & 6 deletions src/mean/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,28 @@ test("(Modern) Mean from GeoJSON", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await mean(url, geojson)
eq(result, [38]);
})
const result = await mean(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await mean(url, geojson)
eq(result, [33.61065313887127]);
})
const result = await mean(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels (sync)", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const georaster = await load(url);
const result = await mean(georaster, geojson);
eq(result, [38]);
});

test("virtual resampling with bbox", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geom = [166.06811846012403, -46.42907237702467, 166.23189176587618, -46.40887433963862];
const result = await mean(url, geom);
eq(result, [38]);
});
2 changes: 1 addition & 1 deletion src/median/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import stat from "../stat";
*/

export default function median(georaster, geometry, test) {
return stat(georaster, geometry, "median", test, { vrm: 100 });
return stat(georaster, geometry, "median", test, { vrm: "minimal" });
}
12 changes: 6 additions & 6 deletions src/median/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ test("Get Median from Whole Raster from url", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await median(url, geojson)
eq(result, [38]);
})
const result = await median(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await median(url, geojson)
eq(result, [38]);
})
const result = await median(url, geojson);
eq(result, [38]);
});
2 changes: 1 addition & 1 deletion src/min/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import stat from "../stat";
*/

export default function min(georaster, geometry, test) {
return stat(georaster, geometry, "min", test, { vrm: 100 });
return stat(georaster, geometry, "min", test, { vrm: "minimal" });
}
14 changes: 7 additions & 7 deletions src/min/min.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { serve } from "srvd";
import load from "../load";
import min from ".";

serve({ debug: true, max: 5, port: 3000 });
serve({ debug: true, max: 5, port: 3000, wait: 120 });

const url = "http://localhost:3000/data/test.tiff";
const bbox = [80.63, 7.42, 84.21, 10.1];
Expand Down Expand Up @@ -65,13 +65,13 @@ test("(Modern) Get Min from whole Raster", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await min(url, geojson)
eq(result, [38]);
})
const result = await min(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await min(url, geojson)
eq(result, [1]);
})
const result = await min(url, geojson);
eq(result, [38]);
});
2 changes: 1 addition & 1 deletion src/mode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import stat from "../stat";
*/

export default function mode(georaster, geometry, test) {
return stat(georaster, geometry, "mode", test, { vrm: 100 });
return stat(georaster, geometry, "mode", test, { vrm: "minimal" });
}
12 changes: 6 additions & 6 deletions src/mode/mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ test("(Modern) Mode Polygon", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await mode(url, geojson)
eq(result, [38]);
})
const result = await mode(url, geojson);
eq(result, [38]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await mode(url, geojson)
eq(result, [38]);
})
const result = await mode(url, geojson);
eq(result, [38]);
});
2 changes: 1 addition & 1 deletion src/modes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import stat from "../stat";
*/

export default function modes(georaster, geometry, test) {
return stat(georaster, geometry, "modes", test, { vrm: 100 });
return stat(georaster, geometry, "modes", test, { vrm: "minimal" });
}
12 changes: 6 additions & 6 deletions src/modes/modes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ test("(Modern) Modes Polygon", async ({ eq }) => {
test("virtual resampling, contained", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await modes(url, geojson)
eq(result, [38]);
})
const result = await modes(url, geojson);
eq(result, [[38]]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await modes(url, geojson)
eq(result, [38]);
})
const result = await modes(url, geojson);
eq(result, [[38]]);
});
2 changes: 1 addition & 1 deletion src/range/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import stat from "../stat";
*/

export default function range(georaster, geometry, test) {
return stat(georaster, geometry, "range", test, { vrm: 100 });
return stat(georaster, geometry, "range", test, { vrm: "minimal" });
}
22 changes: 14 additions & 8 deletions src/range/range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import test from "flug";
import { serve } from "srvd";
import range from ".";

serve({ debug: true, max: 25, port: 3000 });
const sleep = ms => new Promise(resolve => setTimeout(() => resolve(), ms));

serve({ debug: true, max: 22, port: 3000, wait: 160 });

const url = "http://localhost:3000/data/test.tiff";
const bbox = [80.63, 7.42, 84.21, 10.1];
Expand Down Expand Up @@ -40,18 +42,22 @@ test("(Modern) Get Range from Polygon", async ({ eq }) => {
test("(Modern) Get Range from whole Raster", async ({ eq }) => {
const results = await range(url);
eq(results, [8131.2]);

// weird hack to give time for main thread and/or srvd to clear up tasks before next request is sent
await sleep(1000);
});

test("virtual resampling, contained", async ({ eq }) => {
const geojson_res = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson");
const geojson = await geojson_res.json();
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-one.geojson").then(res => res.json());
const result = await range(url, geojson)
eq(result, [0]);
})
const result = await range(url, geojson);
eq(result, [0]);
});

test("virtual resampling, intersecting 4 pixels", async ({ eq }) => {
const url = "http://localhost:3000/data/geotiff-test-data/nz_habitat_anticross_4326_1deg.tif";
const geojson = await fetch("http://localhost:3000/data/virtual-resampling/virtual-resampling-intersect.geojson").then(res => res.json());
const result = await range(url, geojson)
eq(result, [37]);
})
const result = await range(url, geojson);
eq(result, [0]); // range is zero because by default doing minimal (i.e. bare minimum) resampling
});
Loading

0 comments on commit d4a1d39

Please sign in to comment.