Skip to content

Commit

Permalink
add more tests to getFeaturesForSketchBBoxes, improve squareSketches,…
Browse files Browse the repository at this point in the history
… add squares.fgb with txt file showing layout, update squares.json
  • Loading branch information
twelch committed Dec 12, 2024
1 parent 170db00 commit cc4b829
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 155 deletions.
Binary file added packages/geoprocessing/data/in/squares.fgb
Binary file not shown.
101 changes: 53 additions & 48 deletions packages/geoprocessing/data/in/squares.json
Original file line number Diff line number Diff line change
@@ -1,100 +1,105 @@
{
"type": "FeatureCollection",
"name": "squares",
"crs": {
"type": "name",
"properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }
},
"features": [
{
"type": "Feature",
"properties": {},
"properties": { "name": "outsideTwoByPolyBottomRight" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0, 0],
[2, 0],
[2, 2],
[0, 2],
[0, 0]
[3.0, 0.0],
[4.0, 0.0],
[4.0, 1.0],
[3.0, 1.0],
[3.0, 0.0]
]
],
"type": "Polygon"
]
}
},
{
"type": "Feature",
"properties": {},
"properties": { "name": "outsideTwoByPolyTopRight" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0]
[3.0, 3.0],
[4.0, 3.0],
[4.0, 4.0],
[3.0, 4.0],
[3.0, 3.0]
]
],
"type": "Polygon"
]
}
},
{
"type": "Feature",
"properties": {},
"properties": { "name": "outsideTwoByPolyTopLeft" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[1, 1],
[3, 1],
[3, 2],
[1, 2],
[1, 1]
[0.0, 3.0],
[1.0, 3.0],
[1.0, 4.0],
[0.0, 4.0],
[0.0, 3.0]
]
],
"type": "Polygon"
]
}
},
{
"type": "Feature",
"properties": {},
"properties": { "name": "halfInsideTwoByPoly" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[3, 3],
[4, 3],
[4, 4],
[3, 4],
[3, 3]
[1.0, 1.0],
[3.0, 1.0],
[3.0, 2.0],
[1.0, 2.0],
[1.0, 1.0]
]
],
"type": "Polygon"
]
}
},
{
"type": "Feature",
"properties": {},
"properties": { "name": "twoByPoly" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[0, 3],
[1, 3],
[1, 4],
[0, 4],
[0, 3]
[0.0, 0.0],
[2.0, 0.0],
[2.0, 2.0],
[0.0, 2.0],
[0.0, 0.0]
]
],
"type": "Polygon"
]
}
},
{
"type": "Feature",
"properties": {},
"properties": { "name": "insideTwoByPoly" },
"geometry": {
"type": "Polygon",
"coordinates": [
[
[3, 0],
[4, 0],
[4, 1],
[3, 1],
[3, 0]
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0],
[0.0, 0.0]
]
],
"type": "Polygon"
]
}
}
]
Expand Down
54 changes: 54 additions & 0 deletions packages/geoprocessing/data/in/squaresReadme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@




squares.fgb polygons are rectangles in increments of 1 degree

4 @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
@ ---------------- @ @ ---------------- @
3 @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@








2 wide polygon overlaps top right of 2x2
2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
@ ---------------- @ +++++++++++++++++@ ---------------- @
1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
@+++++++++++++++++ @ ---------------- @ @ ---------------- @
0 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@
1x1 poly overlaps
bottom left of
2x2 square

0 1 2 3 4


Original file line number Diff line number Diff line change
@@ -1,39 +1,100 @@
import { expect, test } from "vitest";
import { loadFgb } from "./flatgeobuf.js";
import canonicalize from "../util/canonicalize.js";
import { deserialize } from "flatgeobuf/lib/mjs/geojson.js";

