Skip to content

Commit d286211

Browse files
turf-tessellate: Pass original coordinate elevations through the tessellate function. (#2855)
* fix tessellate support for elevation --------- Co-authored-by: James Beard <[email protected]>
1 parent d3b14a1 commit d286211

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

packages/turf-tesselate/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,28 @@ function tesselate(
5050

5151
function processPolygon(coordinates: Position[][]) {
5252
const data = flattenCoords(coordinates);
53-
const dim = 2;
53+
// coordinates are normalized to 3 dimensions by passing through original elevation value, or padding with undefined
54+
const dim = 3;
5455
const result = earcut(data.vertices, data.holes, dim);
5556

5657
const features: Feature<Polygon>[] = [];
5758
const vertices: Position[] = [];
5859

5960
result.forEach(function (vert: any, i: number) {
6061
const index = result[i];
61-
vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]);
62+
// if elevation component is included in the original coordinate, include it in the output coordinate
63+
if (data.vertices[index * dim + 2] !== undefined) {
64+
vertices.push([
65+
data.vertices[index * dim],
66+
data.vertices[index * dim + 1],
67+
data.vertices[index * dim + 2],
68+
]);
69+
} else {
70+
vertices.push([
71+
data.vertices[index * dim],
72+
data.vertices[index * dim + 1],
73+
]);
74+
}
6275
});
6376

6477
for (var i = 0; i < vertices.length; i += 3) {
@@ -71,7 +84,8 @@ function processPolygon(coordinates: Position[][]) {
7184
}
7285

7386
function flattenCoords(data: Position[][]) {
74-
const dim: number = data[0][0].length,
87+
// coordinates are normalized to 3 dimensions by passing through original elevation value, or padding with undefined
88+
const dim = 3,
7589
result: { vertices: number[]; holes: number[]; dimensions: number } = {
7690
vertices: [],
7791
holes: [],
@@ -81,6 +95,7 @@ function flattenCoords(data: Position[][]) {
8195

8296
for (let i = 0; i < data.length; i++) {
8397
for (let j = 0; j < data[i].length; j++) {
98+
// elevation member is either included, or undefined is put in its place
8499
for (let d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
85100
}
86101
if (i > 0) {

packages/turf-tesselate/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Abel Vázquez <@AbelVM>",
88
"Morgan Herlocker <@morganherlocker>",
99
"Tom MacWright <@tmcw>",
10-
"Vladimir Agafonkin <@mourner>"
10+
"Vladimir Agafonkin <@mourner>",
11+
"Pavel Rozvora <@prozvora>"
1112
],
1213
"license": "MIT",
1314
"bugs": {

packages/turf-tesselate/test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,5 +3499,98 @@ test("tesselate", function (t) {
34993499
tesselate(featurecollection([]));
35003500
}, /input must be a Polygon or MultiPolygon/);
35013501

3502+
var simplePolygonWithElevation = {
3503+
type: "Feature",
3504+
id: "CoordsWithElevation",
3505+
properties: { name: "CoordsWithElevation" },
3506+
geometry: {
3507+
type: "Polygon",
3508+
coordinates: [
3509+
[
3510+
[-123.233256, 42.006186, 130],
3511+
[-114.634459, 35.00118, 130],
3512+
[-118.183517, 33.763391, 130],
3513+
[-124.213628, 42.000709, 130],
3514+
[-123.233256, 42.006186, 130],
3515+
],
3516+
],
3517+
},
3518+
};
3519+
3520+
var simpleTrianglesWithElevation = tesselate(simplePolygonWithElevation);
3521+
t.equal(
3522+
simpleTrianglesWithElevation.type,
3523+
"FeatureCollection",
3524+
"Polygon returns a FeatureCollection"
3525+
);
3526+
t.equal(
3527+
simpleTrianglesWithElevation.features[0].geometry.type,
3528+
"Polygon",
3529+
"contains at least 1 triangle"
3530+
);
3531+
t.equal(
3532+
simpleTrianglesWithElevation.features[0].geometry.coordinates[0].length,
3533+
4,
3534+
"triangle is valid"
3535+
);
3536+
t.equal(
3537+
simpleTrianglesWithElevation.features[0].geometry.coordinates[0][0][2],
3538+
130,
3539+
"triangle coordinates contain elevation"
3540+
);
3541+
3542+
var simpleSquareWithVariableElevation = {
3543+
type: "Feature",
3544+
id: "SquareWithVariableElevation",
3545+
properties: { name: "SquareWithVariableElevation" },
3546+
geometry: {
3547+
type: "Polygon",
3548+
coordinates: [
3549+
[
3550+
[1, 1],
3551+
[1, 2, 50],
3552+
[2, 2, 75],
3553+
[2, 1],
3554+
[1, 1],
3555+
],
3556+
],
3557+
},
3558+
};
3559+
3560+
var simpleVariableElevationTriangles = tesselate(
3561+
simpleSquareWithVariableElevation
3562+
);
3563+
3564+
t.equal(
3565+
simpleVariableElevationTriangles.type,
3566+
"FeatureCollection",
3567+
"Polygon returns a FeatureCollection"
3568+
);
3569+
3570+
t.deepEqual(
3571+
simpleVariableElevationTriangles.features[0].geometry.coordinates,
3572+
[
3573+
[
3574+
[1, 2, 50],
3575+
[1, 1],
3576+
[2, 1],
3577+
[1, 2, 50],
3578+
],
3579+
],
3580+
"first triangle coordinates contain original elevations"
3581+
);
3582+
t.deepEqual(
3583+
simpleVariableElevationTriangles.features[1].geometry.coordinates,
3584+
[
3585+
[
3586+
[2, 1],
3587+
[2, 2, 75],
3588+
[1, 2, 50],
3589+
[2, 1],
3590+
],
3591+
],
3592+
"second triangle coordinates contain original elevations"
3593+
);
3594+
35023595
t.end();
35033596
});

0 commit comments

Comments
 (0)