/
geojson.go
executable file
·123 lines (86 loc) · 2.57 KB
/
geojson.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
package tables
import (
"context"
"database/sql"
"fmt"
wof_sql "github.com/whosonfirst/go-whosonfirst-database-sql"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
wof_tables "github.com/whosonfirst/go-whosonfirst-sql/tables"
"github.com/whosonfirst/go-whosonfirst-uri"
)
type GeoJSONTable struct {
wof_sql.Table
}
func NewGeoJSONTableWithDatabase(ctx context.Context, db wof_sql.Database) (wof_sql.Table, error) {
t, err := NewGeoJSONTable(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to create GeoJSON table, %w", err)
}
err = t.InitializeTable(ctx, db)
if err != nil {
return nil, fmt.Errorf("Failed to initialize GeoJSON table, %w", err)
}
return t, nil
}
func NewGeoJSONTable(ctx context.Context) (wof_sql.Table, error) {
t := GeoJSONTable{}
return &t, nil
}
func (t *GeoJSONTable) Name() string {
return wof_tables.GEOJSON_TABLE_NAME
}
func (t *GeoJSONTable) Schema() string {
s, _ := wof_tables.LoadSchema("mysql", wof_tables.GEOJSON_TABLE_NAME)
return s
}
func (t *GeoJSONTable) InitializeTable(ctx context.Context, db wof_sql.Database) error {
return wof_sql.CreateTableIfNecessary(ctx, db, t)
}
func (t *GeoJSONTable) IndexRecord(ctx context.Context, db wof_sql.Database, i interface{}, custom ...interface{}) error {
conn, err := db.Conn()
if err != nil {
return fmt.Errorf("Failed to establish database connection, %w", err)
}
tx, err := conn.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return fmt.Errorf("Failed to create transaction, %w", err)
}
err = t.IndexFeature(ctx, tx, i.([]byte), custom...)
if err != nil {
tx.Rollback()
return fmt.Errorf("Failed to index %s table, %w", t.Name(), err)
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("Failed to commit transaction, %w", err)
}
return nil
}
func (t *GeoJSONTable) IndexFeature(ctx context.Context, tx *sql.Tx, body []byte, custom ...interface{}) error {
id, err := properties.Id(body)
if err != nil {
return fmt.Errorf("Failed to derive ID, %w", err)
}
var alt *uri.AltGeom
if len(custom) >= 1 {
alt = custom[0].(*uri.AltGeom)
}
lastmod := properties.LastModified(body)
str_alt := ""
if alt != nil {
str_alt, err = alt.String()
if err != nil {
return fmt.Errorf("Failed to stringify alt, %w", err)
}
}
q := fmt.Sprintf(`REPLACE INTO %s (
id, alt, body, lastmodified
) VALUES (
?, ?, ?, ?
)`, wof_tables.GEOJSON_TABLE_NAME)
_, err = tx.ExecContext(ctx, q, id, str_alt, string(body), lastmod)
if err != nil {
return fmt.Errorf("Failed to update geojson table, %w", err)
}
return nil
}