-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspr.go
404 lines (315 loc) · 10.4 KB
/
spr.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package spr
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"github.com/sfomuseum/go-edtf"
"github.com/sfomuseum/go-edtf/parser"
"github.com/whosonfirst/go-whosonfirst-flags"
"github.com/whosonfirst/go-whosonfirst-flags/existential"
wof_spr "github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-uri"
database_sql "github.com/sfomuseum/go-database/sql"
)
// SQLiteResults is a struct that implements the `whosonfirst/go-whosonfirst-spr.StandardPlacesResults`
// interface for returning a list of `StandardPlacesResults.StandardPlacesResults` instances.
type SQLiteResults struct {
wof_spr.StandardPlacesResults `json:",omitempty"`
// Places are the list of `StandardPlacesResults.StandardPlacesResults` instances contained by the struct.
Places []wof_spr.StandardPlacesResult `json:"places"`
}
// Results returns a list of `StandardPlacesResults.StandardPlacesResults` instances.
func (r *SQLiteResults) Results() []wof_spr.StandardPlacesResult {
return r.Places
}
// SQLiteStandardPlacesResult is a struct that implements the `whosonfirst/go-whosonfirst-spr.StandardPlacesResult`
// interface for records stored in a SQLite database and which have been indexed by the `whosonfirst/go-whosonfirst-sqlite-features`
// package.
type SQLiteStandardPlacesResult struct {
wof_spr.StandardPlacesResult `json:",omitempty"`
WOFId string `json:"wof:id"`
WOFParentId string `json:"wof:parent_id"`
WOFName string `json:"wof:name"`
WOFCountry string `json:"wof:country"`
WOFPlacetype string `json:"wof:placetype"`
MZLatitude float64 `json:"mz:latitude"`
MZLongitude float64 `json:"mz:longitude"`
MZMinLatitude float64 `json:"mz:min_latitude"`
MZMinLongitude float64 `json:"mz:min_longitude"`
MZMaxLatitude float64 `json:"mz:max_latitude"`
MZMaxLongitude float64 `json:"mz:max_longitude"`
MZIsCurrent int64 `json:"mz:is_current"`
MZIsDeprecated int64 `json:"mz:is_deprecated"`
MZIsCeased int64 `json:"mz:is_ceased"`
MZIsSuperseded int64 `json:"mz:is_superseded"`
MZIsSuperseding int64 `json:"mz:is_superseding"`
EDTFInception string `json:"edtf:inception"`
EDTFCessation string `json:"edtf:cessation"`
WOFSupersedes []int64 `json:"wof:supersedes"`
WOFSupersededBy []int64 `json:"wof:superseded_by"`
WOFBelongsTo []int64 `json:"wof:belongsto"`
WOFPath string `json:"wof:path"`
WOFRepo string `json:"wof:repo"`
WOFLastModified int64 `json:"wof:lastmodified"`
}
func (spr *SQLiteStandardPlacesResult) Id() string {
return spr.WOFId
}
func (spr *SQLiteStandardPlacesResult) ParentId() string {
return spr.WOFParentId
}
func (spr *SQLiteStandardPlacesResult) Name() string {
return spr.WOFName
}
func (spr *SQLiteStandardPlacesResult) Placetype() string {
return spr.WOFPlacetype
}
func (spr *SQLiteStandardPlacesResult) Country() string {
return spr.WOFCountry
}
func (spr *SQLiteStandardPlacesResult) Repo() string {
return spr.WOFRepo
}
func (spr *SQLiteStandardPlacesResult) Path() string {
return spr.WOFPath
}
func (spr *SQLiteStandardPlacesResult) URI() string {
return ""
}
func (spr *SQLiteStandardPlacesResult) Latitude() float64 {
return spr.MZLatitude
}
func (spr *SQLiteStandardPlacesResult) Longitude() float64 {
return spr.MZLongitude
}
func (spr *SQLiteStandardPlacesResult) MinLatitude() float64 {
return spr.MZMinLatitude
}
func (spr *SQLiteStandardPlacesResult) MinLongitude() float64 {
return spr.MZMinLongitude
}
func (spr *SQLiteStandardPlacesResult) MaxLatitude() float64 {
return spr.MZMaxLatitude
}
func (spr *SQLiteStandardPlacesResult) MaxLongitude() float64 {
return spr.MZMaxLongitude
}
func (spr *SQLiteStandardPlacesResult) IsCurrent() flags.ExistentialFlag {
return existentialFlag(spr.MZIsCurrent)
}
func (spr *SQLiteStandardPlacesResult) IsCeased() flags.ExistentialFlag {
return existentialFlag(spr.MZIsCeased)
}
func (spr *SQLiteStandardPlacesResult) IsDeprecated() flags.ExistentialFlag {
return existentialFlag(spr.MZIsDeprecated)
}
func (spr *SQLiteStandardPlacesResult) IsSuperseded() flags.ExistentialFlag {
return existentialFlag(spr.MZIsSuperseded)
}
func (spr *SQLiteStandardPlacesResult) Inception() *edtf.EDTFDate {
d, err := parser.ParseString(spr.EDTFInception)
if err != nil {
return nil
}
return d
}
func (spr *SQLiteStandardPlacesResult) Cessation() *edtf.EDTFDate {
d, err := parser.ParseString(spr.EDTFCessation)
if err != nil {
return nil
}
return d
}
func (spr *SQLiteStandardPlacesResult) IsSuperseding() flags.ExistentialFlag {
return existentialFlag(spr.MZIsSuperseding)
}
func (spr *SQLiteStandardPlacesResult) SupersededBy() []int64 {
return spr.WOFSupersededBy
}
func (spr *SQLiteStandardPlacesResult) Supersedes() []int64 {
return spr.WOFSupersedes
}
func (spr *SQLiteStandardPlacesResult) BelongsTo() []int64 {
return spr.WOFBelongsTo
}
func (spr *SQLiteStandardPlacesResult) LastModified() int64 {
return spr.WOFLastModified
}
func RetrieveSPR(ctx context.Context, spr_db *sql.DB, spr_table database_sql.Table, id int64, alt_label string) (wof_spr.StandardPlacesResult, error) {
args := []interface{}{
id,
alt_label,
}
// supersedes and superseding need to be added here pending
// https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14
spr_q := fmt.Sprintf(`SELECT
id, parent_id, name, placetype,
inception, cessation,
country, repo,
latitude, longitude,
min_latitude, min_longitude,
max_latitude, max_longitude,
is_current, is_deprecated, is_ceased,is_superseded, is_superseding,
supersedes, superseded_by, belongsto,
is_alt, alt_label,
lastmodified
FROM %s WHERE id = ? AND alt_label = ?`, spr_table.Name())
row := spr_db.QueryRowContext(ctx, spr_q, args...)
return RetrieveSPRWithRow(ctx, row)
}
// See notes below
func RetrieveSPRWithRow(ctx context.Context, row *sql.Row) (wof_spr.StandardPlacesResult, error) {
return retrieveSPRWithScanner(ctx, row)
}
// See notes below
func RetrieveSPRWithRows(ctx context.Context, rows *sql.Rows) (wof_spr.StandardPlacesResult, error) {
return retrieveSPRWithScanner(ctx, rows)
}
// We go to the trouble of all this indirection because neither *sql.Row or *sql.Rows implement
// the sql.Scanner interface.
// The latter expects: Scan(src interface{}) error
// But the former pxpect: Scan(src ...interface{}) error
func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.StandardPlacesResult, error) {
switch scanner.(type) {
case *sql.Row, *sql.Rows:
// pass
default:
return nil, fmt.Errorf("Unsupported scanner")
}
var spr_id string
var parent_id string
var name string
var placetype string
var country string
var repo string
var inception string
var cessation string
var latitude float64
var longitude float64
var min_latitude float64
var max_latitude float64
var min_longitude float64
var max_longitude float64
var is_current int64
var is_deprecated int64
var is_ceased int64
var is_superseded int64
var is_superseding int64
// supersedes and superseding and belongsto need to be added here pending
// https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14
var str_supersedes string
var str_superseded_by string
var str_belongs_to string
var is_alt int64
var alt_label string
var lastmodified int64
// supersedes and superseding need to be added here pending
// https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14
var scanner_err error
switch scanner.(type) {
case *sql.Rows:
scanner_err = scanner.(*sql.Rows).Scan(
&spr_id, &parent_id, &name, &placetype,
&inception, &cessation,
&country, &repo,
&latitude, &longitude,
&min_latitude, &max_latitude, &min_longitude, &max_longitude,
&is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding,
&str_supersedes, &str_superseded_by, &str_belongs_to,
&is_alt, &alt_label,
&lastmodified,
)
default:
scanner_err = scanner.(*sql.Row).Scan(
&spr_id, &parent_id, &name, &placetype,
&inception, &cessation,
&country, &repo,
&latitude, &longitude,
&min_latitude, &max_latitude, &min_longitude, &max_longitude,
&is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding,
&str_supersedes, &str_superseded_by, &str_belongs_to,
&is_alt, &alt_label,
&lastmodified,
)
}
if scanner_err != nil {
return nil, scanner_err
}
id, err := strconv.ParseInt(spr_id, 10, 64)
if err != nil {
return nil, err
}
path, err := uri.Id2RelPath(id)
if err != nil {
return nil, err
}
_, err = parser.ParseString(inception)
if err != nil {
return nil, err
}
_, err = parser.ParseString(cessation)
if err != nil {
return nil, err
}
supersedes, err := stringToInt64(str_supersedes)
if err != nil {
return nil, err
}
superseded_by, err := stringToInt64(str_superseded_by)
if err != nil {
return nil, err
}
belongs_to, err := stringToInt64(str_belongs_to)
if err != nil {
return nil, err
}
s := &SQLiteStandardPlacesResult{
WOFId: spr_id,
WOFParentId: parent_id,
WOFName: name,
WOFCountry: country,
WOFPlacetype: placetype,
MZLatitude: latitude,
MZLongitude: longitude,
MZMinLatitude: min_latitude,
MZMaxLatitude: max_latitude,
MZMinLongitude: min_longitude,
MZMaxLongitude: max_longitude,
MZIsCurrent: is_current,
MZIsDeprecated: is_deprecated,
MZIsCeased: is_ceased,
MZIsSuperseded: is_superseded,
MZIsSuperseding: is_superseding,
WOFSupersedes: supersedes,
WOFSupersededBy: superseded_by,
WOFBelongsTo: belongs_to,
WOFPath: path,
WOFRepo: repo,
WOFLastModified: lastmodified,
EDTFInception: inception,
EDTFCessation: cessation,
}
return s, nil
}
func existentialFlag(i int64) flags.ExistentialFlag {
fl, _ := existential.NewKnownUnknownFlag(i)
return fl
}
func stringToInt64(str string) ([]int64, error) {
str = strings.Trim(str, " ")
if str == "" {
return []int64{}, nil
}
parts := strings.Split(str, ",")
ints := make([]int64, len(parts))
for idx, s := range parts {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, err
}
ints[idx] = i
}
return ints, nil
}