-
Notifications
You must be signed in to change notification settings - Fork 4
/
nominatim_api.go
162 lines (148 loc) · 4.33 KB
/
nominatim_api.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
package mapquest
import (
"fmt"
"log"
"net/url"
"strings"
)
var _ = log.Print
const (
// NominatimPathPrefix is the default path prefix for the Nominatim API.
NominatimPathPrefix = "/nominatim/v1"
)
// NominatimAPI is a geographic search service that relies solely on the
// data contributed to OpenStreetMap.
// See http://open.mapquestapi.com/nominatim/ for details.
type NominatimAPI struct {
c *Client
}
// Search searches for details given an address.
func (api *NominatimAPI) Search(req *NominatimSearchRequest) (*NominatimSearchResponse, error) {
u, err := api.buildSearchURL(req)
if err != nil {
return nil, err
}
res := new(NominatimSearchResponse)
res.Results = make([]*NominatimSearchResult, 0)
if err := api.c.getJSON(u, &res.Results); err != nil {
return nil, err
}
return res, nil
}
// buildSearchURL returns the complete URL for the request,
// including the key to query the MapQuest API.
func (api *NominatimAPI) buildSearchURL(req *NominatimSearchRequest) (string, error) {
urls := fmt.Sprintf("%s%s/search.php", api.c.BaseURL(), NominatimPathPrefix)
u, err := url.Parse(urls)
if err != nil {
return "", err
}
// Add key and other parameters to the query string
q := u.Query()
q.Set("format", "json")
if req.Query != "" {
q.Set("q", req.Query)
} else {
if req.Street != "" {
q.Set("street", req.Street)
}
if req.City != "" {
q.Set("city", req.City)
}
if req.County != "" {
q.Set("county", req.County)
}
if req.State != "" {
q.Set("state", req.State)
}
if req.Country != "" {
q.Set("country", req.Country)
}
if req.PostalCode != "" {
q.Set("postalcode", req.PostalCode)
}
}
q.Set("addressdetails", "1")
if req.Limit > 0 {
q.Set("limit", fmt.Sprintf("%d", req.Limit))
}
if len(req.CountryCodes) > 0 {
q.Set("countrycodes", strings.Join(req.CountryCodes, ","))
}
if len(req.ViewBox) == 4 {
q.Set("viewbox", fmt.Sprintf("%f,%f,%f,%f", req.ViewBox[0], req.ViewBox[1], req.ViewBox[2], req.ViewBox[3]))
}
if len(req.ExcludePlaceIds) > 0 {
q.Set("exclude_place_ids", strings.Join(req.ExcludePlaceIds, ","))
}
if req.Bounded != nil {
if *req.Bounded {
q.Set("bounded", "1")
} else {
q.Set("bounded", "0")
}
}
// TODO(oe): routewidth
if req.RouteWidth != nil {
q.Set("routewidth", fmt.Sprintf("%f", *req.RouteWidth))
}
if req.OSMType != "" {
q.Set("osm_type", req.OSMType)
}
if req.OSMId != "" {
q.Set("osm_id", req.OSMId)
}
// No key here!
u.RawQuery = q.Encode()
return u.String(), nil
}
type NominatimSearchRequest struct {
Query string
Street string
City string
County string
State string
Country string
PostalCode string
Limit int
CountryCodes []string
ViewBox []float64
ExcludePlaceIds []string
Bounded *bool
RouteWidth *float64
OSMType string
OSMId string
}
type NominatimSearchResponse struct {
Results []*NominatimSearchResult
}
type NominatimSearchResult struct {
Address *struct {
City string `json:"city,omitempty"`
CityDistrict string `json:"city_district,omitempty"`
Continent string `json:"continent,omitempty"`
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code,omitempty"`
County string `json:"county,omitempty"`
Hamlet string `json:"hamlet,omitempty"`
HouseNumber string `json:"house_number,omitempty"`
Pedestrian string `json:"pedestrian,omitempty"`
Neighbourhood string `json:"neighbourhood,omitempty"`
PostCode string `json:"postcode,omitempty"`
Road string `json:"road,omitempty"`
State string `json:"state,omitempty"`
StateDistrict string `json:"state_district,omitempty"`
Suburb string `json:"suburb,omitempty"`
} `json:"address,omitempty"`
//BoundingBox []float64 `json:"boundingbox,omitempty"`
Class string `json:"class,omitempty"`
DisplayName string `json:"display_name,omitempty"`
Importance float64 `json:"importance,omitempty"`
Latitude float64 `json:"lat,string,omitempty"`
Longitude float64 `json:"lon,string,omitempty"`
OSMId string `json:"osm_id,omitempty"`
OSMType string `json:"osm_type,omitempty"`
PlaceId string `json:"place_id,omitempty"`
Type string `json:"type,omitempty"`
License string `json:"licence,omitempty"` // typo in API?
}