Skip to content

Commit

Permalink
add wof-as-csv tool
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Jun 13, 2023
1 parent d4e5851 commit 52d10f6
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 18 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cli:
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-merge-featurecollection cmd/wof-merge-featurecollection/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-supersede-with-parent cmd/wof-supersede-with-parent/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-as-featurecollection cmd/wof-as-featurecollection/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-as-csv cmd/wof-as-csv/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-as-jsonl cmd/wof-as-jsonl/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-rename-property cmd/wof-rename-property/main.go
go build -mod $(GOMOD) -ldflags="-s -w" -o bin/wof-remove-properties cmd/wof-remove-properties/main.go
Expand Down
74 changes: 57 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,69 @@ At some point the various application might get separated out in to their own pa

## Tools

> requires `go1.16` or later
```
$> make cli
go build -mod vendor -o bin/wof-assign-geometry cmd/wof-assign-geometry/main.go
go build -mod vendor -o bin/wof-assign-parent cmd/wof-assign-parent/main.go
go build -mod vendor -o bin/wof-exportify cmd/wof-exportify/main.go
go build -mod vendor -o bin/wof-create cmd/wof-create/main.go
go build -mod vendor -o bin/wof-deprecate cmd/wof-deprecate/main.go
go build -mod vendor -o bin/wof-cessate cmd/wof-cessate/main.go
go build -mod vendor -o bin/wof-superseded-by cmd/wof-superseded-by/main.go
go build -mod vendor -o bin/wof-ensure-properties cmd/wof-ensure-properties/main.go
go build -mod vendor -o bin/wof-deprecate-and-supersede cmd/wof-deprecate-and-supersede/main.go
go build -mod vendor -o bin/wof-merge-featurecollection cmd/wof-merge-featurecollection/main.go
go build -mod vendor -o bin/wof-supersede-with-parent cmd/wof-supersede-with-parent/main.go
go build -mod vendor -o bin/wof-as-featurecollection cmd/wof-as-featurecollection/main.go
go build -mod vendor -o bin/wof-as-jsonl cmd/wof-as-jsonl/main.go
go build -mod vendor -o bin/wof-rename-property cmd/wof-rename-property/main.go
go build -mod vendor -o bin/wof-remove-properties cmd/wof-remove-properties/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-assign-geometry cmd/wof-assign-geometry/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-assign-parent cmd/wof-assign-parent/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-exportify cmd/wof-exportify/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-create cmd/wof-create/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-deprecate cmd/wof-deprecate/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-cessate cmd/wof-cessate/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-superseded-by cmd/wof-superseded-by/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-ensure-properties cmd/wof-ensure-properties/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-deprecate-and-supersede cmd/wof-deprecate-and-supersede/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-merge-featurecollection cmd/wof-merge-featurecollection/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-supersede-with-parent cmd/wof-supersede-with-parent/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-as-featurecollection cmd/wof-as-featurecollection/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-as-csv cmd/wof-as-csv/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-as-jsonl cmd/wof-as-jsonl/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-rename-property cmd/wof-rename-property/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-remove-properties cmd/wof-remove-properties/main.go
go build -mod vendor -ldflags="-s -w" -o bin/wof-clone-feature cmd/wof-clone-feature/main.go
```

As of this writing these tools may contain duplicate, or at least common, code that would be well-served from being moved in to a package or library. That hasn't happened yet.

### wof-as-csv

Export one or more WOF records as a CSV document written to `STDOUT`.

```
$> ./bin/wof-as-csv -h
Export one or more WOF records as a CSV document written to STDOUT
Usage:
./bin/wof-as-csv [options] path-(N) path-(N)
For example:
./bin/wof-as-csv -field wof:id -field wof:name -field centroid -iterator-uri 'repo://?include=properties.mz:is_current=1' /usr/local/data/sfomuseum-data-publicart/
Valid options are:
-field value
One or more relative 'properties.FIELDNAME' paths to include the CSV output. If the fieldname is 'path' the filename of the current record will be included. If the fieldname is 'centroid' the primary centroid of the current record will be derived and included as 'latitude' and 'longitude' columns.
-iterator-uri string
(default "repo://")
```

For example:

```
$> ./bin/wof-as-csv \
-field wof:id \
-field centroid \
-field wof:name \
-iterator-uri 'repo://?include=properties.sfomuseum:placetype=boardingarea&include=properties.mz:is_current=1' \
/usr/local/data/sfomuseum-data-architecture/
wof:id,latitude,longitude,wof:name
1763588233,37.615069504933075,-122.38306184920478,Boarding Area C
1763588177,37.612307411494186,-122.38518056697221,Boarding Area B
1763588335,37.61888020848815,-122.38446905789152,Boarding Area E
1763588433,37.617858008730636,-122.39137174044615,Boarding Area G
1763588125,37.61734905835808,-122.38196681238652,Boarding Area D
1763588271,37.62010253190792,-122.38804674158527,Boarding Area F
1763588371,37.61289407736701,-122.38934858141738,Boarding Area A
```

### wof-as-featurecollection

Export one or more WOF records as a GeoJSON FeatureCollection
Expand Down
135 changes: 135 additions & 0 deletions cmd/wof-as-csv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"sync"

"github.com/sfomuseum/go-csvdict"
"github.com/sfomuseum/go-flags/multi"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/iterator"
)

