Skip to content

Commit

Permalink
update implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
friend0 committed Dec 21, 2021
1 parent 9c10e90 commit 0e76c65
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 191 deletions.
7 changes: 4 additions & 3 deletions data/geocoder.go
Original file line number Diff line number Diff line change
@@ -1,7 1,8 @@
package data

import (
"github.com/codingsince1985/geo-golang"
"context"
"github.com/altiscope/geo-golang"
)

// AddressToLocation maps address string to location (lat, long)
Expand All @@ -25,7 26,7 @@ func Geocoder(addressToLocation AddressToLocation, LocationToAddress LocationToA
}

// Geocode returns location for address
func (d dataGeocoder) Geocode(address string) (*geo.Location, error) {
func (d dataGeocoder) Geocode(ctx context.Context, address string) (*geo.Location, error) {
addr := geo.Address{
FormattedAddress: address,
}
Expand All @@ -37,7 38,7 @@ func (d dataGeocoder) Geocode(address string) (*geo.Location, error) {
}

// ReverseGeocode returns address for location
func (d dataGeocoder) ReverseGeocode(lat, lng float64) (*geo.Address, error) {
func (d dataGeocoder) ReverseGeocode(ctx context.Context, lat, lng float64) (*geo.Address, error) {
if address, ok := d.LocationToAddress[geo.Location{Lat: lat, Lng: lng}]; ok {
return &address, nil
}
Expand Down
14 changes: 9 additions & 5 deletions data/geocoder_test.go
Original file line number Diff line number Diff line change
@@ -1,11 1,12 @@
package data_test

import (
"context"
"github.com/altiscope/geo-golang"
"github.com/altiscope/geo-golang/data"
"strings"
"testing"

"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/data"
"github.com/stretchr/testify/assert"
)

Expand All @@ -28,20 29,23 @@ var (
)

func TestGeocode(t *testing.T) {
location, err := geocoder.Geocode(addressFixture.FormattedAddress)
ctx := context.TODO()
location, err := geocoder.Geocode(ctx, addressFixture.FormattedAddress)
assert.NoError(t, err)
assert.Equal(t, geo.Location{Lat: -37.814107, Lng: 144.96328}, *location)
}

func TestReverseGeocode(t *testing.T) {
address, err := geocoder.ReverseGeocode(locationFixture.Lat, locationFixture.Lng)
ctx := context.TODO()
address, err := geocoder.ReverseGeocode(ctx, locationFixture.Lat, locationFixture.Lng)
assert.Nil(t, err)
assert.NotNil(t, address)
assert.True(t, strings.Contains(address.FormattedAddress, "Melbourne, Victoria 3000, Australia"))
}

func TestReverseGeocodeWithNoResult(t *testing.T) {
addr, err := geocoder.ReverseGeocode(1, 2)
ctx := context.TODO()
addr, err := geocoder.ReverseGeocode(ctx, 1, 2)
assert.Nil(t, err)
assert.Nil(t, addr)
}
195 changes: 14 additions & 181 deletions examples/geocoder_example.go
Original file line number Diff line number Diff line change
@@ -1,28 1,13 @@
package main

import (
"context"
"fmt"
"github.com/altiscope/geo-golang"
"github.com/altiscope/geo-golang/chained"
"github.com/altiscope/geo-golang/mapbox"
"os"

"github.com/codingsince1985/geo-golang"
"github.com/codingsince1985/geo-golang/arcgis"
"github.com/codingsince1985/geo-golang/baidu"
"github.com/codingsince1985/geo-golang/bing"
"github.com/codingsince1985/geo-golang/chained"
"github.com/codingsince1985/geo-golang/frenchapigouv"
"github.com/codingsince1985/geo-golang/geocod"
"github.com/codingsince1985/geo-golang/google"
"github.com/codingsince1985/geo-golang/here"
"github.com/codingsince1985/geo-golang/locationiq"
"github.com/codingsince1985/geo-golang/mapbox"
"github.com/codingsince1985/geo-golang/mapquest/nominatim"
"github.com/codingsince1985/geo-golang/mapquest/open"
"github.com/codingsince1985/geo-golang/mapzen"
"github.com/codingsince1985/geo-golang/opencage"
"github.com/codingsince1985/geo-golang/openstreetmap"
"github.com/codingsince1985/geo-golang/pickpoint"
"github.com/codingsince1985/geo-golang/tomtom"
"github.com/codingsince1985/geo-golang/yandex"
)

const (
Expand All @@ -40,188 25,35 @@ func main() {

// ExampleGeocoder demonstrates the different geocoding services
func ExampleGeocoder() {
fmt.Println("Google Geocoding API")
try(google.Geocoder(os.Getenv("GOOGLE_API_KEY")))

fmt.Println("Mapquest Nominatim")
try(nominatim.Geocoder(os.Getenv("MAPQUEST_NOMINATIM_KEY")))

fmt.Println("Mapquest Open streetmaps")
try(open.Geocoder(os.Getenv("MAPQUEST_OPEN_KEY")))

fmt.Println("OpenCage Data")
try(opencage.Geocoder(os.Getenv("OPENCAGE_API_KEY")))

fmt.Println("HERE Geocoder API")
try(here.Geocoder(os.Getenv("HERE_APP_ID"), os.Getenv("HERE_APP_CODE"), radius))

fmt.Println("Bing Geocoding API")
try(bing.Geocoder(os.Getenv("BING_API_KEY")))

fmt.Println("Baidu Geocoding API")
try(baidu.Geocoder(os.Getenv("BAIDU_API_KEY"), "en"))

fmt.Println("Mapbox API")
try(mapbox.Geocoder(os.Getenv("MAPBOX_API_KEY")))

fmt.Println("OpenStreetMap")
try(openstreetmap.Geocoder())

fmt.Println("PickPoint")
try(pickpoint.Geocoder(os.Getenv("PICKPOINT_API_KEY")))

fmt.Println("LocationIQ")
try(locationiq.Geocoder(os.Getenv("LOCATIONIQ_API_KEY"), zoom))

fmt.Println("ArcGIS")
try(arcgis.Geocoder(os.Getenv("ARCGIS_TOKEN")))

fmt.Println("geocod.io")
try(geocod.Geocoder(os.Getenv("GEOCOD_API_KEY")))

fmt.Println("Mapzen")
try(mapzen.Geocoder(os.Getenv("MAPZEN_API_KEY")))

fmt.Println("TomTom")
try(tomtom.Geocoder(os.Getenv("TOMTOM_API_KEY")))

fmt.Println("Yandex")
try(yandex.Geocoder(os.Getenv("YANDEX_API_KEY")))

// To use only with french locations or addresses,
// or values ​​could be estimated and will be false
fmt.Println("FrenchAPIGouv")
tryOnlyFRData(frenchapigouv.Geocoder())

// Chained geocoder will fallback to subsequent geocoders
fmt.Println("ChainedAPI[OpenStreetmap -> Google]")
try(chained.Geocoder(
openstreetmap.Geocoder(),
google.Geocoder(os.Getenv("GOOGLE_API_KEY")),
mapbox.Geocoder(os.Getenv("MAPBOX_API_KEY")),
mapbox.Geocoder(os.Getenv("MAPBOX_API_KEY")),
))
// Output: Google Geocoding API
// Melbourne VIC location is (-37.813611, 144.963056)
// Address of (-37.813611,144.963056) is 197 Elizabeth St, Melbourne VIC 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"197 Elizabeth St, Melbourne VIC 3000, Australia",
// Street:"Elizabeth Street", HouseNumber:"197", Suburb:"", Postcode:"3000", State:"Victoria",
// StateDistrict:"Melbourne City", County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// Mapquest Nominatim
// Melbourne VIC location is (-37.814218, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Melbourne, City of Melbourne,
// Greater Melbourne, Victoria, 3000, Australia", Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne",
// Postcode:"3000", State:"Victoria", StateDistrict:"", County:"City of Melbourne", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// Mapquest Open streetmaps
// Melbourne VIC location is (-37.814218, 144.963161)
// Address of (-37.813611,144.963056) is Elizabeth Street, Melbourne, Victoria, AU
// Detailed address: &geo.Address{FormattedAddress:"Elizabeth Street, 3000, Melbourne, Victoria, AU",
// Street:"Elizabeth Street", HouseNumber:"", Suburb:"", Postcode:"3000", State:"Victoria", StateDistrict:"",
// County:"", Country:"", CountryCode:"AU", City:"Melbourne"}
//
// OpenCage Data
// Melbourne VIC location is (-37.814217, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Melbourne VIC 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Melbourne VIC 3000, Australia",
// Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne (3000)", Postcode:"3000", State:"Victoria",
// StateDistrict:"", County:"City of Melbourne", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// HERE Geocoder API
// Melbourne VIC location is (-37.817530, 144.967150)
// Address of (-37.813611,144.963056) is 197 Elizabeth St, Melbourne VIC 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"197 Elizabeth St, Melbourne VIC 3000, Australia", Street:"Elizabeth St",
// HouseNumber:"197", Suburb:"", Postcode:"3000", State:"Victoria", StateDistrict:"", County:"", Country:"Australia",
// CountryCode:"AUS", City:"Melbourne"}
//
// Bing Geocoding API
// Melbourne VIC location is (-37.824299, 144.977997)
// Address of (-37.813611,144.963056) is Elizabeth St, Melbourne, VIC 3000
// Detailed address: &geo.Address{FormattedAddress:"Elizabeth St, Melbourne, VIC 3000", Street:"Elizabeth St",
// HouseNumber:"", Suburb:"", Postcode:"3000", State:"", StateDistrict:"", County:"", Country:"Australia", CountryCode:"", City:"Melbourne"}
//
// Baidu Geocoding API
// Melbourne VIC location is (31.227015, 121.456967)
// Address of (-37.813611,144.963056) is 341 Little Bourke Street, Melbourne, Victoria, Australia
// Detailed address: &geo.Address{FormattedAddress:"341 Little Bourke Street, Melbourne, Victoria, Australia", Street:"Little Bourke Street", HouseNumber:"341", Suburb:"", Postcode:"", State:"Victoria", StateCode:"", StateDistrict:"", County:"", Country:"Australia", CountryCode:"AUS", City:"Melbourne"}
//

// Mapbox API
// Melbourne VIC location is (-37.814200, 144.963200)
// Address of (-37.813611,144.963056) is Elwood Park Playground, Melbourne, Victoria 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Elwood Park Playground, Melbourne, Victoria 3000, Australia",
// Street:"Elwood Park Playground", HouseNumber:"", Suburb:"", Postcode:"3000", State:"Victoria", StateDistrict:"",
// County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// OpenStreetMap
// Melbourne VIC location is (-37.814217, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne,
// Victoria, 3000, Australia", Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne", Postcode:"3000", State:"Victoria",
// StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// PickPoint
// Melbourne VIC location is (-37.814217, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne,
// Victoria, 3000, Australia", Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne", Postcode:"3000", State:"Victoria",
// StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// LocationIQ
// Melbourne VIC location is (-37.814217, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne,
// Victoria, 3000, Australia", Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne", Postcode:"3000", State:"Victoria",
// StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}
//
// ArcGIS
// Melbourne VIC location is (-37.817530, 144.967150)
// Address of (-37.813611,144.963056) is Melbourne's Gpo
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's Gpo", Street:"350 Bourke Street Mall", HouseNumber:"350", Suburb:"", Postcode:"3000", State:"Victoria", StateDistrict:"", County:"", Country:"", CountryCode:"AUS", City:""}
//
// geocod.io
// Melbourne VIC location is (28.079357, -80.623618)
// got <nil> address
//
// Mapzen
// Melbourne VIC location is (45.551136, 11.533929)
// Address of (-37.813611,144.963056) is Stop 3: Bourke Street Mall, Bourke Street, Melbourne, Australia
// Detailed address: &geo.Address{FormattedAddress:"Stop 3: Bourke Street Mall, Bourke Street, Melbourne, Australia", Street:"", HouseNumber:"", Suburb:"", Postcode:"", State:"Victoria", StateDistrict:"", County:"", Country:"Australia", CountryCode:"AUS", City:""}
//
// TomTom
// Melbourne VIC location is (-37.815340, 144.963230)
// Address of (-37.813611,144.963056) is Doyles Road, Elaine, West Central Victoria, Victoria, 3334
// Detailed address: &geo.Address{FormattedAddress:"Doyles Road, Elaine, West Central Victoria, Victoria, 3334", Street:"Doyles Road", HouseNumber:"", Suburb:"", Postcode:"3334", State:"Victoria", StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU", City:"Elaine"}
//
// Yandex
// Melbourne VIC location is (41.926823, 2.254232)
// Address of (-37.813611,144.963056) is Victoria, City of Melbourne, Elizabeth Street
// Detailed address: &geo.Address{FormattedAddress:"Victoria, City of Melbourne, Elizabeth Street", Street:"Elizabeth Street",
// HouseNumber:"", Suburb:"", Postcode:"", State:"Victoria", StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU",
// City:"City of Melbourne"}
//
// FrenchAPIGouv
// Champs de Mars Paris location is (2.304770, 48.854395)
// Address of (48.854395,2.304770) is 9001, Parc du Champs de Mars, 75007, Paris, Paris, Île-de-France, France
// Detailed address: &geo.Address{FormattedAddress:"9001, Parc du Champs de Mars, 75007, Paris, Paris, Île-de-France, France",
// Street:"Parc du Champs de Mars", HouseNumber:"9001", Suburb:"", Postcode:"75007", State:" Île-de-France", StateDistrict:"",
// County:" Paris", Country:"France", CountryCode:"FRA", City:"Paris"}
//
// ChainedAPI[OpenStreetmap -> Google]
// Melbourne VIC location is (-37.814217, 144.963161)
// Address of (-37.813611,144.963056) is Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne, Victoria, 3000, Australia
// Detailed address: &geo.Address{FormattedAddress:"Melbourne's GPO, Postal Lane, Chinatown, Melbourne, City of Melbourne, Greater Melbourne,
// Victoria, 3000, Australia", Street:"Postal Lane", HouseNumber:"", Suburb:"Melbourne", Postcode:"3000", State:"Victoria",
// StateDistrict:"", County:"", Country:"Australia", CountryCode:"AU", City:"Melbourne"}

}

func try(geocoder geo.Geocoder) {
location, _ := geocoder.Geocode(addr)
ctx := context.TODO()
location, _ := geocoder.Geocode(ctx, addr)
if location != nil {
fmt.Printf("%s location is (%.6f, %.6f)\n", addr, location.Lat, location.Lng)
} else {
fmt.Println("got <nil> location")
}
address, _ := geocoder.ReverseGeocode(lat, lng)
address, _ := geocoder.ReverseGeocode(ctx, lat, lng)
if address != nil {
fmt.Printf("Address of (%.6f,%.6f) is %s\n", lat, lng, address.FormattedAddress)
fmt.Printf("Detailed address: %#v\n", address)
Expand All @@ -232,13 64,14 @@ func try(geocoder geo.Geocoder) {
}

func tryOnlyFRData(geocoder geo.Geocoder) {
location, _ := geocoder.Geocode(addrFR)
ctx := context.TODO()
location, _ := geocoder.Geocode(ctx, addrFR)
if location != nil {
fmt.Printf("%s location is (%.6f, %.6f)\n", addrFR, location.Lat, location.Lng)
} else {
fmt.Println("got <nil> location")
}
address, _ := geocoder.ReverseGeocode(latFR, lngFR)
address, _ := geocoder.ReverseGeocode(ctx, latFR, lngFR)
if address != nil {
fmt.Printf("Address of (%.6f,%.6f) is %s\n", latFR, lngFR, address.FormattedAddress)
fmt.Printf("Detailed address: %#v\n", address)
Expand Down
4 changes: 2 additions & 2 deletions http_geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 40,7 @@ type HTTPGeocoder struct {
}

// Geocode returns location for address
func (g HTTPGeocoder) Geocode(address string) (*Location, error) {
func (g HTTPGeocoder) Geocode(ctx context.Context, address string) (*Location, error) {
responseParser := g.ResponseParserFactory()

ctx, cancel := context.WithTimeout(context.TODO(), DefaultTimeout)
Expand Down Expand Up @@ -76,7 76,7 @@ func (g HTTPGeocoder) Geocode(address string) (*Location, error) {
}

// ReverseGeocode returns address for location
func (g HTTPGeocoder) ReverseGeocode(lat, lng float64) (*Address, error) {
func (g HTTPGeocoder) ReverseGeocode(ctx context.Context, lat, lng float64) (*Address, error) {
responseParser := g.ResponseParserFactory()

ctx, cancel := context.WithTimeout(context.TODO(), DefaultTimeout)
Expand Down

0 comments on commit 0e76c65

Please sign in to comment.