forked from dfellis/h3-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h3napi.c
1421 lines (1221 loc) · 38.1 KB
/
h3napi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <node_api.h>
#include <stdio.h>
#include "h3api.h"
#define napiFn(N) napi_value N ## Napi(napi_env env, napi_callback_info info)
#define napiArgs(N) \
napi_value argv[N];\
size_t argc = N;\
\
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);\
\
if (argc < N) {\
napi_throw_error(env, "EINVAL", "Too few arguments");\
return NULL;\
}
#define napiArgsWithOptional(N, M) \
napi_value argv[N + M];\
size_t argc = N + M;\
\
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);\
\
if (argc < N) {\
napi_throw_error(env, "EINVAL", "Too few arguments");\
return NULL;\
}
#define napiGetNapiArg(I, N) \
napi_value N = argv[I];
#define napiGetArg(I, J, T, N) \
T N;\
if (napi_get_value_ ## J(env, argv[I], (T *) &N) != napi_ok) {\
napi_throw_error(env, "EINVAL", "Expected " #J " in arg " #I);\
return NULL;\
}
#define napiGetStringArg(I, L, N) \
char N[L];\
size_t N ## Count;\
\
if (napi_get_value_string_utf8(env, argv[I], N, L, &N ## Count) != napi_ok) {\
napi_throw_error(env, "EINVAL", "Expected unit argument");\
return NULL;\
}
#define napiToVar(V, J, T, N) \
T N;\
napi_status napiToVar ## V ## N = napi_get_value_ ## J(env, V, &N);\
if (napiToVar ## V ## N != napi_ok) {\
napi_throw_error(env, "EINVAL", "Could not get " #J " value from " #V);\
}\
if (napiToVar ## V ## N != napi_ok)
#define napiFromArray(A, I, N) \
napi_value N;\
napi_status napiFromArray ## N = napi_get_element(env, A, I, &N);\
if (napiFromArray ## N != napi_ok) {\
napi_throw_error(env, "EINVAL", "Could not get element from array at index " #I);\
}\
if (napiFromArray ## N != napi_ok)
#define napiArrayLen(A, N) \
uint32_t N;\
napi_status napiArrayLen ## A ## N = napi_get_array_length(env, A, &N);\
if (napiArrayLen ## A ## N != napi_ok) {\
napi_throw_error(env, "EINVAL", "Did not receive a valid array");\
}\
if (napiArrayLen ## A ## N != napi_ok)
#define napiGetH3IndexArg(I, O) \
H3Index O;\
/*For string form input*/\
char O ## Str[17];\
size_t O ## StrCount;\
/*For typedarray form input*/\
napi_typedarray_type O ## Type;\
size_t O ## Length;\
void* O ## Data;\
/*For array form input*/\
napiGetNapiArg(I, O ## Arr);\
uint32_t O ## ArrSz;\
\
if (napi_get_value_string_utf8(env, argv[I], O ## Str, 17, &O ## StrCount) == napi_ok) {\
O = stringToH3(O ## Str);\
} else if (napi_get_typedarray_info(env, O ## Arr, &O ## Type, &O ## Length, &O ## Data, NULL, NULL) == napi_ok) {\
if (O ## Type != napi_uint32_array || O ## Length != 2) {\
napi_throw_error(env, "EINVAL", "Invalid Uint32Array H3 index in arg " #I);\
return NULL;\
}\
O = ((H3Index)*(((uint32_t*)O ## Data) + 1) << 32) | *((uint32_t*)O ## Data);\
} else if (napi_get_array_length(env, O ## Arr, &O ## ArrSz) == napi_ok) {\
if (O ## ArrSz != 2) {\
napi_throw_error(env, "EINVAL", "Invalid length array H3 index in arg " #I);\
return NULL;\
}\
napi_value O ## Val0, O ## Val1;\
if (napi_get_element(env, O ## Arr, 0, &O ## Val0) != napi_ok || napi_get_element(env, O ## Arr, 1, &O ## Val1) != napi_ok) {\
napi_throw_error(env, "EINVAL", "Invalid array H3 index in arg " #I);\
return NULL;\
}\
uint32_t O ## Part0, O ## Part1;\
if (napi_get_value_uint32(env, O ## Val0, &O ## Part0) != napi_ok || napi_get_value_uint32(env, O ## Val1, &O ## Part1) != napi_ok) {\
napi_throw_error(env, "EINVAL", "Invalid integer array H3 index in arg " #I);\
return NULL;\
}\
O = ((H3Index)(O ## Part1) << 32) | (O ## Part0);\
} else {\
napi_throw_error(env, "EINVAL", "Expected string or array H3 index in arg " #I);\
return NULL;\
}
#define napiGetH3Index(I, O) \
char O ## Str[17];\
size_t O ## StrCount;\
napi_status napiGetH3Index ## I = napi_get_value_string_utf8(env, I, O ## Str, 17, &O ## StrCount);\
if (napiGetH3Index ## I != napi_ok) {\
napi_throw_error(env, "EINVAL", "Expected string h3 index in arg " #I);\
}\
H3Index O = stringToH3(O ## Str);\
if (napiGetH3Index ## I != napi_ok)
#define napiFixedArray(V, L) \
napi_value V;\
napi_status napiFixedArray ## V = napi_create_array_with_length(env, L, &V);\
if (napiFixedArray ## V != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create fixed length array");\
return NULL;\
}\
if (napiFixedArray ## V != napi_ok)
#define napiVarArray(V) \
napi_value V;\
napi_status napiVarArray ## V = napi_create_array(env, &V);\
if (napiVarArray ## V != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create variable array");\
}\
if (napiVarArray ## V != napi_ok)
#define napiSetNapiValue(A, I, N) \
napi_status napiSetNapiValue ## N = napi_set_element(env, A, I, N);\
if (napiSetNapiValue ## N != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not store " #N " in array");\
}\
if (napiSetNapiValue ## N != napi_ok)
#define napiSetValue(A, I, J, V, N) \
napi_value N;\
\
if (napi_create_ ## J(env, V, &N) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not write " #N #J);\
return NULL;\
}\
napiSetNapiValue(A, I, N)
#define napiNapiH3Index(V, O) \
char V ## String[17];\
h3ToString(V, V ## String, 17);\
napi_value O;\
napi_status napiNapiH3Index ## V = napi_create_string_utf8(env, V ## String, 15, &O);\
if (napiNapiH3Index ## V != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not write H3 string");\
}\
if (napiNapiH3Index ## V != napi_ok)
#define napiNapiUnidirectionalEdge(V, O) \
char V ## String[17];\
h3ToString(V, V ## String, 17);\
napi_value O;\
napi_status napiNapiUnidirectionalEdge ## V = napi_create_string_utf8(env, V ## String, 16, &O);\
if (napiNapiUnidirectionalEdge ## V != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not write H3 string");\
}\
if (napiNapiUnidirectionalEdge ## V != napi_ok)
#define napiNapiValue(V, T, N) \
napi_value N;\
if (napi_create_ ## T(env, V, &N) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create " #N);\
return NULL;\
}
#define napiNapiBool(V, N) \
napi_value N;\
if (napi_get_boolean(env, V, &N) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create boolean");\
return NULL;\
}
#define napiObject(O) \
napi_value O;\
if (napi_create_object(env, &O) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create object");\
return NULL;\
}
#define napiStoreNapiInObject(O, N, V) \
napi_set_named_property(env, O, N, V);
#define napiStoreInObject(O, N, T, V) \
napi_value N;\
if (napi_create_ ## T(env, V, &N) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not create " #N);\
return NULL;\
}\
napiStoreNapiInObject(O, #N, N);
#define napiGetFromObject(O, N, T, R) \
napi_value N;\
if (napi_get_named_property(env, O, #N, &N) != napi_ok) {\
napi_throw_error(env, "ENOSPC", "Could not retrieve " #N);\
return NULL;\
}\
if (napi_get_value_ ## T(env, N, R) != napi_ok) {\
napi_throw_error(env, "EINVAL", "Expected " #N " in object");\
return NULL;\
}
#define napiExport(N) \
napi_value N ## Fn;\
napi_create_function(env, NULL, 0, N ## Napi, NULL, &N ## Fn);\
napi_set_named_property(env, exports, #N, N ## Fn);
///////////////////////////////////////////////////////////////////////////////
// Indexing Functions //
///////////////////////////////////////////////////////////////////////////////
napiFn(geoToH3) {
napiArgs(3);
napiGetArg(0, double, double, lat);
napiGetArg(1, double, double, lng);
napiGetArg(2, int32, int, res);
GeoCoord geo = { degsToRads(lat), degsToRads(lng) };
H3Index h3 = geoToH3(&geo, res);
napiNapiH3Index(h3, result);
return result;
}
napiFn(h3ToGeo) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
GeoCoord geo = { 0 };
h3ToGeo(h3, &geo);
napiFixedArray(result, 2);
napiSetValue(result, 0, double, radsToDegs(geo.lat), lat);
napiSetValue(result, 1, double, radsToDegs(geo.lon), lng);
return result;
}
napiFn(h3ToGeoBoundary) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
GeoBoundary geoBoundary = { 0 };
h3ToGeoBoundary(h3, &geoBoundary);
napiFixedArray(result, geoBoundary.numVerts);
for (int i = 0; i < geoBoundary.numVerts; i++) {
napiFixedArray(latlngArray, 2);
napiSetValue(latlngArray, 0, double, radsToDegs(geoBoundary.verts[i].lat), lat);
napiSetValue(latlngArray, 1, double, radsToDegs(geoBoundary.verts[i].lon), lng);
napiSetNapiValue(result, i, latlngArray);
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Inspection Functions //
///////////////////////////////////////////////////////////////////////////////
napiFn(h3GetResolution) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
int res = h3GetResolution(h3);
napiNapiValue(res, int32, result);
return result;
}
napiFn(h3GetBaseCell) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
int baseCell = h3GetBaseCell(h3);
napiNapiValue(baseCell, int32, result);
return result;
}
napiFn(h3IsValid) {
napiArgs(1);
// inlined napiGetH3IndexArg in order to return false instead of throwing
H3Index h3;
/*For string form input*/
char h3Str[17];
size_t h3StrCount;
/*For typedarray form input*/
napi_typedarray_type h3Type;
size_t h3Length;
void* h3Data;
/*For array form input*/
napiGetNapiArg(0, h3Arr);
uint32_t h3ArrSz;
if (napi_get_value_string_utf8(env, argv[0], h3Str, 17, &h3StrCount) == napi_ok) {
h3 = stringToH3(h3Str);
} else if (napi_get_typedarray_info(env, h3Arr, &h3Type, &h3Length, &h3Data, NULL, NULL) == napi_ok) {
if (h3Type != napi_uint32_array || h3Length != 2) {
napiNapiBool(false, result);
return result;
}
h3 = ((H3Index)*(((uint32_t*)h3Data) + 1) << 32) | *((uint32_t*)h3Data);
} else if (napi_get_array_length(env, h3Arr, &h3ArrSz) == napi_ok) {
if (h3ArrSz != 2) {
napiNapiBool(false, result);
return result;
}
napi_value h3Val0, h3Val1;
if (napi_get_element(env, h3Arr, 0, &h3Val0) != napi_ok || napi_get_element(env, h3Arr, 1, &h3Val1) != napi_ok) {
napiNapiBool(false, result);
return result;
}
uint32_t h3Part0, h3Part1;
if (napi_get_value_uint32(env, h3Val0, &h3Part0) != napi_ok || napi_get_value_uint32(env, h3Val1, &h3Part1) != napi_ok) {
napiNapiBool(false, result);
return result;
}
h3 = ((H3Index)(h3Part1) << 32) | (h3Part0);
} else {
napiNapiBool(false, result);
return result;
}
int isValid = h3IsValid(h3);
napiNapiBool(isValid, result);
return result;
}
napiFn(h3IsResClassIII) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
int isResClassIII = h3IsResClassIII(h3);
napiNapiBool(isResClassIII, result);
return result;
}
napiFn(h3IsPentagon) {
napiArgs(1);
napiGetH3IndexArg(0, h3);
int isPentagon = h3IsPentagon(h3);
napiNapiBool(isPentagon, result);
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Traversal Functions //
///////////////////////////////////////////////////////////////////////////////
napiFn(kRing) {
napiArgs(2);
napiGetH3IndexArg(0, h3);
napiGetArg(1, int32, int, k);
int maxSize = maxKringSize(k);
H3Index* kRingOut = calloc(maxSize, sizeof(H3Index));
kRing(h3, k, kRingOut);
int arrayIndex = 0;
napiVarArray(result) {
free(kRingOut);
return NULL;
}
for (int i = 0; i < maxSize; i++) {
if (kRingOut[i] == 0) continue;
H3Index h3Num = kRingOut[i];
napiNapiH3Index(h3Num, h3Val) {
free(kRingOut);
return NULL;
}
napiSetNapiValue(result, arrayIndex, h3Val) {
free(kRingOut);
return NULL;
}
arrayIndex++;
}
free(kRingOut);
return result;
}
napiFn(kRingDistances) {
napiArgs(2);
napiGetH3IndexArg(0, h3);
napiGetArg(1, int32, int, k);
int maxSize = maxKringSize(k);
H3Index* kRingOut = calloc(maxSize, sizeof(H3Index));
int* distances = calloc(maxSize, sizeof(int));
kRingDistances(h3, k, kRingOut, distances);
napiVarArray(result) {
free(distances);
free(kRingOut);
return NULL;
}
for (int i = 0; i < k + 1; i++) {
napiVarArray(ring) {
free(distances);
free(kRingOut);
return NULL;
}
napiSetNapiValue(result, i, ring) {
free(distances);
free(kRingOut);
return NULL;
}
}
int* arrayIndices = calloc(k + 1, sizeof(int));
for (int i = 0; i < maxSize; i++) {
if (kRingOut[i] == 0) continue;
H3Index h3Num = kRingOut[i];
int ring = distances[i];
napiNapiH3Index(h3Num, h3Val) {
free(arrayIndices);
free(distances);
free(kRingOut);
return NULL;
}
napiFromArray(result, ring, ringArray) {
free(arrayIndices);
free(distances);
free(kRingOut);
return NULL;
}
napiSetNapiValue(ringArray, arrayIndices[ring], h3Val) {
free(arrayIndices);
free(distances);
free(kRingOut);
return NULL;
}
arrayIndices[ring]++;
}
free(arrayIndices);
free(distances);
free(kRingOut);
return result;
}
napiFn(hexRing) {
napiArgs(2);
napiGetH3IndexArg(0, h3);
napiGetArg(1, int32, int, k);
int maxSize = k == 0 ? 1 : 6 * k;
H3Index* hexRingOut = calloc(maxSize, sizeof(H3Index));
if (hexRing(h3, k, hexRingOut) != 0) {
napi_throw_error(env, "EINVAL", "Pentagon encountered in range of ring");
}
int arrayIndex = 0;
napiVarArray(result) {
free(hexRingOut);
return NULL;
}
for (int i = 0; i < maxSize; i++) {
if (hexRingOut[i] == 0) continue;
H3Index h3Num = hexRingOut[i];
napiNapiH3Index(h3Num, h3Val) {
free(hexRingOut);
return NULL;
}
napiSetNapiValue(result, arrayIndex, h3Val) {
free(hexRingOut);
return NULL;
}
arrayIndex++;
}
free(hexRingOut);
return result;
}
napiFn(h3Distance) {
napiArgs(2);
napiGetH3IndexArg(0, origin);
napiGetH3IndexArg(1, destination);
int distance = h3Distance(origin, destination);
napiNapiValue(distance, int32, result);
return result;
}
napiFn(experimentalH3ToLocalIj) {
napiArgs(2);
napiGetH3IndexArg(0, origin);
napiGetH3IndexArg(1, destination);
CoordIJ ij = { 0 };
int errorCode = experimentalH3ToLocalIj(origin, destination, &ij);
switch (errorCode) {
case 0:
break;
case 1:
napi_throw_error(env, "EINVAL", "Incompatible origin and index");
return NULL;
case 2:
default:
napi_throw_error(env, "EINVAL", "Local IJ coordinates undefined for this origin and index"
" pair. The index may be too far from the origin.");
return NULL;
case 3:
case 4:
case 5:
napi_throw_error(env, "EINVAL", "Encountered possible pentagon distortion.");
return NULL;
}
napiObject(result);
napiStoreInObject(result, i, int32, ij.i);
napiStoreInObject(result, j, int32, ij.j);
return result;
}
napiFn(experimentalLocalIjToH3) {
napiArgs(2);
napiGetH3IndexArg(0, origin);
napiGetNapiArg(1, coords);
CoordIJ ij = { 0 };
H3Index h3;
napiGetFromObject(coords, i, int32, &ij.i);
napiGetFromObject(coords, j, int32, &ij.j);
int errorCode = experimentalLocalIjToH3(origin, &ij, &h3);
if (errorCode != 0) {
napi_throw_error(env, "EINVAL", "Index not defined for this origin and IJ coordinate pair. "
"IJ coordinates may be too far from origin, or a pentagon distortion was encountered.");
return NULL;
}
napiNapiH3Index(h3, result);
return result;
}
napiFn(h3Line) {
napiArgs(2);
napiGetH3IndexArg(0, origin);
napiGetH3IndexArg(1, destination);
int numHexes = h3LineSize(origin, destination);
if (numHexes <= 0) {
napi_throw_error(env, "EINVAL", "Line cannot be calculated");
return NULL;
}
H3Index* line = calloc(numHexes, sizeof(H3Index));
int errorCode = h3Line(origin, destination, line);
if (errorCode != 0) {
napi_throw_error(env, "EINVAL", "Line cannot be calculated");
free(line);
return NULL;
}
int arrayIndex = 0;
napiVarArray(result) {
free(line);
return NULL;
}
for (int i = 0; i < numHexes; i++) {
if (line[i] == 0) continue;
H3Index h3Num = line[i];
napiNapiH3Index(h3Num, h3Val) {
free(line);
return NULL;
}
napiSetNapiValue(result, arrayIndex, h3Val) {
free(line);
return NULL;
}
arrayIndex++;
}
free(line);
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Hierarchy Functions //
///////////////////////////////////////////////////////////////////////////////
napiFn(h3ToParent) {
napiArgs(2);
napiGetH3IndexArg(0, h3);
napiGetArg(1, int32, int, res);
H3Index h3Parent = h3ToParent(h3, res);
napiNapiH3Index(h3Parent, result);
return result;
}
napiFn(h3ToChildren) {
napiArgs(2);
napiGetH3IndexArg(0, h3);
napiGetArg(1, int32, int, res);
int maxSize = maxH3ToChildrenSize(h3, res);
H3Index* children = calloc(maxSize, sizeof(H3Index));
h3ToChildren(h3, res, children);
napiVarArray(result) {
free(children);
return NULL;
}
int arrayIndex = 0;
for (int i = 0; i < maxSize; i++) {
H3Index child = children[i];
if (child == 0) continue;
napiNapiH3Index(child, childObj) {
free(children);
return NULL;
}
napiSetNapiValue(result, arrayIndex, childObj) {
free(children);
return NULL;
}
arrayIndex++;
}
free(children);
return result;
}
napiFn(compact) {
napiArgs(1);
napiGetNapiArg(0, h3IndexArrayObj);
uint32_t arrayLen;
if (napi_get_array_length(env, h3IndexArrayObj, &arrayLen) != napi_ok) {
napi_throw_error(env, "EINVAL", "Expected an array of h3 indexes");
return NULL;
}
H3Index* uncompactedArray = calloc(arrayLen, sizeof(H3Index));
H3Index* compactedArray = calloc(arrayLen, sizeof(H3Index));
for (int i = 0; i < arrayLen; i++) {
napiFromArray(h3IndexArrayObj, i, h3IndexObj) {
free(uncompactedArray);
free(compactedArray);
return NULL;
}
napiGetH3Index(h3IndexObj, h3Index) {
free(uncompactedArray);
free(compactedArray);
return NULL;
}
uncompactedArray[i] = h3Index;
}
int errorCode = compact(uncompactedArray, compactedArray, arrayLen);
if (errorCode != 0) {
napi_throw_error(env, "EINVAL", "Unexpected non-hexagon in provided set");
free(compactedArray);
free(uncompactedArray);
return NULL;
}
napiVarArray(result) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
int arrayIndex = 0;
for (int i = 0; i < arrayLen; i++) {
H3Index compacted = compactedArray[i];
if (compacted == 0) continue;
napiNapiH3Index(compacted, compactedObj) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
napiSetNapiValue(result, arrayIndex, compactedObj) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
arrayIndex++;
}
free(compactedArray);
free(uncompactedArray);
return result;
}
napiFn(uncompact) {
napiArgs(2);
napiGetNapiArg(0, h3IndexArrayObj);
napiGetArg(1, int32, int, res);
uint32_t arrayLen;
if (napi_get_array_length(env, h3IndexArrayObj, &arrayLen) != napi_ok) {
napi_throw_error(env, "EINVAL", "Expected an array of h3 indexes");
return NULL;
}
H3Index* compactedArray = calloc(arrayLen, sizeof(H3Index));
for (int i = 0; i < arrayLen; i++) {
napiFromArray(h3IndexArrayObj, i, h3IndexObj) {
free(compactedArray);
return NULL;
}
napiGetH3Index(h3IndexObj, h3Index) {
free(compactedArray);
return NULL;
}
compactedArray[i] = h3Index;
}
int maxSize = maxUncompactSize(compactedArray, arrayLen, res);
H3Index* uncompactedArray = calloc(maxSize, sizeof(H3Index));
int errorCode = uncompact(compactedArray, arrayLen, uncompactedArray, maxSize, res);
if (errorCode != 0) {
napi_throw_error(env, "EINVAL", "Unexpected non-hexagon in provided set");
free(compactedArray);
free(uncompactedArray);
return NULL;
}
napiVarArray(result) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
int arrayIndex = 0;
for (int i = 0; i < maxSize; i++) {
H3Index uncompacted = uncompactedArray[i];
if (uncompacted == 0) continue;
napiNapiH3Index(uncompacted, uncompactedObj) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
napiSetNapiValue(result, arrayIndex, uncompactedObj) {
free(compactedArray);
free(uncompactedArray);
return NULL;
}
arrayIndex++;
}
free(compactedArray);
free(uncompactedArray);
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Region Functions //
///////////////////////////////////////////////////////////////////////////////
bool polygonArrayToGeofence(napi_env env, Geofence* geofence, napi_value polygonArray, bool isGeoJson) {
uint32_t arrayLength;
if (napi_get_array_length(env, polygonArray, &arrayLength) != napi_ok) {
napi_throw_error(env, "EINVAL", "Could not read length of geofence array");
// Sorta-useless calloc to make sure error-handling 'free' below always works
geofence->verts = calloc(1, sizeof(GeoCoord));
return false;
}
geofence->numVerts = arrayLength;
geofence->verts = calloc(arrayLength, sizeof(GeoCoord));
for (int i = 0; i < arrayLength; i++) {
napiFromArray(polygonArray, i, latlngArray) {
return false;
}
napiFromArray(latlngArray, 0, a) {
return false;
}
napiFromArray(latlngArray, 1, b) {
return false;
}
if (isGeoJson) {
napiToVar(b, double, double, lat) {
return false;
}
napiToVar(a, double, double, lng) {
return false;
}
geofence->verts[i].lat = degsToRads(lat);
geofence->verts[i].lon = degsToRads(lng);
} else {
napiToVar(a, double, double, lat) {
return false;
}
napiToVar(b, double, double, lng) {
return false;
}
geofence->verts[i].lat = degsToRads(lat);
geofence->verts[i].lon = degsToRads(lng);
}
}
return true;
}
napiFn(polyfill) {
napiArgsWithOptional(2, 1);
napiGetNapiArg(0, geofenceArrayObj);
napiGetArg(1, int32, int, res);
napi_value isGeoJsonObj;
if (napi_coerce_to_bool(env, argv[2], &isGeoJsonObj) != napi_ok) {
napi_throw_error(env, "EINVAL", "Somehow cannot coerce a boolean?");
return NULL;
}
bool isGeoJson;
if (napi_get_value_bool(env, isGeoJsonObj, &isGeoJson) != napi_ok) {
napi_throw_error(env, "EINVAL", "Somehow cannot extract boolean from JS object");
return NULL;
}
napi_valuetype typepoke;
if (napi_typeof(env, geofenceArrayObj, &typepoke) != napi_ok) {
napi_throw_error(env, "EINVAL", "No geofence array provided?");
return NULL;
}
if (typepoke != napi_object) {
napi_throw_error(env, "EINVAL", "Non-array value provided as geofence array.");
return NULL;
}
uint32_t arrayLength;
if (napi_get_array_length(env, geofenceArrayObj, &arrayLength) != napi_ok) {
napi_throw_error(env, "EINVAL", "Could not read length of geofence array");
return NULL;
}
napiVarArray(result);
if (arrayLength == 0) return result;
napiFromArray(geofenceArrayObj, 0, firstInnerArray);
if (napi_get_array_length(env, firstInnerArray, &arrayLength) != napi_ok) {
napi_throw_error(env, "EINVAL", "Could not read length of the first inner array");
return NULL;
}
if (arrayLength == 0) return result;
napiFromArray(firstInnerArray, 0, nestingTest);
if (napi_typeof(env, nestingTest, &typepoke) != napi_ok) {
napi_throw_error(env, "EINVAL", "No geofence data provided?");
return NULL;
}
GeoPolygon polygon = { 0 };
if (typepoke == napi_number) {
// Single polygon shape, no holes
polygon.numHoles = 0;
if (!polygonArrayToGeofence(env, &polygon.geofence, geofenceArrayObj, isGeoJson)) {
free(polygon.geofence.verts);
return NULL;
}
} else {
// Multiple polygon shapes, has holes
if (napi_get_array_length(env, geofenceArrayObj, &arrayLength) != napi_ok) {
napi_throw_error(env, "EINVAL", "Could not read length of geofence array");
return NULL;
}
polygon.numHoles = arrayLength - 1;
if (polygon.numHoles > 0) {
polygon.holes = calloc(polygon.numHoles, sizeof(Geofence));
}
for (int i = 0; i < arrayLength; i++) {
napiFromArray(geofenceArrayObj, i, polygonArray) {
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return NULL;
}
if (i == 0) {
// Main geofence case
if (!polygonArrayToGeofence(env, &polygon.geofence, polygonArray, isGeoJson)) {
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return NULL;
}
} else {
// Hole case
if (!polygonArrayToGeofence(env, &polygon.holes[i - 1], polygonArray, isGeoJson)) {
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return NULL;
}
}
}
}
int maxSize = maxPolyfillSize(&polygon, res);
H3Index* fill = calloc(maxSize, sizeof(H3Index));
polyfill(&polygon, res, fill);
int arrayIndex = 0;
for (int i = 0; i < maxSize; i++) {
H3Index h3 = fill[i];
if (h3 == 0) continue;
napiNapiH3Index(h3, h3Obj) {
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return NULL;
}
napiSetNapiValue(result, arrayIndex, h3Obj) {
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return NULL;
}
arrayIndex++;
}
if (polygon.geofence.numVerts > 0) {
free(polygon.geofence.verts);
}
if (polygon.numHoles > 0) {
for (int x = 0; x < polygon.numHoles; x++) {
if (polygon.holes[x].numVerts > 0) {
free(polygon.holes[x].verts);
}
}
free(polygon.holes);
}
return result;
}
napiFn(h3SetToMultiPolygon) {
napiArgsWithOptional(1, 1);
napiGetNapiArg(0, h3SetArrayObj);
napi_value formatAsGeoJsonObj;
if (napi_coerce_to_bool(env, argv[1], &formatAsGeoJsonObj) != napi_ok) {
napi_throw_error(env, "EINVAL", "Somehow cannot coerce a boolean?");
return NULL;
}
bool formatAsGeoJson;