Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/turf-isobands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ value breaks and generates filled contour isobands.

### Parameters

* `pointGrid` **[FeatureCollection][1]<[Point][2]>** input points - must be square or rectangular
* `pointGrid` **[FeatureCollection][1]<[Point][2]>** input points - must be square or rectangular and already gridded
* `breaks` **[Array][3]<[number][4]>** where to draw contours
* `options` **[Object][5]** options on output (optional, default `{}`)

Expand Down
15 changes: 14 additions & 1 deletion packages/turf-isobands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type GroupedRings =
* value breaks and generates filled contour isobands.
*
* @function
* @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular
* @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular and already gridded
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a clearer way to express this? Maybe cribbing from the comment where we actually do the check:

must be square or rectangular, and already gridded. That is, have consistent x and y dimensions, and be at least 2x2 in size.

* @param {Array<number>} breaks where to draw contours
* @param {Object} [options={}] options on output
* @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
Expand Down Expand Up @@ -70,6 +70,19 @@ function isobands(

// Isoband methods
const matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });

// check that the resulting matrix has consistent x and y dimensions and
// has at least a 2x2 size so that we can actually build grid squares
const dx = matrix[0].length;
if (matrix.length < 2 || dx < 2) {
throw new Error("Matrix of points must be at least 2x2");
}
for (let i = 1; i < matrix.length; i++) {
if (matrix[i].length !== dx) {
throw new Error("Matrix of points is not uniform in the x dimension");
}
}

let contours = createContourLines(matrix, breaks, zProperty);
contours = rescaleContours(contours, matrix, pointGrid);

Expand Down
45 changes: 45 additions & 0 deletions packages/turf-isobands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { lineString } from "@turf/helpers";
import { randomPolygon } from "@turf/random";
import { matrixToGrid } from "./lib/matrix-to-grid.js";
import { isobands } from "./index.js";
import { FeatureCollection, Point } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -92,3 +93,47 @@ test("isobands -- throws", (t) => {

t.end();
});

test("isobands -- checks for usable grid", (t) => {
const input: FeatureCollection<Point> = {
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [0, 0],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [0, 1],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [1, 1],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [1e-10, 1], // almost the same lng as the first feature, but different enough to break everything
},
},
],
};
t.throws(() => {
isobands(input, [0, 1], { zProperty: "z" });
});
t.end();
});
2 changes: 1 addition & 1 deletion packages/turf-isolines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ value breaks and generates [isolines][3].

### Parameters

* `pointGrid` **[FeatureCollection][1]<[Point][2]>** input points
* `pointGrid` **[FeatureCollection][1]<[Point][2]>** input points - must be square or rectangular and already gridded
* `breaks` **[Array][4]<[number][5]>** values of `zProperty` where to draw isolines
* `options` **[Object][6]** Optional parameters (optional, default `{}`)

Expand Down
15 changes: 14 additions & 1 deletion packages/turf-isolines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
* value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).
*
* @function
* @param {FeatureCollection<Point>} pointGrid input points
* @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular and already gridded
* @param {Array<number>} breaks values of `zProperty` where to draw isolines
* @param {Object} [options={}] Optional parameters
* @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
Expand Down Expand Up @@ -69,6 +69,19 @@ function isolines(

// Isoline methods
const matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do really need to find a way to share common code internally 😅

// check that the resulting matrix has consistent x and y dimensions and
// has at least a 2x2 size so that we can actually build grid squares
const dx = matrix[0].length;
if (matrix.length < 2 || dx < 2) {
throw new Error("Matrix of points must be at least 2x2");
}
for (let i = 1; i < matrix.length; i++) {
if (matrix[i].length !== dx) {
throw new Error("Matrix of points is not uniform in the x dimension");
}
}

const createdIsoLines = createIsoLines(
matrix,
breaks,
Expand Down
45 changes: 45 additions & 0 deletions packages/turf-isolines/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { randomPolygon } from "@turf/random";
import { lineString } from "@turf/helpers";
import { matrixToGrid } from "./lib/matrix-to-grid.js";
import { isolines } from "./index.js";
import { FeatureCollection, Point } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -136,3 +137,47 @@ test("isolines -- handling properties", (t) => {
t.equal(lines.features[0].properties.source, "foobar");
t.end();
});

test("isolines -- checks for usable grid", (t) => {
const input: FeatureCollection<Point> = {
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [0, 0],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [0, 1],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [1, 1],
},
},
{
type: "Feature",
properties: { z: 0 },
geometry: {
type: "Point",
coordinates: [1e-10, 1],
},
},
],
};
t.throws(() => {
isolines(input, [0, 1], { zProperty: "z" });
});
t.end();
});