-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpkg.go
249 lines (215 loc) · 5.04 KB
/
gpkg.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
package gpkg
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"github.com/golang/geo/s2"
"github.com/peterstace/simplefeatures/geom"
"github.com/smilyorg/tinygpkg/binary"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
var ErrNotFound = errors.New("not found")
var poolSize = 10
var skipValidationOpts = []geom.ConstructorOption{
geom.DisableAllValidations,
}
type Direction string
type FeatureId int64
const (
Asc Direction = "ASC"
Desc Direction = "DESC"
)
type Order struct {
Column string
Direction Direction
}
type GeometryCache interface {
Get(fid FeatureId) (geom.Geometry, error)
Set(fid FeatureId, g geom.Geometry) error
}
type GeoPackage struct {
pool *sqlitex.Pool
table string
cols []string
colSelect string
Order Order
Validate bool
Cache GeometryCache
}
// Open opens a GeoPackage file at the specified path
//
// The table parameter specifies the name of the table to query,
// and the cols parameter specifies the columns to select.
// If table is an empty string, Open will attempt to auto-configure
// using the first table listed in "gpkg_contents".
//
// cols defines the columns of the table to return from ReverseGeocode.
//
// You can set the Cache field to a GeometryCache implementation to
// cache geometries in memory. This can be useful if you are querying
// similar locations multiple times.
// Warning: the table and columns are not sanitized, so they are prone
// to SQL injection attacks if provided by user input.
func Open(path, table string, cols []string) (*GeoPackage, error) {
g := &GeoPackage{}
var err error
g.table = table
g.cols = cols
g.pool, err = sqlitex.Open(
path,
sqlite.OpenReadOnly|sqlite.OpenURI,
poolSize,
)
if err != nil {
return nil, err
}
if g.table == "" {
if err := g.autoconfTable(); err != nil {
g.Close()
return nil, err
}
}
if len(g.cols) == 0 {
g.Close()
return nil, errors.New("no columns specified")
}
g.colSelect = g.cols[0]
for i := 1; i < len(g.cols); i++ {
g.colSelect += ", " + g.cols[i]
}
return g, nil
}
func (g *GeoPackage) autoconfTable() error {
conn := g.pool.Get(context.Background())
defer g.pool.Put(conn)
sql := `
SELECT table_name
FROM gpkg_contents
LIMIT 1`
stmt := conn.Prep(sql)
defer stmt.Reset()
if exists, err := stmt.Step(); err != nil {
return fmt.Errorf("error auto-configuring table: %w", err)
} else if !exists {
return errors.New("error auto-configuring table: no table found")
}
g.table = stmt.ColumnText(0)
if g.table == "" {
return errors.New("error auto-configuring table: table name is empty")
}
return nil
}
func (g *GeoPackage) Close() error {
if g == nil || g.pool == nil {
return nil
}
err := g.pool.Close()
if err != nil {
return fmt.Errorf("error closing geopackage: %w", err)
}
g.pool = nil
return nil
}
func (g *GeoPackage) ReverseGeocode(ctx context.Context, l s2.LatLng) ([]string, error) {
conn := g.pool.Get(ctx)
defer g.pool.Put(conn)
sql := `
SELECT fid, geom, ` + g.colSelect + `
FROM ` + g.table + `
WHERE fid IN (
SELECT id
FROM rtree_` + g.table + `_geom
WHERE
:x >= minx AND :x <= maxx AND
:y >= miny AND :y <= maxy
)`
if g.Order.Column != "" {
if g.Order.Direction != Asc && g.Order.Direction != Desc {
return nil, errors.New("invalid order direction")
}
sql += ` ORDER BY ` + g.Order.Column + ` ` + string(g.Order.Direction)
}
stmt := conn.Prep(sql)
defer stmt.Reset()
stmt.BindFloat(1, l.Lng.Degrees())
stmt.BindFloat(2, l.Lat.Degrees())
var opts []geom.ConstructorOption
if !g.Validate {
opts = skipValidationOpts
}
for {
if exists, err := stmt.Step(); err != nil {
return nil, err
} else if !exists {
break
}
fid := FeatureId(stmt.ColumnInt64(0))
var gm geom.Geometry
read := true
if g.Cache != nil {
var err error
gm, err = g.Cache.Get(fid)
if err == nil {
read = false
}
}
if read {
var err error
r := stmt.ColumnReader(1)
gm, err = readGeometry(r, opts)
if err != nil {
return nil, err
}
if g.Cache != nil {
g.Cache.Set(fid, gm)
}
}
p, err := geom.NewPoint(geom.Coordinates{
XY: geom.XY{
X: l.Lng.Degrees(),
Y: l.Lat.Degrees(),
},
})
if err != nil {
return nil, err
}
contains := geom.Intersects(gm, p.AsGeometry())
if contains {
cols := make([]string, len(g.cols))
for i := 0; i < len(g.cols); i++ {
cols[i] = stmt.ColumnText(2 + i)
}
return cols, nil
}
}
return nil, ErrNotFound
}
func readGeometry(r io.Reader, opts []geom.ConstructorOption) (geom.Geometry, error) {
var g geom.Geometry
h, err := binary.Read(r)
if err != nil {
return g, err
}
if h.Empty() {
return g, nil
}
b, err := io.ReadAll(r)
if err != nil {
return g, err
}
switch {
case h.Type() == binary.StandardType:
g, err = geom.UnmarshalWKB(b, opts...)
case h.Type() == binary.ExtendedType && bytes.Equal(h.ExtensionCode, binary.ExtensionTWKB):
g, err = geom.UnmarshalTWKB(b, opts...)
default:
return g, errors.New("unsupported geometry type")
}
if err != nil {
return g, err
}
return g, nil
}