sqlite

package module
v0.15.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2025 License: BSD-3-Clause Imports: 30 Imported by: 9

README

go-whosonfirst-spatial-sqlite

SQLite-backed implementation of the go-whosonfirst-spatial interfaces.

Important

This is work in progress. It may change still. The goal is to have a package that conforms to the database.SpatialDatabase interface using mattn/go-sqlite3 and SQLite's RTree extension.

Documentation

Go Reference

Documentation is incomplete.

Databases

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

  • rtree - this table is used to perform point-in-polygon spatial queries.
  • spr - this table is used to generate standard place response (SPR) results.
  • properties - this table is used to append extra properties (to the SPR response) for spatial.PropertiesResponseResults responses.
  • geojson - this table is used to satisfy the whosonfirst/go-reader.Reader requirements in the spatial.SpatialDatabase interface. It is meant to be a simple ID to bytes (or filehandle) lookup rather than a data structure that is parsed or queried.

Here's an example of the creating a compatible SQLite database for all the administative data in Canada using the wof-sqlite-index tool which is part of the go-whosonfirst-database-sqlite package:

$> ./bin/wof-sqlite-index \
	-index-alt-files \
	-spatial-tables \
	-timings \
	-database-uri 'sql://sqlite3?dsn=/usr/local/ca-alt.db' \
	-iterator-uri repo:// \
	/usr/local/data/whosonfirst-data-admin-ca/

13:09:44.642004 [wof-sqlite-index-features] STATUS time to index rtree (11860) : 30.469010289s
13:09:44.642136 [wof-sqlite-index-features] STATUS time to index geometry (11860) : 5.155172377s
13:09:44.642141 [wof-sqlite-index-features] STATUS time to index properties (11860) : 4.631908497s
13:09:44.642143 [wof-sqlite-index-features] STATUS time to index spr (11860) : 19.160260741s
13:09:44.642146 [wof-sqlite-index-features] STATUS time to index all (11860) : 1m0.000182571s
13:10:44.642848 [wof-sqlite-index-features] STATUS time to index spr (32724) : 39.852608874s
13:10:44.642861 [wof-sqlite-index-features] STATUS time to index rtree (32724) : 57.361318918s
13:10:44.642864 [wof-sqlite-index-features] STATUS time to index geometry (32724) : 10.242155898s
13:10:44.642868 [wof-sqlite-index-features] STATUS time to index properties (32724) : 10.815961878s
13:10:44.642871 [wof-sqlite-index-features] STATUS time to index all (32724) : 2m0.000429956s

And then...

$> ./bin/pip \
	-database-uri 'sqlite://sqlite3?dsn=/usr/local/data/ca-alt.db' \
	-latitude 45.572744 \
	-longitude -73.586295
| jq \
| grep wof:id

2020/12/16 13:25:32 Time to point in polygon, 395.201983ms
      "wof:id": "85633041",
      "wof:id": "85874359",
      "wof:id": "1108955735",
      "wof:id": "85874359",
      "wof:id": "85633041",
      "wof:id": "890458661",
      "wof:id": "136251273",
      "wof:id": "136251273",
      "wof:id": "85633041",
      "wof:id": "136251273",
      "wof:id": "85633041",

TBW: Indexing tables on start-up.

Database URIs and "drivers"

Database URIs for the go-whosonfirst-spatial-sqlite package take the form of:

"sqlite://" + {DATABASE_SQL_ENGINE} + "?dsn=" + {DATABASE_SQL_DSN}

Where DATABASE_SQL refers to the build-in database/sql package.

For example:

sqlite://sqlite3?dsn=test.db

By default this package bundles support for the mattn/go-sqlite3 driver but does NOT enable it by default. You will need to pass in the -tag mattn argument when building tools to enable it. This is the default behaviour in the cli Makefile target for building binary tools.

If you want or need to use the modernc.org/sqlite driver take a look at the database_mattn.go file for an example of how you might go about enabling it. As of this writing the modernc.org/sqlite package is not bundled with this package because it adds ~200MB of code to the vendor directory.

Example

package main

import (
	"context"
	"encoding/json"
	"fmt"

	_ "github.com/mattn/go-sqlite3"
	_ "github.com/whosonfirst/go-whosonfirst-spatial-sqlite"
	
	"github.com/whosonfirst/go-whosonfirst-spatial/database"
	"github.com/whosonfirst/go-whosonfirst-spatial/filter"
	"github.com/whosonfirst/go-whosonfirst-spatial/geo"
	"github.com/whosonfirst/go-whosonfirst-spatial/properties"
	"github.com/whosonfirst/go-whosonfirst-spr"
)

