Skip to content

Commit

Permalink
Merge pull request #8 from vojtatom/dev
Browse files Browse the repository at this point in the history
Adding types to Records
  • Loading branch information
vojtatom authored May 10, 2023
2 parents 51ba46d + 10ee156 commit 218553d
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: 'Setup Node.js'
uses: 'actions/setup-node@v1'
uses: 'actions/setup-node@v3'
with:
node-version: 18
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shpts",
"private": false,
"version": "1.0.5",
"version": "1.0.6",
"type": "module",
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions shpts/geometry/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export abstract class BaseRecord {
return 4;
}

abstract get type(): string;

protected static readBbox(stream: MemoryStream): BoundingBox {
const xMin = stream.readDouble(true);
const yMin = stream.readDouble(true);
Expand Down
5 changes: 5 additions & 0 deletions shpts/geometry/multipatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class MultiPatchRecord extends BaseRingedRecord {
super(coordType);
}

get type() {
if (this.coords.length === 1) return 'Polygon';
return 'MultiPolygon';
}

static fromPresetReader(reader: ShapeReader, header: GeomHeader) {
const hasZ = reader.hasZ;
const hasM = reader.hasM;
Expand Down
4 changes: 4 additions & 0 deletions shpts/geometry/multipoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export class MultiPointRecord extends BaseRecord {
super(coordType);
}

get type() {
return 'MultiPoint';
}

static fromPresetReader(reader: ShapeReader, header: GeomHeader) {
const hasZ = reader.hasZ;
const hasM = reader.hasM;
Expand Down
4 changes: 4 additions & 0 deletions shpts/geometry/null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class ShpNullGeom extends BaseRecord {
super(CoordType.NULL);
}

get type() {
return 'Null';
}

public toGeoJson(): GeoJsonGeom {
throw new Error('Method cannot be implemented.');
}
Expand Down
4 changes: 4 additions & 0 deletions shpts/geometry/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class PointRecord extends BaseRecord {
super(coordType);
}

get type() {
return 'Point';
}

static fromPresetReader(reader: ShapeReader, header: GeomHeader) {
const hasZ = reader.hasZ;
const hasM = reader.hasM;
Expand Down
5 changes: 5 additions & 0 deletions shpts/geometry/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class PolygonRecord extends BaseRingedRecord {
super(coordType);
}

get type() {
if (this.coords.length === 1) return 'Polygon';
return 'MultiPolygon';
}

static fromPresetReader(reader: ShapeReader, header: GeomHeader) {
const hasZ = reader.hasZ;
const hasM = reader.hasM;
Expand Down
6 changes: 5 additions & 1 deletion shpts/geometry/polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { BaseRingedRecord } from './base';
import { GeoJsonCoord, GeoJsonLineString, GeoJsonMultiLineString } from '@shpts/types/geojson';
import { GeomUtil } from '@shpts/utils/geometry';
import { GeomHeader } from '@shpts/types/data';
import { MemoryStream } from '@shpts/utils/stream';

export class PolyLineRecord extends BaseRingedRecord {
constructor(public coords: PolyLineCoord, coordType: CoordType) {
super(coordType);
}

get type() {
if (this.coords.length === 1) return 'LineString';
return 'MultiLineString';
}

static fromPresetReader(reader: ShapeReader, header: GeomHeader) {
const hasZ = reader.hasZ;
const hasM = reader.hasM;
Expand Down
4 changes: 4 additions & 0 deletions test/multipatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test('Reading MultiPatchRecord with Z', async () => {

expect(reader.recordCount).toEqual(1);
let geom = expectGeometry(reader, 0, CoordType.XYZM, MultiPatchRecord);
expect(geom.type).toEqual('MultiPolygon');
expect(geom.coords.length).toEqual(12);
let polygon = geom.coords[0];
expectRing(polygon[0], [
Expand Down Expand Up @@ -116,6 +117,7 @@ test('Reading MultiPatchRecord with Outer/Inner Rings', async () => {
expect(reader.recordCount).toEqual(1);

let geom = expectGeometry(reader, 0, CoordType.XYZM, MultiPatchRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);
let polygon = geom.coords[0];
expect(polygon.length).toBe(3);
Expand Down Expand Up @@ -150,6 +152,7 @@ test('Reading MultiPatchRecord with First Ring and Rings', async () => {
expect(reader.recordCount).toEqual(1);

let geom = expectGeometry(reader, 0, CoordType.XYZM, MultiPatchRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);
let polygon = geom.coords[0];
expect(polygon.length).toBe(3);
Expand Down Expand Up @@ -184,6 +187,7 @@ test('Reading MultiPatchRecord with First Ring and Rings', async () => {
expect(reader.recordCount).toEqual(1);

let geom = expectGeometry(reader, 0, CoordType.XYZM, MultiPatchRecord);
expect(geom.type).toEqual('MultiPolygon');
expect(geom.coords.length).toBe(3);
let polygon = geom.coords[0];
expect(polygon.length).toBe(1);
Expand Down
9 changes: 9 additions & 0 deletions test/multipoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('Reading PointRecord', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XY, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(5);
expectPointsEqual(geom.coords[0], [36.963112042621276, -129.36489649456098]);
expectPointsEqual(geom.coords[1], [37.148521428162326, -118.98197090426271]);
Expand All @@ -17,13 +18,15 @@ test('Reading PointRecord', async () => {
expectPointsEqual(geom.coords[4], [48.273084560624625, -124.35884308495292]);

geom = expectGeometry(reader, 1, CoordType.XY, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(4);
expectPointsEqual(geom.coords[0], [56.060278753348484, -131.96062789213556]);
expectPointsEqual(geom.coords[1], [57.72896322321782, -136.41045314512053]);
expectPointsEqual(geom.coords[2], [61.0663321629566, -136.03963437403843]);
expectPointsEqual(geom.coords[3], [63.662063560531124, -131.21899034997142]);

geom = expectGeometry(reader, 2, CoordType.XY, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(3);
expectPointsEqual(geom.coords[0], [44.564896849803915, -151.05779460286274]);
expectPointsEqual(geom.coords[1], [47.71685640400153, -142.3435534824339]);
Expand All @@ -37,6 +40,7 @@ test('Reading PointRecord with M', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(5);
expectPointsEqual(geom.coords[0], [88.87773999411263, -137.89372822944887, 1]);
expectPointsEqual(geom.coords[1], [92.95674647601561, -143.08519102459798, 2]);
Expand All @@ -45,12 +49,14 @@ test('Reading PointRecord with M', async () => {
expectPointsEqual(geom.coords[4], [97.59198111454151, -141.41650655472864, 5]);

geom = expectGeometry(reader, 1, CoordType.XYM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(3);
expectPointsEqual(geom.coords[0], [96.10870603021323, -121.94852107291939, 44]);
expectPointsEqual(geom.coords[1], [98.33361865670565, -127.32539325360949, 55]);
expectPointsEqual(geom.coords[2], [101.11475943982134, -122.69015861508353, 66]);

geom = expectGeometry(reader, 2, CoordType.XYM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(4);
expectPointsEqual(geom.coords[0], [104.45212837956001, -133.2584935909228, 5]);
expectPointsEqual(geom.coords[1], [106.86245039159343, -136.96668130174362, 55]);
Expand All @@ -65,6 +71,7 @@ test('Reading PointRecord with Z', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYZM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(6);
expectPointsEqual(geom.coords[0], [106.12081284942929, -160.14285449437375, 0, NaN]);
expectPointsEqual(geom.coords[1], [106.86245039159343, -155.32221047030666, 0, NaN]);
Expand All @@ -74,10 +81,12 @@ test('Reading PointRecord with Z', async () => {
expectPointsEqual(geom.coords[5], [114.09341642769414, -160.3282638799148, 0, NaN]);

geom = expectGeometry(reader, 1, CoordType.XYZM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(1);
expectPointsEqual(geom.coords[0], [110.01440994579121, -149.57451951853437, -10, -10]);

geom = expectGeometry(reader, 2, CoordType.XYZM, MultiPointRecord);
expect(geom.type).toEqual('MultiPoint');
expect(geom.coords.length).toBe(3);
expectPointsEqual(geom.coords[0], [89.06314937965368, -154.02434477151942, 22, -10]);
expectPointsEqual(geom.coords[1], [91.8442901627692, -161.81153896424308, 33, -20]);
Expand Down
9 changes: 8 additions & 1 deletion test/point.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from 'vitest';
import { expect, test } from 'vitest';
import { expectGeometry, expectPointsEqual, openFileAsArray } from './utils';
import { ShapeReader, PointRecord, CoordType } from '@shpts/shpts';

Expand All @@ -9,6 +9,7 @@ test('Reading PointRecord', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XY, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual(geom.coords, [-155, -154]);
geom = expectGeometry(reader, 4, CoordType.XY, PointRecord);
expectPointsEqual(geom.coords, [-147.43741476950902, -157.28937716011498]);
Expand All @@ -21,10 +22,13 @@ test('Reading PointRecord with M ', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-178, -160, 1]);
geom = expectGeometry(reader, 1, CoordType.XYM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-177, -165, 1000]);
geom = expectGeometry(reader, 2, CoordType.XYM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-171, -165, NaN]);
});

Expand All @@ -35,9 +39,12 @@ test('Reading PointRecord with Z', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYZM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-153, -178, 1, 2]);
geom = expectGeometry(reader, 1, CoordType.XYZM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-158, -175, 2, 3]);
geom = expectGeometry(reader, 5, CoordType.XYZM, PointRecord);
expect(geom.type).toEqual('Point');
expectPointsEqual((geom as PointRecord).coords, [-159, -186, 99.999, NaN]);
});
7 changes: 7 additions & 0 deletions test/polygon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('Reading PolygonRecord', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XY, PolygonRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);
let polygon = geom.coords[0];
expect(polygon.length).toBe(3);
Expand Down Expand Up @@ -38,6 +39,7 @@ test('Reading PolygonRecord', async () => {
]);

geom = expectGeometry(reader, 1, CoordType.XY, PolygonRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);
polygon = geom.coords[0];
expect(polygon.length).toBe(1);
Expand All @@ -50,6 +52,7 @@ test('Reading PolygonRecord', async () => {
]);

geom = expectGeometry(reader, 2, CoordType.XY, PolygonRecord);
expect(geom.type).toEqual('MultiPolygon');
expect(geom.coords.length).toBe(3);
polygon = geom.coords[0];
expect(polygon.length).toBe(3);
Expand Down Expand Up @@ -111,6 +114,7 @@ test('Reading PolygonRecord with M', async () => {
expect(reader.recordCount).toBe(2);

let geom = expectGeometry(reader, 0, CoordType.XYM, PolygonRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);
let polygon = geom.coords[0];
expect(polygon.length).toBe(1);
Expand All @@ -122,6 +126,7 @@ test('Reading PolygonRecord with M', async () => {
]);

geom = expectGeometry(reader, 1, CoordType.XYM, PolygonRecord);
expect(geom.type).toEqual('MultiPolygon');
expect(geom.coords.length).toBe(3);

polygon = geom.coords[0];
Expand Down Expand Up @@ -184,6 +189,7 @@ test('Reading PolygonRecord with Z', async () => {
expect(reader.recordCount).toBe(2);

let geom = expectGeometry(reader, 0, CoordType.XYZM, PolygonRecord);
expect(geom.type).toEqual('Polygon');
expect(geom.coords.length).toBe(1);

let polygon = geom.coords[0];
Expand All @@ -197,6 +203,7 @@ test('Reading PolygonRecord with Z', async () => {
]);

geom = expectGeometry(reader, 1, CoordType.XYZM, PolygonRecord);
expect(geom.type).toEqual('MultiPolygon');
expect(geom.coords.length).toBe(3); //testing only the first polygon bellow

polygon = geom.coords[0];
Expand Down
9 changes: 9 additions & 0 deletions test/polyline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('Reading PolyLineRecord', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XY, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);
let segment = geom.coords[0];
expect(segment.length).toBe(3);
Expand All @@ -17,9 +18,11 @@ test('Reading PolyLineRecord', async () => {
expectPointsEqual(segment[2], [-146.82619944462766, -153.40391265875377]);

geom = expectGeometry(reader, 1, CoordType.XY, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);

geom = expectGeometry(reader, 2, CoordType.XY, PolyLineRecord);
expect(geom.type).toEqual('MultiLineString');
expect(geom.coords.length).toBe(3);
segment = geom.coords[0];
expect(segment.length).toBe(4);
Expand All @@ -46,6 +49,7 @@ test('Reading PolyLineRecord with M', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYM, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);
let segment = geom.coords[0];
expect(segment.length).toBe(3);
Expand All @@ -54,6 +58,7 @@ test('Reading PolyLineRecord with M', async () => {
expectPointsEqual(segment[2], [64.17836737175264, -110.86618752096422, 30]);

geom = expectGeometry(reader, 1, CoordType.XYM, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);
segment = geom.coords[0];
expect(segment.length).toBe(5);
Expand All @@ -64,6 +69,7 @@ test('Reading PolyLineRecord with M', async () => {
expectPointsEqual(segment[4], [75.77823977517522, -100.75347824618558, 5]);

geom = expectGeometry(reader, 2, CoordType.XYM, PolyLineRecord);
expect(geom.type).toEqual('MultiLineString');
expect(geom.coords.length).toBe(3);
segment = geom.coords[0];
expect(segment.length).toBe(3);
Expand All @@ -89,6 +95,7 @@ test('Reading PolyLineRecord with Z', async () => {
const reader = await ShapeReader.fromArrayBuffer(shpBuffer, shxBuffer);

let geom = expectGeometry(reader, 0, CoordType.XYZM, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);
let segment = geom.coords[0];
expectPointsEqual(segment[0], [-115.47093856843338, -100.45604562045679, 100, 5000]);
Expand All @@ -98,6 +105,7 @@ test('Reading PolyLineRecord with Z', async () => {
expectPointsEqual(segment[4], [-29.215477107085803, -93.91252785442344, 104, 9000]);

geom = expectGeometry(reader, 1, CoordType.XYZM, PolyLineRecord);
expect(geom.type).toEqual('LineString');
expect(geom.coords.length).toBe(1);
segment = geom.coords[0];
expectPointsEqual(segment[0], [-112.79404493687429, -114.43537902970968, 1000, NaN]);
Expand All @@ -107,6 +115,7 @@ test('Reading PolyLineRecord with Z', async () => {
expectPointsEqual(segment[4], [-23.861689843967667, -106.4046981350325, 1004, NaN]);

geom = expectGeometry(reader, 2, CoordType.XYZM, PolyLineRecord);
expect(geom.type).toEqual('MultiLineString');
expect(geom.coords.length).toBe(3);
segment = geom.coords[0];
expectPointsEqual(segment[0], [-111.60670519384564, -126.90578866647462, 11, 1]);
Expand Down

0 comments on commit 218553d

Please sign in to comment.