geo

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2021 License: AGPL-3.0-or-later Imports: 11 Imported by: 0

README

Frontware

Introduction

Golang library to handle common geo functions in Frontware applications.

Calculate distance in km between 2 coordinates, get address based on coordinates, get location based on IP, ...

Some functions use online API requiring API key.

Frontware Go Report Card

Go Playground

Try it on Go playground

Doc

See the doc online

PkgGoDev


© 2020 Frontware International. All Rights Reserved.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Distance

func Distance(lat1, lon1, lat2, lon2 float64) float64

Distance function returns the distance (in meters) between two points of a given longitude and latitude relatively accurately (using a spherical approximation of the Earth) through the Haversin Distance Formula for great arc distance on a sphere with accuracy for small distances point coordinates are supplied in degrees and converted into rad. in the func distance returned is METERS!!!!!! http://en.wikipedia.org/wiki/Haversine_formula

Here we get the distance
geo.Distance(13.76,100.50, 13.89, 101.12)

func GeoLocate

func GeoLocate(address Address) (lat, long float64)

GeoLocate returns coordinates based on address

GeoLocate(geo.Address{City:"Bangkok","Road":"Latprao 94, Town in Town",PostCode:10310})

func SetGoogleAPI

func SetGoogleAPI(key string)

SetGoogleAPI set Google API key

func SetIPStackAPI

func SetIPStackAPI(key string)

SetIPStackAPI set the API key for IPSTACK. Get the key here https://ipstack.com/quickstart

func SetRapidAPI

func SetRapidAPI(key string)

Set Rapid API key

Types

type Address

type Address struct {
	Country  string `json:"country_code"`
	Road     string `json:"road"`
	City     string `json:"city"`
	Postcode string `json:"postcode"`
	Region   string `json:"state"`
}

Address use to query Mapstreet for reverse geo location

func (*Address) MarshalJSON

func (j *Address) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Address) MarshalJSONBuf

func (j *Address) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Address) UnmarshalJSON

func (j *Address) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Address) UnmarshalJSONFFLexer

func (j *Address) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type GoogleOpeningHours

type GoogleOpeningHours struct {
	OpenNow bool `json:"open_now"` // is a boolean value indicating if the place is open at the current time
	// WeekdayText is an array of seven strings representing the formatted opening hours for each day of the week.
	// If a language parameter was specified in the Place Details request,
	// the Places Service will format and localize the opening hours appropriately for that language.
	// The ordering of the elements in this array depends on the language parameter.
	// Some languages start the week on Monday while others start on Sunday.
	WeekdayText []string `json:"weekday_text"`
	//Periods is an array of opening periods covering seven days, starting from Sunday, in chronological order.
	Periods []struct {
		Close struct {
			Day  int    `json:"day"`  // day a number from 0–6, corresponding to the days of the week, starting on Sunday. For example, 2 means Tuesday.
			Time string `json:"time"` // time may contain a time of day in 24-hour hhmm format. Values are in the range 0000–2359. The time will be reported in the place’s time zone.
		} `json:"close"`
		Open struct {
			Day  int    `json:"day"`
			Time string `json:"time"`
		} `json:"open"`
	}
}

GoogleOpeningHours is the structure of Open hours returned by Google API.

func (*GoogleOpeningHours) MarshalJSON

func (j *GoogleOpeningHours) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*GoogleOpeningHours) MarshalJSONBuf

func (j *GoogleOpeningHours) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*GoogleOpeningHours) UnmarshalJSON

func (j *GoogleOpeningHours) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*GoogleOpeningHours) UnmarshalJSONFFLexer

func (j *GoogleOpeningHours) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type GooglePlace

type GooglePlace struct {
	Geometry struct {
		LocationType string `json:"location_type "` // APPROXIMATE, RANGE_INTERPOLATED
		Location     struct {
			Lat float64 `json:"lat"`
			Lng float64 `json:"lng"`
		} `json:"location"`
	} `json:"geometry"`
	Icon         string             `json:"icon"`
	ID           string             `json:"id"`
	Name         string             `json:"name"`
	OpeningHours GoogleOpeningHours `json:"opening_hours"`
	Photos       []struct {
		Height           int64    `json:"height"`
		HTMLAttributions []string `json:"html_attributions"`
		PhotoReference   string   `json:"photo_reference"`
		Width            int64    `json:"width"`
	} `json:"photos"`
	//PlaceID A textual identifier that uniquely identifies a place.
	// To retrieve information about the place, pass this identifier in the placeId field of a Places API request.
	// For more information about place IDs: https://developers.google.com/places/web-service/place-id
	PlaceID string `json:"place_id"`
	//price_level — The price level of the place, on a scale of 0 to 4. The exact amount indicated by a specific value will vary from region to region. Price levels are interpreted as follows:
	//0 — Free
	//1 — Inexpensive
	//2 — Moderate
	//3 — Expensive
	//4 — Very Expensive
	PriceLevel        int         `json:"price_level"`
	Rating            interface{} `json:"rating"`
	Reference         string      `json:"reference"`
	Scope             string      `json:"scope"`
	Types             []string    `json:"types"`
	PartialMatch      bool        `json:"partial_match"`
	Vicinity          string      `json:"vicinity"`
	AddressComponents []struct {
		LongName  string   `json:"long_name"`
		ShortName string   `json:"short_name"`
		Types     []string `json:"types"`
	} `json:"address_components"`
	AdrAddress               string `json:"adr_address"`
	FormattedAddress         string `json:"formatted_address"`
	FormattedPhoneNumber     string `json:"formatted_phone_number"`
	InternationalPhoneNumber string `json:"international_phone_number"`
	//PermanentlyClosed is a boolean flag indicating whether the place has permanently shut down (value true).
	// If the place is not permanently closed, the flag is absent from the response.
	PermanentlyClosed bool       `json:"permanently_closed"`
	Reviews           []gpReview `json:"reviews"`
	URL               string     `json:"url"`
	UserRatingsTotal  int64      `json:"user_ratings_total"`
	UtcOffset         int64      `json:"utc_offset"`
	Website           string     `json:"website,omitempty"`
}

GooglePlace structure returned by Google API to describe a place.

func GeoCode

func GeoCode(address, lg string) (g GooglePlace, err error)

GeoCode gets coordinates based on address from Google Service.

GeoCode("Avenue Louise 24, Bruxelles, Belgium","en")

func (*GooglePlace) MarshalJSON

func (j *GooglePlace) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*GooglePlace) MarshalJSONBuf

func (j *GooglePlace) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*GooglePlace) UnmarshalJSON

func (j *GooglePlace) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*GooglePlace) UnmarshalJSONFFLexer

func (j *GooglePlace) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type IPAPI

type IPAPI struct {
	IP                 string  `json:"ip"`
	Version            string  `json:"version"`
	City               string  `json:"city"`
	Region             string  `json:"region"`
	RegionCode         string  `json:"region_code"`
	CountryCode        string  `json:"country_code"`
	CountryCodeIso3    string  `json:"country_code_iso3"`
	CountryName        string  `json:"country_name"`
	CountryCapital     string  `json:"country_capital"`
	CountryTld         string  `json:"country_tld"`
	ContinentCode      string  `json:"continent_code"`
	InEu               bool    `json:"in_eu"`
	Postal             string  `json:"postal"`
	Latitude           float64 `json:"latitude"`
	Longitude          float64 `json:"longitude"`
	Timezone           string  `json:"timezone"`
	UtcOffset          string  `json:"utc_offset"`
	CountryCallingCode string  `json:"country_calling_code"`
	Currency           string  `json:"currency"`
	CurrencyName       string  `json:"currency_name"`
	Languages          string  `json:"languages"`
	CountryArea        float64 `json:"country_area"`
	CountryPopulation  float64 `json:"country_population"`
	Asn                string  `json:"asn"`
	Org                string  `json:"org"`
}

