h3

package module
v0.0.0-...-f6915f2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2023 License: Apache-2.0 Imports: 5 Imported by: 4

README

goh3

This is an experiment h3 build using ccgo.

It's implementing two features that are not yet present in the current h3-go repo:

func (c Cell) ChildPosToCell(pos, res int) Cell
func (c Cell) ChildPos(res int) int

WIP, use at your own risk

Thread local storage

This lib can achieve speed close to the h3 Go using CGO library by batching, lowering pressure on allocating the thread local storage.

Each function is taking an optional opts ...OptionsFunc, you can pass your existing TLS: WithTLS(tls).

tls = libc.NewTLS()
defer tls.Close()

g := h3.CellToLatLng(validCell, h3.WithTLS(tls))

Code Generation

  CC=/usr/bin/gcc ccgo  -pkgname ch3  -trace-translation-units -export-externs X -export-defines D -export-fields F -export-structs S -export-typedefs T -Isrc/h3lib/include  -I../src/h3lib/include ../src/h3lib/lib/*.c

Patch h3 c sources to build on Linux (not needed on OSX)

Patch the original source to avoid builtin isfinite isssue (C tests are passing).

bool isXfinite(double f) { return !isnan(f - f); }

Replace occurence of isfinite() with isXfinite() eg:

/**
 * Encodes a coordinate on the sphere to the H3 index of the containing cell at
 * the specified resolution.
 *
 * Returns 0 on invalid input.
 *
 * @param g The spherical coordinates to encode.
 * @param res The desired H3 resolution for the encoding.
 * @return The encoded H3Index (or H3_NULL on failure).
 */
H3Index H3_EXPORT(geoToH3)(const GeoCoord* g, int res) {
    if (res < 0 || res > MAX_H3_RES) {
        return H3_NULL;
    }
    if (!isXFinite(g->lat) || !isXFinite(g->lon)) {
        return H3_NULL;
    }

    FaceIJK fijk;
    _geoToFaceIjk(g, res, &fijk);
    return _faceIjkToH3(&fijk, res);
}

Documentation

Index

Examples

Constants

View Source
const (

	// MaxCellBndryVerts is the maximum number of vertices that can be used
	// to represent the shape of a cell.
	MaxCellBndryVerts = ch3.DMAX_CELL_BNDRY_VERTS
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {
	// contains filtered or unexported fields
}

func NewBatch

func NewBatch() *Batch

func (*Batch) CellToLatLng

func (b *Batch) CellToLatLng(c Cell) LatLng

CellToLatLng returns the geographic centerpoint of a Cell.

func (*Batch) Close

func (b *Batch) Close()

func (*Batch) LatLngToCell

func (b *Batch) LatLngToCell(latLng LatLng, resolution int) Cell

type Cell

type Cell uint64

Cell is an Index that identifies a single hexagon cell at a resolution.

func LatLngToCell

func LatLngToCell(latLng LatLng, resolution int, opts ...OptionsFunc) Cell

LatLngToCell returns the Cell at resolution for a geographic coordinate.

Example
package main

import (
	"fmt"

	h3 "github.com/akhenakh/goh3"
)

func main() {
	latLng := h3.NewLatLng(37.775938728915946, -122.41795063018799)
	resolution := 9
	c := h3.LatLngToCell(latLng, resolution)
	fmt.Printf("%s", c)
}
Output:

8928308280fffff

func (Cell) ChildPos

func (c Cell) ChildPos(res int, opts ...OptionsFunc) int

ChildPos returns the position of the cell within an ordered list of all children of the cell's parent at the specified resolution res.

func (Cell) ChildPosToCell

func (c Cell) ChildPosToCell(pos, res int, opts ...OptionsFunc) Cell

ChildPosToCell returns the child cell at a given position within an ordered list of all children at the specified resolution res.

func (Cell) Children

func (c Cell) Children(resolution int, opts ...OptionsFunc) []Cell

Children returns the children or grandchildren cells of this Cell.

func (Cell) DirectedEdge

func (c Cell) DirectedEdge(other Cell, opts ...OptionsFunc) DirectedEdge

DirectedEdge returns a DirectedEdge from this Cell to other.

func (Cell) DirectedEdges

func (c Cell) DirectedEdges(opts ...OptionsFunc) []DirectedEdge

DirectedEdges returns 6 directed edges with h as the origin.

func (Cell) ImmediateChildren

func (c Cell) ImmediateChildren(opts ...OptionsFunc) []Cell

ImmediateChildren returns the children or grandchildren cells of this Cell.

func (Cell) ImmediateParent

func (c Cell) ImmediateParent(opts ...OptionsFunc) Cell

Parent returns the parent or grandparent Cell of this Cell.

func (Cell) IsNeighbor

func (c Cell) IsNeighbor(other Cell, opts ...OptionsFunc) bool

IsNeighbor returns true if this Cell is a neighbor of the other Cell.

func (Cell) IsPentagon

func (c Cell) IsPentagon(opts ...OptionsFunc) bool

IsPentagon returns true if this is a pentagon.

func (Cell) IsValid

func (c Cell) IsValid(opts ...OptionsFunc) bool

IsValid returns if a Cell is a valid cell (hexagon or pentagon).

func (Cell) LatLng

func (c Cell) LatLng(opts ...OptionsFunc) LatLng

LatLng returns the Cell at resolution for a geographic coordinate.

func (Cell) Parent

func (c Cell) Parent(resolution int, opts ...OptionsFunc) Cell

Parent returns the parent or grandparent Cell of this Cell.

func (Cell) Resolution

func (c Cell) Resolution(opts ...OptionsFunc) int

func (Cell) String

func (c Cell) String() string

type CellBoundary

type CellBoundary []LatLng

CellBoundary is a slice of LatLng. Note, len(CellBoundary) will never exceed MaxCellBndryVerts.

type DirectedEdge

type DirectedEdge int64

func (DirectedEdge) Boundary

func (e DirectedEdge) Boundary(opts ...OptionsFunc) CellBoundary

Boundary provides the coordinates of the boundary of the directed edge. Note, the type returned is CellBoundary, but the coordinates will be from the center of the origin to the center of the destination. There may be more than 2 coordinates to account for crossing faces.

func (DirectedEdge) Cells

func (e DirectedEdge) Cells(opts ...OptionsFunc) []Cell

Cells returns the origin and destination cells in that order.

func (DirectedEdge) Destination

func (e DirectedEdge) Destination(opts ...OptionsFunc) Cell

Destination returns the destination cell of this directed edge.

func (DirectedEdge) IsValid

func (e DirectedEdge) IsValid(opts ...OptionsFunc) bool

func (DirectedEdge) Origin

func (e DirectedEdge) Origin(opts ...OptionsFunc) Cell

Origin returns the origin cell of this directed edge.

type GeoLoop

type GeoLoop []LatLng

GeoLoop is a slice of LatLng points that make up a loop.

type H3Index

type H3Index ch3.TH3Index

type LatLng

type LatLng struct {
	Lat, Lng float64
}

LatLng is a struct for geographic coordinates in degrees.

func CellToLatLng

func CellToLatLng(c Cell, opts ...OptionsFunc) LatLng

CellToLatLng returns the geographic centerpoint of a Cell.

func NewLatLng

func NewLatLng(lat, lng float64) LatLng

type Options

type Options struct {
	TLS *libc.TLS
}

type OptionsFunc

type OptionsFunc func(*Options)

func WithTLS

func WithTLS(tls *libc.TLS) OptionsFunc

WithMultipleFences enable multi fences in responses

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL