Skip to content

Commit

Permalink
Increase coverage of C functions (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
retbrown committed Mar 15, 2021
1 parent b4c4808 commit cd94145
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 1 deletion.
30 changes: 29 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ This project tracks the **major** and **minor** versions set by
[`h3`](github.com/uber/h3), and introduces backwards-compatible updates and/or
fixes via patches with patch version bumps.

## Unreleased

### Added

* Functions to cover full functionality
* `Res0IndexCount`
* `GetRes0Indexes`
* `DistanceBetween`
* `ToCenterChild`
* `MaxFaceCount`
* `GetFaces`
* `PentagonIndexCount`
* `GetPentagonIndexes`
* `HexAreaKm2`
* `HexAreaM2`
* `PointDistRads`
* `PointDistKm`
* `PointDistM`
* `CellAreaRads2`
* `CellAreaKm2`
* `CellAreaM2`
* `EdgeLengthKm`
* `EdgeLengthM`
* `ExactEdgeLengthRads`
* `ExactEdgeLengthKm`
* `ExactEdgeLengthM`
* `NumHexagons`

## 3.7.0

### Added
Expand Down Expand Up @@ -46,4 +74,4 @@ fixes via patches with patch version bumps.

### Added

* everything! first commit.
* everything! first commit.
129 changes: 129 additions & 0 deletions v3/h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,122 @@ func Line(start, end H3Index) []H3Index {
return h3SliceFromC(cout)
}

// HexAreaKm2 returns the average hexagon area in square kilometers at the given resolution.
func HexAreaKm2(resolution int) float64 {
return float64(C.hexAreaKm2(C.int(resolution)))
}

// HexAreaM2 returns the average hexagon area in square meters at the given resolution.
func HexAreaM2(resolution int) float64 {
return float64(C.hexAreaM2(C.int(resolution)))
}

// PointDistRads returns the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lng pairs) in radians.
func PointDistRads(a, b GeoCoord) float64 {
return float64(C.pointDistRads(a.toCPtr(), b.toCPtr()))
}

// PointDistKm returns the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lng pairs) in kilometers.
func PointDistKm(a, b GeoCoord) float64 {
return float64(C.pointDistKm(a.toCPtr(), b.toCPtr()))
}

// PointDistM returns the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lng pairs) in meters.
func PointDistM(a, b GeoCoord) float64 {
return float64(C.pointDistM(a.toCPtr(), b.toCPtr()))
}

// CellAreaRads2 returns the exact area of specific cell in square radians.
func CellAreaRads2(h H3Index) float64 {
return float64(C.cellAreaRads2(h))
}

// CellAreaKm2 returns the exact area of specific cell in square kilometers.
func CellAreaKm2(h H3Index) float64 {
return float64(C.cellAreaKm2(h))
}

// CellAreaM2 returns the exact area of specific cell in square meters.
func CellAreaM2(h H3Index) float64 {
return float64(C.cellAreaM2(h))
}

// EdgeLengthKm returns the average hexagon edge length in kilometers at the given resolution.
func EdgeLengthKm(resolution int) float64 {
return float64(C.edgeLengthKm(C.int(resolution)))
}

// EdgeLengthM returns the average hexagon edge length in meters at the given resolution.
func EdgeLengthM(resolution int) float64 {
return float64(C.edgeLengthM(C.int(resolution)))
}

// ExactEdgeLengthRads returns the exact edge length of specific unidirectional edge in radians.
func ExactEdgeLengthRads(h H3Index) float64 {
return float64(C.exactEdgeLengthRads(h))
}

// ExactEdgeLengthKm returns the exact edge length of specific unidirectional edge in kilometers.
func ExactEdgeLengthKm(h H3Index) float64 {
return float64(C.exactEdgeLengthKm(h))
}

// ExactEdgeLengthM returns the exact edge length of specific unidirectional edge in meters.
func ExactEdgeLengthM(h H3Index) float64 {
return float64(C.exactEdgeLengthM(h))
}

// NumHexagons returns the number of unique H3 indexes at the given resolution.
func NumHexagons(resolution int) int {
return int(C.numHexagons(C.int(resolution)))
}

// Res0IndexCount returns the number of resolution 0 H3 indexes.
func Res0IndexCount() int {
return int(C.res0IndexCount())
}

// GetRes0Indexes returns all the resolution 0 H3 indexes.
func GetRes0Indexes() []H3Index {
out := make([]C.H3Index, Res0IndexCount())
C.getRes0Indexes(&out[0])
return h3SliceFromC(out)
}

// DistanceBetween returns the distance in grid cells between the two indexes
func DistanceBetween(origin, dest H3Index) int {
return int(C.h3Distance(origin, dest))
}

// ToCenterChild returns the center child (finer) index contained by h at resolution.
func ToCenterChild(h H3Index, resolution int) H3Index {
return C.h3ToCenterChild(h, C.int(resolution))
}

// MaxFaceCount returns the maximum number of icosahedron faces the given H3 index may intersect.
func MaxFaceCount(h H3Index) int {
return int(C.maxFaceCount(h))
}

// GetFaces returns all icosahedron faces intersected by a given H3 index
func GetFaces(h H3Index) []int {
out := make([]C.int, MaxFaceCount(h))
C.h3GetFaces(h, &out[0])
return intSliceFromC(out)
}

// PentagonIndexCount returns the number of pentagon H3 indexes per resolution (This is always 12)
func PentagonIndexCount() int {
return int(C.pentagonIndexCount())
}

// GetPentagonIndex returns all the pentagon H3 indexes at the specified resolution.
func GetPentagonIndexes(resolution int) []H3Index {
out := make([]C.H3Index, PentagonIndexCount())
C.getPentagonIndexes(C.int(resolution), &out[0])
return h3SliceFromC(out)
}

func geoCoordFromC(cg C.GeoCoord) GeoCoord {
g := GeoCoord{}
g.Latitude = rad2deg * float64(cg.lat)
Expand Down Expand Up @@ -464,6 +580,19 @@ func h3SliceToC(hs []H3Index) []C.H3Index {
return out
}

func intSliceFromC(chs []C.int) []int {
out := make([]int, 0, len(chs))
for _, ch := range chs {
// C API returns a sparse array of indexes in the event pentagons and
// deleted sequences are encountered.
if ch == -1 {
continue
}
out = append(out, int(ch))
}
return out
}

func ringSize(k int) int {
if k == 0 {
return 1
Expand Down
Loading

0 comments on commit cd94145

Please sign in to comment.