Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 023db16

Browse files
committed
fix: migrate to wkt-io-ts which will validate the geojson and WKT
1 parent 4904890 commit 023db16

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"io-ts": "^2.0.1",
112112
"io-ts-types": "^0.5.0",
113113
"monocle-ts": "^2.0.0",
114-
"wkx": "^0.4.8"
114+
"wkt-io-ts": "^1.0.0"
115115
},
116116
"devDependencies": {
117117
"@holvonix-open/release-config-js": "^1.0.4",

src/Q.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import {
3434
import * as util from 'util';
3535

3636
import * as spatial from './spatial';
37-
import * as wkx from 'wkx';
37+
import * as wktio from 'wkt-io-ts';
38+
import { WKT } from 'wkt-io-ts';
39+
import { isRight } from 'fp-ts/lib/Either';
40+
import { PathReporter } from 'io-ts/lib/PathReporter';
3841

3942
export { spatial };
4043

@@ -254,8 +257,12 @@ function nope(): never {
254257
throw new Error('unsupported');
255258
}
256259

257-
function wkt(g: Spatial['value']['geom']): string {
258-
return wkx.Geometry.parseGeoJSON(g).toWkt();
260+
function wkt(g: Spatial['value']['geom']): WKT {
261+
const d = wktio.WKTStringFromGeometry.decode(g);
262+
if (!isRight(d)) {
263+
throw new Error('Cannot parse: ' + PathReporter.report(d).join('; '));
264+
}
265+
return d.right;
259266
}
260267

261268
function toLiteralString<T extends Primitive>(

test/test-Q.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,19 @@ describe('Q', () => {
663663
],
664664
};
665665
const obj = d(PolygonIO.decode(p));
666+
it('throws on bad geojson', () => {
667+
const bad: Polygon = {
668+
type: 'PolygonZ' as 'Polygon',
669+
coordinates: [
670+
[[10.0, 0.0], [101.0, 21.0], [60.0, 1.024], [10.0, 0.0]],
671+
[[0, 0], [-10, -10], [20, 30], [0, 0]],
672+
],
673+
};
674+
assert.throws(
675+
() => Q.toString(Q.term('geo', Q.spatial.intersects(bad))),
676+
/Invalid value/
677+
);
678+
});
666679
it('intersects', () => {
667680
assert.deepStrictEqual(
668681
Q.toString(Q.term('geo', Q.spatial.intersects(obj))),

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5612,6 +5612,18 @@ windows-release@^3.1.0:
56125612
dependencies:
56135613
execa "^1.0.0"
56145614

5615+
wkt-io-ts@^1.0.0:
5616+
version "1.0.0"
5617+
resolved "https://registry.yarnpkg.com/wkt-io-ts/-/wkt-io-ts-1.0.0.tgz#a68d0a3c534a66377ae851617cdd41f643e15ae5"
5618+
integrity sha512-DhFv1zt6lwojE3ZSdxP7KOt4HXVmLplsLbyiw48ldFsaYQjzU2vzPyeDQSBn3YJYS82EqZYlLknKXjHYcetzHg==
5619+
dependencies:
5620+
"@holvonix-open/geojson-io-ts" "^5.0.0"
5621+
fp-ts "^2.0.5"
5622+
io-ts "^2.0.1"
5623+
io-ts-types "^0.5.0"
5624+
monocle-ts "^2.0.0"
5625+
wkx "^0.4.8"
5626+
56155627
wkx@^0.4.8:
56165628
version "0.4.8"
56175629
resolved "https://registry.yarnpkg.com/wkx/-/wkx-0.4.8.tgz#a092cf088d112683fdc7182fd31493b2c5820003"

0 commit comments

Comments
 (0)