Skip to content

Instantly share code, notes, and snippets.

@thisisaaronland
Last active August 31, 2022 01:05
Show Gist options
  • Save thisisaaronland/e2f60fc6f06c74803c7aceb0c8786956 to your computer and use it in GitHub Desktop.
Save thisisaaronland/e2f60fc6f06c74803c7aceb0c8786956 to your computer and use it in GitHub Desktop.
$> git clone [email protected]:whosonfirst-data/whosonfirst-data-admin-gb.git /usr/local/data/whosonfirst-data-admin-gb
$> cd /usr/local/whosonfirst/go-whosonfirst-sqlite-features-index
$> ./bin/wof-sqlite-index-features -dsn /usr/local/data/gb.db -timings -spatial-tables /usr/local/data/whosonfirst-data-admin-gb
Then in `cmd/test/main.go`:
```
package main
import (
"context"
"log"
"io"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/iterator"
"github.com/whosonfirst/go-whosonfirst-spatial-hierarchy"
"github.com/whosonfirst/go-whosonfirst-spatial/database"
"github.com/whosonfirst/go-whosonfirst-spatial/filter"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"fmt"
"flag"
_ "github.com/whosonfirst/go-whosonfirst-spatial-sqlite"
)
func main() {
iterator_uri := flag.String("iterator-uri", "repo://?exclude=properties.edtf:deprecated=.*", "...")
database_uri := flag.String("spatial-database-uri", "sqlite://?dsn=/usr/local/data/gb.db", "...")
flag.Parse()
ctx := context.Background()
logger := log.Default()
db, err := database.NewSpatialDatabase(ctx, *database_uri)
if err != nil {
logger.Fatalf("Failed to create spatial database, %v", err)
}
resolver, err := hierarchy.NewPointInPolygonHierarchyResolver(ctx, db, nil)
if err != nil {
logger.Fatalf("Failed to create hierarchy resolver, %v", err)
}
iter_cb := func(ctx context.Context, path string, r io.ReadSeeker, args ...interface{}) error {
body, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("Failed to read body for %s, %w", path, err)
}
inputs := &filter.SPRInputs{}
resultsCallback := hierarchy.FirstButForgivingSPRResultsFunc
updateCallback := hierarchy.DefaultPointInPolygonHierarchyResolverUpdateCallback()
has_changed, new_body, err := resolver.PointInPolygonAndUpdate(ctx, inputs, resultsCallback, updateCallback, body)
if err != nil {
return fmt.Errorf("Failed to PIP %s, %w", path, err)
}
if !has_changed {
return nil
}
parent_id, err := properties.ParentId(new_body)
if err != nil {
return fmt.Errorf("Failed to derive parent ID for updated %s, %w", path, err)
}
fmt.Println(path, parent_id)
return nil
}
iter, err := iterator.NewIterator(ctx, *iterator_uri, iter_cb)
if err != nil {
logger.Fatalf("Failed to create iterator, %v", err)
}
iter_uris := flag.Args()
err = iter.IterateURIs(ctx, iter_uris...)
if err != nil {
logger.Fatalf("Failed to iterate URIs, %v", err)
}
}
```
And then:
$> go run cmd/test/main.go /usr/local/data/whosonfirst-data-postalcode-gb
/usr/local/data/whosonfirst-data-postalcode-gb/data/381/279/06/38127906.geojson 1125887097
/usr/local/data/whosonfirst-data-postalcode-gb/data/383/231/07/38323107.geojson 101750595
/usr/local/data/whosonfirst-data-postalcode-gb/data/382/235/04/38223504.geojson 101750397
/usr/local/data/whosonfirst-data-postalcode-gb/data/384/203/07/38420307.geojson 101914023
/usr/local/data/whosonfirst-data-postalcode-gb/data/383/231/16/38323116.geojson 1226177803
/usr/local/data/whosonfirst-data-postalcode-gb/data/382/235/13/38223513.geojson 101854835
/usr/local/data/whosonfirst-data-postalcode-gb/data/384/203/16/38420316.geojson 101750525
/usr/local/data/whosonfirst-data-postalcode-gb/data/381/279/15/38127915.geojson 1125840247
/usr/local/data/whosonfirst-data-postalcode-gb/data/382/235/22/38223522.geojson 1126027181
/usr/local/data/whosonfirst-data-postalcode-gb/data/384/203/25/38420325.geojson 1360758319
/usr/local/data/whosonfirst-data-postalcode-gb/data/381/279/24/38127924.geojson 101750527
/usr/local/data/whosonfirst-data-postalcode-gb/data/383/231/25/38323125.geojson 1125834847
... and so on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment