-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-grpc.go
212 lines (177 loc) · 4.9 KB
/
map-grpc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package goautowp
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"github.com/autowp/goautowp/users"
"github.com/autowp/goautowp/image/storage"
"github.com/autowp/goautowp/items"
"github.com/autowp/goautowp/pictures"
"github.com/doug-martin/goqu/v9"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/wkb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type MapGRPCServer struct {
UnimplementedMapServer
db *goqu.Database
imageStorage *storage.Storage
}
func NewMapGRPCServer(db *goqu.Database, imageStorage *storage.Storage) *MapGRPCServer {
return &MapGRPCServer{
db: db,
imageStorage: imageStorage,
}
}
func (s *MapGRPCServer) GetPoints(ctx context.Context, in *MapGetPointsRequest) (*MapPoints, error) {
const numberOfBounds = 4
bounds := strings.Split(in.GetBounds(), ",")
if len(bounds) < numberOfBounds {
return nil, status.Error(codes.InvalidArgument, "Invalid bounds")
}
const bitSize64 = 64
lngLo, err := strconv.ParseFloat(bounds[0], bitSize64)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
latLo, err := strconv.ParseFloat(bounds[1], bitSize64)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
lngHi, err := strconv.ParseFloat(bounds[2], bitSize64)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
latHi, err := strconv.ParseFloat(bounds[3], bitSize64)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
pointsOnly := in.GetPointsOnly()
// $language = $this->language();
mapPoints := make([]*MapPoint, 0)
polygon := fmt.Sprintf("POLYGON((%F %F, %F %F, %F %F, %F %F, %F %F))",
lngLo,
latLo,
lngLo,
latHi,
lngHi,
latHi,
lngHi,
latLo,
lngLo,
latLo,
)
if pointsOnly {
rows, err := s.db.QueryContext(
ctx,
`
SELECT ST_AsBinary(point)
FROM item_point
WHERE ST_Contains(ST_GeomFromText(?), point)
`,
polygon,
)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, status.Error(codes.Internal, err.Error())
}
for rows.Next() {
var p orb.Point
err = rows.Scan(wkb.Scanner(&p))
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
mapPoints = append(mapPoints, &MapPoint{
Location: &Point{
Lat: p.Lat(),
Lng: p.Lon(),
},
})
}
} else {
rows, err := s.db.QueryContext(
ctx,
`
SELECT ST_AsBinary(item_point.point), item.id, item.name, item.begin_year, item.end_year,
item.item_type_id, item.today
FROM item
INNER JOIN item_point ON item.id = item_point.item_id
WHERE ST_Contains(ST_GeomFromText(?), item_point.point)
`,
polygon,
)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, status.Error(codes.Internal, err.Error())
}
nameFormatter := items.ItemNameFormatter{}
for rows.Next() {
var p orb.Point
var id int64
var name string
var nullableBeginYear sql.NullInt32
var nullableEndYear sql.NullInt32
var itemTypeID items.ItemType
var today sql.NullBool
err = rows.Scan(wkb.Scanner(&p), &id, &name, &nullableBeginYear, &nullableEndYear, &itemTypeID, &today)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
beginYear := 0
if nullableBeginYear.Valid {
beginYear = int(nullableBeginYear.Int32)
}
endYear := 0
if nullableEndYear.Valid {
endYear = int(nullableEndYear.Int32)
}
var todayRef *bool
if today.Valid {
todayRef = &today.Bool
}
mapPoint := &MapPoint{
Id: fmt.Sprintf("factory%d", id),
Location: &Point{
Lat: p.Lat(),
Lng: p.Lon(),
},
Name: nameFormatter.Format(items.ItemNameFormatterOptions{
Name: name,
BeginYear: beginYear,
EndYear: endYear,
Today: todayRef,
}, in.GetLanguage()),
}
const decimal = 10
switch itemTypeID { //nolint:exhaustive
case items.FACTORY:
mapPoint.Url = []string{"/factories", strconv.FormatInt(id, decimal)}
case items.MUSEUM:
mapPoint.Url = []string{"/museums", strconv.FormatInt(id, decimal)}
}
var imageID sql.NullInt64
err = s.db.QueryRowContext(ctx, `
SELECT pictures.image_id
FROM pictures
INNER JOIN picture_item ON pictures.id = picture_item.picture_id
WHERE pictures.status = ? AND picture_item.item_id = ?
`, pictures.StatusAccepted, id).Scan(&imageID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, status.Error(codes.Internal, err.Error())
}
if !errors.Is(err, sql.ErrNoRows) && imageID.Valid {
image, err := s.imageStorage.FormattedImage(ctx, int(imageID.Int64), "format9")
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
mapPoint.Image = APIImageToGRPC(users.ImageToAPIImage(image))
}
mapPoints = append(mapPoints, mapPoint)
}
}
return &MapPoints{
Points: mapPoints,
}, nil
}