pk

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

Unofficial Placekey Go SDK ci Go Reference

go install github.com/ringsaturn/pk

Cli usage:

go install github.com/ringsaturn/pk/cmd/placekey@latest

# ToGeo
placekey ToGeo -pk "@627-wbz-tjv"
40.71237820442784 -74.0056425771711

# FromGeo
placekey FromGeo -lat 40.71237820442784 -long -74.0056425771711
@627-wbz-tjv

References:

Documentation

Overview

Package pk provide part of https://github.com/Placekey/placekey-py features in pure Go. Most of codes translated from Python SDK. See details in https://docs.placekey.io/Placekey_Encoding_Specification_White_Paper.pdf

Placekey has two parts: `what` and `where`.

The `where` part. The core of `where` part is cut 21 bits of 64-bit integer which is a H3 integer, only use 43 bits left. Because the resolution is a const `10`, so each H3 id under the same resolution could remove common info. Then use alphabet represent the 43-bit integer as the `where` part of the whole place key.

The `what` part need use Placekey's API. Check API doc https://docs.placekey.io

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeoToPlacekey

func GeoToPlacekey(lat, long float64) (string, error)
Example
package main

import (
	"fmt"

	"github.com/ringsaturn/pk"
)

func main() {
	k, _ := pk.GeoToPlacekey(39.9289, 116.3883)
	fmt.Println(k)
}
Output:

@6qk-v3d-brk

func PlacekeyDistance

func PlacekeyDistance(pk1 string, pk2 string) (float64, error)
Example
package main

import (
	"fmt"

	"github.com/ringsaturn/pk"
)

func main() {
	dist, _ := pk.PlacekeyDistance("@qjk-m7r-whq", "@hvb-5d7-92k")
	fmt.Printf("%.1f\n", dist/1000)
}
Output:

13597.5

func PlacekeyToGeo

func PlacekeyToGeo(placekey string) (float64, float64, error)

PlacekeyToGeo convert placekey to latitude,longitude

Example
package main

import (
	"fmt"

	"github.com/ringsaturn/pk"
)

func main() {
	lat, long, _ := pk.PlacekeyToGeo("@6qk-v3d-brk")
	fmt.Printf("%.3f %.3f \n", lat, long)
}
Output:

39.929 116.388

func PlacekeyToH3

func PlacekeyToH3(placekey string) (*h3.Cell, error)

PlacekeyToH3 convert placekey to H3 Index

func ValidatePlacekey added in v0.3.0

func ValidatePlacekey(pk string) bool

ValidatePlacekey will use Regex and H3 to validate Placekey's what(if provided) and where part.

Example
package main

import (
	"fmt"

	"github.com/ringsaturn/pk"
)

func main() {
	fmt.Println(pk.ValidatePlacekey("@627-wbz-tjv"))
}
Output:

true

Types

type Client added in v0.2.0

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

func NewClient added in v0.2.0

func NewClient(apiKey string, opts ...ClientOption) (*Client, error)

func (*Client) GetPlacekeyFromAddress added in v0.2.0

func (c *Client) GetPlacekeyFromAddress(
	ctx context.Context,
	streetAddress string,
	city string, region string,
	postalCode string, isoCountryCode string,
	queryID string,
) (*Response, error)

GetPlacekeyFromAddress could get both what&where part of placekey.

func (*Client) GetPlacekeyFromGeo added in v0.2.0

func (c *Client) GetPlacekeyFromGeo(
	ctx context.Context, latitude float64, longitude float64,
	queryID string) (*Response, error)

GetPlacekeyFromGeo almost equal like GeoToPlacekey, no `what` part.

type ClientOption added in v0.5.2

type ClientOption func(*Client)

func WithHTTPClient added in v0.5.2

func WithHTTPClient(httpClient *http.Client) ClientOption

type Response added in v0.2.0

type Response struct {
	Placekey string `json:"placekey"`
	QueryID  string `json:"query_id"` // filed default value is `"0"`
	Message  string `json:"message"`  // message for error response
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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