Skip to content

Commit

Permalink
Fixing turf-isolines to no longer ignore the first break passed. Wher…
Browse files Browse the repository at this point in the history
…e it looked like the unit tests have had a 0 break added as a work around e.g. 5 breaks but only 4 break properties, have removed the presumed workaround and reviewed test fixture output. Resolves #2129.
  • Loading branch information
smallsaucepan committed Nov 9, 2023
1 parent 6d5fda5 commit bfc14e5
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/turf-isolines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function createIsoLines(
breaksProperties: GeoJsonProperties[]
): Feature<MultiLineString>[] {
const results = [];
for (let i = 1; i < breaks.length; i++) {
for (let i = 0; i < breaks.length; i++) {
const threshold = +breaks[i]; // make sure it's a number

const properties = { ...commonProperties, ...breaksProperties[i] };
Expand Down
35 changes: 34 additions & 1 deletion packages/turf-isolines/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ test("isolines", (t) => {
t.end();
});

test("isolines - skipping first break, from issue #2129", (t) => {
const points = pointGrid([0, 10, 20, 30], 100);
for (var i = 0; i < points.features.length; i++) {
points.features[i].properties.temperature = Math.random() * 12;
}

const breaks = [5, 10];

const lines = isolines(points, breaks, {
zProperty: "temperature",
breaksProperties: [
{ name: "break5", stroke: "#F00" },
{ name: "break10", stroke: "#0F0" },
],
});

lines.features.push(
lineString(getCoords(envelope(points))[0], {
description: "Debug line for testing",
stroke: "#F00",
"stroke-width": 1,
})
);

// Make sure an isoline is created for each break, and that it's
// geometry isn't empty.
t.equal(lines.features[0].properties.name, "break5");
t.assert(lines.features[0].geometry.coordinates[0].length > 1);
t.equal(lines.features[1].properties.name, "break10");
t.assert(lines.features[1].geometry.coordinates[0].length > 1);
t.end();
});

test("isolines -- throws", (t) => {
const points = pointGrid([-70.823364, -33.553984, -70.473175, -33.302986], 5);

Expand Down Expand Up @@ -92,7 +125,7 @@ test("isolines -- handling properties", (t) => {
commonProperties: commonProperties,
breaksProperties: breaksProperties,
});
t.equal(lines.features[0].properties.name, "break2");
t.equal(lines.features[0].properties.name, "break1");
t.equal(lines.features[0].properties.source, "foobar");
t.end();
});
2 changes: 1 addition & 1 deletion packages/turf-isolines/test/in/bigMatrix.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"origin": [6.5, 46],
"cellSize": 10,
"breaks": [0, 0.24, 0.5, 1.5, 2.5, 100],
"breaks": [0.24, 0.5, 1.5, 2.5, 100],
"zProperty": "pressure",
"commonProperties": {
"stroke-width": 4,
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-isolines/test/in/matrix2.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"origin": [10.85, 44],
"cellSize": 20,
"breaks": [0, 4.5, 9, 13.5, 18],
"breaks": [4.5, 9, 13.5, 18],
"commonProperties": {
"stroke-width": 3,
"stroke": "darkred",
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-isolines/test/in/pointGrid.geojson
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "FeatureCollection",
"properties": {
"breaks": [0, 20, 40, 80, 160],
"breaks": [20, 40, 80, 160],
"breaksProperties": [
{
"fill-opacity": 0.5
Expand Down
23 changes: 11 additions & 12 deletions packages/turf-isolines/test/out/bigMatrix.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"properties": {
"stroke-width": 4,
"fill-opacity": 0.4,
"stroke": "blue",
"fill": "blue",
"stroke": "grey",
"fill": "grey",
"pressure": 0.24
},
"geometry": {
Expand Down Expand Up @@ -265,8 +265,8 @@
"properties": {
"stroke-width": 4,
"fill-opacity": 0.4,
"stroke": "green",
"fill": "green",
"stroke": "blue",
"fill": "blue",
"pressure": 0.5
},
"geometry": {
Expand Down Expand Up @@ -620,8 +620,8 @@
"properties": {
"stroke-width": 4,
"fill-opacity": 0.4,
"stroke": "yellow",
"fill": "yellow",
"stroke": "green",
"fill": "green",
"pressure": 1.5
},
"geometry": {
Expand Down Expand Up @@ -964,8 +964,8 @@
"properties": {
"stroke-width": 4,
"fill-opacity": 0.4,
"stroke": "red",
"fill": "red",
"stroke": "yellow",
"fill": "yellow",
"pressure": 2.5
},
"geometry": {
Expand Down Expand Up @@ -1162,12 +1162,11 @@
"properties": {
"stroke-width": 4,
"fill-opacity": 0.4,
"stroke": "red",
"fill": "red",
"pressure": 100
},
"geometry": {
"type": "MultiLineString",
"coordinates": []
}
"geometry": { "type": "MultiLineString", "coordinates": [] }
},
{
"type": "Feature",
Expand Down
42 changes: 33 additions & 9 deletions packages/turf-isolines/test/out/matrix1.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,37 @@
"features": [
{
"type": "Feature",
"properties": {
"temperature": 4
},
"properties": { "temperature": 2 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[10.863591, 44.279864],
[11.054363, 44.144966],
[11.308726, 44.144966],
[11.56309, 44.144966],
[11.817453, 44.144966],
[12.071816, 44.144966],
[12.262588, 44.279864],
[12.262588, 44.459728],
[12.262588, 44.639592],
[12.262588, 44.819456],
[12.071816, 44.954354],
[11.817453, 44.954354],
[11.56309, 44.954354],
[11.308726, 44.954354],
[11.054363, 44.954354],
[10.863591, 44.819456],
[10.863591, 44.639592],
[10.863591, 44.459728],
[10.863591, 44.279864]
]
]
}
},
{
"type": "Feature",
"properties": { "temperature": 4 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand Down Expand Up @@ -35,9 +63,7 @@
},
{
"type": "Feature",
"properties": {
"temperature": 8
},
"properties": { "temperature": 8 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand All @@ -59,9 +85,7 @@
},
{
"type": "Feature",
"properties": {
"temperature": 12
},
"properties": { "temperature": 12 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand Down
7 changes: 4 additions & 3 deletions packages/turf-isolines/test/out/matrix2.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"stroke-width": 3,
"stroke": "darkred",
"fill": "darkred",
"fill-opacity": 0.5,
"fill-opacity": 0.4,
"elevation": 4.5
},
"geometry": {
Expand Down Expand Up @@ -56,7 +56,7 @@
"stroke-width": 3,
"stroke": "darkred",
"fill": "darkred",
"fill-opacity": 0.7,
"fill-opacity": 0.5,
"elevation": 9
},
"geometry": {
Expand Down Expand Up @@ -105,7 +105,7 @@
"stroke-width": 3,
"stroke": "darkred",
"fill": "darkred",
"fill-opacity": 0.8,
"fill-opacity": 0.7,
"elevation": 13.5
},
"geometry": {
Expand Down Expand Up @@ -136,6 +136,7 @@
"stroke-width": 3,
"stroke": "darkred",
"fill": "darkred",
"fill-opacity": 0.8,
"elevation": 18
},
"geometry": {
Expand Down
24 changes: 5 additions & 19 deletions packages/turf-isolines/test/out/pointGrid.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"features": [
{
"type": "Feature",
"properties": {
"fill-opacity": 0.6,
"population": 20
},
"properties": { "fill-opacity": 0.5, "population": 20 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand Down Expand Up @@ -66,10 +63,7 @@
},
{
"type": "Feature",
"properties": {
"fill-opacity": 0.7,
"population": 40
},
"properties": { "fill-opacity": 0.6, "population": 40 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand Down Expand Up @@ -133,10 +127,7 @@
},
{
"type": "Feature",
"properties": {
"fill-opacity": 0.8,
"population": 80
},
"properties": { "fill-opacity": 0.7, "population": 80 },
"geometry": {
"type": "MultiLineString",
"coordinates": [
Expand All @@ -162,13 +153,8 @@
},
{
"type": "Feature",
"properties": {
"population": 160
},
"geometry": {
"type": "MultiLineString",
"coordinates": []
}
"properties": { "fill-opacity": 0.8, "population": 160 },
"geometry": { "type": "MultiLineString", "coordinates": [] }
},
{
"type": "Feature",
Expand Down

0 comments on commit bfc14e5

Please sign in to comment.