Skip to content

Commit

Permalink
Update to reflect changes to whosonfirst/go-whosonfirst-spatial v0.9.0 (
Browse files Browse the repository at this point in the history
#8)

* snapshot: new branch to reflect changes to go-whosonfirst-spatial v0.9.0

* snapshot: replace all the flags code; compile but untested

* snapshot: compiles again, still untested

* update docs

---------

Co-authored-by: sfomuseumbot <sfomuseumbot@localhost>
  • Loading branch information
thisisaaronland and sfomuseumbot authored May 22, 2024
1 parent 6f62e5d commit 8aed128
Show file tree
Hide file tree
Showing 384 changed files with 58,601 additions and 19,868 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
GOMOD=$(shell test -f "go.work" && echo "readonly" || echo "vendor")
LDFLAGS=-s -w

# https://developers.google.com/protocol-buffers/docs/reference/go-generated
# go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

proto:
protoc --go_out=. --go_opt=paths=source_relative --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative spatial/spatial.proto

cli:
go build -mod vendor -o bin/server cmd/server/main.go
go build -mod vendor -o bin/client cmd/client/main.go
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/server cmd/server/main.go
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/client cmd/client/main.go

debug:
go run -mod vendor cmd/server/main.go -mode directory:// /usr/local/data/sfomuseum-data-maps/data
go run -mod $(GOMOD) -ldflags="$(LDFLAGS)" cmd/server/main.go \
-iterator-uri repo:// \
/usr/local/data/sfomuseum-data-maps
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

gRPC support for the `go-whosonfirst-spatial` interfaces.

## Important
## Documentation

Work in progress. Everything will change. Documentation to follow.
Documentatin is incomplete.

## Tools

Expand All @@ -17,18 +17,16 @@ go build -mod vendor -o bin/client cmd/client/main.go
### client

```
$> ./bin/client -h
> ./bin/client -h
-alternate-geometry value
One or more alternate geometry labels (wof:alt_label) values to filter results by.
-cessation-date string
-cessation string
A valid EDTF date string.
-dev-null
Emit results to /dev/null
-geometries string
Valid options are: all, alt, default. (default "all")
-host string
The host to listen for requests on (default "localhost")
-inception-date string
-inception string
A valid EDTF date string.
-is-ceased value
One or more existential flags (-1, 0, 1) to filter results by.
Expand All @@ -44,6 +42,8 @@ $> ./bin/client -h
A valid latitude.
-longitude float
A valid longitude.
-null
Emit results to /dev/null
-placetype value
One or more place types to filter results by.
-port int
Expand Down Expand Up @@ -92,25 +92,23 @@ $> ./bin/client -latitude 43.873889 -longitude 18.408611 -inception-date 199X |
### server

```
$> ./bin/server -h
> ./bin/server -h
-custom-placetypes string
A JSON-encoded string containing custom placetypes defined using the syntax described in the whosonfirst/go-whosonfirst-placetypes repository.
-enable-custom-placetypes
Enable wof:placetype values that are not explicitly defined in the whosonfirst/go-whosonfirst-placetypes repository.
-host string
The host to listen for requests on (default "localhost")
... (default "localhost")
-is-wof
Input data is WOF-flavoured GeoJSON. (Pass a value of '0' or 'false' if you need to index non-WOF documents. (default true)
-iterator-uri string
A valid whosonfirst/go-whosonfirst-iterate/v2 URI. Supported schemes are: directory://, featurecollection://, file://, filelist://, geojsonl://, null://, repo://. (default "repo://")
-port int
The port to listen for requests on (default 8082)
... (default 8082)
-properties-reader-uri string
A valid whosonfirst/go-reader.Reader URI. Available options are: [fs:// null:// repo:// stdin://]. If the value is {spatial-database-uri} then the value of the '-spatial-database-uri' implements the reader.Reader interface and will be used.
-spatial-database-uri string
A valid whosonfirst/go-whosonfirst-spatial/data.SpatialDatabase URI. options are: []
-verbose
Be chatty.
A valid whosonfirst/go-whosonfirst-spatial/data.SpatialDatabase URI. options are: [rtree://] (default "rtree://")
```

For example:
Expand All @@ -127,7 +125,6 @@ $> ./bin/server -spatial-database-uri rtree:// /usr/local/data/whosonfirst-data-
## See also

* https://github.com/whosonfirst/go-whosonfirst-spatial
* https://github.com/whosonfirst/go-whosonfirst-spatial-pip
* https://github.com/whosonfirst/go-whosonfirst-spatial-grpc-sqlite
* https://github.com/whosonfirst/go-whosonfirst-spatial-grpc-pmtiles
* https://github.com/whosonfirst/go-whosonfirst-spr
Expand Down
58 changes: 32 additions & 26 deletions app/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/sfomuseum/go-flags/flagset"
"github.com/sfomuseum/go-flags/lookup"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"

"github.com/whosonfirst/go-whosonfirst-spatial-grpc/request"
"github.com/whosonfirst/go-whosonfirst-spatial-grpc/spatial"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
"github.com/whosonfirst/go-whosonfirst-spatial/pip"
"google.golang.org/grpc"
"io"
"log"
Expand All @@ -30,35 +28,46 @@ func Run(ctx context.Context, logger *log.Logger) error {

func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *log.Logger) error {

flagset.Parse(fs)

err := grpc_flags.ValidateGRPCClientFlags(fs)
opts, err := RunOptionsFromFlagSet(ctx, fs)

if err != nil {
return fmt.Errorf("Failed to validate grpc client flags, %w", err)
return fmt.Errorf("Failed to derive options from flagset, %w", err)
}

err = spatial_flags.ValidateQueryFlags(fs)
return RunWithOptions(ctx, opts, logger)
}

if err != nil {
return fmt.Errorf("Failed to validate query flags, %w", err)
func RunWithOptions(ctx context.Context, opts *RunOptions, logger *log.Logger) error {

pip_req := &pip.PointInPolygonRequest{
Latitude: opts.Latitude,
Longitude: opts.Longitude,
Placetypes: opts.Placetypes,
Geometries: opts.Geometries,
AlternateGeometries: opts.AlternateGeometries,
IsCurrent: opts.IsCurrent,
IsCeased: opts.IsCeased,
IsDeprecated: opts.IsDeprecated,
IsSuperseded: opts.IsSuperseded,
IsSuperseding: opts.IsSuperseding,
InceptionDate: opts.InceptionDate,
CessationDate: opts.CessationDate,
Properties: opts.Properties,
Sort: opts.Sort,
}

req, err := request.NewPointInPolygonRequestFromFlagSet(fs)
spatial_req, err := request.NewPointInPolygonRequest(pip_req)

if err != nil {
return fmt.Errorf("Failed to create PIP request, %v", err)
return fmt.Errorf("Failed to create spatial PIP request, %w", err)
}

host, _ := lookup.StringVar(fs, grpc_flags.HOST)
port, _ := lookup.IntVar(fs, grpc_flags.PORT)

var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
var grpc_opts []grpc.DialOption
grpc_opts = append(grpc_opts, grpc.WithInsecure())

addr := fmt.Sprintf("%s:%d", host, port)
addr := fmt.Sprintf("%s:%d", opts.Host, opts.Port)

conn, err := grpc.Dial(addr, opts...)
conn, err := grpc.Dial(addr, grpc_opts...)

if err != nil {
return fmt.Errorf("Failed to dial '%s', %v", addr, err)
Expand All @@ -68,22 +77,19 @@ func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *log.Logger) e

client := spatial.NewSpatialClient(conn)

stream, err := client.PointInPolygon(ctx, req)
stream, err := client.PointInPolygon(ctx, spatial_req)

if err != nil {
return fmt.Errorf("Failed to perform point in polygon operation, %w", err)
}

to_stdout, _ := lookup.BoolVar(fs, grpc_flags.TO_STDOUT)
to_null, _ := lookup.BoolVar(fs, grpc_flags.TO_NULL)

writers := make([]io.Writer, 0)

if to_stdout {
if opts.Stdout {
writers = append(writers, os.Stdout)
}

if to_null {
if opts.Null {
writers = append(writers, io.Discard)
}

Expand Down
63 changes: 52 additions & 11 deletions app/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,68 @@ package client

import (
"flag"
"fmt"

"github.com/sfomuseum/go-flags/flagset"
grpc_flags "github.com/whosonfirst/go-whosonfirst-spatial-grpc/flags"
spatial_flags "github.com/whosonfirst/go-whosonfirst-spatial/flags"
"github.com/sfomuseum/go-flags/multi"
)

var host string
var port int

var latitude float64
var longitude float64
var geometries string

var inception string
var cessation string

var props multi.MultiString
var placetypes multi.MultiString
var alt_geoms multi.MultiString

var is_current multi.MultiInt64
var is_ceased multi.MultiInt64
var is_deprecated multi.MultiInt64
var is_superseded multi.MultiInt64
var is_superseding multi.MultiInt64

var sort_uris multi.MultiString

var stdout bool
var null bool

func DefaultFlagSet() (*flag.FlagSet, error) {

fs := flagset.NewFlagSet("client")

err := grpc_flags.AppendGRPCClientFlags(fs)
fs.StringVar(&host, "host", "localhost", "The host to listen for requests on")
fs.IntVar(&port, "port", 8082, "The port to listen for requests on")

fs.BoolVar(&stdout, "stdout", true, "Emit results to STDOUT")
fs.BoolVar(&null, "null", false, "Emit results to /dev/null")

// query flags

fs.Float64Var(&latitude, "latitude", 0.0, "A valid latitude.")
fs.Float64Var(&longitude, "longitude", 0.0, "A valid longitude.")

fs.StringVar(&geometries, "geometries", "all", "Valid options are: all, alt, default.")

fs.StringVar(&inception, "inception", "", "A valid EDTF date string.")
fs.StringVar(&cessation, "cessation", "", "A valid EDTF date string.")

fs.Var(&props, "property", "One or more Who's On First properties to append to each result.")
fs.Var(&placetypes, "placetype", "One or more place types to filter results by.")

if err != nil {
return nil, fmt.Errorf("Failed to append grpc client flags, %w", err)
}
fs.Var(&alt_geoms, "alternate-geometry", "One or more alternate geometry labels (wof:alt_label) values to filter results by.")

err = spatial_flags.AppendQueryFlags(fs)
fs.Var(&is_current, "is-current", "One or more existential flags (-1, 0, 1) to filter results by.")
fs.Var(&is_ceased, "is-ceased", "One or more existential flags (-1, 0, 1) to filter results by.")
fs.Var(&is_deprecated, "is-deprecated", "One or more existential flags (-1, 0, 1) to filter results by.")
fs.Var(&is_superseded, "is-superseded", "One or more existential flags (-1, 0, 1) to filter results by.")
fs.Var(&is_superseding, "is-superseding", "One or more existential flags (-1, 0, 1) to filter results by.")

if err != nil {
return nil, fmt.Errorf("Failed to append query flags, %w", err)
}
fs.Var(&sort_uris, "sort-uri", "Zero or more whosonfirst/go-whosonfirst-spr/sort URIs.")

return fs, nil
}
63 changes: 63 additions & 0 deletions app/client/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package client

import (
"context"
"flag"

"github.com/sfomuseum/go-flags/flagset"
)

type RunOptions struct {
Host string `json:"host"`
Port int `json:"port"`
Stdout bool `json:"stdout"`
Null bool `json:"null"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Placetypes []string `json:"placetypes,omitempty"`
Geometries string `json:"geometries,omitempty"`
AlternateGeometries []string `json:"alternate_geometries,omitempty"`
IsCurrent []int64 `json:"is_current,omitempty"`
IsCeased []int64 `json:"is_ceased,omitempty"`
IsDeprecated []int64 `json:"is_deprecated,omitempty"`
IsSuperseded []int64 `json:"is_superseded,omitempty"`
IsSuperseding []int64 `json:"is_superseding,omitempty"`
InceptionDate string `json:"inception_date,omitempty"`
CessationDate string `json:"cessation_date,omitempty"`
Properties []string `json:"properties,omitempty"`
Sort []string `json:"sort,omitempty"`
}

func RunOptionsFromFlagSet(ctx context.Context, fs *flag.FlagSet) (*RunOptions, error) {

flagset.Parse(fs)

err := flagset.SetFlagsFromEnvVars(fs, "WHOSONFIRST")

if err != nil {
return nil, err
}

opts := &RunOptions{
Host: host,
Port: port,
Stdout: stdout,
Null: null,
Latitude: latitude,
Longitude: longitude,
Placetypes: placetypes,
Geometries: geometries,
AlternateGeometries: alt_geoms,
IsCurrent: is_current,
IsCeased: is_ceased,
IsDeprecated: is_deprecated,
IsSuperseded: is_superseded,
IsSuperseding: is_superseding,
InceptionDate: inception,
CessationDate: cessation,
Properties: props,
Sort: sort_uris,
}

return opts, nil
}
Loading

0 comments on commit 8aed128

Please sign in to comment.