Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing Experimental PolygonToCells and Updating to Master #68

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package h3
#include <stdlib.h>
#include <h3_h3api.h>
#include <h3_h3Index.h>
#include <h3_polygon.h>
#include <h3_polyfill.h>
*/
import "C"

Expand Down Expand Up @@ -65,6 +67,13 @@ const (

DegsToRads = math.Pi / 180.0
RadsToDegs = 180.0 / math.Pi

// PolygonToCells containment modes
CONTAINMENT_CENTER ContainmentMode = C.CONTAINMENT_CENTER // Cell center is contained in the shape
CONTAINMENT_FULL ContainmentMode = C.CONTAINMENT_FULL // Cell is fully contained in the shape
CONTAINMENT_OVERLAPPING ContainmentMode = C.CONTAINMENT_OVERLAPPING // Cell overlaps the shape at any point
CONTAINMENT_OVERLAPPING_BBOX ContainmentMode = C.CONTAINMENT_OVERLAPPING_BBOX // Cell bounding box overlaps shape
CONTAINMENT_INVALID ContainmentMode = C.CONTAINMENT_INVALID // This mode is invalid and should not be used
)

type (
Expand Down Expand Up @@ -100,6 +109,9 @@ type (
// LinkedGeoPolygon is a linked-list of GeoPolygons.
// TODO: not implemented.
LinkedGeoPolygon struct{}

// ContainmentMode is an int for specifying PolygonToCell containment behavior.
ContainmentMode C.uint32_t
)

func NewLatLng(lat, lng float64) LatLng {
Expand Down Expand Up @@ -218,7 +230,11 @@ func (c Cell) GridDiskDistances(k int) [][]Cell {
// 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 {
func PolygonToCells(polygon GeoPolygon, resolution int, mode ...ContainmentMode) []Cell {
var containmentFlag ContainmentMode = CONTAINMENT_CENTER
if len(mode) > 0 {
containmentFlag = mode[0]
}
if len(polygon.GeoLoop) == 0 {
return nil
}
Expand All @@ -227,10 +243,10 @@ func PolygonToCells(polygon GeoPolygon, resolution int) []Cell {
defer freeCGeoPolygon(&cpoly)

maxLen := new(C.int64_t)
C.maxPolygonToCellsSize(&cpoly, C.int(resolution), 0, maxLen)
C.maxPolygonToCellsSize(&cpoly, C.int(resolution), C.uint32_t(containmentFlag), maxLen)

out := make([]C.H3Index, *maxLen)
C.polygonToCells(&cpoly, C.int(resolution), 0, &out[0])
C.polygonToCellsExperimental(&cpoly, C.int(resolution), C.uint32_t(containmentFlag), &out[0])

return cellsFromC(out, true, false)
}
Expand All @@ -242,8 +258,8 @@ func PolygonToCells(polygon GeoPolygon, resolution int) []Cell {
// 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 (p GeoPolygon) Cells(resolution int, mode ...ContainmentMode) []Cell {
return PolygonToCells(p, resolution, mode...)
}

func CellsToMultiPolygon(cells []Cell) *LinkedGeoPolygon {
Expand Down
10 changes: 6 additions & 4 deletions h3_algos.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion h3_baseCells.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions h3_bbox.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions h3_bbox.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading