Skip to content
Open
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
34 changes: 20 additions & 14 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ import (
"strconv"
"strings"
"unsafe"

"github.com/uber/h3-go/v4/internal/h3core"
)

const (
Expand All @@ -49,7 +47,7 @@ const (
MaxCellBndryVerts = C.MAX_CELL_BNDRY_VERTS

// MaxResolution is the maximum H3 resolution a LatLng can be indexed to.
MaxResolution = h3core.MaxResolution
MaxResolution = C.MAX_H3_RES

// NumIcosaFaces is the number of faces on an icosahedron.
NumIcosaFaces = C.NUM_ICOSA_FACES
Expand Down Expand Up @@ -85,18 +83,18 @@ const (
// to avoid re-allocation.
latLngStringSize = 32

cellMode = h3core.CellMode
directedEdgeMode = h3core.DirectedEdgeMode
vertexMode = h3core.VertexMode
modeOffset = h3core.ModeOffset
cellMode = 1 // H3_CELL_MODE
directedEdgeMode = 2 // H3_DIRECTEDEDGE_MODE
vertexMode = 4 // H3_VERTEX_MODE
modeOffset = 59 // H3_MODE_OFFSET
reservedOffset = 56 // H3_RESERVED_OFFSET
resolutionOffset = h3core.ResolutionOffset
baseCellOffset = h3core.BaseCellOffset
perDigitOffset = h3core.PerDigitOffset
digitMask = h3core.DigitMask
resolutionOffset = 52 // H3_RES_OFFSET
baseCellOffset = 45 // H3_BC_OFFSET
perDigitOffset = 3 // H3_PER_DIGIT_OFFSET
digitMask = 7 // H3_DIGIT_MASK
earthRadiusKm = C.EARTH_RADIUS_KM

resolutionMask = h3core.ResolutionMask
resolutionMask = 0xF // low 4 bits of the resolution field
baseCellMask = 0x7F // 7 bits
modeMask = 0xF // 4 bits

Expand Down Expand Up @@ -131,6 +129,14 @@ var (
}
// compile-time check: pow7 must have exactly MaxResolution+1 entries.
_ = pow7[MaxResolution]

// isBaseCellPentagon maps base cell number to whether it is a pentagon.
// There are exactly 12 pentagons at every resolution, one for each vertex
// of the icosahedron.
isBaseCellPentagon = [128]bool{
4: true, 14: true, 24: true, 38: true, 49: true, 58: true,
63: true, 72: true, 83: true, 97: true, 107: true, 117: true,
}
)

// PolygonToCells containment modes
Expand Down Expand Up @@ -1590,7 +1596,7 @@ func hasAll7AfterRes(h uint64, res int) bool {
}

func hasDeletedSubsequence(h uint64, baseCell int) bool {
if !h3core.IsBaseCellPentagon[baseCell] {
if !isBaseCellPentagon[baseCell] {
return false
}
h <<= digitRegionOffset
Expand All @@ -1606,7 +1612,7 @@ func firstOneIndex(h uint64) int {
}

func isPentagonCell(c Cell) bool {
if !h3core.IsBaseCellPentagon[baseCellNumber(c)] {
if !isBaseCellPentagon[baseCellNumber(c)] {
return false
}
for r := 1; r <= resolution(c); r++ {
Expand Down
61 changes: 0 additions & 61 deletions internal/h3core/h3core.go

This file was deleted.

36 changes: 18 additions & 18 deletions x/h3go/bbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,19 @@ package h3go

import "math"

// bbox is a geographic bounding box with degree coordinates, matching the units
// of the public GeoPolygon. east < west indicates a box crossing the
// antimeridian.
type bbox struct {
north, south, east, west float64
}

// Longitude span constants in degrees, the units of the public API. They stand
// in for the H3 C library's radian M_PI / M_2PI when detecting and normalizing
// antimeridian-crossing geometry.
const (
piDeg = 180.0
twoPiDeg = 360.0
halfPiDeg = 90.0
)

// twoPiRad is a full longitude turn in radians, used to normalize
// antimeridian-crossing geometry where the math must run at the radian scale of
// the H3 C library.
const twoPiRad = 2 * math.Pi
// twoPiRad is a full longitude turn in radians, used to normalize
// antimeridian-crossing geometry where the math must run at the radian scale
// of the H3 C library.
twoPiRad = 2 * math.Pi
)

// longitudeNormalization selects how a longitude is shifted so two bounding
// boxes, either of which may cross the antimeridian, can be compared in one
Expand All @@ -50,6 +43,13 @@ const (
normalizeWest
)

// bbox is a geographic bounding box with degree coordinates, matching the units
// of the public GeoPolygon. east < west indicates a box crossing the
// antimeridian.
type bbox struct {
north, south, east, west float64
}

// isTransmeridian reports whether the bounding box crosses the antimeridian.
func (b bbox) isTransmeridian() bool {
return b.east < b.west
Expand All @@ -68,10 +68,10 @@ func (b bbox) contains(point LatLng) bool {
return point.Lng >= b.west && point.Lng <= b.east
}

// bboxFromGeoLoop computes the bounding box of a loop of coordinates. It does not
// toBbox computes the bounding box of a loop of coordinates. It does not
// support loops with adjacent points more than 180° of longitude apart (treated
// as antimeridian crossings) or loops containing a pole.
func bboxFromGeoLoop(loop GeoLoop) bbox {
func (loop GeoLoop) toBbox() bbox {
if len(loop) == 0 {
return bbox{}
}
Expand Down Expand Up @@ -111,14 +111,14 @@ func bboxFromGeoLoop(loop GeoLoop) bbox {
return out
}

// bboxesFromGeoPolygon returns the bounding box for the outer loop followed by
// toBboxes returns the bounding box for the outer loop followed by
// one for each hole, in order.
func bboxesFromGeoPolygon(polygon GeoPolygon) []bbox {
func (polygon GeoPolygon) toBboxes() []bbox {
bboxes := make([]bbox, len(polygon.Holes)+1)
bboxes[0] = bboxFromGeoLoop(polygon.GeoLoop)
bboxes[0] = polygon.GeoLoop.toBbox()

for i := range polygon.Holes {
bboxes[i+1] = bboxFromGeoLoop(polygon.Holes[i])
bboxes[i+1] = polygon.Holes[i].toBbox()
}

return bboxes
Expand Down
7 changes: 7 additions & 0 deletions x/h3go/boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package h3go

type (
// CellBoundary is the ordered set of geographic vertices that outline a cell.
// It never has more vertices than a cell has topological vertices plus its
// distortion vertices.
CellBoundary []LatLng
)

// CellToBoundary returns the geographic boundary of a cell as an ordered list of
// vertices in degrees.
func CellToBoundary(c Cell) (CellBoundary, error) {
Expand Down
Loading
Loading