Skip to content

Commit

Permalink
fix: enforce lon lat order for corners in postgis use
Browse files Browse the repository at this point in the history
  • Loading branch information
alrico88 committed Sep 2, 2021
1 parent a7556a0 commit 16def35
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/interfaces/geopoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

**latitude**: *number*

Defined in: [modules/bbox.ts:4](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/bbox.ts#L4)
Defined in: [modules/bbox.ts:4](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/bbox.ts#L4)

___

### longitude

**longitude**: *number*

Defined in: [modules/bbox.ts:5](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/bbox.ts#L5)
Defined in: [modules/bbox.ts:5](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/bbox.ts#L5)
30 changes: 15 additions & 15 deletions docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Name | Type |

**Returns:** *number*

Defined in: [modules/datasets.ts:4](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/datasets.ts#L4)
Defined in: [modules/datasets.ts:4](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/datasets.ts#L4)

___

### BBox

Ƭ **BBox**: [*number*, *number*, *number*, *number*]

Defined in: [modules/bbox.ts:1](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/bbox.ts#L1)
Defined in: [modules/bbox.ts:1](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/bbox.ts#L1)

## Functions

Expand All @@ -77,7 +77,7 @@ Name | Type | Description |

The corners as [lat, lon] coordinates

Defined in: [modules/helpers.ts:84](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/helpers.ts#L84)
Defined in: [modules/helpers.ts:84](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/helpers.ts#L84)

___

Expand All @@ -94,7 +94,7 @@ Name | Type |

**Returns:** *Feature*<Polygon\>

Defined in: [modules/geojson.ts:17](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/geojson.ts#L17)
Defined in: [modules/geojson.ts:17](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/geojson.ts#L17)

___

Expand All @@ -110,7 +110,7 @@ Name | Type |

**Returns:** Polygon

Defined in: [modules/geojson.ts:6](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/geojson.ts#L6)
Defined in: [modules/geojson.ts:6](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/geojson.ts#L6)

___

Expand All @@ -133,7 +133,7 @@ Name | Type | Default value | Description |

The resulting WK

Defined in: [modules/wk.ts:15](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/wk.ts#L15)
Defined in: [modules/wk.ts:15](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/wk.ts#L15)

___

Expand All @@ -153,7 +153,7 @@ Name | Type | Description |

The SQL sentences

Defined in: [modules/sql.ts:40](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/sql.ts#L40)
Defined in: [modules/sql.ts:40](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/sql.ts#L40)

___

Expand All @@ -178,7 +178,7 @@ Name | Type | Description |

The SQL sentence

Defined in: [modules/sql.ts:15](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/sql.ts#L15)
Defined in: [modules/sql.ts:15](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/sql.ts#L15)

___

Expand All @@ -201,7 +201,7 @@ Name | Type | Description |

The dataset's bbox

Defined in: [modules/datasets.ts:52](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/datasets.ts#L52)
Defined in: [modules/datasets.ts:52](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/datasets.ts#L52)

___

Expand All @@ -217,7 +217,7 @@ Name | Type |

**Returns:** [*BBox*](modules.md#bbox)

Defined in: [modules/geojson.ts:25](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/geojson.ts#L25)
Defined in: [modules/geojson.ts:25](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/geojson.ts#L25)

___

Expand All @@ -239,7 +239,7 @@ Name | Type | Description |

The BBox of the geohash

Defined in: [modules/geohash.ts:14](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/geohash.ts#L14)
Defined in: [modules/geohash.ts:14](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/geohash.ts#L14)

___

Expand All @@ -262,7 +262,7 @@ Name | Type | Description |

Array of geohash strings

Defined in: [modules/geohash.ts:28](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/geohash.ts#L28)
Defined in: [modules/geohash.ts:28](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/geohash.ts#L28)

___

Expand All @@ -284,7 +284,7 @@ Name | Type | Description |

The WK BBox

Defined in: [modules/wk.ts:29](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/wk.ts#L29)
Defined in: [modules/wk.ts:29](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/wk.ts#L29)

___

Expand All @@ -307,7 +307,7 @@ Name | Type | Description |

Whether the BBox is inside the other BBox

Defined in: [modules/bbox.ts:16](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/bbox.ts#L16)
Defined in: [modules/bbox.ts:16](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/bbox.ts#L16)

___

Expand All @@ -330,4 +330,4 @@ Name | Type | Description |

Whether the point is inside the BBox

Defined in: [modules/bbox.ts:36](https://github.com/alrico88/bbox-helper-functions/blob/0cf0f5e/src/modules/bbox.ts#L36)
Defined in: [modules/bbox.ts:36](https://github.com/alrico88/bbox-helper-functions/blob/a7556a0/src/modules/bbox.ts#L36)
21 changes: 20 additions & 1 deletion src/modules/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function checkBBox(bbox: BBox | number[]): void {

type Coord = [number, number];

interface Corners {
export interface Corners {
sw: Coord;
nw: Coord;
ne: Coord;
Expand Down Expand Up @@ -85,6 +85,25 @@ export function BBoxToCorners(bbox: BBox): Corners {
return new CornersFromBBox(bbox).toPlain();
}

/**
* Returns corners in a lon, lat order
*
* @param {Corners} corners
* @return {Corners}
*/
export function enforceLonLatCorners(corners: Corners): Corners {
function reverseClone(coords: Coord): Coord {
return [...coords].reverse() as Coord;
}

return {
sw: reverseClone(corners.sw),
se: reverseClone(corners.se),
nw: reverseClone(corners.nw),
ne: reverseClone(corners.ne),
};
}

/**
* Checks if number is between a min and max
*
Expand Down
4 changes: 2 additions & 2 deletions src/modules/sql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Formatter from 'string-object-formatter';
import {BBox} from './bbox';
import {BBoxToCorners, checkBBox} from './helpers';
import {BBoxToCorners, checkBBox, enforceLonLatCorners} from './helpers';

/**
* Gets a SQL sentence to use to filter a BBox
Expand Down Expand Up @@ -38,7 +38,7 @@ export function getBBoxSQLSentence(bbox: BBox, latitudeCol: string, longitudeCol
* @return {string} The SQL sentences
*/
export function getBBoxPostGISSentence(bbox: BBox): string {
const {sw, se, nw, ne} = BBoxToCorners(bbox);
const {sw, se, nw, ne} = enforceLonLatCorners(BBoxToCorners(bbox));

return `ST_MAKEPOLYGON(ST_MAKELINE([ST_GEOGPOINT(${sw.toString()}),ST_GEOGPOINT(${nw.toString()}),ST_GEOGPOINT(${ne.toString()}),ST_GEOGPOINT(${se.toString()})]))`;
}
7 changes: 4 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {convertToWK} from 'wkt-parser-helper';
import {geohashToPolygonFeature} from 'geohash-to-geojson';
import turfBbox from '@turf/bbox';
import {Feature, Polygon} from 'geojson';
import {enforceLonLatCorners, Corners} from '../src/modules/helpers';

const testBBox: BBox = [-3.708011, 40.418038, -3.687877, 40.428754];

Expand Down Expand Up @@ -54,7 +55,7 @@ const dataset: DatasetItem[] = [
},
];

const bboxCorners = {
const bboxCorners: Corners = {
sw: [minLat, minLon],
nw: [maxLat, minLon],
ne: [maxLat, maxLon],
Expand Down Expand Up @@ -184,8 +185,8 @@ describe('Testing BBoxHelper methods', () => {

test('Test PostGIS methods', () => {
const sentence = getBBoxPostGISSentence(testBBox);
const {sw, se, ne, nw} = bboxCorners;
const {sw, se, ne, nw} = enforceLonLatCorners(bboxCorners);

expect(sentence).toBe(`ST_MAKEPOLYGON(ST_MAKELINE([ST_GEOGPOINT(${sw.toString()}),ST_GEOGPOINT(${nw.toString()}),ST_GEOGPOINT(${ne.toString()}),ST_GEOGPOINT(${se.toString()})]))`)
expect(sentence).toBe(`ST_MAKEPOLYGON(ST_MAKELINE([ST_GEOGPOINT(${sw.toString()}),ST_GEOGPOINT(${nw.toString()}),ST_GEOGPOINT(${ne.toString()}),ST_GEOGPOINT(${se.toString()})]))`);
});
});

0 comments on commit 16def35

Please sign in to comment.