Skip to content

Commit

Permalink
[ts] Typing correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Jul 13, 2024
1 parent 275824c commit 4b2d108
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
33 changes: 16 additions & 17 deletions src/ts/geojson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
MultiLineString,
Polygon,
MultiPolygon,
Position,
} from 'geojson';

function makeFeatureCollection(wkt: string, properties?: any) {
Expand Down Expand Up @@ -60,8 +61,8 @@ describe('geojson module', () => {
const expected = makeFeatureCollection('POINT(1.2 -2.1)');
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
deserialize(stream) as AsyncGenerator,
const actual = await takeAsync<IGeoJsonFeature>(
deserialize(stream),
);
expect(actual).to.deep.equal(expected.features);
});
Expand Down Expand Up @@ -129,10 +130,8 @@ describe('geojson module', () => {
);
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
const actual = await takeAsync<IGeoJsonFeature>(
deserialize(stream as unknown as ReadableStream<any>),
);
expect(actual).to.deep.equal(expected.features);
});
Expand Down Expand Up @@ -384,42 +383,42 @@ describe('geojson module', () => {
| MultiLineString
| Polygon
| MultiPolygon;
expect((g.coordinates[0] as number[]).length).to.be.greaterThan(
0,
);
expect(
(g.coordinates[0] as Position[]).length,
).to.be.greaterThan(0);
}
});

it('Should parse countries fgb produced from GDAL stream filter', async () => {
const r: Rect = { minX: 12, minY: 56, maxX: 12, maxY: 56 };
const features = await takeAsync(
const features = await takeAsync<IGeoJsonFeature>(
deserialize(
'http://flatgeobuf.septima.dk/countries.fgb',
r,
undefined,
false,
) as AsyncGenerator,
),
);
expect(features.length).to.eq(3);
for (const f of features)
expect(
(f.geometry.coordinates[0] as number[]).length,
((f.geometry as Polygon).coordinates[0] as Position[])
.length,
).to.be.greaterThan(0);
});

it('Should parse countries fgb produced from GDAL stream no filter', async () => {
const buffer = readFileSync('./test/data/countries.fgb');
const bytes = new Uint8Array(buffer);
const stream = arrayToStream(bytes.buffer);
const features = await takeAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
const features = await takeAsync<IGeoJsonFeature>(
deserialize(stream as unknown as ReadableStream<any>),
);
expect(features.length).to.eq(179);
for (const f of features)
expect(
(f.geometry.coordinates[0] as number[]).length,
((f.geometry as Polygon).coordinates[0] as Position[])
.length,
).to.be.greaterThan(0);
});

Expand Down
43 changes: 22 additions & 21 deletions src/ts/ol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Geometry from 'ol/geom/Geometry.js';
const format = new WKT();
const geojson = new GeoJSON();

const g = (features) => geojson.writeFeatures(features);
const g = (features: Array<Feature<Geometry>>) =>
geojson.writeFeatures(features);

function makeFeatureCollection(wkt: string /*, properties?: any*/) {
return makeFeatureCollectionFromArray([wkt] /*, properties*/);
Expand All @@ -35,18 +36,18 @@ describe('ol module', () => {
it('Point', () => {
const expected = makeFeatureCollection('POINT(1.2 -2.1)');
const s = serialize(expected);
const actual = deserialize(s);
const actual = deserialize(s) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('Point via stream', async () => {
const expected = makeFeatureCollection('POINT(1.2 -2.1)');
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
const actual = await takeAsync<Feature>(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
) as AsyncGenerator<Feature>,
);
expect(g(actual)).to.equal(g(expected));
});
Expand All @@ -56,71 +57,71 @@ describe('ol module', () => {
'POINT(1.2 -2.1)',
'POINT(2.4 -4.8)',
]);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('MultiPoint', () => {
const expected = makeFeatureCollection(
'MULTIPOINT(10 40, 40 30, 20 20, 30 10)',
);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('LineString', () => {
const expected = makeFeatureCollection(
'LINESTRING(1.2 -2.1, 2.4 -4.8)',
);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('MultiLineString', () => {
const expected =
makeFeatureCollection(`MULTILINESTRING((10 10, 20 20, 10 40),
(40 40, 30 30, 40 20, 30 10), (50 50, 60 60, 50 90))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('MultiLineStringSinglePart', () => {
const expected = makeFeatureCollection(
`MULTILINESTRING((1.2 -2.1, 2.4 -4.8))`,
);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('Polygon', () => {
const expected = makeFeatureCollection(
`POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))`,
);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('PolygonWithHole', () => {
const expected =
makeFeatureCollection(`POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('PolygonWithTwoHoles', () => {
const expected =
makeFeatureCollection(`POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30), (20 30, 35 35, 30 20, 20 30))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('MultiPolygon', () => {
const expected =
makeFeatureCollection(`MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),
((15 5, 40 10, 10 20, 5 10, 15 5)))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
// should encode into 18 flat coords, ends [8, 16] endss [1, 1]
expect(g(actual)).to.equal(g(expected));
});
Expand All @@ -129,7 +130,7 @@ describe('ol module', () => {
const expected =
makeFeatureCollection(`MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
// NOTE: 28 flat coords, ends = [4, 10, 14], endss = [1, 2]
expect(g(actual)).to.equal(g(expected));
});
Expand All @@ -138,15 +139,15 @@ describe('ol module', () => {
const expected = makeFeatureCollection(
`MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)))`,
);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

it('MultiPolygonSinglePartWithHole', () => {
const expected =
makeFeatureCollection(`MULTIPOLYGON (((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))))`);
const actual = deserialize(serialize(expected));
const actual = deserialize(serialize(expected)) as Feature[];
expect(g(actual)).to.equal(g(expected));
});

Expand Down Expand Up @@ -192,7 +193,7 @@ describe('ol module', () => {
};
const actual = deserialize(
serialize(geojson.readFeatures(expected)),
);
) as Feature[];
expect(JSON.parse(g(actual))).to.deep.equal(expected);
});

Expand Down Expand Up @@ -224,10 +225,10 @@ describe('ol module', () => {
const buffer = readFileSync('./test/data/countries.fgb');
const bytes = new Uint8Array(buffer);
const stream = arrayToStream(bytes.buffer);
const features = await takeAsync(
const features = await takeAsync<Feature>(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
) as AsyncGenerator<Feature>,
);
expect(features.length).to.eq(179);
for (const f of features)
Expand Down Expand Up @@ -287,7 +288,7 @@ describe('ol module', () => {
};
const actual = deserialize(
serialize(geojson.readFeatures(expected)),
);
) as Feature[];
expect(JSON.parse(g(actual))).to.deep.equal(expected);
});

Expand Down Expand Up @@ -318,7 +319,7 @@ describe('ol module', () => {
};
const actual = deserialize(
serialize(geojson.readFeatures(expected)),
);
) as Feature[];
expect(JSON.parse(g(actual))).to.deep.equal(expected);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/ts/streams/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export function arrayToStream(array: ArrayBuffer): ReadableStream {
return webReader;
}

export async function takeAsync(
asyncIterable: AsyncIterable<any>,
export async function takeAsync<T>(
asyncIterable: AsyncIterable<T>,
count = Infinity,
): Promise<any[]> {
const result: any[] = [];
): Promise<T[]> {
const result: T[] = [];
const iterator = asyncIterable[Symbol.asyncIterator]();
while (result.length < count) {
const { value, done } = await iterator.next();
Expand Down

0 comments on commit 4b2d108

Please sign in to comment.