/
geopackage.go
427 lines (373 loc) · 16 KB
/
geopackage.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package geopackage
import (
"context"
"database/sql"
"fmt"
"log"
"maps"
"os"
"path"
"sync"
"time"
"github.com/PDOK/gokoala/config"
"github.com/PDOK/gokoala/engine/util"
"github.com/PDOK/gokoala/ogc/features/datasources"
"github.com/PDOK/gokoala/ogc/features/domain"
"github.com/go-spatial/geom"
"github.com/go-spatial/geom/encoding/gpkg"
"github.com/go-spatial/geom/encoding/wkt"
"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
"github.com/qustavo/sqlhooks/v2"
_ "github.com/mattn/go-sqlite3" // import for side effect (= sqlite3 driver) only
)
const (
sqliteDriverName = "sqlite3_with_extensions"
)
var once sync.Once
// Load sqlite (with extensions) once.
//
// Extensions are by default expected in /usr/lib. For spatialite you can
// alternatively/optionally set SPATIALITE_LIBRARY_PATH.
func loadDriver() {
once.Do(func() {
spatialite := path.Join(os.Getenv("SPATIALITE_LIBRARY_PATH"), "mod_spatialite")
driver := &sqlite3.SQLiteDriver{Extensions: []string{spatialite}}
sql.Register(sqliteDriverName, sqlhooks.Wrap(driver, datasources.NewSQLLogFromEnv()))
})
}
type geoPackageBackend interface {
getDB() *sqlx.DB
close()
}
type featureTable struct {
TableName string `db:"table_name"`
DataType string `db:"data_type"` // always 'features'
Identifier string `db:"identifier"`
Description string `db:"description"`
GeometryColumnName string `db:"column_name"`
GeometryType string `db:"geometry_type_name"`
LastChange time.Time `db:"last_change"`
MinX float64 `db:"min_x"` // bbox
MinY float64 `db:"min_y"` // bbox
MaxX float64 `db:"max_x"` // bbox
MaxY float64 `db:"max_y"` // bbox
SRS int64 `db:"srs_id"`
ColumnsWithDateType map[string]string
}
func (ft featureTable) ColumnsWithDataType() map[string]string {
return ft.ColumnsWithDateType
}
type GeoPackage struct {
backend geoPackageBackend
preparedStmtCache *PreparedStatementCache
fidColumn string
featureTableByCollectionID map[string]*featureTable
queryTimeout time.Duration
maxBBoxSizeToUseWithRTree int
}
func NewGeoPackage(collections config.GeoSpatialCollections, gpkgConfig config.GeoPackage) *GeoPackage {
loadDriver()
g := &GeoPackage{}
g.preparedStmtCache = NewCache()
warmUp := false
switch {
case gpkgConfig.Local != nil:
g.backend = newLocalGeoPackage(gpkgConfig.Local)
g.fidColumn = gpkgConfig.Local.Fid
g.queryTimeout = gpkgConfig.Local.QueryTimeout.Duration
g.maxBBoxSizeToUseWithRTree = gpkgConfig.Local.MaxBBoxSizeToUseWithRTree
case gpkgConfig.Cloud != nil:
g.backend = newCloudBackedGeoPackage(gpkgConfig.Cloud)
g.fidColumn = gpkgConfig.Cloud.Fid
g.queryTimeout = gpkgConfig.Cloud.QueryTimeout.Duration
g.maxBBoxSizeToUseWithRTree = gpkgConfig.Cloud.MaxBBoxSizeToUseWithRTree
warmUp = gpkgConfig.Cloud.Cache.WarmUp
default:
log.Fatal("unknown GeoPackage config encountered")
}
metadata, err := readDriverMetadata(g.backend.getDB())
if err != nil {
log.Fatalf("failed to connect with GeoPackage: %v", err)
}
log.Println(metadata)
g.featureTableByCollectionID, err = readGpkgContents(collections, g.backend.getDB())
if err != nil {
log.Fatal(err)
}
if err = assertIndexesExist(collections, g.featureTableByCollectionID, g.backend.getDB(), g.fidColumn); err != nil {
log.Fatal(err)
}
if warmUp {
// perform warmup async since it can take a long time
go func() {
if err = warmUpFeatureTables(collections, g.featureTableByCollectionID, g.backend.getDB()); err != nil {
log.Fatal(err)
}
}()
}
return g
}
func (g *GeoPackage) Close() {
g.preparedStmtCache.Close()
g.backend.close()
}
func (g *GeoPackage) GetFeatureIDs(ctx context.Context, collection string, criteria datasources.FeaturesCriteria) ([]int64, domain.Cursors, error) {
table, err := g.getFeatureTable(collection)
if err != nil {
return nil, domain.Cursors{}, err
}
queryCtx, cancel := context.WithTimeout(ctx, g.queryTimeout) // https://go.dev/doc/database/cancel-operations
defer cancel()
stmt, query, queryArgs, err := g.makeFeaturesQuery(queryCtx, table, true, criteria) //nolint:sqlclosecheck // prepared statement is cached, will be closed when evicted from cache
if err != nil {
return nil, domain.Cursors{}, fmt.Errorf("failed to create query '%s' error: %w", query, err)
}
rows, err := stmt.QueryxContext(queryCtx, queryArgs)
if err != nil {
return nil, domain.Cursors{}, fmt.Errorf("failed to execute query '%s' error: %w", query, err)
}
defer rows.Close()
featureIDs, prevNext, err := domain.MapRowsToFeatureIDs(rows)
if err != nil {
return nil, domain.Cursors{}, err
}
if prevNext == nil {
return nil, domain.Cursors{}, nil
}
return featureIDs, domain.NewCursors(*prevNext, criteria.Cursor.FiltersChecksum), nil
}
func (g *GeoPackage) GetFeaturesByID(ctx context.Context, collection string, featureIDs []int64) (*domain.FeatureCollection, error) {
table, err := g.getFeatureTable(collection)
if err != nil {
return nil, err
}
queryCtx, cancel := context.WithTimeout(ctx, g.queryTimeout) // https://go.dev/doc/database/cancel-operations
defer cancel()
fids := map[string]any{"fids": featureIDs}
query, queryArgs, err := sqlx.Named(fmt.Sprintf("select * from %s where %s in (:fids)", table.TableName, g.fidColumn), fids)
if err != nil {
return nil, fmt.Errorf("failed to make features query, error: %w", err)
}
query, queryArgs, err = sqlx.In(query, queryArgs...)
if err != nil {
return nil, fmt.Errorf("failed to make IN-clause, error: %w", err)
}
rows, err := g.backend.getDB().QueryxContext(queryCtx, g.backend.getDB().Rebind(query), queryArgs...)
if err != nil {
return nil, fmt.Errorf("failed to execute query '%s' error: %w", query, err)
}
defer rows.Close()
fc := domain.FeatureCollection{}
fc.Features, _, err = domain.MapRowsToFeatures(rows, g.fidColumn, table.GeometryColumnName, readGpkgGeometry)
if err != nil {
return nil, err
}
fc.NumberReturned = len(fc.Features)
return &fc, nil
}
func (g *GeoPackage) GetFeatures(ctx context.Context, collection string, criteria datasources.FeaturesCriteria) (*domain.FeatureCollection, domain.Cursors, error) {
table, err := g.getFeatureTable(collection)
if err != nil {
return nil, domain.Cursors{}, err
}
queryCtx, cancel := context.WithTimeout(ctx, g.queryTimeout) // https://go.dev/doc/database/cancel-operations
defer cancel()
stmt, query, queryArgs, err := g.makeFeaturesQuery(queryCtx, table, false, criteria) //nolint:sqlclosecheck // prepared statement is cached, will be closed when evicted from cache
if err != nil {
return nil, domain.Cursors{}, fmt.Errorf("failed to create query '%s' error: %w", query, err)
}
rows, err := stmt.QueryxContext(queryCtx, queryArgs)
if err != nil {
return nil, domain.Cursors{}, fmt.Errorf("failed to execute query '%s' error: %w", query, err)
}
defer rows.Close()
var prevNext *domain.PrevNextFID
fc := domain.FeatureCollection{}
fc.Features, prevNext, err = domain.MapRowsToFeatures(rows, g.fidColumn, table.GeometryColumnName, readGpkgGeometry)
if err != nil {
return nil, domain.Cursors{}, err
}
if prevNext == nil {
return nil, domain.Cursors{}, nil
}
fc.NumberReturned = len(fc.Features)
return &fc, domain.NewCursors(*prevNext, criteria.Cursor.FiltersChecksum), nil
}
func (g *GeoPackage) GetFeature(ctx context.Context, collection string, featureID int64) (*domain.Feature, error) {
table, err := g.getFeatureTable(collection)
if err != nil {
return nil, err
}
queryCtx, cancel := context.WithTimeout(ctx, g.queryTimeout) // https://go.dev/doc/database/cancel-operations
defer cancel()
query := fmt.Sprintf("select * from %s f where f.%s = :fid limit 1", table.TableName, g.fidColumn)
rows, err := g.backend.getDB().NamedQueryContext(queryCtx, query, map[string]any{"fid": featureID})
if err != nil {
return nil, fmt.Errorf("query '%s' failed: %w", query, err)
}
defer rows.Close()
features, _, err := domain.MapRowsToFeatures(rows, g.fidColumn, table.GeometryColumnName, readGpkgGeometry)
if err != nil {
return nil, err
}
if len(features) != 1 {
return nil, nil
}
return features[0], nil
}
func (g *GeoPackage) GetFeatureTableMetadata(collection string) (datasources.FeatureTableMetadata, error) {
val, ok := g.featureTableByCollectionID[collection]
if !ok {
return nil, fmt.Errorf("no metadata for %s", collection)
}
return val, nil
}
// Build specific features queries based on the given options.
// Make sure to use SQL bind variables and return named params: https://jmoiron.github.io/sqlx/#namedParams
func (g *GeoPackage) makeFeaturesQuery(ctx context.Context, table *featureTable, onlyFIDs bool,
criteria datasources.FeaturesCriteria) (stmt *sqlx.NamedStmt, query string, queryArgs map[string]any, err error) {
// make query
if criteria.Bbox != nil {
query, queryArgs, err = g.makeBboxQuery(table, onlyFIDs, criteria)
if err != nil {
return
}
} else {
query, queryArgs = g.makeDefaultQuery(table, criteria)
}
// lookup prepared statement for given query, or create new one
stmt, err = g.preparedStmtCache.Lookup(ctx, g.backend.getDB(), query)
return
}
func (g *GeoPackage) makeDefaultQuery(table *featureTable, criteria datasources.FeaturesCriteria) (string, map[string]any) {
pfClause, pfNamedParams := propertyFiltersToSQL(criteria.PropertyFilters)
temporalClause, temporalNamedParams := temporalCriteriaToSQL(criteria.TemporalCriteria)
defaultQuery := fmt.Sprintf(`
with
next as (select * from "%[1]s" where "%[2]s" >= :fid %[3]s %[4]s order by %[2]s asc limit :limit + 1),
prev as (select * from "%[1]s" where "%[2]s" < :fid %[3]s %[4]s order by %[2]s desc limit :limit),
nextprev as (select * from next union all select * from prev),
nextprevfeat as (select *, lag("%[2]s", :limit) over (order by %[2]s) as prevfid, lead("%[2]s", :limit) over (order by "%[2]s") as nextfid from nextprev)
select * from nextprevfeat where "%[2]s" >= :fid %[3]s %[4]s limit :limit
`, table.TableName, g.fidColumn, temporalClause, pfClause) // don't add user input here, use named params for user input!
namedParams := map[string]any{
"fid": criteria.Cursor.FID,
"limit": criteria.Limit,
}
maps.Copy(namedParams, pfNamedParams)
maps.Copy(namedParams, temporalNamedParams)
return defaultQuery, namedParams
}
func (g *GeoPackage) makeBboxQuery(table *featureTable, onlyFIDs bool, criteria datasources.FeaturesCriteria) (string, map[string]any, error) {
selectClause := "*"
if onlyFIDs {
selectClause = "\"" + g.fidColumn + "\", prevfid, nextfid"
}
btreeIndexHint := fmt.Sprintf("indexed by \"%s_spatial_idx\"", table.TableName)
pfClause, pfNamedParams := propertyFiltersToSQL(criteria.PropertyFilters)
if pfClause != "" {
// don't force btree index when using property filter, let SQLite decide
// whether to use the BTree index or the property filter index
btreeIndexHint = ""
}
temporalClause, temporalNamedParams := temporalCriteriaToSQL(criteria.TemporalCriteria)
bboxQuery := fmt.Sprintf(`
with
given_bbox as (select geomfromtext(:bboxWkt, :bboxSrid)),
bbox_size as (select iif(count(id) < %[3]d, 'small', 'big') as bbox_size
from (select id from rtree_%[1]s_%[4]s
where minx <= :maxx and maxx >= :minx and miny <= :maxy and maxy >= :miny
limit %[3]d)),
next_bbox_rtree as (select f.*
from "%[1]s" f inner join rtree_%[1]s_%[4]s rf on f."%[2]s" = rf.id
where rf.minx <= :maxx and rf.maxx >= :minx and rf.miny <= :maxy and rf.maxy >= :miny
and st_intersects((select * from given_bbox), castautomagic(f.%[4]s)) = 1
and f."%[2]s" >= :fid %[6]s %[7]s
order by f."%[2]s" asc
limit (select iif(bbox_size == 'small', :limit + 1, 0) from bbox_size)),
next_bbox_btree as (select f.*
from "%[1]s" f %[8]s
where f.minx <= :maxx and f.maxx >= :minx and f.miny <= :maxy and f.maxy >= :miny
and st_intersects((select * from given_bbox), castautomagic(f.%[4]s)) = 1
and f."%[2]s" >= :fid %[6]s %[7]s
order by f."%[2]s" asc
limit (select iif(bbox_size == 'big', :limit + 1, 0) from bbox_size)),
next as (select * from next_bbox_rtree union all select * from next_bbox_btree),
prev_bbox_rtree as (select f.*
from "%[1]s" f inner join rtree_%[1]s_%[4]s rf on f."%[2]s" = rf.id
where rf.minx <= :maxx and rf.maxx >= :minx and rf.miny <= :maxy and rf.maxy >= :miny
and st_intersects((select * from given_bbox), castautomagic(f.%[4]s)) = 1
and f."%[2]s" < :fid %[6]s %[7]s
order by f."%[2]s" desc
limit (select iif(bbox_size == 'small', :limit, 0) from bbox_size)),
prev_bbox_btree as (select f.*
from "%[1]s" f %[8]s
where f.minx <= :maxx and f.maxx >= :minx and f.miny <= :maxy and f.maxy >= :miny
and st_intersects((select * from given_bbox), castautomagic(f.%[4]s)) = 1
and f."%[2]s" < :fid %[6]s %[7]s
order by f."%[2]s" desc
limit (select iif(bbox_size == 'big', :limit, 0) from bbox_size)),
prev as (select * from prev_bbox_rtree union all select * from prev_bbox_btree),
nextprev as (select * from next union all select * from prev),
nextprevfeat as (select *, lag("%[2]s", :limit) over (order by "%[2]s") as prevfid, lead("%[2]s", :limit) over (order by "%[2]s") as nextfid from nextprev)
select %[5]s from nextprevfeat where "%[2]s" >= :fid %[6]s %[7]s limit :limit
`, table.TableName, g.fidColumn, g.maxBBoxSizeToUseWithRTree, table.GeometryColumnName,
selectClause, temporalClause, pfClause, btreeIndexHint) // don't add user input here, use named params for user input!
bboxAsWKT, err := wkt.EncodeString(criteria.Bbox)
if err != nil {
return "", nil, err
}
namedParams := map[string]any{
"fid": criteria.Cursor.FID,
"limit": criteria.Limit,
"bboxWkt": bboxAsWKT,
"maxx": criteria.Bbox.MaxX(),
"minx": criteria.Bbox.MinX(),
"maxy": criteria.Bbox.MaxY(),
"miny": criteria.Bbox.MinY(),
"bboxSrid": criteria.InputSRID}
maps.Copy(namedParams, pfNamedParams)
maps.Copy(namedParams, temporalNamedParams)
return bboxQuery, namedParams, nil
}
func (g *GeoPackage) getFeatureTable(collection string) (*featureTable, error) {
table, ok := g.featureTableByCollectionID[collection]
if !ok {
return nil, fmt.Errorf("can't query collection '%s' since it doesn't exist in "+
"geopackage, available in geopackage: %v", collection, util.Keys(g.featureTableByCollectionID))
}
return table, nil
}
func readGpkgGeometry(rawGeom []byte) (geom.Geometry, error) {
geometry, err := gpkg.DecodeGeometry(rawGeom)
if err != nil {
return nil, err
}
return geometry.Geometry, nil
}
func propertyFiltersToSQL(pf map[string]string) (sql string, namedParams map[string]any) {
namedParams = make(map[string]any)
if len(pf) > 0 {
position := 0
for k, v := range pf {
position++
namedParam := fmt.Sprintf("pf%d", position)
// column name in double quotes in case it is a reserved keyword
// also: we don't currently support LIKE since wildcard searches don't use the index
sql += fmt.Sprintf(" and \"%s\" = :%s", k, namedParam)
namedParams[namedParam] = v
}
}
return sql, namedParams
}
func temporalCriteriaToSQL(temporalCriteria datasources.TemporalCriteria) (sql string, namedParams map[string]any) {
namedParams = make(map[string]any)
if !temporalCriteria.ReferenceDate.IsZero() {
namedParams["referenceDate"] = temporalCriteria.ReferenceDate
startDate := temporalCriteria.StartDateProperty
endDate := temporalCriteria.EndDateProperty
sql = fmt.Sprintf(" and \"%[1]s\" <= :referenceDate and (\"%[2]s\" >= :referenceDate or \"%[2]s\" is null)", startDate, endDate)
}
return sql, namedParams
}