Skip to content

Commit ec55d44

Browse files
author
thisisaaronland
committed
update to reflect changes to go-whosonfirst-spatial interfaces
1 parent f99ce90 commit ec55d44

File tree

18 files changed

+74
-1007
lines changed

18 files changed

+74
-1007
lines changed

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ This is work in progress. It may change, probably has bugs and isn't properly do
66

77
The goal is to have a package that conforms to the [database.SpatialDatabase](https://github.com/whosonfirst/go-whosonfirst-spatial#spatialdatabase) interface using [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) and SQLite's [RTree](https://www.sqlite.org/rtree.html) extension.
88

9-
Also, this is not as fast as it should be. This is largely with the way WOF records are inflated and passed around in order to support GeoJSON output. There is [an open ticket](https://github.com/whosonfirst/go-whosonfirst-spatial-sqlite/issues/2) to address this.
10-
119
## Databases
1210

1311
This code depends on (4) tables as indexed by the `go-whosonfirst-sqlite-features` package:
1412

1513
* [rtree](https://github.com/whosonfirst/go-whosonfirst-sqlite-features#rtree) - this table is used to perform point-in-polygon spatial queries.
1614
* [spr](https://github.com/whosonfirst/go-whosonfirst-sqlite-features#spr) - this table is used to generate [standard place response](#) (SPR) results.
17-
* [geometry](https://github.com/whosonfirst/go-whosonfirst-sqlite-features#geometry) - this table is used to append geometries to GeoJSON-formatted results.
18-
* [properties](https://github.com/whosonfirst/go-whosonfirst-sqlite-features#properties) - this table is used to append extra properties (to the SPR response) for GeoJSON-formatted results.
19-
20-
The `go-whosonfirst-sqlite-features` package also indexes a `geojson` table but it turns out that retrieving, and parsing, properties and geometries from their own tables is faster.
15+
* [properties](https://github.com/whosonfirst/go-whosonfirst-sqlite-features#properties) - this table is used to append extra properties (to the SPR response) for `spatial.PropertiesResponseResults` responses.
2116

2217
Here's an example of the creating a compatible SQLite database for all the [administative data in Canada](https://github.com/whosonfirst-data/whosonfirst-data-admin-ca) using the `wof-sqlite-index-features` tool which is part of the [go-whosonfirst-sqlite-features-index](https://github.com/whosonfirst/go-whosonfirst-sqlite-features-index) package:
2318

@@ -61,18 +56,18 @@ $> ./bin/query \
6156
2020/12/15 15:32:06 Unable to parse placetype (alt) for ID 85633041, because 'Invalid placetype' - skipping placetype filters
6257
2020/12/15 15:32:06 Unable to parse placetype (alt) for ID 136251273, because 'Invalid placetype' - skipping placetype filters
6358
2020/12/15 15:32:06 Unable to parse placetype (alt) for ID 85633041, because 'Invalid placetype' - skipping placetype filters
64-
2020/12/15 15:32:06 Time to point in polygon, 596.579126ms
65-
"wof:id": "1108955735",
59+
2020/12/16 13:25:32 Time to point in polygon, 395.201983ms
60+
"wof:id": "85633041",
6661
"wof:id": "85874359",
62+
"wof:id": "1108955735",
6763
"wof:id": "85874359",
68-
"wof:id": "890458661",
6964
"wof:id": "85633041",
65+
"wof:id": "890458661",
7066
"wof:id": "136251273",
71-
"wof:id": "85633041",
72-
"wof:id": "85633041",
7367
"wof:id": "136251273",
7468
"wof:id": "85633041",
7569
"wof:id": "136251273",
70+
"wof:id": "85633041",
7671
```
7772

7873
_TBW: Indexing tables on start-up._
@@ -237,7 +232,7 @@ $> ./bin/query \
237232
Note: This assumes a database that was previously indexed using the [whosonfirst/go-whosonfirst-sqlite-features](https://github.com/whosonfirst/go-whosonfirst-sqlite-features) `wof-sqlite-index-features` tool. For example:
238233

239234
```
240-
$> ./bin/wof-sqlite-index-features -rtree -geojson -dsn /tmp/test.db -mode repo:// /usr/local/data/sfomuseum-data-architecture/
235+
$> ./bin/wof-sqlite-index-features -rtree -spr -properties -dsn /tmp/test.db -mode repo:// /usr/local/data/sfomuseum-data-architecture/
241236
```
242237

243238
## See also

database.go

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"github.com/skelterjohn/geom"
1212
wof_geojson "github.com/whosonfirst/go-whosonfirst-geojson-v2"
1313
"github.com/whosonfirst/go-whosonfirst-log"
14-
"github.com/whosonfirst/go-whosonfirst-spatial"
14+
"github.com/whosonfirst/go-whosonfirst-spatial"
1515
"github.com/whosonfirst/go-whosonfirst-spatial/database"
1616
"github.com/whosonfirst/go-whosonfirst-spatial/filter"
17-
"github.com/whosonfirst/go-whosonfirst-spatial/timer"
1817
"github.com/whosonfirst/go-whosonfirst-spatial/geo"
18+
"github.com/whosonfirst/go-whosonfirst-spatial/timer"
1919
"github.com/whosonfirst/go-whosonfirst-spr"
2020
"github.com/whosonfirst/go-whosonfirst-sqlite"
2121
"github.com/whosonfirst/go-whosonfirst-sqlite-features/tables"
@@ -34,16 +34,14 @@ func init() {
3434

3535
type SQLiteSpatialDatabase struct {
3636
database.SpatialDatabase
37-
Logger *log.WOFLogger
38-
mu *sync.RWMutex
39-
db *sqlite_database.SQLiteDatabase
40-
rtree_table sqlite.Table
41-
geometry_table sqlite.Table
42-
spr_table sqlite.Table
43-
gocache *gocache.Cache
44-
dsn string
45-
strict bool
46-
timer *timer.Timer
37+
Logger *log.WOFLogger
38+
Timer *timer.Timer
39+
mu *sync.RWMutex
40+
db *sqlite_database.SQLiteDatabase
41+
rtree_table sqlite.Table
42+
spr_table sqlite.Table
43+
gocache *gocache.Cache
44+
dsn string
4745
}
4846

4947
type RTreeSpatialIndex struct {
@@ -99,12 +97,6 @@ func NewSQLiteSpatialDatabase(ctx context.Context, uri string) (database.Spatial
9997
return nil, err
10098
}
10199

102-
geometry_table, err := tables.NewGeometryTableWithDatabase(sqlite_db)
103-
104-
if err != nil {
105-
return nil, err
106-
}
107-
108100
rtree_table, err := tables.NewRTreeTableWithDatabase(sqlite_db)
109101

110102
if err != nil {
@@ -117,12 +109,6 @@ func NewSQLiteSpatialDatabase(ctx context.Context, uri string) (database.Spatial
117109
return nil, err
118110
}
119111

120-
strict := true
121-
122-
if q.Get("strict") == "false" {
123-
strict = false
124-
}
125-
126112
logger := log.SimpleWOFLogger("index")
127113

128114
expires := 5 * time.Minute
@@ -135,16 +121,14 @@ func NewSQLiteSpatialDatabase(ctx context.Context, uri string) (database.Spatial
135121
t := timer.NewTimer()
136122

137123
spatial_db := &SQLiteSpatialDatabase{
138-
Logger: logger,
139-
db: sqlite_db,
140-
rtree_table: rtree_table,
141-
geometry_table: geometry_table,
142-
spr_table: spr_table,
143-
gocache: gc,
144-
dsn: dsn,
145-
strict: strict,
146-
mu: mu,
147-
timer: t,
124+
Logger: logger,
125+
Timer: t,
126+
db: sqlite_db,
127+
rtree_table: rtree_table,
128+
spr_table: spr_table,
129+
gocache: gc,
130+
dsn: dsn,
131+
mu: mu,
148132
}
149133

150134
return spatial_db, nil
@@ -170,12 +154,6 @@ func (r *SQLiteSpatialDatabase) IndexFeature(ctx context.Context, f wof_geojson.
170154
return err
171155
}
172156

173-
err = r.geometry_table.IndexRecord(r.db, f)
174-
175-
if err != nil {
176-
return err
177-
}
178-
179157
return nil
180158
}
181159

@@ -218,12 +196,14 @@ func (r *SQLiteSpatialDatabase) PointInPolygon(ctx context.Context, coord *geom.
218196
}
219197
}
220198

221-
for label, timings := range r.timer.Timings {
199+
/*
200+
for label, timings := range r.Timer.Timings {
222201
223-
for _, tm := range timings {
224-
golog.Printf("[%s] %s\n", label, tm)
202+
for _, tm := range timings {
203+
golog.Printf("[%s] %s\n", label, tm)
204+
}
225205
}
226-
}
206+
*/
227207

228208
spr_results := &SQLiteResults{
229209
Places: results,
@@ -301,14 +281,14 @@ func (r *SQLiteSpatialDatabase) PointInPolygonCandidatesWithChannels(ctx context
301281
for _, sp := range intersects {
302282

303283
bounds := sp.Bounds()
304-
284+
305285
c := &spatial.PointInPolygonCandidate{
306-
Id: sp.Id,
307-
WOFId: sp.WOFId,
286+
Id: sp.Id,
287+
WOFId: sp.WOFId,
308288
AltLabel: sp.AltLabel,
309-
Bounds: &bounds,
289+
Bounds: &bounds,
310290
}
311-
291+
312292
rsp_ch <- c
313293
}
314294

@@ -441,7 +421,7 @@ func (r *SQLiteSpatialDatabase) inflateSpatialIndexWithChannels(ctx context.Cont
441421
t1 := time.Now()
442422

443423
defer func() {
444-
r.timer.Add(ctx, sp_id, "time to inflate", time.Since(t1))
424+
r.Timer.Add(ctx, sp_id, "time to inflate", time.Since(t1))
445425
}()
446426

447427
// have we already looked up the filters for this ID?
@@ -458,12 +438,12 @@ func (r *SQLiteSpatialDatabase) inflateSpatialIndexWithChannels(ctx context.Cont
458438
t2 := time.Now()
459439

460440
// this needs to be sped up (20201216/thisisaaronland)
461-
441+
462442
var coords [][][]float64
463443

464444
err := json.Unmarshal([]byte(sp.geometry), &coords)
465445

466-
r.timer.Add(ctx, sp_id, "time to unmarshal geometry", time.Since(t2))
446+
r.Timer.Add(ctx, sp_id, "time to unmarshal geometry", time.Since(t2))
467447

468448
if err != nil {
469449
err_ch <- err
@@ -481,7 +461,7 @@ func (r *SQLiteSpatialDatabase) inflateSpatialIndexWithChannels(ctx context.Cont
481461
return
482462
}
483463

484-
r.timer.Add(ctx, sp_id, "time to perform contains test", time.Since(t3))
464+
r.Timer.Add(ctx, sp_id, "time to perform contains test", time.Since(t3))
485465

486466
// there is at least one ring that contains the coord
487467
// now we check the filters - whether or not they pass
@@ -501,7 +481,7 @@ func (r *SQLiteSpatialDatabase) inflateSpatialIndexWithChannels(ctx context.Cont
501481
return
502482
}
503483

504-
r.timer.Add(ctx, sp_id, "time to retrieve SPR", time.Since(t4))
484+
r.Timer.Add(ctx, sp_id, "time to retrieve SPR", time.Since(t4))
505485

506486
if err != nil {
507487
r.Logger.Error("Failed to retrieve feature cache for %s, %v", sp_id, err)
@@ -520,7 +500,7 @@ func (r *SQLiteSpatialDatabase) inflateSpatialIndexWithChannels(ctx context.Cont
520500
}
521501
}
522502

523-
r.timer.Add(ctx, sp_id, "time to filter SPR", time.Since(t5))
503+
r.Timer.Add(ctx, sp_id, "time to filter SPR", time.Since(t5))
524504

525505
rsp_ch <- s
526506
}
@@ -532,7 +512,7 @@ func (r *SQLiteSpatialDatabase) retrieveSPR(ctx context.Context, uri_str string)
532512
if ok {
533513
return c.(*SQLiteStandardPlacesResult), nil
534514
}
535-
515+
536516
id, uri_args, err := uri.ParseURI(uri_str)
537517

538518
if err != nil {

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ go 1.12
44

55
require (
66
github.com/patrickmn/go-cache v2.1.0+incompatible
7-
github.com/paulmach/go.geojson v1.4.0
87
github.com/sfomuseum/go-flags v0.4.2
98
github.com/skelterjohn/geom v0.0.0-20180103142417-96f3e8a219c5
10-
github.com/tidwall/gjson v1.6.4
119
github.com/whosonfirst/go-whosonfirst-flags v0.1.0
1210
github.com/whosonfirst/go-whosonfirst-geojson-v2 v0.14.0
1311
github.com/whosonfirst/go-whosonfirst-log v0.1.0
14-
github.com/whosonfirst/go-whosonfirst-spatial v0.0.13
12+
github.com/whosonfirst/go-whosonfirst-spatial v0.0.14
1513
github.com/whosonfirst/go-whosonfirst-spr v0.1.0
1614
github.com/whosonfirst/go-whosonfirst-sqlite v0.1.6
17-
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.2
15+
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.3
1816
github.com/whosonfirst/go-whosonfirst-uri v0.2.0
1917
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ github.com/whosonfirst/go-whosonfirst-spatial v0.0.12 h1:Hl++UT0fye7f4c9/IxI7pAX
141141
github.com/whosonfirst/go-whosonfirst-spatial v0.0.12/go.mod h1:PywJosSLB0RCNgTPyWgQ+gMjxRj8Uw3hX3zPa+JZKug=
142142
github.com/whosonfirst/go-whosonfirst-spatial v0.0.13 h1:q1jf79ahX+4yGfmwMwWx9TYScq9AQOR2HXjdVmfUoQ0=
143143
github.com/whosonfirst/go-whosonfirst-spatial v0.0.13/go.mod h1:+zpBqUkaRifbarW9f3GIahiD87H+aNkJU3RsfRA/RxA=
144+
github.com/whosonfirst/go-whosonfirst-spatial v0.0.14 h1:BwAuGx+zp793p7t9TDM5HC3HNe10MdX2FQaMiloQ4N0=
145+
github.com/whosonfirst/go-whosonfirst-spatial v0.0.14/go.mod h1:+zpBqUkaRifbarW9f3GIahiD87H+aNkJU3RsfRA/RxA=
144146
github.com/whosonfirst/go-whosonfirst-spr v0.1.0 h1:5qE629nCiucF2upy5NjPOEl9cFatsljykYY0l2JKgAk=
145147
github.com/whosonfirst/go-whosonfirst-spr v0.1.0/go.mod h1:R8GtEVz1GVSnwwOjzcoVUd172ZK26Q7hQSLI6SGG7lM=
146148
github.com/whosonfirst/go-whosonfirst-sqlite v0.1.6 h1:XhAlLoPm7y/4565du5H7R5Swjf/pBl+cuXHoAs6evLA=
@@ -149,6 +151,8 @@ github.com/whosonfirst/go-whosonfirst-sqlite-features v0.4.0 h1:hogsGiJCxzyHo7L+
149151
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.4.0/go.mod h1:FG3V5mkagABuTB5hOeLm1LKq80pSJpW67TdC4vNPMaY=
150152
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.2 h1:7eWyeIwsHZTeMmSdmRVuj6X9bvjEUamE2caDrOZ0uXw=
151153
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.2/go.mod h1:jZXXBBarIOYmkFl/LiKPjE4dQEunUZjV4fGqQImnXYQ=
154+
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.3 h1:1FH//dWDsBUr1ZVC6CbGj+Y7TDU44SvKs8UiZc3tr80=
155+
github.com/whosonfirst/go-whosonfirst-sqlite-features v0.6.3/go.mod h1:jZXXBBarIOYmkFl/LiKPjE4dQEunUZjV4fGqQImnXYQ=
152156
github.com/whosonfirst/go-whosonfirst-uri v0.1.0/go.mod h1:8eaDVcc4v+HHHEDaRbApdmhPwM4/JQllw2PktvZcPVs=
153157
github.com/whosonfirst/go-whosonfirst-uri v0.2.0 h1:iODHdyvW+8IXqHZTixZ/9GEZy1dVKGj6dMRg7fn0d2M=
154158
github.com/whosonfirst/go-whosonfirst-uri v0.2.0/go.mod h1:8eaDVcc4v+HHHEDaRbApdmhPwM4/JQllw2PktvZcPVs=

0 commit comments

Comments
 (0)