func main() {

	database_uri := "sqlite://sqlite3?dsn=whosonfirst.db"
	properties_uri := "sqlite://sqlite3?dsn=whosonfirst.db"
	latitude := 37.616951
	longitude := -122.383747

	props := []string{
		"wof:concordances",
		"wof:hierarchy",
		"sfomuseum:*",
	}

	ctx := context.Background()
	
	db, _ := database.NewSpatialDatabase(ctx, *database_uri)
	pr, _ := properties.NewPropertiesReader(ctx, *properties_uri)
	
	c, _ := geo.NewCoordinate(*longitude, *latitude)
	f, _ := filter.NewSPRFilter()
	r, _ := db.PointInPolygon(ctx, c, f)

	r, _ = pr.PropertiesResponseResultsWithStandardPlacesResults(ctx, r, props)

	enc, _ := json.Marshal(r)
	fmt.Println(string(enc))
}

Error handling removed for the sake of brevity.

Filters

To be written

Tools

$> make cli
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/http-server cmd/http-server/main.go
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/grpc-server cmd/grpc-server/main.go
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/grpc-client cmd/grpc-client/main.go
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/update-hierarchies cmd/update-hierarchies/main.go
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/pip cmd/pip/main.go
go build -tags mattn -ldflags="-s -w" -mod vendor -o bin/intersects cmd/intersects/main.go
pip

Documentation for the pip tool has been moved in to cmd/pip/README.md

intersects

Documentation for the pip tool can be found in cmd/intersects/README.md

http-server

Documentation for the pip tool has been moved in to cmd/http-server/README.md

grpc-server

Documentation for the pip tool has been moved in to cmd/grpc-server/README.md

grpc-client

Documentation for the pip tool has been moved in to cmd/grpc-client/README.md

See also

Documentation

Overview

package sqlite provides methods for indexing and performing spatial queries on data stored in a SQLite databases from tables defined by the `whosonfirst/go-whosonfirst-sqlite-features/tables` package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSQLiteSpatialDatabase

func NewSQLiteSpatialDatabase(ctx context.Context, uri string) (database.SpatialDatabase, error)

NewSQLiteSpatialDatabase returns a new `whosonfirst/go-whosonfirst-spatial/database.database.SpatialDatabase` instance for performing spatial operations derived from 'uri'.

func NewSQLiteSpatialDatabaseReader added in v0.5.5

func NewSQLiteSpatialDatabaseReader(ctx context.Context, uri string) (reader.Reader, error)

func NewSQLiteSpatialDatabaseWithDatabase added in v0.0.20

func NewSQLiteSpatialDatabaseWithDatabase(ctx context.Context, uri string, db *sql.DB) (database.SpatialDatabase, error)

NewSQLiteSpatialDatabaseWithDatabase returns a new `whosonfirst/go-whosonfirst-spatial/database.database.SpatialDatabase` instance for performing spatial operations derived from 'uri' and an existing `aaronland/go-sqlite/database.SQLiteDatabase` instance defined by 'sqlite_db'.

func NewSQLiteSpatialDatabaseWriter added in v0.5.5

func NewSQLiteSpatialDatabaseWriter(ctx context.Context, uri string) (writer.Writer, error)

Types

type RTreeSpatialIndex

type RTreeSpatialIndex struct {
	Id        string
	FeatureId string
	// A boolean flag indicating whether the feature associated with the index is an alternate geometry.
	IsAlt bool
	// The label for the feature (associated with the index) if it is an alternate geometry.
	AltLabel string
	// contains filtered or unexported fields
}

RTreeSpatialIndex is a struct representing an RTree based spatial index

func (RTreeSpatialIndex) Bounds

func (sp RTreeSpatialIndex) Bounds() orb.Bound

func (RTreeSpatialIndex) Path added in v0.0.6

func (sp RTreeSpatialIndex) Path() string

type SQLiteResults

type SQLiteResults struct {
	spr.StandardPlacesResults `json:",omitempty"`
	// Places is the list of `whosonfirst/go-whosonfirst-spr.StandardPlacesResult` instances returned for a spatial query.
	Places []spr.StandardPlacesResult `json:"places"`
}

SQLiteResults is a struct that implements the `whosonfirst/go-whosonfirst-spr.StandardPlacesResults` interface for rows matching a spatial query.

func (*SQLiteResults) Results

func (r *SQLiteResults) Results() []spr.StandardPlacesResult

