Skip to content

Commit cbdd941

Browse files
committed
Reuse calculateSignedArea
1 parent a7d7621 commit cbdd941

File tree

2 files changed

+3
-14
lines changed

2 files changed

+3
-14
lines changed

src/expression/evaluation_context.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Color} from './values';
22
import type {FormattedSection} from './types/formatted';
33
import type {GlobalProperties, Feature, FeatureState} from './index';
44
import {ICanonicalTileID} from '../tiles_and_coordinates';
5+
import {calculateSignedArea} from '../util/classify_rings';
56

67
const geometryTypes = ['Unknown', 'Point', 'LineString', 'Polygon'];
78
const simpleGeometryType = {
@@ -39,18 +40,6 @@ class EvaluationContext {
3940
return this.feature && 'id' in this.feature ? this.feature.id : null;
4041
}
4142

42-
// Bluntly copied from
43-
// https://github.com/mapbox/vector-tile-js/blob/77851380b63b07fd0af3d5a3f144cc86fb39fdd1/lib/vectortilefeature.js#L225-L233
44-
_signedArea(ring) {
45-
let sum = 0;
46-
for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
47-
p1 = ring[i];
48-
p2 = ring[j];
49-
sum += (p2.x - p1.x) * (p1.y + p2.y);
50-
}
51-
return sum;
52-
}
53-
5443
geometryDollarType() {
5544
return this.feature ?
5645
typeof this.feature.type === 'number' ? geometryTypes[this.feature.type] : simpleGeometryType[this.feature.type] :
@@ -75,7 +64,7 @@ class EvaluationContext {
7564
case 'Polygon':
7665
// Following https://github.com/mapbox/vector-tile-js/blob/77851380b63b07fd0af3d5a3f144cc86fb39fdd1/lib/vectortilefeature.js#L197
7766
for (let i = 0, ccw; i < len; i++) {
78-
const area = this._signedArea(geom[i]);
67+
const area = calculateSignedArea(geom[i]);
7968
if (area === 0) continue;
8069
if (ccw === undefined) {
8170
ccw = area < 0;

src/util/classify_rings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function compareAreas(a: {area: number}, b: {area: number}) {
6060
* @param ring - Exterior or interior ring
6161
* @returns Signed area
6262
*/
63-
function calculateSignedArea(ring: Point2D[]): number {
63+
export function calculateSignedArea(ring: Point2D[]): number {
6464
let sum = 0;
6565
for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
6666
p1 = ring[i];

0 commit comments

Comments
 (0)