webdggrid 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  // @ts-ignore
2
2
  import { loadWasm, unloadWasm } from './libdggrid.wasm.js';
3
-
3
+ import { Feature, FeatureCollection, GeoJsonProperties, Polygon, Position } from 'geojson';
4
4
  /**
5
5
  * Cell Topology
6
6
  *
@@ -24,11 +24,27 @@ export enum Projection {
24
24
  'FULLER' = 'FULLER',
25
25
  }
26
26
 
27
+ /**
28
+ * A geographic coordinate
29
+ */
27
30
  export interface Coordinate {
28
31
  lat: number;
29
32
  lng: number;
30
33
  }
31
34
 
35
+ /**
36
+ * Geojson properties type.
37
+ * TODO: Better handeling the types
38
+ */
39
+ export type DGGSGeoJsonProperty = GeoJsonProperties & {
40
+ /**
41
+ * It stores the seq number if exists
42
+ */
43
+ id?: BigInt;
44
+ i?: BigInt;
45
+ j?: BigInt;
46
+ }
47
+
32
48
  export interface IDGGSProps {
33
49
  poleCoordinates: Coordinate;
34
50
  azimuth: number;
@@ -259,7 +275,7 @@ export class Webdggrid {
259
275
  sequenceNumToGeo(
260
276
  sequenceNum: bigint[],
261
277
  resolution: number = DEFAULT_RESOLUTION
262
- ): number[][] {
278
+ ): Position[] {
263
279
  const {
264
280
  poleCoordinates: { lat, lng },
265
281
  azimuth,
@@ -279,10 +295,10 @@ export class Webdggrid {
279
295
  sequenceNum
280
296
  );
281
297
 
282
- const size = resultArray.length/2;
283
- const arrayOfArrays:number[][] = [];
298
+ const size = resultArray.length / 2;
299
+ const arrayOfArrays: number[][] = [];
284
300
  for (let i = 0; i < size; i += 1) {
285
- arrayOfArrays.push([resultArray[i], resultArray[i+size]]);
301
+ arrayOfArrays.push([resultArray[i], resultArray[i + size]]);
286
302
  }
287
303
 
288
304
  return arrayOfArrays;
@@ -296,7 +312,7 @@ export class Webdggrid {
296
312
  geoToGeo(
297
313
  coordinates: number[][],
298
314
  resolution: number = DEFAULT_RESOLUTION
299
- ): number[][] {
315
+ ): Position[] {
300
316
  const {
301
317
  poleCoordinates: { lat, lng },
302
318
  azimuth,
@@ -320,47 +336,106 @@ export class Webdggrid {
320
336
  yCoords
321
337
  );
322
338
 
323
- const size = resultArray.length/2;
324
- const arrayOfArrays:number[][] = [];
339
+ const size = resultArray.length / 2;
340
+ const arrayOfArrays: number[][] = [];
325
341
  for (let i = 0; i < size; i += 1) {
326
- arrayOfArrays.push([resultArray[i], resultArray[i+size]]);
342
+ arrayOfArrays.push([resultArray[i], resultArray[i + size]]);
327
343
  }
328
344
 
329
345
  return arrayOfArrays;
330
346
  }
331
347
 
332
- _is2dArray(array: any): boolean { return array.some((item: any) => Array.isArray(item)); }
333
-
334
- _arrayToVector(array: any) {
335
- const is2d = this._is2dArray(array);
336
- if (is2d) {
337
- const dDVector = new this._module.DoubleVectorVector();
338
- array.forEach((item: any) => {
339
- const dVector = new this._module.DoubleVector();
340
- dVector.push_back(item[0]);
341
- dVector.push_back(item[1]);
342
- dDVector.push_back(dVector);
343
- });
344
- return dDVector;
345
- }
346
- }
347
- _vectorToArray(vector: any) { return new Array(vector.size()).fill(0).map((_, id) => vector.get(id)); }
348
+ /**
349
+ * Convert an array of sequence numbers to the grid coordinates with format of `[lng,lat]`. The output is an array with the same
350
+ * size as input `sequenceNum` and it includes an array of `CoordinateLike` objects.
351
+ * @param sequenceNum
352
+ * @param resolution [resolution=DEFAULT_RESOLUTION]
353
+ * @returns An array of [lng,lat]
354
+ */
355
+ sequenceNumToGrid(
356
+ sequenceNum: bigint[],
357
+ resolution: number = DEFAULT_RESOLUTION
358
+ ): Position[][] {
359
+ const {
360
+ poleCoordinates: { lat, lng },
361
+ azimuth,
362
+ topology,
363
+ projection,
364
+ aperture,
365
+ } = this.dggs;
348
366
 
349
- _wVectorToArray = (vector: any) => {
350
- if (vector.size() === 0) {
351
- return [];
367
+ let resultArray = [];
368
+ try {
369
+ resultArray = this._module.SeqNumGrid(
370
+ lat,
371
+ lng,
372
+ azimuth,
373
+ aperture,
374
+ resolution,
375
+ topology,
376
+ projection,
377
+ sequenceNum
378
+ );
379
+ } catch (e) {
380
+ console.error(this._module.getExceptionMessage(e).toString());
381
+ throw(e);
352
382
  }
353
383
 
354
- const objectType = vector.$$.ptrType.name;
384
+ const inputSize = sequenceNum.length;
385
+
386
+ const allShapeVertexes = resultArray.slice(0, inputSize);
355
387
 
356
- switch (objectType) {
357
- case 'BigIntegerVector*':
358
- return this._vectorToArray(vector);
388
+ const sumVertexes = allShapeVertexes.reduce((accumulator, currentValue) => {
389
+ return accumulator + currentValue;
390
+ }, 0);
359
391
 
360
- default:
361
- return [];
392
+ const featureSet: Position[][] = [];
393
+
394
+ let xOffset = inputSize;
395
+ let yOffset = inputSize + sumVertexes;
396
+ for (let i = 0; i < allShapeVertexes.length; i += 1) {
397
+ const numVertexes = allShapeVertexes[i];
398
+
399
+ const currentShapeXVertexes = resultArray.slice(xOffset, xOffset + numVertexes);
400
+ const currentShapeYVertexes = resultArray.slice(yOffset, yOffset + numVertexes);
401
+
402
+ const coordinates: Position[] = [];
403
+ for (let i = 0; i < numVertexes; i += 1) {
404
+ coordinates.push([currentShapeXVertexes[i], currentShapeYVertexes[i]]);
405
+ }
406
+ featureSet.push(coordinates);
407
+ xOffset += numVertexes;
408
+ yOffset += numVertexes;
362
409
  }
363
- };
364
410
 
365
- // _extractColumn(arr: any, column: number) { return arr.map((x: any) => x[column]); }
411
+ return featureSet;
412
+ }
413
+
414
+ sequenceNumToGridFeatureCollection(
415
+ sequenceNum: bigint[],
416
+ resolution: number = DEFAULT_RESOLUTION
417
+ ): FeatureCollection<Polygon, DGGSGeoJsonProperty> {
418
+
419
+ const coordinatesArray = this.sequenceNumToGrid(sequenceNum, resolution);
420
+
421
+ const features = coordinatesArray.map((coordinates, index) => {
422
+ const seqNum = sequenceNum[index];
423
+ return {
424
+ type: 'Feature',
425
+ geometry: {
426
+ type: 'Polygon',
427
+ coordinates: [coordinates]
428
+ },
429
+ id: seqNum as unknown as number,
430
+ properties: {
431
+ id: seqNum
432
+ } as DGGSGeoJsonProperty
433
+ } as Feature<Polygon, DGGSGeoJsonProperty>;
434
+ });
435
+
436
+ return {
437
+ type: 'FeatureCollection',
438
+ features,
439
+ };
440
+ }
366
441
  }
@@ -1,3 +1,4 @@
1
+ import { FeatureCollection, GeoJsonProperties, Polygon, Position } from 'geojson';
1
2
  /**
2
3
  * Cell Topology
3
4
  *
@@ -20,10 +21,25 @@ export declare enum Projection {
20
21
  'ISEA' = "ISEA",
21
22
  'FULLER' = "FULLER"
22
23
  }
24
+ /**
25
+ * A geographic coordinate
26
+ */
23
27
  export interface Coordinate {
24
28
  lat: number;
25
29
  lng: number;
26
30
  }
31
+ /**
32
+ * Geojson properties type.
33
+ * TODO: Better handeling the types
34
+ */
35
+ export type DGGSGeoJsonProperty = GeoJsonProperties & {
36
+ /**
37
+ * It stores the seq number if exists
38
+ */
39
+ id?: BigInt;
40
+ i?: BigInt;
41
+ j?: BigInt;
42
+ };
27
43
  export interface IDGGSProps {
28
44
  poleCoordinates: Coordinate;
29
45
  azimuth: number;
@@ -102,17 +118,22 @@ export declare class Webdggrid {
102
118
  * @param resolution [resolution=DEFAULT_RESOLUTION]
103
119
  * @returns An array of [lng,lat]
104
120
  */
105
- sequenceNumToGeo(sequenceNum: bigint[], resolution?: number): number[][];
121
+ sequenceNumToGeo(sequenceNum: bigint[], resolution?: number): Position[];
106
122
  /**
107
123
  * Converts a set of coordinates to the cell centroid values
108
124
  * @param coordinates A 2d array of lng and lat values
109
125
  * @param resolution [resolution=DEFAULT_RESOLUTION] The resolution of the dggs
110
126
  * @returns An array of dggs cell centroid coordinates
111
127
  */
112
- geoToGeo(coordinates: number[][], resolution?: number): number[][];
113
- _is2dArray(array: any): boolean;
114
- _arrayToVector(array: any): any;
115
- _vectorToArray(vector: any): any[];
116
- _wVectorToArray: (vector: any) => any[];
128
+ geoToGeo(coordinates: number[][], resolution?: number): Position[];
129
+ /**
130
+ * Convert an array of sequence numbers to the grid coordinates with format of `[lng,lat]`. The output is an array with the same
131
+ * size as input `sequenceNum` and it includes an array of `CoordinateLike` objects.
132
+ * @param sequenceNum
133
+ * @param resolution [resolution=DEFAULT_RESOLUTION]
134
+ * @returns An array of [lng,lat]
135
+ */
136
+ sequenceNumToGrid(sequenceNum: bigint[], resolution?: number): Position[][];
137
+ sequenceNumToGridFeatureCollection(sequenceNum: bigint[], resolution?: number): FeatureCollection<Polygon, DGGSGeoJsonProperty>;
117
138
  }
118
139
  //# sourceMappingURL=webdggrid.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webdggrid.d.ts","sourceRoot":"","sources":["../src-ts/webdggrid.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,oBAAY,QAAQ;IAChB,SAAS,YAAY;IACrB,UAAU,aAAa;IACvB,QAAQ,WAAW;IACnB,SAAS,YAAY;CACxB;AACD;;;;;GAKG;AACH,oBAAY,UAAU;IAClB,MAAM,SAAS;IACf,QAAQ,WAAW;CACtB;AAED,MAAM,WAAW,UAAU;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,UAAU;IACvB,eAAe,EAAE,UAAU,CAAC;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACxB,QAAQ,EAAE,QAAQ,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC;CAC1B;AAWD,qBAAa,SAAS;IAKE,SAAS,CAAC,OAAO,EAAE,GAAG;IAH1C,IAAI,EAAE,UAAU,CAAgB;IAChC,UAAU,EAAE,MAAM,CAAsB;IAExC,OAAO;IAKP;;;;;;;;OAQG;IACH,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,OAAO,SAAS,CAAC;IAMxC;;OAEG;IACH,MAAM,CAAC,MAAM;IAIb;;OAEG;IACH,OAAO,IAAI,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