@@ -25,6 +25,8 @@ package h3
25
25
#include <stdlib.h>
26
26
#include <h3_h3api.h>
27
27
#include <h3_h3Index.h>
28
+ #include <h3_polygon.h>
29
+ #include <h3_polyfill.h>
28
30
*/
29
31
import "C"
30
32
@@ -66,6 +68,13 @@ const (
66
68
67
69
DegsToRads = math .Pi / 180.0
68
70
RadsToDegs = 180.0 / math .Pi
71
+
72
+ // PolygonToCells containment modes
73
+ ContainmentCenter ContainmentMode = C .CONTAINMENT_CENTER // Cell center is contained in the shape
74
+ ContainmentFull ContainmentMode = C .CONTAINMENT_FULL // Cell is fully contained in the shape
75
+ ContainmentOverlapping ContainmentMode = C .CONTAINMENT_OVERLAPPING // Cell overlaps the shape at any point
76
+ ContainmentOverlappingBbox ContainmentMode = C .CONTAINMENT_OVERLAPPING_BBOX // Cell bounding box overlaps shape
77
+ ContainmentInvalid ContainmentMode = C .CONTAINMENT_INVALID // This mode is invalid and should not be used
69
78
)
70
79
71
80
// Error codes.
@@ -137,6 +146,9 @@ type (
137
146
GeoLoop GeoLoop
138
147
Holes []GeoLoop
139
148
}
149
+
150
+ // ContainmentMode is an int for specifying PolygonToCell containment behavior.
151
+ ContainmentMode C.uint32_t
140
152
)
141
153
142
154
func NewLatLng (lat , lng float64 ) LatLng {
@@ -222,6 +234,7 @@ func GridDiskDistances(origin Cell, k int) ([][]Cell, error) {
222
234
rsz := maxGridDiskSize (k )
223
235
outHexes := make ([]C.H3Index , rsz )
224
236
outDists := make ([]C.int , rsz )
237
+
225
238
if err := toErr (C .gridDiskDistances (C .H3Index (origin ), C .int (k ), & outHexes [0 ], & outDists [0 ])); err != nil {
226
239
return nil , err
227
240
}
@@ -276,6 +289,36 @@ func PolygonToCells(polygon GeoPolygon, resolution int) ([]Cell, error) {
276
289
return cellsFromC (out , true , false ), toErr (errC )
277
290
}
278
291
292
+ // PolygonToCells takes a given GeoJSON-like data structure fills it with the
293
+ // hexagon cells that are contained by the GeoJSON-like data structure.
294
+ //
295
+ // This implementation traces the GeoJSON geoloop(s) in cartesian space with
296
+ // hexagons, tests them and their neighbors to be contained by the geoloop(s),
297
+ // and then any newly found hexagons are used to test again until no new
298
+ // hexagons are found.
299
+ func PolygonToCellsExperimental (polygon GeoPolygon , resolution int , mode ContainmentMode , maxNumCellsReturn ... int64 ) ([]Cell , error ) {
300
+ var maxNumCells int64 = math .MaxInt64
301
+ if len (maxNumCellsReturn ) > 0 {
302
+ maxNumCells = maxNumCellsReturn [0 ]
303
+ }
304
+ if len (polygon .GeoLoop ) == 0 {
305
+ return nil , nil
306
+ }
307
+ cpoly := allocCGeoPolygon (polygon )
308
+
309
+ defer freeCGeoPolygon (& cpoly )
310
+
311
+ maxLen := new (C.int64_t )
312
+ if err := toErr (C .maxPolygonToCellsSizeExperimental (& cpoly , C .int (resolution ), C .uint32_t (mode ), maxLen )); err != nil {
313
+ return nil , err
314
+ }
315
+
316
+ out := make ([]C.H3Index , * maxLen )
317
+ errC := C .polygonToCellsExperimental (& cpoly , C .int (resolution ), C .uint32_t (mode ), C .int64_t (maxNumCells ), & out [0 ])
318
+
319
+ return cellsFromC (out , true , false ), toErr (errC )
320
+ }
321
+
279
322
// PolygonToCells takes a given GeoJSON-like data structure fills it with the
280
323
// hexagon cells that are contained by the GeoJSON-like data structure.
281
324
//
0 commit comments