Skip to content

Commit

Permalink
Merge pull request mapbox#55 from mapbox/correct-rhr
Browse files Browse the repository at this point in the history
fix RHR logic
  • Loading branch information
tmcw authored Nov 15, 2016
2 parents 691033b + f9e2cc9 commit a0bcd5a
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 395 deletions.
8 changes: 6 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function hint(gj, options) {
message: 'the first and last positions in a LinearRing of coordinates must be the same',
line: line
});
return true;
}
} else if (type === 'Line' && coords.length < 2) {
return errors.push({
Expand All @@ -216,8 +217,11 @@ function hint(gj, options) {
line: line
});
} else {
coords.forEach(function(c) {
positionArray(c, type, depth - 1, c.__line__ || line);
var results = coords.map(function(c) {
return positionArray(c, type, depth - 1, c.__line__ || line);
});
return results.some(function(r) {
return r;
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function isRingClockwise (coords) {

function isPolyRHR (coords) {
if (coords && coords.length > 0) {
if (!isRingClockwise(coords[0]))
if (isRingClockwise(coords[0]))
return false;
var interiorCoords = coords.slice(1, coords.length);
if (interiorCoords.some(isRingClockwise))
if (!interiorCoords.every(isRingClockwise))
return false;
}
return true;
Expand Down
7 changes: 7 additions & 0 deletions test/data/bad/bad-polygon-interiorring-rhr.cli-output-json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message",
"line": 1
}
]
3 changes: 3 additions & 0 deletions test/data/bad/bad-polygon-interiorring-rhr.cli-output-pretty
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1:1 warning Polygons and MultiPolygons should follow the right-hand rule

⚠ 1 warning
6 changes: 6 additions & 0 deletions test/data/bad/bad-polygon-interiorring-rhr.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ "type": "Polygon",
"coordinates": [
[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]
]
}
7 changes: 7 additions & 0 deletions test/data/bad/bad-polygon-interiorring-rhr.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message",
"line": 1
}
]
6 changes: 6 additions & 0 deletions test/data/bad/bad-polygon-interiorring-rhr.result-object
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message"
}
]
5 changes: 0 additions & 5 deletions test/data/bad/bad-polygonloop.cli-output-json
Original file line number Diff line number Diff line change
Expand Up @@ -1442,10 +1442,5 @@
{
"message": "a number was found where a coordinate array should have been found: this needs to be nested more deeply",
"line": 1445
},
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message",
"line": 2
}
]
725 changes: 362 additions & 363 deletions test/data/bad/bad-polygonloop.cli-output-pretty

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions test/data/bad/bad-polygonloop.result
Original file line number Diff line number Diff line change
Expand Up @@ -1442,10 +1442,5 @@
{
"message": "a number was found where a coordinate array should have been found: this needs to be nested more deeply",
"line": 1445
},
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message",
"line": 2
}
]
]
6 changes: 1 addition & 5 deletions test/data/bad/bad-polygonloop.result-object
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,5 @@
},
{
"message": "a number was found where a coordinate array should have been found: this needs to be nested more deeply"
},
{
"message": "Polygons and MultiPolygons should follow the right-hand rule",
"level": "message"
}
]
]
2 changes: 1 addition & 1 deletion test/data/bad/different-first-last.geojson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0] ]
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0] ]
]
}
4 changes: 2 additions & 2 deletions test/data/bad/different-first-size.geojson
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [102.0, 3.0], [103.0, 3.0], [103.0, 2.0], [102.0, 2.0]]],
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0, 4.9]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
[[100.2, 0.2], [100.2, 0.8], [100.8, 0.8], [100.8, 0.2], [100.2, 0.2]]]
]
}
2 changes: 1 addition & 1 deletion test/data/bad/featurecollection_changed_semantics.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ]
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion test/data/bad/polygon_winding_order.geojson
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ "type":"Polygon",
"coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]
"coordinates":[[[100,0],[100,1],[101,1],[101,0],[100,0]]]
}
4 changes: 2 additions & 2 deletions test/data/good/good-featurecollection-extensions.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"type": "Polygon",
"custom": true,
"coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0],
[101.0, 0.0], [100.0, 0.0] ]
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion test/data/good/good-featurecollection.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ]
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion test/data/good/good-polygon.geojson
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ]
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}
6 changes: 3 additions & 3 deletions test/data/good/multipolygon.geojson
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [102.0, 3.0], [103.0, 3.0], [103.0, 2.0], [102.0, 2.0]]],
[[[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.2, 0.8], [100.8, 0.8], [100.8, 0.2], [100.2, 0.2]]]
]
}

0 comments on commit a0bcd5a

Please sign in to comment.