|
1 | | -const { getFitboundsLonRange, unwrapLonRange, doesCrossAntiMeridian } = require('../../../src/lib/geo_location_utils'); |
| 1 | +const { computeBbox, getFitboundsLonRange, unwrapLonRange, doesCrossAntiMeridian } = require('../../../src/lib/geo_location_utils'); |
2 | 2 |
|
3 | 3 | describe('Test geo_location_utils.getFitboundsLonRange', () => { |
4 | 4 | it('returns the compact crossing range when point data straddles the antimeridian', () => { |
@@ -38,6 +38,69 @@ describe('Test geo_location_utils.unwrapLonRange', () => { |
38 | 38 | }); |
39 | 39 | }); |
40 | 40 |
|
| 41 | +describe('Test geo_location_utils.computeBbox', () => { |
| 42 | + const franceCCW = { type: 'Polygon', coordinates: [[[-5, 41], [10, 41], [10, 51], [-5, 51], [-5, 41]]] }; |
| 43 | + const franceCW = { type: 'Polygon', coordinates: [[[-5, 41], [-5, 51], [10, 51], [10, 41], [-5, 41]]] }; |
| 44 | + // Fiji-ish narrow band crossing the antimeridian. |
| 45 | + const fiji = { type: 'Polygon', coordinates: [[[176, -19], [180, -19], [-178, -19], [-178, -16], [180, -16], [176, -16], [176, -19]]] }; |
| 46 | + // Russia-ish MultiPolygon with parts on both sides of ±180. |
| 47 | + const russia = { |
| 48 | + type: 'MultiPolygon', |
| 49 | + coordinates: [ |
| 50 | + [[[30, 55], [170, 55], [170, 75], [30, 75], [30, 55]]], |
| 51 | + [[[-180, 65], [-170, 65], [-170, 72], [-180, 72], [-180, 65]]] |
| 52 | + ] |
| 53 | + }; |
| 54 | + |
| 55 | + it('returns a degenerate bbox for a single Point', () => { |
| 56 | + expect(computeBbox({ type: 'Point', coordinates: [10, 45] })).toEqual([10, 45, 10, 45]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns a normal bbox for a non-antimeridian polygon', () => { |
| 60 | + expect(computeBbox(franceCCW)).toEqual([-5, 41, 10, 51]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('is winding-agnostic (CCW and CW polygons yield the same bbox)', () => { |
| 64 | + expect(computeBbox(franceCW)).toEqual(computeBbox(franceCCW)); |
| 65 | + }); |
| 66 | + |
| 67 | + it('unwraps east past 180° for a polygon that crosses the antimeridian', () => { |
| 68 | + expect(computeBbox(fiji)).toEqual([176, -19, 182, -16]); |
| 69 | + }); |
| 70 | + |
| 71 | + it('unwraps east past 180° for a MultiPolygon that crosses the antimeridian', () => { |
| 72 | + expect(computeBbox(russia)).toEqual([30, 55, 190, 75]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('handles a FeatureCollection mixing antimeridian and non-antimeridian features', () => { |
| 76 | + const fc = { |
| 77 | + type: 'FeatureCollection', |
| 78 | + features: [ |
| 79 | + { type: 'Feature', geometry: russia, properties: {} }, |
| 80 | + { type: 'Feature', geometry: franceCCW, properties: {} } |
| 81 | + ] |
| 82 | + }; |
| 83 | + expect(computeBbox(fc)).toEqual([-5, 41, 190, 75]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('unwraps identically whether the input is a raw Geometry or wrapped in a Feature', () => { |
| 87 | + const raw = computeBbox(russia); |
| 88 | + const wrapped = computeBbox({ type: 'Feature', geometry: russia, properties: {} }); |
| 89 | + expect(wrapped).toEqual(raw); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns null for inputs with no extractable coordinates', () => { |
| 93 | + expect(computeBbox({ type: 'Sphere' })).toBe(null); |
| 94 | + expect(computeBbox({ type: 'FeatureCollection', features: [] })).toBe(null); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns null for nullish or malformed inputs instead of throwing', () => { |
| 98 | + expect(computeBbox(null)).toBe(null); |
| 99 | + expect(computeBbox(undefined)).toBe(null); |
| 100 | + expect(computeBbox({})).toBe(null); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
41 | 104 | describe('Test geo_location_utils.doesCrossAntiMeridian', () => { |
42 | 105 | it('returns the index of the first positive-to-negative longitude transition', () => { |
43 | 106 | expect(doesCrossAntiMeridian([[170, 0], [179, 0], [-179, 0], [-170, 0]])).toBe(1); |
|
0 commit comments