-
Notifications
You must be signed in to change notification settings - Fork 4
/
geocode.go
168 lines (142 loc) · 3.77 KB
/
geocode.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (C) 2014 Constantin Schomburg <me@cschomburg.com>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package location
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
)
const API_URL = "https://nominatim.openstreetmap.org"
type GeoAddress struct {
HouseNumber string `json:"house_number"`
Building string `json:"building"`
Road string `json:"road"`
Residential string `json:"residential"`
Suburb string `json:"suburb"`
Village string `json:"village"`
Town string `json:"town"`
CityDistrict string `json:"city_district"`
City string `json:"city"`
County string `json:"county"`
PostCode string `json:"postcode"`
State string `json:"state"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
Continent string `json:"continent"`
}
type GeoName struct {
Name string `json:"name,omitempty"`
}
type GeoPlace struct {
BoundingBox BoundingBoxSlice `json:"boundingbox,[]string"`
Latitude float64 `json:"lat,string"`
Longitude float64 `json:"lon,string"`
Name string `json:"display_name"`
Class string `json:"class"`
Type string `json:"type"`
Address GeoAddress `json:"address"`
NameDetails GeoName `json:"namedetails,omitempty"`
}
func (p GeoPlace) Pretty() string {
address := ""
if p.NameDetails.Name != "" {
address += p.Name
}
if p.Address.Road != "" {
if address != "" {
address += ", "
}
address += p.Address.Road
if p.Address.HouseNumber != "" {
address += " " + p.Address.HouseNumber
}
}
if address != "" {
address += ", "
}
if p.Address.PostCode != "" {
address += p.Address.PostCode + " "
}
if p.Address.Village != "" {
address += p.Address.Village
} else if p.Address.Town != "" {
address += p.Address.Town
} else if p.Address.City != "" {
address += p.Address.City
}
if address == "" {
address = p.Name
}
return address
}
func Geocode(query string) ([]GeoPlace, error) {
client := &http.Client{}
u, err := url.Parse(API_URL + "/search/" + query)
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("format", "json")
v.Set("addressdetails", "1")
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
req.Header.Set("User-Agent", "github.com/sarifsystems/sarif")
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("geocode: unexpected status " + resp.Status)
}
results := make([]GeoPlace, 0)
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&results)
return results, err
}
type reverseResponse struct {
GeoPlace
Error string
}
func ReverseGeocode(loc Location) (GeoPlace, error) {
var r reverseResponse
client := &http.Client{}
u, err := url.Parse(API_URL + "/reverse")
if err != nil {
return r.GeoPlace, err
}
v := url.Values{}
v.Set("format", "json")
v.Set("addressdetails", "1")
v.Set("namedetails", "1")
v.Set("zoom", "18")
v.Set("lat", strconv.FormatFloat(loc.Latitude, 'f', -1, 64))
v.Set("lon", strconv.FormatFloat(loc.Longitude, 'f', -1, 64))
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
req.Header.Set("User-Agent", "github.com/sarifsystems/sarif")
if err != nil {
return r.GeoPlace, err
}
resp, err := client.Do(req)
if err != nil {
return r.GeoPlace, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return r.GeoPlace, errors.New("geocode: unexpected status " + resp.Status)
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&r)
if r.Error != "" {
err = errors.New("geocode: " + r.Error)
}
return r.GeoPlace, err
}