8000 feat: add GridRing and GridRingUnsafe by justinhwang · Pull Request #82 · uber/h3-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add GridRing and GridRingUnsafe #82

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

Merged
merged 1 commit into from
Jun 4, 2025
Merged
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
< 8000 /option>
Diff view
44 changes: 44 additions & 0 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,50 @@ func (c Cell) GridDiskDistances(k int) ([][]Cell, error) {
return GridDiskDistances(c, k)
}

// GridRing produces the "hollow" ring of cells at exactly grid distance k from the origin cell.
//
// k-ring 0 returns just the origin hexagon.
//
// Elements of the output array may be left zero, as can happen when crossing a pentagon.
func GridRing(origin Cell, k int) ([]Cell, error) {
if cells, err := GridRingUnsafe(origin, k); err == nil {
return cells, nil
}
diskDistances, err := GridDiskDistances(origin, k)
if err != nil {
return nil, err
}
return diskDistances[k], nil
}

// GridRing produces the "hollow" ring of cells at exactly grid distance k from the origin cell.
//
// k-ring 0 returns just the origin hexagon.
//
// Elements of the output array may be left zero, as can happen when crossing a pentagon.
func (c Cell) GridRing(k int) ([]Cell, error) {
return GridRing(c, k)
}

// GridRingUnsafe produces the "hollow" ring of cells at exactly grid distance k from the origin cell.
//
// k-ring 0 returns just the origin hexagon.
func GridRingUnsafe(origin Cell, k int) ([]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
out := make([]C.H3Index, ringSize(k))
errC := C.gridRingUnsafe(C.H3Index(origin), C.int(k), &out[0])
return cellsFromC(out, true, false), toErr(errC)
}

// GridRingUnsafe produces the "hollow" ring of cells at exactly grid distance k from the origin cell.
//
// k-ring 0 returns just the origin hexagon.
func (c Cell) GridRingUnsafe(k int) ([]Cell, error) {
return GridRingUnsafe(c, k)
}

// PolygonToCells takes a given GeoJSON-like data structure fills it with the
// hexagon cells that are contained by the GeoJSON-like data structure.
//
Expand Down
73 changes: 72 additions & 1 deletion h3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,77 @@ func TestGridDiskDistances(t *testing.T) {
})
}

func TestGridRing(t *testing.T) {
t.Parallel()

t.Run("success", func(t *testing.T) {
t.Parallel()

gr, err := validCell.GridRing(1)
assertEqualDisks(t,
validDiskDist3_1[1],
gr,
)
assertNoErr(t, err)
})

t.Run("success/pentagon", func(t *testing.T) {
t.Parallel()

gr, err := GridRing(pentagonCell, 1)
assertEqual(t, 5, len(gr))
assertNoErr(t, err)
})

t.Run("err/invalid_cell", func(t *testing.T) {
t.Parallel()

c := Cell(-1)
_, err := c.GridRing(1)
assertErr(t, err)
assertErrIs(t, err, ErrCellInvalid)
})

t.Run("err/invalid_kring", func(t *testing.T) {
rings, err := GridRing(pentagonCell, -1)
assertErr(t, err)
assertErrIs(t, err, ErrDomain)
assertNil(t, rings)
})
}

func TestGridRingUnsafe(t *testing.T) {
t.Parallel()

t.Run("success", func(t *testing.T) {
t.Parallel()

gr, err := validCell.GridRingUnsafe(1)
assertEqualDisks(t,
validDiskDist3_1[1],
gr,
)
assertNoErr(t, err)
})

t.Run("err/pentagon", func(t *testing.T) {
t.Parallel()

_, err := GridRingUnsafe(pentagonCell, 1)
assertErr(t, err)
assertErrIs(t, err, ErrPentagon)
})

t.Run("err/invalid_cell", func(t *testing.T) {
t.Parallel()

c := Cell(-1)
_, err := c.GridRingUnsafe(1)
assertErr(t, err)
assertErrIs(t, err, ErrCellInvalid)
})
}

func TestIsValid(t *testing.T) {
t.Parallel()
assertTrue(t, validCell.IsValid())
Expand Down Expand Up @@ -1401,7 +1472,7 @@ func flattenDisks(diskDist [][]Cell) []Cell {

flat := make([]Cell, 0, maxGridDiskSize(len(diskDist)-1))
for _, disk := range diskDist {
flat = append(flat, append([]Cell{}, disk...)...)
flat = append(flat, disk...)
}

return flat
Expand Down
Loading
0