IPAPI structure of data returned by ipapi Web Service.

func GetLocationFromIP

func GetLocationFromIP(ip string) (ipapi IPAPI, err error)

GetLocationFromIP returns Location information based on IP More info https://ipapi.co/api/?go#introduction

func (*IPAPI) MarshalJSON

func (j *IPAPI) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*IPAPI) MarshalJSONBuf

func (j *IPAPI) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*IPAPI) UnmarshalJSON

func (j *IPAPI) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*IPAPI) UnmarshalJSONFFLexer

func (j *IPAPI) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type IPLocation

type IPLocation struct {
	City          interface{} `json:"city"`
	ContinentCode string      `json:"continent_code"`
	ContinentName string      `json:"continent_name"`
	CountryCode   string      `json:"country_code"`
	CountryName   string      `json:"country_name"`
	IP            string      `json:"ip"`
	Latitude      int64       `json:"latitude"`
	Location      struct {
		CallingCode             string      `json:"calling_code"`
		Capital                 string      `json:"capital"`
		CountryFlag             string      `json:"country_flag"`
		CountryFlagEmoji        string      `json:"country_flag_emoji"`
		CountryFlagEmojiUnicode string      `json:"country_flag_emoji_unicode"`
		GeonameID               interface{} `json:"geoname_id"`
		IsEu                    bool        `json:"is_eu"`
		Languages               []struct {
			Code   string `json:"code"`
			Name   string `json:"name"`
			Native string `json:"native"`
		} `json:"languages"`
	} `json:"location"`
	Longitude  int64       `json:"longitude"`
	RegionCode interface{} `json:"region_code"`
	RegionName interface{} `json:"region_name"`
	Type       string      `json:"type"`
	Zip        interface{} `json:"zip"`
}

IPLocation structure of data returned by LocateIP() . ipstack Web Service.

func LocateIP

func LocateIP(ip string) (loc IPLocation, err error)

LocateIP returns location based on IP address. Serviced by IPStack: https://ipstack.com/documentation You must provide IPStack API key prior to use the function

SetIPStackAPI("MY IP STACK KEY")
fmt.Prinln(LocateIP ("10.8.2.1"))

func (*IPLocation) MarshalJSON

func (j *IPLocation) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*IPLocation) MarshalJSONBuf

func (j *IPLocation) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*IPLocation) UnmarshalJSON

func (j *IPLocation) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*IPLocation) UnmarshalJSONFFLexer

func (j *IPLocation) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Nominatim

type Nominatim struct {
	DisplayName string   `json:"display_name"`
	Address     *Address `json:"address"`
}

Nominatim is address structure returned by nominatim API.

func Reverse

func Reverse(lat, lon float64) (address Nominatim, err error)

Reverse returns location name based on coordinates from openstreetmap API We wait 1 second before start because there is a rate limitation of 1 request per second

Reverse(13.7665269,100.6068431)

func (*Nominatim) MarshalJSON

func (j *Nominatim) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Nominatim) MarshalJSONBuf

func (j *Nominatim) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Nominatim) UnmarshalJSON

func (j *Nominatim) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Nominatim) UnmarshalJSONFFLexer

func (j *Nominatim) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Place

type Place struct {
	Lat         float64 `json:"lat"`
	Long        float64 `json:"lon"`
	PlaceID     string  `json:"place_id"`
	DisplayName string  `json:"display_name"`
	Class       string  `json:"class"`
	Type        string  `json:"type"`
	Importance  float64 `json:"importance"`
	OSMType     string  `json:"osm_type"`
}

Place is the struct of a geo place from nominatim.

func (*Place) MarshalJSON

func (j *Place) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Place) MarshalJSONBuf

func (j *Place) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Place) UnmarshalJSON

func (j *Place) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Place) UnmarshalJSONFFLexer

func (j *Place) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

Jump to

Keyboard shortcuts

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