func main() {

var fields multi.MultiCSVString

iterator_uri := flag.String("iterator-uri", "repo://", "")
flag.Var(&fields, "field", "One or more relative 'properties.FIELDNAME' paths to include the CSV output. If the fieldname is 'path' the filename of the current record will be included. If the fieldname is 'centroid' the primary centroid of the current record will be derived and included as 'latitude' and 'longitude' columns.")

flag.Usage = func() {

fmt.Fprintf(os.Stderr, "Export one or more WOF records as a CSV document written to STDOUT\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t %s [options] path-(N) path-(N)\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "For example:\n")
fmt.Fprintf(os.Stderr, "\t%s -field wof:id -field wof:name -field centroid -iterator-uri 'repo://?include=properties.mz:is_current=1' /usr/local/data/sfomuseum-data-publicart/\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Valid options are:\n")
flag.PrintDefaults()
}

flag.Parse()

uris := flag.Args()

ctx := context.Background()

writers := []io.Writer{
os.Stdout,
}

mw := io.MultiWriter(writers...)

var csv_wr *csvdict.Writer

mu := new(sync.RWMutex)

iter_cb := func(ctx context.Context, path string, fh io.ReadSeeker, args ...interface{}) error {

body, err := io.ReadAll(fh)

if err != nil {
return fmt.Errorf("Failed to read %s, %w", path, err)
}

out := make(map[string]string)

for _, k := range fields {

switch k {
case "path":
out[k] = path
case "centroid":

c, _, err := properties.Centroid(body)

if err != nil {
return fmt.Errorf("Failed to derive centroid for %s, %w", path, err)
}

out["latitude"] = strconv.FormatFloat(c.Lat(), 'f', -1, 64)
out["longitude"] = strconv.FormatFloat(c.Lon(), 'f', -1, 64)

default:

fq_k := fmt.Sprintf("properties.%s", k)
rsp := gjson.GetBytes(body, fq_k)
out[k] = rsp.String()
}
}

if csv_wr == nil {

fieldnames := make([]string, 0)

for f, _ := range out {
fieldnames = append(fieldnames, f)
}

wr, err := csvdict.NewWriter(mw, fieldnames)

if err != nil {
return fmt.Errorf("Failed to create CSV writer, %w", err)
}

csv_wr = wr

err = csv_wr.WriteHeader()

if err != nil {
return fmt.Errorf("Failed to write CSV header, %w", err)
}
}

mu.Lock()
defer mu.Unlock()

err = csv_wr.WriteRow(out)

if err != nil {
return fmt.Errorf("Failed to write row for %s, %w", path, err)
}

return nil
}

iter, err := iterator.NewIterator(ctx, *iterator_uri, iter_cb)

if err != nil {
log.Fatalf("Failed to create iterator, %v", err)
}

err = iter.IterateURIs(ctx, uris...)

if err != nil {
log.Fatalf("Failed to iterate URIs, %v", err)
}

csv_wr.Flush()
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ go 1.18
require (
github.com/aaronland/go-json-query v0.1.4
github.com/paulmach/orb v0.9.2
github.com/sfomuseum/go-csvdict v1.0.0
github.com/sfomuseum/go-edtf v1.1.1
github.com/sfomuseum/go-flags v0.10.0
github.com/tidwall/gjson v1.14.4
github.com/tidwall/sjson v1.2.5
github.com/whosonfirst/go-reader v1.0.2
github.com/whosonfirst/go-whosonfirst-export/v2 v2.7.0
github.com/whosonfirst/go-whosonfirst-feature v0.0.26
github.com/whosonfirst/go-whosonfirst-iterate-reader v1.0.0
github.com/whosonfirst/go-whosonfirst-iterate/v2 v2.3.4
github.com/whosonfirst/go-whosonfirst-reader v1.0.1
Expand Down Expand Up @@ -59,7 +61,6 @@ require (
github.com/whosonfirst/go-rfc-5646 v0.1.0 // indirect
github.com/whosonfirst/go-sanitize v0.1.0 // indirect
github.com/whosonfirst/go-whosonfirst-crawl v0.2.2 // indirect
github.com/whosonfirst/go-whosonfirst-feature v0.0.26 // indirect
github.com/whosonfirst/go-whosonfirst-flags v0.5.1 // indirect
github.com/whosonfirst/go-whosonfirst-format v0.4.1 // indirect
github.com/whosonfirst/go-whosonfirst-id v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sfomuseum/go-csvdict v1.0.0 h1:ECLdgya7eXmJ8oJpk8JKmiUmQhahgXehHPWIC+bTUc0=
github.com/sfomuseum/go-csvdict v1.0.0/go.mod h1:cXhcPaOj1q//WQpkcH1HEXeAdNd0N/P+EniPVNfnBxs=
github.com/sfomuseum/go-edtf v0.2.3/go.mod h1:1rP0EJZ/84j3HO80vGcnG2T9MFBDAFyTNtjrr8cv3T4=
github.com/sfomuseum/go-edtf v0.3.1/go.mod h1:1rP0EJZ/84j3HO80vGcnG2T9MFBDAFyTNtjrr8cv3T4=
github.com/sfomuseum/go-edtf v1.1.1 h1:R5gElndHGDaK/rGSh2X+ulaLtlcHCdQA1cTzB8e9wv8=
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/sfomuseum/go-csvdict/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/sfomuseum/go-csvdict/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
82 changes: 82 additions & 0 deletions vendor/github.com/sfomuseum/go-csvdict/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 52d10f6

Please sign in to comment.