Results returns a `whosonfirst/go-whosonfirst-spr.StandardPlacesResults` instance for rows matching a spatial query.

type SQLiteSpatialDatabase

type SQLiteSpatialDatabase struct {
	database.SpatialDatabase
	// contains filtered or unexported fields
}

SQLiteSpatialDatabase is a struct that implements the `database.SpatialDatabase` for performing spatial queries on data stored in a SQLite databases from tables defined by the `whosonfirst/go-whosonfirst-sqlite-features/tables` package.

func (*SQLiteSpatialDatabase) Close

Close implements the whosonfirst/go-writer interface so that the database itself can be used as a writer.Writer instance. This method is a no-op and simply returns `nil`.

func (*SQLiteSpatialDatabase) Disconnect added in v0.0.28

func (r *SQLiteSpatialDatabase) Disconnect(ctx context.Context) error

Disconnect will close the underlying database connection.

func (*SQLiteSpatialDatabase) Exists added in v0.15.1

func (r *SQLiteSpatialDatabase) Exists(ctx context.Context, str_uri string) (bool, error)

Exists returns a boolean value indicating whether 'str_uri` exists.

func (*SQLiteSpatialDatabase) Flush added in v0.5.2

Flush implements the whosonfirst/go-writer interface so that the database itself can be used as a writer.Writer instance. This method is a no-op and simply returns `nil`.

func (*SQLiteSpatialDatabase) IndexFeature

func (r *SQLiteSpatialDatabase) IndexFeature(ctx context.Context, body []byte) error

IndexFeature will index a Who's On First GeoJSON Feature record, defined in 'body', in the spatial database.

func (*SQLiteSpatialDatabase) Intersects added in v0.13.0

func (db *SQLiteSpatialDatabase) Intersects(ctx context.Context, geom orb.Geometry, filters ...spatial.Filter) (spr.StandardPlacesResults, error)

func (*SQLiteSpatialDatabase) IntersectsWithIterator added in v0.13.0

func (db *SQLiteSpatialDatabase) IntersectsWithIterator(ctx context.Context, geom orb.Geometry, filters ...spatial.Filter) iter.Seq2[spr.StandardPlacesResult, error]

func (*SQLiteSpatialDatabase) PointInPolygon

func (db *SQLiteSpatialDatabase) PointInPolygon(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) (spr.StandardPlacesResults, error)

PointInPolygon will perform a point in polygon query against the database for records that contain 'coord' and that are inclusive of any filters defined by 'filters'.

func (*SQLiteSpatialDatabase) PointInPolygonWithIterator added in v0.13.0

func (db *SQLiteSpatialDatabase) PointInPolygonWithIterator(ctx context.Context, coord *orb.Point, filters ...spatial.Filter) iter.Seq2[spr.StandardPlacesResult, error]

func (*SQLiteSpatialDatabase) Read added in v0.0.24

Read implements the whosonfirst/go-reader interface so that the database itself can be used as a reader.Reader instance (reading features from the `geojson` table.

func (*SQLiteSpatialDatabase) ReaderURI added in v0.0.28

func (r *SQLiteSpatialDatabase) ReaderURI(ctx context.Context, str_uri string) string

ReadURI implements the whosonfirst/go-reader interface so that the database itself can be used as a reader.Reader instance

func (*SQLiteSpatialDatabase) RemoveFeature added in v0.2.0

func (r *SQLiteSpatialDatabase) RemoveFeature(ctx context.Context, str_id string) error

RemoveFeature will remove the database record with ID 'id' from the database.

func (*SQLiteSpatialDatabase) SetLogger added in v0.5.2

func (r *SQLiteSpatialDatabase) SetLogger(ctx context.Context, logger *log.Logger) error

SetLogger implements the whosonfirst/go-writer interface so that the database itself can be used as a writer.Writer instance. This method is a no-op and simply returns `nil`.

func (*SQLiteSpatialDatabase) Write added in v0.0.28

func (r *SQLiteSpatialDatabase) Write(ctx context.Context, key string, fh io.ReadSeeker) (int64, error)

Write implements the whosonfirst/go-writer interface so that the database itself can be used as a writer.Writer instance (by invoking the `IndexFeature` method).

func (*SQLiteSpatialDatabase) WriterURI added in v0.0.28

func (r *SQLiteSpatialDatabase) WriterURI(ctx context.Context, str_uri string) string

WriterURI implements the whosonfirst/go-writer interface so that the database itself can be used as a writer.Writer instance

Directories

Path Synopsis
cmd
grpc-client command
grpc-server command
http-server command
intersects command
pip command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL