/
nearby_things.go
145 lines (121 loc) · 3.38 KB
/
nearby_things.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
package webservices
import (
"net/http"
"strconv"
"strings"
"sync"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/jamesrr39/goutil/errorsx"
"github.com/jamesrr39/goutil/logpkg"
"github.com/jamesrr39/ownmap-app/ownmap"
"github.com/jamesrr39/ownmap-app/ownmapdal"
"github.com/paulmach/osm"
)
type NearbyThingsWebService struct {
logger *logpkg.Logger
dbConnSet *ownmapdal.DBConnSet
chi.Router
}
func NewNearbyThingsWebService(logger *logpkg.Logger, dbConnSet *ownmapdal.DBConnSet) *NearbyThingsWebService {
router := chi.NewRouter()
service := &NearbyThingsWebService{logger, dbConnSet, router}
router.Get("/", service.handleGet)
return service
}
type getNearbyPlacesResponseType struct {
Places []*ownmap.OSMNode `json:"places"`
}
func (s *NearbyThingsWebService) handleGet(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
bounds, err := parseBoundsString(r.URL.Query().Get("bounds"))
if err != nil {
errorsx.HTTPError(w, s.logger, errorsx.Wrap(err), http.StatusBadRequest)
return
}
// desiredTagKey := ownmapdal.TagKeyWithType{
// TagKey: ownmap.TagKey("place"),
// ObjectType: ownmap.ObjectTypeNode,
// }
desiredTagKey := &ownmapdal.TagKeyWithType{
TagKey: ownmap.TagKey("natural"),
ObjectType: ownmap.ObjectTypeWay,
}
conns, err := s.dbConnSet.GetConnsForBounds(*bounds)
if err != nil {
errorsx.HTTPError(w, s.logger, errorsx.Wrap(err), http.StatusInternalServerError)
return
}
if len(conns) == 0 {
render.JSON(w, r, getNearbyPlacesResponseType{})
return
}
errChan := make(chan errorsx.Error)
nodeMapChan := make(chan ownmapdal.TagNodeMap)
nodeMap := make(ownmapdal.TagNodeMap)
var wg sync.WaitGroup
go func() {
for {
select {
case errFromChan := <-errChan:
err = errFromChan
case nodeMapFromChan := <-nodeMapChan:
for k, v := range nodeMapFromChan {
nodeMap[k] = append(nodeMap[k], v...)
}
}
wg.Done()
}
}()
for _, conn := range conns {
wg.Add(1)
go func(conn *ownmapdal.ChosenConnForBounds) {
defer wg.Done()
nodeMap, _, _, err := conn.GetInBounds(ctx, *bounds, &ownmapdal.GetInBoundsFilter{
Objects: []*ownmapdal.TagKeyWithType{desiredTagKey},
})
if err != nil {
wg.Add(1)
errChan <- errorsx.Wrap(err)
return
}
wg.Add(1)
nodeMapChan <- nodeMap
}(conn)
}
wg.Wait()
if err != nil {
errorsx.HTTPError(w, s.logger, errorsx.Wrap(err), http.StatusInternalServerError)
return
}
render.JSON(w, r, getNearbyPlacesResponseType{nodeMap[desiredTagKey.TagKey]})
}
// (S,W,N,E)
// (52.533251,-1.394072,52.800548,-0.898208)
func parseBoundsString(boundsString string) (*osm.Bounds, errorsx.Error) {
bounds := &osm.Bounds{}
withoutBrackets := strings.TrimPrefix(strings.TrimSuffix(boundsString, ")"), "(")
fragments := strings.Split(withoutBrackets, ",")
if len(fragments) != 4 {
return nil, errorsx.Errorf("expected 4 bounds, but got %d. A bounds URL parameter should be in the format 'bounds=(S,W,N,E)'", len(fragments))
}
for index, fragment := range fragments {
trimmedFragment := strings.TrimSpace(fragment)
coordinate, err := strconv.ParseFloat(trimmedFragment, 64)
if err != nil {
return nil, errorsx.Wrap(err)
}
switch index {
case 0:
bounds.MinLat = coordinate
case 1:
bounds.MinLon = coordinate
case 2:
bounds.MaxLat = coordinate
case 3:
bounds.MaxLon = coordinate
}
}
return bounds, nil
}