-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
gpkg_register.go
419 lines (351 loc) · 11.9 KB
/
gpkg_register.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
//go:build cgo
// +build cgo
package gpkg
import (
"bytes"
"database/sql"
"errors"
"fmt"
"os"
"regexp"
"sort"
"strings"
conf "github.com/go-spatial/tegola/config"
_ "github.com/mattn/go-sqlite3"
"github.com/go-spatial/geom"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/dict"
"github.com/go-spatial/tegola/internal/log"
"github.com/go-spatial/tegola/provider"
)
var colFinder *regexp.Regexp
func init() {
provider.Register(provider.TypeStd.Prefix()+Name, NewTileProvider, Cleanup)
colFinder = regexp.MustCompile(`^(([a-zA-Z_][a-zA-Z0-9_]*)|"([^"]+)")\s`)
}
// Metadata for feature tables in gpkg database
type featureTableDetails struct {
colNames []string
idFieldname string
geomFieldname string
geomType geom.Geometry
srid uint64
bbox *geom.Extent
}
// Creates a config instance of the type NewTileProvider() requires including all available feature
// tables in the gpkg at 'gpkgPath'.
func AutoConfig(gpkgPath string) (map[string]interface{}, error) {
// Get all feature tables
db, err := sql.Open("sqlite3", gpkgPath)
if err != nil {
return nil, err
}
ftMetaData, err := featureTableMetaData(db)
if err != nil {
return nil, err
}
// Handle table config creation in consistent order to facilitate testing
tnames := make([]string, len(ftMetaData))
i := 0
for tname := range ftMetaData {
tnames[i] = tname
i++
}
sort.Strings(tnames)
conf := make(map[string]interface{})
conf["name"] = "autoconfd_gpkg"
conf["type"] = provider.TypeStd.Prefix() + Name
conf["filepath"] = gpkgPath
conf["layers"] = make([]map[string]interface{}, len(tnames))
for i, tablename := range tnames {
// Use all columns besides the primary key (id) and geometry columns in "fields"
propFields := make([]string, 0, len(ftMetaData[tablename].colNames))
for _, colName := range ftMetaData[tablename].colNames {
if colName != ftMetaData[tablename].idFieldname && colName != ftMetaData[tablename].geomFieldname {
propFields = append(propFields, colName)
}
}
lconf := make(map[string]interface{})
lconf["name"] = tablename
lconf["tablename"] = tablename
lconf["id_fieldname"] = ftMetaData[tablename].idFieldname
lconf["fields"] = propFields
conf["layers"].([]map[string]interface{})[i] = lconf
}
return conf, nil
}
// extractColsAndPKFromSQL extracts all column names and the primary key colum
// from an SQL definition string.
func extractColsAndPKFromSQL(sql string) ([]string, string) {
defs := extractColDefsFromSQL(sql)
var pkCol string
colNames := make([]string, 0, len(defs))
// match unquoted (`column_name`) or quoted (`"column name"`) indentifiers
for _, def := range defs {
matches := colFinder.FindStringSubmatch(def)
if matches == nil {
continue
}
colName := matches[2] + matches[3] // either from unquoted, or quoted submatch
colNames = append(colNames, colName)
if strings.Contains(strings.ToLower(def), "primary key") {
pkCol = colName
}
}
// Sort colNames for consistent output to facilitate testing
sort.Strings(colNames)
return colNames, pkCol
}
// extractColDefsFromSQL extracts all column definitions an SQL definition string.
func extractColDefsFromSQL(sql string) []string {
// Simple parser for SQL definitions. Skips everything before the first
// parentheses, splits definitions at comma, but ignores commas between
// subsequent parentheses.
// Does not handle comments or quoted commas.
var defs []string
var col bytes.Buffer
p := 0 // count number of open parentheses
for _, r := range sql {
if r == ')' && p == 1 {
// closing outer brace of column definitions
defs = append(defs, strings.TrimSpace(col.String()))
col.Reset()
break
}
if r == ',' && p == 1 {
// next definition
defs = append(defs, strings.TrimSpace(col.String()))
col.Reset()
continue
}
col.WriteRune(r)
if r == '(' {
if p == 0 {
// start of column definitions, ignore CREATE TABLE ...
col.Reset()
}
p++
}
if r == ')' {
p--
}
}
return defs
}
// Collect meta data about all feature tables in opened gpkg.
func featureTableMetaData(gpkg *sql.DB) (map[string]featureTableDetails, error) {
// this query is used to read the metadata from the gpkg_contents, gpkg_geometry_columns, and
// sqlite_master tables for tables that store geographic features.
qtext := `
SELECT
c.table_name, c.min_x, c.min_y, c.max_x, c.max_y, c.srs_id, gc.column_name, gc.geometry_type_name, sm.sql
FROM
gpkg_contents c JOIN gpkg_geometry_columns gc ON c.table_name == gc.table_name JOIN sqlite_master sm ON c.table_name = sm.tbl_name
WHERE
c.data_type = 'features' AND sm.type = 'table';`
rows, err := gpkg.Query(qtext)
if err != nil {
log.Errorf("error during query: %v - %v", qtext, err)
return nil, err
}
defer rows.Close()
// container for tracking metadata for each table with a geometry
geomTableDetails := make(map[string]featureTableDetails)
// iterate each row extracting meta data about each table
for rows.Next() {
var tablename, geomCol, geomType, tableSql sql.NullString
var minX, minY, maxX, maxY sql.NullFloat64
var srid sql.NullInt64
if err = rows.Scan(&tablename, &minX, &minY, &maxX, &maxY, &srid, &geomCol, &geomType, &tableSql); err != nil {
return nil, err
}
if !tableSql.Valid {
return nil, fmt.Errorf("invalid sql for table '%v'", tablename)
}
// map the returned geom type to a tegola geom type
tg, err := geomNameToGeom(geomType.String)
if err != nil {
log.Errorf("error mapping geom type (%v): %v", geomType, err)
return nil, err
}
bbox := geom.NewExtent(
[2]float64{minX.Float64, minY.Float64},
[2]float64{maxX.Float64, maxY.Float64},
)
colNames, pkCol := extractColsAndPKFromSQL(tableSql.String)
geomTableDetails[tablename.String] = featureTableDetails{
colNames: colNames,
idFieldname: pkCol,
geomFieldname: geomCol.String,
geomType: tg,
srid: uint64(srid.Int64),
// the extent of the layer's features
//bbox: geom.BoundingBox{minX.Float64, minY.Float64, maxX.Float64, maxY.Float64},
bbox: bbox,
}
}
return geomTableDetails, nil
}
func NewTileProvider(config dict.Dicter, maps []provider.Map) (provider.Tiler, error) {
log.Debugf("config: %v", config)
filepath, err := config.String(ConfigKeyFilePath, nil)
if err != nil {
return nil, err
}
if filepath == "" {
return nil, ErrInvalidFilePath{filepath}
}
// check the file exists
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return nil, ErrInvalidFilePath{filepath}
}
db, err := sql.Open("sqlite3", filepath)
if err != nil {
return nil, err
}
geomTableDetails, err := featureTableMetaData(db)
if err != nil {
return nil, err
}
p := Provider{
Filepath: filepath,
layers: make(map[string]Layer),
db: db,
}
layers, err := config.MapSlice(ConfigKeyLayers)
if err != nil {
return nil, err
}
lyrsSeen := make(map[string]int)
for i, layerConf := range layers {
layerName, err := layerConf.String(ConfigKeyLayerName, nil)
if err != nil {
return nil, fmt.Errorf("for layer (%v) we got the following error trying to get the layer's name field: %v", i, err)
}
if layerName == "" {
return nil, ErrMissingLayerName
}
// check if we have already seen this layer
if j, ok := lyrsSeen[layerName]; ok {
return nil, fmt.Errorf("layer name (%v) is duplicated in both layer %v and layer %v", layerName, i, j)
}
lyrsSeen[layerName] = i
// ensure only one of sql or tablename exist
_, errTable := layerConf.String(ConfigKeyTableName, nil)
if _, ok := errTable.(dict.ErrKeyRequired); errTable != nil && !ok {
return nil, err
}
_, errSQL := layerConf.String(ConfigKeySQL, nil)
if _, ok := errSQL.(dict.ErrKeyRequired); errSQL != nil && !ok {
return nil, err
}
// err != nil <-> key != exists
if errTable != nil && errSQL != nil {
return nil, errors.New("'tablename' or 'sql' is required for a feature's config")
}
// err == nil <-> key == exists
if errTable == nil && errSQL == nil {
return nil, errors.New("'tablename' or 'sql' is required for a feature's config")
}
idFieldname := DefaultIDFieldName
idFieldname, err = layerConf.String(ConfigKeyGeomIDField, &idFieldname)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v : %v", i, layerName, err)
}
tagFieldnames, err := layerConf.StringSlice(ConfigKeyFields)
if err != nil { // empty slices are okay
return nil, fmt.Errorf("for layer (%v) %v, %q field had the following error: %v", i, layerName, ConfigKeyFields, err)
}
// layer container. will be added to the provider after it's configured
layer := Layer{
name: layerName,
}
if errTable == nil { // layerConf[ConfigKeyTableName] exists
tablename, err := layerConf.String(ConfigKeyTableName, &idFieldname)
if err != nil {
return nil, fmt.Errorf("for layer (%v) %v : %v", i, layerName, err)
}
d, ok := geomTableDetails[tablename]
if !ok {
return nil, fmt.Errorf("table %q does not exist", tablename)
}
layer.tablename = tablename
layer.tagFieldnames = tagFieldnames
layer.geomFieldname = d.geomFieldname
layer.geomType = d.geomType
layer.idFieldname = idFieldname
layer.srid = d.srid
layer.bbox = *d.bbox
} else { // layerConf[ConfigKeySQL] exists
var customSQL string
customSQL, err = layerConf.String(ConfigKeySQL, &customSQL)
if err != nil {
return nil, fmt.Errorf("for %v layer(%v) %v has an error: %v", i, layerName, ConfigKeySQL, err)
}
layer.sql = customSQL
// if a !ZOOM! token exists, all features could be filtered out so we don't have a geometry to inspect it's type.
// TODO(arolek): implement an SQL parser or figure out a different approach. this is brittle but I can't figure out a better
// solution without using an SQL parser on custom SQL statements
allZoomsSQL := "IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)"
tokenReplacer := strings.NewReplacer(
">= "+conf.ZoomToken, allZoomsSQL,
">="+conf.ZoomToken, allZoomsSQL,
"=> "+conf.ZoomToken, allZoomsSQL,
"=>"+conf.ZoomToken, allZoomsSQL,
"=< "+conf.ZoomToken, allZoomsSQL,
"=<"+conf.ZoomToken, allZoomsSQL,
"<= "+conf.ZoomToken, allZoomsSQL,
"<="+conf.ZoomToken, allZoomsSQL,
"!= "+conf.ZoomToken, allZoomsSQL,
"!="+conf.ZoomToken, allZoomsSQL,
"= "+conf.ZoomToken, allZoomsSQL,
"="+conf.ZoomToken, allZoomsSQL,
"> "+conf.ZoomToken, allZoomsSQL,
">"+conf.ZoomToken, allZoomsSQL,
"< "+conf.ZoomToken, allZoomsSQL,
"<"+conf.ZoomToken, allZoomsSQL,
)
customSQL = tokenReplacer.Replace(customSQL)
// Set bounds & zoom params to include all layers
// Bounds checks need params: maxx, minx, maxy, miny
// TODO(arolek): this assumes WGS84. should be more flexible
customSQL = replaceTokens(customSQL, 0, tegola.WGS84Bounds)
// Get geometry type & srid from geometry of first row.
qtext := fmt.Sprintf("SELECT geom FROM (%v) LIMIT 1;", customSQL)
log.Debugf("qtext: %v", qtext)
var geomData []byte
err = db.QueryRow(qtext).Scan(&geomData)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("layer '%v' with custom SQL has 0 rows: %v", layerName, customSQL)
} else if err != nil {
return nil, fmt.Errorf("layer '%v' problem executing custom SQL: %v", layerName, err)
}
h, geo, err := decodeGeometry(geomData)
if err != nil {
return nil, err
}
layer.geomType = geo
layer.srid = uint64(h.SRSId())
layer.geomFieldname = DefaultGeomFieldName
layer.idFieldname = DefaultIDFieldName
}
p.layers[layer.name] = layer
}
// track the provider so we can clean it up later
providers = append(providers, p)
return &p, err
}
// reference to all instantiated providers
var providers []Provider
// Cleanup will close all database connections and destroy all previously instantiated Provider instances
func Cleanup() {
if len(providers) > 0 {
log.Infof("cleaning up gpkg providers")
}
for i := range providers {
if err := providers[i].Close(); err != nil {
log.Errorf("err closing connection: %v", err)
}
}
providers = make([]Provider, 0)
}