UNPKG

1.24 kBJavaScriptView Raw
1function rad(x) {
2 return x * Math.PI / 180;
3}
4
5function isRingClockwise (coords) {
6 var area = 0;
7 if (coords.length > 2) {
8 var p1, p2;
9 for (var i = 0; i < coords.length - 1; i++) {
10 p1 = coords[i];
11 p2 = coords[i + 1];
12 area += rad(p2[0] - p1[0]) * (2 + Math.sin(rad(p1[1])) + Math.sin(rad(p2[1])));
13 }
14 }
15
16 return area >= 0;
17}
18
19function isPolyRHR (coords) {
20 if (coords && coords.length > 0) {
21 if (isRingClockwise(coords[0]))
22 return false;
23 var interiorCoords = coords.slice(1, coords.length);
24 if (!interiorCoords.every(isRingClockwise))
25 return false;
26 }
27 return true;
28}
29
30function rightHandRule (geometry) {
31 if (geometry.type === 'Polygon') {
32 return isPolyRHR(geometry.coordinates);
33 } else if (geometry.type === 'MultiPolygon') {
34 return geometry.coordinates.every(isPolyRHR);
35 }
36}
37
38module.exports = function validateRightHandRule(geometry, errors) {
39 if (!rightHandRule(geometry)) {
40 errors.push({
41 message: 'Polygons and MultiPolygons should follow the right-hand rule',
42 level: 'message',
43 line: geometry.__line__
44 });
45 }
46};