import { readFileSync } from "node:fs";
import path from "node:path";
import { isFeatureCollection } from "../index.js";
import fs from "fs-extra";
import {
getFeaturesForBBoxes,
getFeaturesForSketchBBoxes,
} from "./getFeaturesForSketchBBoxes.js";
import { genSampleSketch } from "../helpers/sketch.js";
import { bbox, featureCollection, polygon } from "@turf/turf";
import fix from "../testing/fixtures/squareSketches.js";
import { SketchCollection } from "../types/sketch.js";
import { Polygon } from "geojson";

describe("getFeaturesForBBoxes", () => {
test("getFeaturesForBBoxes - simple", async () => {
const canonicalStr = canonicalize([
{
id: 0, // this is not in the data, but fgb client automatically adds it on deserialize as of v3.36.0
type: "Feature",
properties: {
name: "World boundary",
description: "World",
},
geometry: {
coordinates: [
[
[-180, 90],
[-180, -90],
[180, -90],
[180, 90],
[-180, 90],
],
],
type: "Polygon",
},
},
]);
const worldJson = fs.readJsonSync("data/in/world.json");
// pull features out of FC and add index based ID, just as the flatgeobuf client does on read
const worldFeatures = worldJson.features.map((f, index) => {
f.id = index;
return f;
});
const canonicalStr = canonicalize(worldFeatures);
const url = "http://127.0.0.1:8080/data/in/world.fgb";
const features = await getFeaturesForBBoxes([[-1, -1, 1, 1]], url);
expect(features.length).toEqual(1);
expect(canonicalize(features)).toEqual(canonicalStr);
});
});

describe("getFeaturesForSketchBBoxes", () => {
test("getFeaturesForSketchBBoxes - simple", async () => {
const worldJson = fs.readJsonSync("data/in/world.json");
// pull features out of FC and add index based ID, just as the flatgeobuf client does on read
const worldFeatures = worldJson.features.map((f, index) => {
f.id = index;
return f;
});
const canonicalStr = canonicalize(worldFeatures);
const url = "http://127.0.0.1:8080/data/in/world.fgb";
const features = await loadFgb(url);
const poly = polygon([
[
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0],
],
]);
const sketch = genSampleSketch(poly.geometry, "sketch1");
const features = await getFeaturesForSketchBBoxes(sketch, url);
expect(features.length).toEqual(1);
expect(canonicalize(features)).toEqual(canonicalStr);
});

test("getFeaturesForSketchBBoxes - bottom left squares", async () => {
const url = "http://127.0.0.1:8080/data/in/squares.fgb";
const poly = polygon([
[
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0],
],
]);
const sketch = genSampleSketch(poly.geometry, "sketch1");
const features = await getFeaturesForSketchBBoxes(sketch, url);
// Should pick up twoByPoly, insideTwoByPoly, and the corner of halfInsideTwoByPoly
expect(features.length).toEqual(3);
});

test("getFeaturesForSketchBBoxes - sketch collection", async () => {
const url = "http://127.0.0.1:8080/data/in/squares.fgb";
const testSC: SketchCollection<Polygon> = {
type: "FeatureCollection",
properties: {
id: "test",
name: "Collection 1",
updatedAt: "2021-11-20T00:00:34.269Z",
createdAt: "2021-11-19T23:34:12.889Z",
sketchClassId: "615b65a2aac8c8285d50d9f3",
isCollection: true,
userAttributes: [],
},
bbox: bbox(
featureCollection([
fix.outsideTwoByPolyTopLeftSketch,
fix.outsideTwoByPolyBottomRightSketch,
]),
),
features: [
fix.outsideTwoByPolyTopLeftSketch,
fix.outsideTwoByPolyBottomRightSketch,
],
};
const features = await getFeaturesForSketchBBoxes(testSC, url);
// Should pick up outsideTwoByPolyTopLeftSketch, outsideTwoByPolyBottomRightSketch, and the corner of halfInsideTwoByPoly
expect(features.length).toEqual(3);
});
});
Loading

0 comments on commit cc4b829

Please sign in to comment.