From b10f273f104be512a31eceb013b44375a104e73f Mon Sep 17 00:00:00 2001 From: "carl.baxter" Date: Sun, 6 Oct 2019 06:50:49 +1000 Subject: [PATCH] Fix direction of ring coordinates --- src/poly.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/poly.js b/src/poly.js index 4ba8c2de..a42988a3 100644 --- a/src/poly.js +++ b/src/poly.js @@ -7,6 +7,20 @@ module.exports.write = function writePoints(geometries, extent, shpView, shxView shxI = 0, shxOffset = 100; + //Ensure polygon rings are ordered in correct direction + if(TYPE === types.geometries.POLYGON){ + geometries = geometries.reduce(function(geom_acc, poly){ + var flipped = poly.reduce(function(poly_acc, ring, idx){ + //External rings must be clockwise, Internal must be counter-clockwise + idx == 0 + ? isClockwise(ring) ? poly_acc.push(ring) : poly_acc.push(ring.reverse()) + : isClockwise(ring) ? poly_acc.push(ring.reverse()) : poly_acc.push(ring); + return poly_acc; + }, []); + geom_acc.push(flipped); + return geom_acc; + }, []); + } geometries.forEach(writePolyLine); function writePolyLine(coordinates, i) { @@ -62,6 +76,16 @@ module.exports.write = function writePoints(geometries, extent, shpView, shxView shpI += contentLength + 8; } + + //Takes a ring of coordinates. Returns true if they're defined in clockwise order + function isClockwise(ring){ + var sum = 0; + for(var i = 0; i < ring.length; i++){ + var prevIdx = i == 0 ? ring.length - 1 : i - 1; + sum += (ring[i][0] - ring[prevIdx][0])*(ring[i][1] + ring[prevIdx][1]); + } + return sum > 0 ? true : false; + } }; module.exports.shpLength = function(geometries) {