/
geojson.go
executable file
·194 lines (136 loc) · 3.65 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
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
package tables
import (
"context"
"fmt"
"github.com/aaronland/go-sqlite"
"github.com/whosonfirst/go-whosonfirst-feature/alt"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-sqlite-features"
)
type GeoJSONTableOptions struct {
IndexAltFiles bool
AllowMissingSourceGeom bool
}
func DefaultGeoJSONTableOptions() (*GeoJSONTableOptions, error) {
opts := GeoJSONTableOptions{
IndexAltFiles: false,
AllowMissingSourceGeom: true,
}
return &opts, nil
}
type GeoJSONTable struct {
features.FeatureTable
name string
options *GeoJSONTableOptions
}
type GeoJSONRow struct {
Id int64
Body string
LastModified int64
}
func NewGeoJSONTableWithDatabase(ctx context.Context, db sqlite.Database) (sqlite.Table, error) {
opts, err := DefaultGeoJSONTableOptions()
if err != nil {
return nil, err
}
return NewGeoJSONTableWithDatabaseAndOptions(ctx, db, opts)
}
func NewGeoJSONTableWithDatabaseAndOptions(ctx context.Context, db sqlite.Database, opts *GeoJSONTableOptions) (sqlite.Table, error) {
t, err := NewGeoJSONTableWithOptions(ctx, opts)
if err != nil {
return nil, err
}
err = t.InitializeTable(ctx, db)
if err != nil {
return nil, err
}
return t, nil
}
func NewGeoJSONTable(ctx context.Context) (sqlite.Table, error) {
opts, err := DefaultGeoJSONTableOptions()
if err != nil {
return nil, err
}
return NewGeoJSONTableWithOptions(ctx, opts)
}
func NewGeoJSONTableWithOptions(ctx context.Context, opts *GeoJSONTableOptions) (sqlite.Table, error) {
t := GeoJSONTable{
name: "geojson",
options: opts,
}
return &t, nil
}
func (t *GeoJSONTable) Name() string {
return t.name
}
func (t *GeoJSONTable) Schema() string {
sql := `CREATE TABLE %s (
id INTEGER NOT NULL,
body TEXT,
source TEXT,
is_alt BOOLEAN,
alt_label TEXT,
lastmodified INTEGER
);
CREATE UNIQUE INDEX geojson_by_id ON %s (id, source, alt_label);
CREATE INDEX geojson_by_alt ON %s (id, is_alt, alt_label);
CREATE INDEX geojson_by_lastmod ON %s (lastmodified);
`
return fmt.Sprintf(sql, t.Name(), t.Name(), t.Name(), t.Name())
}
func (t *GeoJSONTable) InitializeTable(ctx context.Context, db sqlite.Database) error {
return sqlite.CreateTableIfNecessary(ctx, db, t)
}
func (t *GeoJSONTable) IndexRecord(ctx context.Context, db sqlite.Database, i interface{}) error {
return t.IndexFeature(ctx, db, i.([]byte))
}
func (t *GeoJSONTable) IndexFeature(ctx context.Context, db sqlite.Database, f []byte) error {
is_alt := alt.IsAlt(f)
if is_alt && !t.options.IndexAltFiles {
return nil
}
id, err := properties.Id(f)
if err != nil {
return MissingPropertyError(t, "id", err)
}
source, err := properties.Source(f)
if err != nil {
if !t.options.AllowMissingSourceGeom {
return MissingPropertyError(t, "source", err)
}
source = "unknown"
}
alt_label, err := properties.AltLabel(f)
if err != nil {
return MissingPropertyError(t, "alt label", err)
}
lastmod := properties.LastModified(f)
conn, err := db.Conn()
if err != nil {
return DatabaseConnectionError(t, err)
}
tx, err := conn.Begin()
if err != nil {
return BeginTransactionError(t, err)
}
sql := fmt.Sprintf(`INSERT OR REPLACE INTO %s (
id, body, source, is_alt, alt_label, lastmodified
) VALUES (
?, ?, ?, ?, ?, ?
)`, t.Name())
stmt, err := tx.Prepare(sql)
if err != nil {
return PrepareStatementError(t, err)
}
defer stmt.Close()
str_body := string(f)
_, err = stmt.Exec(id, str_body, source, is_alt, alt_label, lastmod)
if err != nil {
return ExecuteStatementError(t, err)
}
err = tx.Commit()
if err != nil {
return CommitTransactionError(t, err)
}
return nil
}