-
Notifications
You must be signed in to change notification settings - Fork 6
/
postgis_helpers.go
63 lines (54 loc) · 1.27 KB
/
postgis_helpers.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
package tilenol
import (
"database/sql"
"errors"
"github.com/paulmach/orb/encoding/wkb"
)
var (
InvalidGeometryErr = errors.New("Column value was not a valid geometry")
)
// RowsToMaps converts Go's sql.Rows data structure into a list of maps where the key is a string
// column name, and the value is the raw SQL value
func RowsToMaps(rows *sql.Rows, geomColumn string) ([]map[string]interface{}, error) {
var maps []map[string]interface{}
cols, err := rows.Columns()
if err != nil {
return nil, err
}
for rows.Next() {
row := make([]interface{}, len(cols))
for idx, col := range cols {
if col == geomColumn {
row[idx] = new(wkb.GeometryScanner)
} else {
row[idx] = new(DumbScanner)
}
}
err := rows.Scan(row...)
if err != nil {
return maps, err
}
m := make(map[string]interface{})
for idx, col := range cols {
if geom, isGeomScanner := row[idx].(*wkb.GeometryScanner); isGeomScanner {
if geom.Valid {
m[col] = geom.Geometry
} else {
return nil, InvalidGeometryErr
}
} else {
ds := row[idx].(*DumbScanner)
m[col] = ds.Value
}
}
maps = append(maps, m)
}
return maps, nil
}
type DumbScanner struct {
Value interface{}
}
func (d *DumbScanner) Scan(src interface{}) error {
d.Value = src
return nil
}