Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Sep 25, 2024
1 parent a99485c commit 4242ae0
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {SpatialFilter} from './types';

/**
* Returns a {@link SpatialFilter} for a given viewport, typically obtained
* from deck.gl's `viewport.getBounds()` method. If the viewport covers the
* entire world (to some margin of error in Web Mercator space), `undefined`
* is returned instead.
* from deck.gl's `viewport.getBounds()` method ([west, south, east, north]).
* If the viewport covers the entire world (to some margin of error in Web
* Mercator space), `undefined` is returned instead.
*
* If the viewport extends beyond longitude range [-180, +180], the polygon
* may be reformatted for compatibility with CARTO APIs.
Expand Down
255 changes: 255 additions & 0 deletions test/geo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import {describe, expect, test} from 'vitest';
import {
createViewportSpatialFilter,
createPolygonSpatialFilter,
} from '@carto/api-client';
import bboxPolygon from '@turf/bbox-polygon';
import {polygon, multiPolygon} from '@turf/helpers';

describe('createViewportSpatialFilter', () => {
test('polygon', () => {
expect(createViewportSpatialFilter([-10, -10, 10, 10])).toStrictEqual(
bboxPolygon([-10, -10, 10, 10]).geometry
);

expect(
createViewportSpatialFilter([-344.26, -75.051, 230.265, 75.051])
).toStrictEqual({
type: 'Polygon',
coordinates: [
[
[-180, -75.051],
[180, -75.051],
[180, 75.051],
[-180, 75.051],
[-180, -75.051],
],
],
});
});

test('multipolygon', () => {
expect(
createViewportSpatialFilter([-125.26, -85.051, 230.265, 85.051])
).toStrictEqual({
type: 'MultiPolygon',
coordinates: [
[
[
[-180, -85.051],
[-129.735, -85.051],
[-129.735, 85.051],
[-180, 85.051],
[-180, -85.051],
],
],
[
[
[-125.26, -85.051],
[180, -85.051],
[180, 85.051],
[-125.26, 85.051],
[-125.26, -85.051],
],
],
],
});
});

test('global', () => {
expect(
createViewportSpatialFilter([-344.259, -85.051, 230.264, 85.051])
).toBeUndefined();
});
});

describe('createPolygonSpatialFilter', () => {
test('undefined', () => {
expect(createPolygonSpatialFilter(null)).toBeUndefined();
});

test('readonly', () => {
let input = bboxPolygon([-10, -10, 10, 10]).geometry;

expect(createPolygonSpatialFilter(input)).toStrictEqual(input);

input = polygon([
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
]).geometry;

expect(createPolygonSpatialFilter(input)).toStrictEqual(input);
});

test('multipolygons-wrapping-from-west', () => {
const input = multiPolygon([
[
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
],
[
[
[-190, -50],
[-170, -70],
[-170, 70],
[-190, 50],
[-190, -50],
],
],
]).geometry;

const expected = multiPolygon([
[
[
[-180, -60],
[-170, -70],
[-170, 70],
[-180, 60],
[-180, -60],
],
],
[
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
],
[
[
[170, -50],
[180, -60],
[180, 60],
[170, 50],
[170, -50],
],
],
]).geometry;

expect(createPolygonSpatialFilter(input)).toStrictEqual(expected);
});

test('multipolygons-wrapping-from-east', () => {
const input = multiPolygon([
[
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
],
[
[
[170, -50],
[190, -70],
[190, 70],
[170, 50],
[170, -50],
],
],
]).geometry;

const expected = multiPolygon([
[
[
[-180, -60],
[-170, -70],
[-170, 70],
[-180, 60],
[-180, -60],
],
],
[
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
],
[
[
[170, -50],
[180, -60],
[180, 60],
[170, 50],
[170, -50],
],
],
]).geometry;

expect(createPolygonSpatialFilter(input)).toStrictEqual(expected);
});

test('unwrap-large-viewport', () => {
const input = polygon([
[
[-200, -80],
[210, -80],
[210, 75],
[-200, 75],
[-200, -80],
],
]).geometry;
const expected = polygon([
[
[-180, -80],
[180, -80],
[180, 75],
[-180, 75],
[-180, -80],
],
]).geometry;
expect(createPolygonSpatialFilter(input)).toStrictEqual(expected);
});

test('remove-degenerate-polygons', () => {
const input = multiPolygon([
[
[
[-200, -80],
[210, -80],
[210, 75],
[-200, 75],
[-200, -80],
],
],
[
[
[-90, 0],
[0, -45],
[90, 0],
[0, 45],
[-90, 0],
],
],
]).geometry;

const expected = polygon([
[
[-180, -80],
[180, -80],
[180, 75],
[-180, 75],
[-180, -80],
],
]).geometry;

expect(createPolygonSpatialFilter(input)).toStrictEqual(expected);
});
});

0 comments on commit 4242ae0

Please sign in to comment.