/
geocoding.go
69 lines (56 loc) · 1.46 KB
/
geocoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"strconv"
"os"
"regexp"
"strings"
"github.com/jasonmoo/geo"
)
// Use the name of the command to figure out if we want to do a straight or a
// reverse lookup to the geocoding API. Anything with reverse in its name will
// be doing a reverse lookup.
func isReverseMode() bool {
commandName := os.Args[0]
reverseMatcher := regexp.MustCompile("reverse")
return reverseMatcher.MatchString(commandName)
}
func parseCoordinates() (latitude, longitude float64, ok bool) {
latitude, longitude, ok = 0, 0, false
// Assuming seperated coordinates
if len(os.Args) == 3 {
lat, latOk := strconv.ParseFloat(os.Args[1], 64)
long, longOk := strconv.ParseFloat(os.Args[2], 64)
if nil != latOk || nil != longOk {
return
}
latitude, longitude, ok = lat, long, true
} else if len(os.Args) == 2 {
latitude = 0
longitude = 1
ok = true
}
return
}
func ReverseGeocode(latitude, longitude float64) (*geo.Address, error) {
coordinateString := fmt.Sprintf("%v,%v", latitude, longitude)
return geo.ReverseGeocode(coordinateString)
}
func main() {
var address *geo.Address
var error error
if isReverseMode() {
latitude, longitude, ok := parseCoordinates()
if ok != false {
address, error = ReverseGeocode(latitude, longitude)
}
} else {
query := strings.Join(os.Args[1:], " ")
address, error = geo.Geocode(query)
}
if nil != error {
fmt.Fprintln(os.Stderr, "Did not get any result")
os.Exit(0)
}
fmt.Println(address)
}