Skip to content

Commit 04cea21

Browse files
committed
refactor(geojson): loop edge cases
1 parent 7f76267 commit 04cea21

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

geojson/loop.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { Loop } from '../s2/Loop'
99
export const marshal = (loop: Loop, ordinal: number): geojson.Position[] => {
1010
const ring = loop.vertices.map(position.marshal)
1111
if (ordinal > 0) ring.reverse() // outer ring remains CCW, inner rings become CW
12-
ring.push(ring[0]) // add matching start/end points
12+
if (ring.length) ring.push(ring[0]) // add matching start/end points
1313
return ring
1414
}
1515

1616
/**
17-
* Constructs an s2 Loop given a geojson Polygon ring & ordinal.
17+
* Constructs an s2 Loop given a geojson Polygon ring.
1818
* @category Constructors
1919
*
2020
* Handles differences between GeoJSON and S2:
@@ -36,7 +36,8 @@ export const unmarshal = (ring: geojson.Position[]): Loop => {
3636
// Loops are not allowed to have duplicate vertices (whether adjacent or not)
3737
if (containsDuplicateVertices(ring)) {
3838
// adjacent duplicates are fixable
39-
ring = removeAdjacentDuplicateVertices(ring)
39+
ring = removeAdjacentDuplicateVertices(ring, 0)
40+
if (ring.length < 3) return new Loop([])
4041

4142
// non-adjacent duplicates are not fixable
4243
if (containsDuplicateVertices(ring)) return new Loop([])

0 commit comments

Comments
 (0)