timezoneLookup

package module
Version: v0.0.0-...-5804073 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 10 Imported by: 0

README

Timezone lookup library

This is a lookup API for GPS Coordinates to Timezone based on GeoJSON files.

  • Support added for MsgPack encoding for faster query.

Data

Data should be obtained via GeoJSON, I recommend: https://github.com/evansiroky/timezone-boundary-builder

Performance

 go run benchmark.go
Performance BoltDB

With Snappy Compression and data loaded from Boltdb

(Msgpack) Average time per query: ~18.015757ms

(Msgpack) Database file size: 89M

(Json) Average time per query: ~38.356527ms

(Json) Database file size: 70M

Alloc = 4 MiB	TotalAlloc = 232 MiB	Sys = 68 MiB	NumGC = 88
Performance Memory

With Snappy Compression and data loaded from memory

Average time per query: ~87.982µs

Database file size: 65M

Alloc = 550 MiB	TotalAlloc = 995 MiB	Sys = 601 MiB	NumGC = 10

Example

  1. Download data from: https://github.com/evansiroky/timezone-boundary-builder/releases
  2. go get github.com/evanoberholster/timezoneLookup
  3. go build github.com/evanoberholster/timezoneLookup/cmd/timezone.go
  4. timezone -json "jsonfilename" -db=timezone -type=(memory or boltdb)
  5. go run example.go
package main
import (
	"fmt"
	timezone "github.com/evanoberholster/timezoneLookup"
)
var tz timezone.TimezoneInterface

func main() {
	tz, err := timezone.LoadTimezones(timezone.Config{
											DatabaseType:"boltdb", // memory or boltdb
											DatabaseName:"timezone", // Name without suffix
											Snappy: true,
											Encoding: "msgpack", // json or msgpack
										})
	if err != nil {
		fmt.Println(err)
	}

	res, err := tz.Query(timezone.Coord{
			Lat: 5.261417, Lon: -3.925778,})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Query Result: ", res)

	tz.Close()
}

Documentation

Index

Constants

View Source
const (
	WithSnappy = true
	NoSnappy   = false
)

Variables

This section is empty.

Functions

func PrintMemUsage

func PrintMemUsage()

Types

type Config

type Config struct {
	DatabaseName string
	DatabaseType string
	Snappy       bool
	Encoding     string
}

type Coord

type Coord struct {
	Lat float32 `json:"lat"`
	Lon float32 `json:"lon"`
}

type Memory

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

func MemoryStorage

func MemoryStorage(snappy bool, filename string) *Memory

func (*Memory) Close

func (m *Memory) Close()

func (*Memory) CreateTimezones

func (m *Memory) CreateTimezones(jsonFilename string) error

func (*Memory) LoadTimezones

func (m *Memory) LoadTimezones() error

func (*Memory) Query

func (m *Memory) Query(q Coord) (string, error)

type Polygon

type Polygon struct {
	Max    Coord   `json:"max"`
	Min    Coord   `json:"min"`
	Coords []Coord `json:"coords"`
}

type PolygonIndex

type PolygonIndex struct {
	Id   uint64 `json:"-"`
	Tzid string `json:"tzid"`
	Max  Coord  `json:"max"`
	Min  Coord  `json:"min"`
}

type Store

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

func (*Store) Close

func (s *Store) Close()

func (*Store) CreateTimezones

func (s *Store) CreateTimezones(jsonFilename string) error

func (*Store) InsertPolygons

func (s *Store) InsertPolygons(tz Timezone)

func (*Store) LoadTimezones

func (s *Store) LoadTimezones() error

func (*Store) OpenDB

func (s *Store) OpenDB(path string) error

func (*Store) Query

func (s *Store) Query(q Coord) (string, error)

type Timezone

type Timezone struct {
	Tzid     string    `json:"tzid"`
	Polygons []Polygon `json:"polygons"`
}

func TimezonesFromGeoJSON

func TimezonesFromGeoJSON(filename string) ([]Timezone, error)

type TimezoneGeoJSON

type TimezoneGeoJSON struct {
	Type     string `json:"type"`
	Features []struct {
		Type       string `json:"type"`
		Properties struct {
			Tzid string `json:"tzid"`
		} `json:"properties"`
		Geometry struct {
			Item        string        `json:"type"`
			Coordinates []interface{} `json:"coordinates"`
		} `json:"geometry"`
	} `json:"features"`
}

type TimezoneInterface

type TimezoneInterface interface {
	CreateTimezones(jsonFilename string) error
	LoadTimezones() error
	Query(q Coord) (string, error)
	Close()
}

func BoltdbStorage

func BoltdbStorage(snappy bool, filename string, encoding string) TimezoneInterface

func LoadTimezones

func LoadTimezones(config Config) (TimezoneInterface, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL