forked from uber/h3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h3.go
897 lines (695 loc) · 23.2 KB
/
h3.go
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
// Package h3 is the go binding for Uber's H3 Geo Index system.
// It uses cgo to link with a statically compiled h3 library
package h3
/*
* Copyright 2018 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
#cgo CFLAGS: -std=c99
#cgo CFLAGS: -DH3_HAVE_VLA=1
#cgo LDFLAGS: -lm
#include <stdlib.h>
#include <h3_h3api.h>
#include <h3_h3Index.h>
*/
import "C"
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"unsafe"
)
const (
// MaxCellBndryVerts is the maximum number of vertices that can be used
// to represent the shape of a cell.
MaxCellBndryVerts = C.MAX_CELL_BNDRY_VERTS
// MaxResolution is the maximum H3 resolution a LatLng can be indexed to.
MaxResolution = C.MAX_H3_RES
// The number of faces on an icosahedron
NumIcosaFaces = C.NUM_ICOSA_FACES
// The number of H3 base cells
NumBaseCells = C.NUM_BASE_CELLS
// The number of H3 pentagon cells (same at every resolution)
NumPentagons = C.NUM_PENTAGONS
// InvalidH3Index is a sentinel value for an invalid H3 index.
InvalidH3Index = C.H3_NULL
base16 = 16
bitSize = 64
numCellEdges = 6
numEdgeCells = 2
DegsToRads = math.Pi / 180.0
RadsToDegs = 180.0 / math.Pi
)
type (
// Cell is an Index that identifies a single hexagon cell at a resolution.
Cell int64
// DirectedEdge is an Index that identifies a directed edge between two cells.
DirectedEdge int64
CoordIJ struct {
I, J int
}
// CellBoundary is a slice of LatLng. Note, len(CellBoundary) will never
// exceed MaxCellBndryVerts.
CellBoundary []LatLng
// GeoLoop is a slice of LatLng points that make up a loop.
GeoLoop []LatLng
// LatLng is a struct for geographic coordinates in degrees.
LatLng struct {
Lat, Lng float64
}
// GeoPolygon is a GeoLoop with 0 or more GeoLoop holes.
GeoPolygon struct {
GeoLoop GeoLoop
Holes []GeoLoop
}
// LinkedGeoPolygon is a linked-list of GeoPolygons.
// TODO: not implemented.
LinkedGeoPolygon struct{}
)
func NewLatLng(lat, lng float64) LatLng {
return LatLng{lat, lng}
}
// LatLngToCell returns the Cell at resolution for a geographic coordinate.
func LatLngToCell(latLng LatLng, resolution int) Cell {
var i C.H3Index
C.latLngToCell(latLng.toCPtr(), C.int(resolution), &i)
return Cell(i)
}
// Cell returns the Cell at resolution for a geographic coordinate.
func (g LatLng) Cell(resolution int) Cell {
return LatLngToCell(g, resolution)
}
// CellToLatLng returns the geographic centerpoint of a Cell.
func CellToLatLng(c Cell) LatLng {
var g C.LatLng
C.cellToLatLng(C.H3Index(c), &g)
return latLngFromC(g)
}
// LatLng returns the Cell at resolution for a geographic coordinate.
func (c Cell) LatLng() LatLng {
return CellToLatLng(c)
}
// CellToBoundary returns a CellBoundary of the Cell.
func CellToBoundary(c Cell) CellBoundary {
var cb C.CellBoundary
C.cellToBoundary(C.H3Index(c), &cb)
return cellBndryFromC(&cb)
}
// Boundary returns a CellBoundary of the Cell.
func (c Cell) Boundary() CellBoundary {
return CellToBoundary(c)
}
// GridDisk produces cells within grid distance k of the origin cell.
//
// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
// all neighboring cells, and so on.
//
// Output is placed in an array in no particular order. Elements of the output
// array may be left zero, as can happen when crossing a pentagon.
func GridDisk(origin Cell, k int) []Cell {
out := make([]C.H3Index, maxGridDiskSize(k))
C.gridDisk(C.H3Index(origin), C.int(k), &out[0])
// QUESTION: should we prune zeroes from the output?
return cellsFromC(out, true, false)
}
// GridDisk produces cells within grid distance k of the origin cell.
//
// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
// all neighboring cells, and so on.
//
// Output is placed in an array in no particular order. Elements of the output
// array may be left zero, as can happen when crossing a pentagon.
func (c Cell) GridDisk(k int) []Cell {
return GridDisk(c, k)
}
// GridDiskDistances produces cells within grid distance k of the origin cell.
//
// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
// all neighboring cells, and so on.
//
// Outer slice is ordered from origin outwards. Inner slices are in no
// particular order. Elements of the output array may be left zero, as can
// happen when crossing a pentagon.
func GridDiskDistances(origin Cell, k int) [][]Cell {
rsz := maxGridDiskSize(k)
outHexes := make([]C.H3Index, rsz)
outDists := make([]C.int, rsz)
C.gridDiskDistances(C.H3Index(origin), C.int(k), &outHexes[0], &outDists[0])
ret := make([][]Cell, k+1)
for i := 0; i <= k; i++ {
ret[i] = make([]Cell, 0, ringSize(i))
}
for i, d := range outDists {
ret[d] = append(ret[d], Cell(outHexes[i]))
}
return ret
}
// GridDiskDistances produces cells within grid distance k of the origin cell.
//
// k-ring 0 is defined as the origin cell, k-ring 1 is defined as k-ring 0 and
// all neighboring cells, and so on.
//
// Outer slice is ordered from origin outwards. Inner slices are in no
// particular order. Elements of the output array may be left zero, as can
// happen when crossing a pentagon.
func (c Cell) GridDiskDistances(k int) [][]Cell {
return GridDiskDistances(c, k)
}
// MaxPolygonToCellsSize returns the number of hexagons to allocate space for
// when computing polygonToCells on the given GeoJSON-like data structure.
func MaxPolygonToCellsSize(polygon GeoPolygon, resolution int) int64 {
if len(polygon.GeoLoop) == 0 {
return 0
}
cpoly := allocCGeoPolygon(polygon)
defer freeCGeoPolygon(&cpoly)
maxLen := new(C.int64_t)
C.maxPolygonToCellsSize(&cpoly, C.int(resolution), 0, maxLen)
return int64(*maxLen)
}
// PolygonToCells takes a given GeoJSON-like data structure fills it with the
// hexagon cells that are contained by the GeoJSON-like data structure.
//
// This implementation traces the GeoJSON geoloop(s) in cartesian space with
// hexagons, tests them and their neighbors to be contained by the geoloop(s),
// and then any newly found hexagons are used to test again until no new
// hexagons are found.
func PolygonToCells(polygon GeoPolygon, resolution int) []Cell {
if len(polygon.GeoLoop) == 0 {
return nil
}
cpoly := allocCGeoPolygon(polygon)
defer freeCGeoPolygon(&cpoly)
maxLen := new(C.int64_t)
C.maxPolygonToCellsSize(&cpoly, C.int(resolution), 0, maxLen)
out := make([]C.H3Index, *maxLen)
C.polygonToCells(&cpoly, C.int(resolution), 0, &out[0])
return cellsFromC(out, true, false)
}
// PolygonToCells takes a given GeoJSON-like data structure fills it with the
// hexagon cells that are contained by the GeoJSON-like data structure.
//
// This implementation traces the GeoJSON geoloop(s) in cartesian space with
// hexagons, tests them and their neighbors to be contained by the geoloop(s),
// and then any newly found hexagons are used to test again until no new
// hexagons are found.
func (p GeoPolygon) Cells(resolution int) []Cell {
return PolygonToCells(p, resolution)
}
func CellsToMultiPolygon(cells []Cell) *LinkedGeoPolygon {
panic("not implemented")
}
// PointDistRads returns the "great circle" or "haversine" distance between
// pairs of LatLng points (lat/lng pairs) in radians.
func GreatCircleDistanceRads(a, b LatLng) float64 {
return float64(C.greatCircleDistanceRads(a.toCPtr(), b.toCPtr()))
}
// PointDistKm returns the "great circle" or "haversine" distance between pairs
// of LatLng points (lat/lng pairs) in kilometers.
func GreatCircleDistanceKm(a, b LatLng) float64 {
return float64(C.greatCircleDistanceKm(a.toCPtr(), b.toCPtr()))
}
// PointDistM returns the "great circle" or "haversine" distance between pairs
// of LatLng points (lat/lng pairs) in meters.
func GreatCircleDistanceM(a, b LatLng) float64 {
return float64(C.greatCircleDistanceM(a.toCPtr(), b.toCPtr()))
}
// HexAreaKm2 returns the average hexagon area in square kilometers at the given
// resolution.
func HexagonAreaAvgKm2(resolution int) float64 {
var out C.double
C.getHexagonAreaAvgKm2(C.int(resolution), &out)
return float64(out)
}
// HexAreaM2 returns the average hexagon area in square meters at the given
// resolution.
func HexagonAreaAvgM2(resolution int) float64 {
var out C.double
C.getHexagonAreaAvgM2(C.int(resolution), &out)
return float64(out)
}
// CellAreaRads2 returns the exact area of specific cell in square radians.
func CellAreaRads2(c Cell) float64 {
var out C.double
C.cellAreaRads2(C.H3Index(c), &out)
return float64(out)
}
// CellAreaKm2 returns the exact area of specific cell in square kilometers.
func CellAreaKm2(c Cell) float64 {
var out C.double
C.cellAreaKm2(C.H3Index(c), &out)
return float64(out)
}
// CellAreaM2 returns the exact area of specific cell in square meters.
func CellAreaM2(c Cell) float64 {
var out C.double
C.cellAreaM2(C.H3Index(c), &out)
return float64(out)
}
// HexagonEdgeLengthAvgKm returns the average hexagon edge length in kilometers
// at the given resolution.
func HexagonEdgeLengthAvgKm(resolution int) float64 {
var out C.double
C.getHexagonEdgeLengthAvgKm(C.int(resolution), &out)
return float64(out)
}
// HexagonEdgeLengthAvgM returns the average hexagon edge length in meters at
// the given resolution.
func HexagonEdgeLengthAvgM(resolution int) float64 {
var out C.double
C.getHexagonEdgeLengthAvgM(C.int(resolution), &out)
return float64(out)
}
// EdgeLengthRads returns the exact edge length of specific unidirectional edge
// in radians.
func EdgeLengthRads(e DirectedEdge) float64 {
var out C.double
C.edgeLengthRads(C.H3Index(e), &out)
return float64(out)
}
// EdgeLengthKm returns the exact edge length of specific unidirectional
// edge in kilometers.
func EdgeLengthKm(e DirectedEdge) float64 {
var out C.double
C.edgeLengthKm(C.H3Index(e), &out)
return float64(out)
}
// EdgeLengthM returns the exact edge length of specific unidirectional
// edge in meters.
func EdgeLengthM(e DirectedEdge) float64 {
var out C.double
C.edgeLengthM(C.H3Index(e), &out)
return float64(out)
}
// NumCells returns the number of cells at the given resolution.
func NumCells(resolution int) int {
// NOTE: this is a mathematical operation, no need to call into H3 library.
// See h3api.h for formula derivation.
return 2 + 120*intPow(7, (resolution)) //nolint:gomnd // math formula
}
// Res0Cells returns all the cells at resolution 0.
func Res0Cells() []Cell {
out := make([]C.H3Index, C.res0CellCount())
C.getRes0Cells(&out[0])
return cellsFromC(out, false, false)
}
// Pentagons returns all the pentagons at resolution.
func Pentagons(resolution int) []Cell {
out := make([]C.H3Index, NumPentagons)
C.getPentagons(C.int(resolution), &out[0])
return cellsFromC(out, false, false)
}
func (c Cell) Resolution() int {
return int(C.getResolution(C.H3Index(c)))
}
func (e DirectedEdge) Resolution() int {
return int(C.getResolution(C.H3Index(e)))
}
// BaseCellNumber returns the integer ID (0-121) of the base cell the H3Index h
// belongs to.
func BaseCellNumber(h Cell) int {
return int(C.getBaseCellNumber(C.H3Index(h)))
}
// BaseCellNumber returns the integer ID (0-121) of the base cell the H3Index h
// belongs to.
func (c Cell) BaseCellNumber() int {
return BaseCellNumber(c)
}
// IndexFromString returns a Cell from a string. Should call c.IsValid() to check
// if the Cell is valid before using it.
func IndexFromString(s string) uint64 {
if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
s = s[2:]
}
c, _ := strconv.ParseUint(s, base16, bitSize)
return c
}
// IndexToString returns a Cell from a string. Should call c.IsValid() to check
// if the Cell is valid before using it.
func IndexToString(i uint64) string {
return strconv.FormatUint(i, base16)
}
// String returns the string representation of the H3Index h.
func (c Cell) String() string {
return IndexToString(uint64(c))
}
// MarshalText implements the encoding.TextMarshaler interface.
func (c Cell) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (c *Cell) UnmarshalText(text []byte) error {
*c = Cell(IndexFromString(string(text)))
if !c.IsValid() {
return errors.New("invalid cell index")
}
return nil
}
// IsValid returns if a Cell is a valid cell (hexagon or pentagon).
func (c Cell) IsValid() bool {
return c != 0 && C.isValidCell(C.H3Index(c)) == 1
}
// Parent returns the parent or grandparent Cell of this Cell.
func (c Cell) Parent(resolution int) Cell {
var out C.H3Index
C.cellToParent(C.H3Index(c), C.int(resolution), &out)
return Cell(out)
}
// Parent returns the parent or grandparent Cell of this Cell.
func (c Cell) ImmediateParent() Cell {
return c.Parent(c.Resolution() - 1)
}
// Children returns the children or grandchildren cells of this Cell.
func (c Cell) Children(resolution int) []Cell {
var outsz C.int64_t
C.cellToChildrenSize(C.H3Index(c), C.int(resolution), &outsz)
out := make([]C.H3Index, outsz)
C.cellToChildren(C.H3Index(c), C.int(resolution), &out[0])
return cellsFromC(out, false, false)
}
// ImmediateChildren returns the children or grandchildren cells of this Cell.
func (c Cell) ImmediateChildren() []Cell {
return c.Children(c.Resolution() + 1)
}
// CenterChild returns the center child Cell of this Cell.
func (c Cell) CenterChild(resolution int) Cell {
var out C.H3Index
C.cellToCenterChild(C.H3Index(c), C.int(resolution), &out)
return Cell(out)
}
// IsResClassIII returns true if this is a class III index. If false, this is a
// class II index.
func (c Cell) IsResClassIII() bool {
return C.isResClassIII(C.H3Index(c)) == 1
}
// IsPentagon returns true if this is a pentagon.
func (c Cell) IsPentagon() bool {
return C.isPentagon(C.H3Index(c)) == 1
}
// IcosahedronFaces finds all icosahedron faces (0-19) intersected by this Cell.
func (c Cell) IcosahedronFaces() []int {
var outsz C.int
C.maxFaceCount(C.H3Index(c), &outsz)
out := make([]C.int, outsz)
C.getIcosahedronFaces(C.H3Index(c), &out[0])
return intsFromC(out)
}
// IsNeighbor returns true if this Cell is a neighbor of the other Cell.
func (c Cell) IsNeighbor(other Cell) bool {
var out C.int
C.areNeighborCells(C.H3Index(c), C.H3Index(other), &out)
return out == 1
}
// DirectedEdge returns a DirectedEdge from this Cell to other.
func (c Cell) DirectedEdge(other Cell) DirectedEdge {
var out C.H3Index
C.cellsToDirectedEdge(C.H3Index(c), C.H3Index(other), &out)
return DirectedEdge(out)
}
// DirectedEdges returns 6 directed edges with h as the origin.
func (c Cell) DirectedEdges() []DirectedEdge {
out := make([]C.H3Index, numCellEdges) // always 6 directed edges
C.originToDirectedEdges(C.H3Index(c), &out[0])
return edgesFromC(out)
}
func (e DirectedEdge) IsValid() bool {
return C.isValidDirectedEdge(C.H3Index(e)) == 1
}
// Origin returns the origin cell of this directed edge.
func (e DirectedEdge) Origin() Cell {
var out C.H3Index
C.getDirectedEdgeOrigin(C.H3Index(e), &out)
return Cell(out)
}
// Destination returns the destination cell of this directed edge.
func (e DirectedEdge) Destination() Cell {
var out C.H3Index
C.getDirectedEdgeDestination(C.H3Index(e), &out)
return Cell(out)
}
// Cells returns the origin and destination cells in that order.
func (e DirectedEdge) Cells() []Cell {
out := make([]C.H3Index, numEdgeCells)
C.directedEdgeToCells(C.H3Index(e), &out[0])
return cellsFromC(out, false, false)
}
// Boundary provides the coordinates of the boundary of the directed edge. Note,
// the type returned is CellBoundary, but the coordinates will be from the
// center of the origin to the center of the destination. There may be more than
// 2 coordinates to account for crossing faces.
func (e DirectedEdge) Boundary() CellBoundary {
var out C.CellBoundary
C.directedEdgeToBoundary(C.H3Index(e), &out)
return cellBndryFromC(&out)
}
// CompactCells merges full sets of children into their parent H3Index
// recursively, until no more merges are possible.
func CompactCells(in []Cell) []Cell {
cin := cellsToC(in)
csz := C.int64_t(len(in))
// worst case no compaction so we need a set **at least** as large as the
// input
cout := make([]C.H3Index, csz)
C.compactCells(&cin[0], &cout[0], csz)
return cellsFromC(cout, false, true)
}
// UncompactCells splits every H3Index in in if its resolution is greater
// than resolution recursively. Returns all the H3Indexes at resolution resolution.
func UncompactCells(in []Cell, resolution int) []Cell {
cin := cellsToC(in)
var csz C.int64_t
C.uncompactCellsSize(&cin[0], C.int64_t(len(cin)), C.int(resolution), &csz)
cout := make([]C.H3Index, csz)
C.uncompactCells(
&cin[0], C.int64_t(len(in)),
&cout[0], csz,
C.int(resolution))
return cellsFromC(cout, false, true)
}
// ChildPosToCell returns the child of cell a at a given position within an ordered list of all
// children at the specified resolution.
func ChildPosToCell(position int, a Cell, resolution int) Cell {
var out C.H3Index
C.childPosToCell(C.int64_t(position), C.H3Index(a), C.int(resolution), &out)
return Cell(out)
}
// ChildPosToCell returns the child cell at a given position within an ordered list of all
// children at the specified resolution.
func (c Cell) ChildPosToCell(position int, resolution int) Cell {
return ChildPosToCell(position, c, resolution)
}
// CellToChildPos returns the position of the cell a within an ordered list of all children of the cell's parent
// at the specified resolution.
func CellToChildPos(a Cell, resolution int) int {
var out C.int64_t
C.cellToChildPos(C.H3Index(a), C.int(resolution), &out)
return int(out)
}
// ChildPos returns the position of the cell within an ordered list of all children of the cell's parent
// at the specified resolution.
func (c Cell) ChildPos(resolution int) int {
return CellToChildPos(c, resolution)
}
func GridDistance(a, b Cell) int {
var out C.int64_t
C.gridDistance(C.H3Index(a), C.H3Index(b), &out)
return int(out)
}
func (c Cell) GridDistance(other Cell) int {
return GridDistance(c, other)
}
func GridPath(a, b Cell) []Cell {
var outsz C.int64_t
C.gridPathCellsSize(C.H3Index(a), C.H3Index(b), &outsz)
out := make([]C.H3Index, outsz)
C.gridPathCells(C.H3Index(a), C.H3Index(b), &out[0])
return cellsFromC(out, false, false)
}
func (c Cell) GridPath(other Cell) []Cell {
return GridPath(c, other)
}
func CellToLocalIJ(origin, cell Cell) CoordIJ {
var out C.CoordIJ
C.cellToLocalIj(C.H3Index(origin), C.H3Index(cell), 0, &out)
return CoordIJ{int(out.i), int(out.j)}
}
func LocalIJToCell(origin Cell, ij CoordIJ) Cell {
var out C.H3Index
C.localIjToCell(C.H3Index(origin), ij.toCPtr(), 0, &out)
return Cell(out)
}
func maxGridDiskSize(k int) int {
return 3*k*(k+1) + 1
}
func latLngFromC(cg C.LatLng) LatLng {
g := LatLng{}
g.Lat = RadsToDegs * float64(cg.lat)
g.Lng = RadsToDegs * float64(cg.lng)
return g
}
func cellBndryFromC(cb *C.CellBoundary) CellBoundary {
g := make(CellBoundary, 0, MaxCellBndryVerts)
for i := C.int(0); i < cb.numVerts; i++ {
g = append(g, latLngFromC(cb.verts[i]))
}
return g
}
func ringSize(k int) int {
if k == 0 {
return 1
}
return 6 * k //nolint:gomnd // math formula
}
// Convert slice of LatLngs to an array of C LatLngs (represented in C-style as
// a pointer to the first item in the array). The caller must free the returned
// pointer when finished with it.
func latLngsToC(coords []LatLng) *C.LatLng {
if len(coords) == 0 {
return nil
}
// Use malloc to construct a C-style struct array for the output
cverts := C.malloc(C.size_t(C.sizeof_LatLng * len(coords)))
pv := cverts
for _, gc := range coords {
*((*C.LatLng)(pv)) = *gc.toCPtr()
pv = unsafe.Pointer(uintptr(pv) + C.sizeof_LatLng)
}
return (*C.LatLng)(cverts)
}
// Convert geofences (slices of slices of LatLnginates) to C geofences (represented in C-style as
// a pointer to the first item in the array). The caller must free the returned pointer and any
// pointer on the verts field when finished using it.
func geoLoopsToC(geofences []GeoLoop) *C.GeoLoop {
if len(geofences) == 0 {
return nil
}
// Use malloc to construct a C-style struct array for the output
cgeofences := C.malloc(C.size_t(C.sizeof_GeoLoop * len(geofences)))
pcgeofences := cgeofences
for _, coords := range geofences {
cverts := latLngsToC(coords)
*((*C.GeoLoop)(pcgeofences)) = C.GeoLoop{
verts: cverts,
numVerts: C.int(len(coords)),
}
pcgeofences = unsafe.Pointer(uintptr(pcgeofences) + C.sizeof_GeoLoop)
}
return (*C.GeoLoop)(cgeofences)
}
// Convert GeoPolygon struct to C equivalent struct.
func allocCGeoPolygon(gp GeoPolygon) C.GeoPolygon {
cverts := latLngsToC(gp.GeoLoop)
choles := geoLoopsToC(gp.Holes)
return C.GeoPolygon{
geoloop: C.GeoLoop{
numVerts: C.int(len(gp.GeoLoop)),
verts: cverts,
},
numHoles: C.int(len(gp.Holes)),
holes: choles,
}
}
// Free pointer values on a C GeoPolygon struct
func freeCGeoPolygon(cgp *C.GeoPolygon) {
C.free(unsafe.Pointer(cgp.geoloop.verts))
cgp.geoloop.verts = nil
ph := unsafe.Pointer(cgp.holes)
for i := C.int(0); i < cgp.numHoles; i++ {
C.free(unsafe.Pointer((*C.GeoLoop)(ph).verts))
(*C.GeoLoop)(ph).verts = nil
ph = unsafe.Pointer(uintptr(ph) + uintptr(C.sizeof_GeoLoop))
}
C.free(unsafe.Pointer(cgp.holes))
cgp.holes = nil
}
// https://stackoverflow.com/questions/64108933/how-to-use-math-pow-with-integers-in-golang
func intPow(n, m int) int {
if m == 0 {
return 1
}
result := n
for i := 2; i <= m; i++ {
result *= n
}
return result
}
func cellsFromC(chs []C.H3Index, prune, refit bool) []Cell {
// OPT: This could be more efficient if we unsafely cast the C array to a
// []H3Index.
out := make([]Cell, 0, len(chs))
for i := range chs {
if prune && chs[i] <= 0 {
continue
}
out = append(out, Cell(chs[i]))
}
if refit {
// Some algorithms require a maximum sized array, but only use a subset
// of the memory. refit sizes the slice to the last non-empty element.
for i := len(out) - 1; i >= 0; i-- {
if out[i] == 0 {
out = out[:i]
}
}
}
return out
}
func edgesFromC(chs []C.H3Index) []DirectedEdge {
out := make([]DirectedEdge, 0, len(chs))
for i := range chs {
if chs[i] <= 0 {
continue
}
out = append(out, DirectedEdge(chs[i]))
}
return out
}
func cellsToC(chs []Cell) []C.H3Index {
// OPT: This could be more efficient if we unsafely cast the array to a
// []C.H3Index.
out := make([]C.H3Index, len(chs))
for i := range chs {
out[i] = C.H3Index(chs[i])
}
return out
}
func intsFromC(chs []C.int) []int {
out := make([]int, 0, len(chs))
for i := range chs {
// C API returns a sparse array of indexes in the event pentagons and
// deleted sequences are encountered.
if chs[i] != -1 {
out = append(out, int(chs[i]))
}
}
return out
}
func (g LatLng) String() string {
return fmt.Sprintf("(%.5f, %.5f)", g.Lat, g.Lng)
}
func (g LatLng) toCPtr() *C.LatLng {
return &C.LatLng{
lat: C.double(DegsToRads * g.Lat),
lng: C.double(DegsToRads * g.Lng),
}
}
func (ij CoordIJ) toCPtr() *C.CoordIJ {
return &C.CoordIJ{
i: C.int(ij.I),
j: C.int(ij.J),
}
}