webdggrid 1.0.1

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.
@@ -0,0 +1,366 @@
1
+ // @ts-ignore
2
+ import { loadWasm, unloadWasm } from './libdggrid.wasm.js';
3
+
4
+ /**
5
+ * Cell Topology
6
+ *
7
+ * @export
8
+ * @enum {String}
9
+ */
10
+ export enum Topology {
11
+ 'HEXAGON' = 'HEXAGON',
12
+ 'TRIANGLE' = 'TRIANGLE',
13
+ 'SQUARE' = 'SQUARE',
14
+ 'DIAMOND' = 'DIAMOND',
15
+ }
16
+ /**
17
+ * Projection type
18
+ *
19
+ * @export
20
+ * @enum {number}
21
+ */
22
+ export enum Projection {
23
+ 'ISEA' = 'ISEA',
24
+ 'FULLER' = 'FULLER',
25
+ }
26
+
27
+ export interface Coordinate {
28
+ lat: number;
29
+ lng: number;
30
+ }
31
+
32
+ export interface IDGGSProps {
33
+ poleCoordinates: Coordinate;
34
+ azimuth: number;
35
+ aperture: 3 | 4 | 5 | 7;
36
+ topology: Topology;
37
+ projection: Projection;
38
+ }
39
+
40
+ const DEFAULT_RESOLUTION = 1;
41
+ const DEFAULT_DGGS = {
42
+ poleCoordinates: { lat: 0, lng: 0 },
43
+ azimuth: 0,
44
+ topology: Topology.HEXAGON,
45
+ projection: Projection.ISEA,
46
+ aperture: 7
47
+ } as IDGGSProps;
48
+
49
+ export class Webdggrid {
50
+
51
+ dggs: IDGGSProps = DEFAULT_DGGS;
52
+ resolution: number = DEFAULT_RESOLUTION;
53
+
54
+ private constructor(protected _module: any) {
55
+ this._module = _module;
56
+
57
+ }
58
+
59
+ /**
60
+ * Compiles and instantiates the raw wasm.
61
+ *
62
+ * ::: info
63
+ * In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
64
+ * :::
65
+ *
66
+ * @returns A promise to an instance of the Webdggrid class.
67
+ */
68
+ static load(): Promise<typeof Webdggrid> {
69
+ return loadWasm().then((module: any) => {
70
+ return new Webdggrid(module);
71
+ }).catch(console.log);
72
+ }
73
+
74
+ /**
75
+ * Unloades the compiled wasm instance.
76
+ */
77
+ static unload() {
78
+ unloadWasm();
79
+ }
80
+
81
+ /**
82
+ * @returns The Webdggrid c++ version
83
+ */
84
+ version(): string {
85
+ return this._module.Webdggrid.prototype.version();
86
+ }
87
+
88
+ /**
89
+ * Set the main dggs configuration
90
+ * @param dggs A dggs object
91
+ */
92
+ setDggs(dggs: IDGGSProps = DEFAULT_DGGS, resolution: number = DEFAULT_RESOLUTION) {
93
+ this.dggs = dggs;
94
+ this.resolution = resolution;
95
+ }
96
+
97
+ /**
98
+ * Get the resolution of the current dggs
99
+ * @returns {number} the current dggs resolution
100
+ * @memberof WebDggrid
101
+ */
102
+ getResolution(): number {
103
+ return this.resolution;
104
+ }
105
+ /**
106
+ * Set the resolution of the dggs
107
+ * @param {number} [resolution=DEFAULT_RESOLUTION] the resolution. It should be a valid integer
108
+ * @memberof WebDggrid
109
+ */
110
+ setResolution(resolution: number) {
111
+ this.resolution = resolution;
112
+ }
113
+
114
+ /**
115
+ * test function
116
+ *
117
+ * @return {*}
118
+ * @memberof WebDggrid
119
+ */
120
+ _main() {
121
+ return this._module._main();
122
+ }
123
+
124
+ /**
125
+ * @follow Hi
126
+ * Returns the number of the cells in specific resolution
127
+ * @param {number} [resolution=DEFAULT_RESOLUTION]
128
+ * @return {number}
129
+ * @memberof WebDggrid
130
+ */
131
+ nCells(resolution: number = DEFAULT_RESOLUTION): number {
132
+ const {
133
+ poleCoordinates: { lat, lng },
134
+ azimuth,
135
+ topology,
136
+ projection,
137
+ aperture,
138
+ } = this.dggs;
139
+
140
+ const cellCount = this._module.nCells(
141
+ lat,
142
+ lng,
143
+ azimuth,
144
+ aperture,
145
+ resolution,
146
+ topology,
147
+ projection
148
+ );
149
+
150
+ return cellCount as number;
151
+ }
152
+
153
+ cellAreaKM(resolution: number = DEFAULT_RESOLUTION): number {
154
+ const {
155
+ poleCoordinates: { lat, lng },
156
+ azimuth,
157
+ topology,
158
+ projection,
159
+ aperture,
160
+ } = this.dggs;
161
+
162
+ const cellCount = this._module.nCells(
163
+ lat,
164
+ lng,
165
+ azimuth,
166
+ aperture,
167
+ resolution,
168
+ topology,
169
+ projection
170
+ );
171
+
172
+ return cellCount as number;
173
+ }
174
+
175
+ cellDistKM(resolution: number = DEFAULT_RESOLUTION): number {
176
+ const {
177
+ poleCoordinates: { lat, lng },
178
+ azimuth,
179
+ topology,
180
+ projection,
181
+ aperture,
182
+ } = this.dggs;
183
+
184
+ const cellCount = this._module.nCells(
185
+ lat,
186
+ lng,
187
+ azimuth,
188
+ aperture,
189
+ resolution,
190
+ topology,
191
+ projection
192
+ );
193
+
194
+ return cellCount as number;
195
+ }
196
+
197
+ gridStatCLS(resolution: number = DEFAULT_RESOLUTION): number {
198
+ const {
199
+ poleCoordinates: { lat, lng },
200
+ azimuth,
201
+ topology,
202
+ projection,
203
+ aperture,
204
+ } = this.dggs;
205
+
206
+ const cellCount = this._module.nCells(
207
+ lat,
208
+ lng,
209
+ azimuth,
210
+ aperture,
211
+ resolution,
212
+ topology,
213
+ projection
214
+ );
215
+
216
+ return cellCount as number;
217
+ }
218
+ /**
219
+ * Converts an array of geography coordinates to the list of the sequence numbers AKA DggId
220
+ * @param coordinates A 2d array of [[lng, lat]] values
221
+ * @param resolution [resolution=DEFAULT_RESOLUTION] The dggs resolution
222
+ * @returns An array of the DggIds
223
+ */
224
+ geoToSequenceNum(
225
+ coordinates: number[][],
226
+ resolution: number = DEFAULT_RESOLUTION
227
+ ): bigint[] {
228
+ const {
229
+ poleCoordinates: { lat, lng },
230
+ azimuth,
231
+ topology,
232
+ projection,
233
+ aperture,
234
+ } = this.dggs;
235
+
236
+ const xCoords = coordinates.map((coord) => coord[0]);
237
+ const yCoords = coordinates.map((coord) => coord[1]);
238
+
239
+ const resultArray = this._module.DgGEO_to_SEQNUM(
240
+ lat,
241
+ lng,
242
+ azimuth,
243
+ aperture,
244
+ resolution,
245
+ topology,
246
+ projection,
247
+ xCoords,
248
+ yCoords
249
+ );
250
+
251
+ return resultArray;
252
+ }
253
+ /**
254
+ * Convert a sequence number to the [lng,lat] of the center of the related cell
255
+ * @param sequenceNum
256
+ * @param resolution [resolution=DEFAULT_RESOLUTION]
257
+ * @returns An array of [lng,lat]
258
+ */
259
+ sequenceNumToGeo(
260
+ sequenceNum: bigint[],
261
+ resolution: number = DEFAULT_RESOLUTION
262
+ ): number[][] {
263
+ const {
264
+ poleCoordinates: { lat, lng },
265
+ azimuth,
266
+ topology,
267
+ projection,
268
+ aperture,
269
+ } = this.dggs;
270
+
271
+ const resultArray = this._module.SEQNUM_to_GEO(
272
+ lat,
273
+ lng,
274
+ azimuth,
275
+ aperture,
276
+ resolution,
277
+ topology,
278
+ projection,
279
+ sequenceNum
280
+ );
281
+
282
+ const size = resultArray.length/2;
283
+ const arrayOfArrays:number[][] = [];
284
+ for (let i = 0; i < size; i += 1) {
285
+ arrayOfArrays.push([resultArray[i], resultArray[i+size]]);
286
+ }
287
+
288
+ return arrayOfArrays;
289
+ }
290
+ /**
291
+ * Converts a set of coordinates to the cell centroid values
292
+ * @param coordinates A 2d array of lng and lat values
293
+ * @param resolution [resolution=DEFAULT_RESOLUTION] The resolution of the dggs
294
+ * @returns An array of dggs cell centroid coordinates
295
+ */
296
+ geoToGeo(
297
+ coordinates: number[][],
298
+ resolution: number = DEFAULT_RESOLUTION
299
+ ): number[][] {
300
+ const {
301
+ poleCoordinates: { lat, lng },
302
+ azimuth,
303
+ topology,
304
+ projection,
305
+ aperture,
306
+ } = this.dggs;
307
+
308
+ const xCoords = coordinates.map((coord) => coord[0]);
309
+ const yCoords = coordinates.map((coord) => coord[1]);
310
+
311
+ const resultArray = this._module.GEO_to_GEO(
312
+ lat,
313
+ lng,
314
+ azimuth,
315
+ aperture,
316
+ resolution,
317
+ topology,
318
+ projection,
319
+ xCoords,
320
+ yCoords
321
+ );
322
+
323
+ const size = resultArray.length/2;
324
+ const arrayOfArrays:number[][] = [];
325
+ for (let i = 0; i < size; i += 1) {
326
+ arrayOfArrays.push([resultArray[i], resultArray[i+size]]);
327
+ }
328
+
329
+ return arrayOfArrays;
330
+ }
331
+
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
+ _wVectorToArray = (vector: any) => {
350
+ if (vector.size() === 0) {
351
+ return [];
352
+ }
353
+
354
+ const objectType = vector.$$.ptrType.name;
355
+
356
+ switch (objectType) {
357
+ case 'BigIntegerVector*':
358
+ return this._vectorToArray(vector);
359
+
360
+ default:
361
+ return [];
362
+ }
363
+ };
364
+
365
+ // _extractColumn(arr: any, column: number) { return arr.map((x: any) => x[column]); }
366
+ }
@@ -0,0 +1,2 @@
1
+ export declare function extract(raw: string): Uint8Array;
2
+ //# sourceMappingURL=extract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../src-ts/extract.ts"],"names":[],"mappings":"AAuCA,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAG/C"}
@@ -0,0 +1,4 @@
1
+ export declare namespace Webdggrid {
2
+ function load(): Promise<typeof import("./webdggrid.js").Webdggrid>;
3
+ }
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src-ts/index.ts"],"names":[],"mappings":"AAAA,yBAAiB,SAAS,CAAC;IACzB,SAAgB,IAAI,uDAEnB;CACF"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Cell Topology
3
+ *
4
+ * @export
5
+ * @enum {String}
6
+ */
7
+ export declare enum Topology {
8
+ 'HEXAGON' = "HEXAGON",
9
+ 'TRIANGLE' = "TRIANGLE",
10
+ 'SQUARE' = "SQUARE",
11
+ 'DIAMOND' = "DIAMOND"
12
+ }
13
+ /**
14
+ * Projection type
15
+ *
16
+ * @export
17
+ * @enum {number}
18
+ */
19
+ export declare enum Projection {
20
+ 'ISEA' = "ISEA",
21
+ 'FULLER' = "FULLER"
22
+ }
23
+ export interface Coordinate {
24
+ lat: number;
25
+ lng: number;
26
+ }
27
+ export interface IDGGSProps {
28
+ poleCoordinates: Coordinate;
29
+ azimuth: number;
30
+ aperture: 3 | 4 | 5 | 7;
31
+ topology: Topology;
32
+ projection: Projection;
33
+ }
34
+ export declare class Webdggrid {
35
+ protected _module: any;
36
+ dggs: IDGGSProps;
37
+ resolution: number;
38
+ private constructor();
39
+ /**
40
+ * Compiles and instantiates the raw wasm.
41
+ *
42
+ * ::: info
43
+ * In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing `load` to be asynchronous;
44
+ * :::
45
+ *
46
+ * @returns A promise to an instance of the Webdggrid class.
47
+ */
48
+ static load(): Promise<typeof Webdggrid>;
49
+ /**
50
+ * Unloades the compiled wasm instance.
51
+ */
52
+ static unload(): void;
53
+ /**
54
+ * @returns The Webdggrid c++ version
55
+ */
56
+ version(): string;
57
+ /**
58
+ * Set the main dggs configuration
59
+ * @param dggs A dggs object
60
+ */
61
+ setDggs(dggs?: IDGGSProps, resolution?: number): void;
62
+ /**
63
+ * Get the resolution of the current dggs
64
+ * @returns {number} the current dggs resolution
65
+ * @memberof WebDggrid
66
+ */
67
+ getResolution(): number;
68
+ /**
69
+ * Set the resolution of the dggs
70
+ * @param {number} [resolution=DEFAULT_RESOLUTION] the resolution. It should be a valid integer
71
+ * @memberof WebDggrid
72
+ */
73
+ setResolution(resolution: number): void;
74
+ /**
75
+ * test function
76
+ *
77
+ * @return {*}
78
+ * @memberof WebDggrid
79
+ */
80
+ _main(): any;
81
+ /**
82
+ * @follow Hi
83
+ * Returns the number of the cells in specific resolution
84
+ * @param {number} [resolution=DEFAULT_RESOLUTION]
85
+ * @return {number}
86
+ * @memberof WebDggrid
87
+ */
88
+ nCells(resolution?: number): number;
89
+ cellAreaKM(resolution?: number): number;
90
+ cellDistKM(resolution?: number): number;
91
+ gridStatCLS(resolution?: number): number;
92
+ /**
93
+ * Converts an array of geography coordinates to the list of the sequence numbers AKA DggId
94
+ * @param coordinates A 2d array of [[lng, lat]] values
95
+ * @param resolution [resolution=DEFAULT_RESOLUTION] The dggs resolution
96
+ * @returns An array of the DggIds
97
+ */
98
+ geoToSequenceNum(coordinates: number[][], resolution?: number): bigint[];
99
+ /**
100
+ * Convert a sequence number to the [lng,lat] of the center of the related cell
101
+ * @param sequenceNum
102
+ * @param resolution [resolution=DEFAULT_RESOLUTION]
103
+ * @returns An array of [lng,lat]
104
+ */
105
+ sequenceNumToGeo(sequenceNum: bigint[], resolution?: number): number[][];
106
+ /**
107
+ * Converts a set of coordinates to the cell centroid values
108
+ * @param coordinates A 2d array of lng and lat values
109
+ * @param resolution [resolution=DEFAULT_RESOLUTION] The resolution of the dggs
110
+ * @returns An array of dggs cell centroid coordinates
111
+ */
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[];
117
+ }
118
+ //# sourceMappingURL=webdggrid.d.ts.map
@@ -0,0 +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;C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