Skip to content

Commit

Permalink
Merge pull request #49 from whosonfirst/mbr
Browse files Browse the repository at this point in the history
Add `cmd/mbr` tool
  • Loading branch information
straup authored Jan 14, 2025
2 parents d77d8a8 + 3d2f8cd commit ebab458
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 113 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
bin
bin
.DS_Store
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ LDFLAGS=-s -w

cli:
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/pip cmd/pip/main.go
go build -mod $(GOMOD) -ldflags="$(LDFLAGS)" -o bin/mbr cmd/mbr/main.go
91 changes: 91 additions & 0 deletions cmd/mbr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

// > go run cmd/mbr/main.go -id 102087579 -id 102086959 -id 102085387
// -123.173825,37.053858,-121.469214,37.929824

import (
"context"
"fmt"
"log"
"os"

_ "github.com/whosonfirst/go-reader-http"

"github.com/paulmach/orb"
"github.com/sfomuseum/go-flags/flagset"
"github.com/sfomuseum/go-flags/multi"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-feature/geometry"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
)

func main() {

var reader_uri string
var ids multi.MultiInt64

fs := flagset.NewFlagSet("flags")
fs.StringVar(&reader_uri, "reader-uri", "http://data.whosonfirst.org", "A registered whosonfirst/go-reader.Reader URI.")
fs.Var(&ids, "id", "One or more Who's On First IDs.")

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Derive one or more intersecting minimum-bounding-rectangles (MBR) for one or more Who's On First IDs.\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t %s [options]\n", os.Args[0])
fs.PrintDefaults()
}

flagset.Parse(fs)

ctx := context.Background()

r, err := reader.NewReader(ctx, reader_uri)

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

bounds := make([]orb.Bound, 0)

for idx, id := range ids {

body, err := wof_reader.LoadBytes(ctx, r, id)

if err != nil {
log.Fatalf("Failed to load record for %d, %v", id, err)
}

geojson_geom, err := geometry.Geometry(body)

if err != nil {
log.Fatalf("Failed to derive geometry for %d, %v", id, err)
}

orb_geom := geojson_geom.Geometry()
this_b := orb_geom.Bound()

switch idx {
case 0:
bounds = append(bounds, this_b)
default:

has_rel := false

for idx_b, other_b := range bounds {

if this_b.Intersects(other_b) {
bounds[idx_b] = other_b.Union(this_b)
has_rel = true
break
}
}

if !has_rel {
bounds = append(bounds, this_b)
}
}
}

for _, b := range bounds {
fmt.Printf("%f,%f,%f,%f\n", b.Min.X(), b.Min.Y(), b.Max.X(), b.Max.Y())
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/tidwall/sjson v1.2.5
github.com/whosonfirst/go-ioutil v1.0.2
github.com/whosonfirst/go-reader v1.0.2
github.com/whosonfirst/go-reader-http v0.3.1
github.com/whosonfirst/go-sanitize v0.1.0
github.com/whosonfirst/go-whosonfirst-export/v2 v2.8.3
github.com/whosonfirst/go-whosonfirst-feature v0.0.28
Expand All @@ -26,7 +27,6 @@ require (
github.com/whosonfirst/go-whosonfirst-uri v1.3.0
github.com/whosonfirst/go-whosonfirst-writer/v3 v3.1.4
github.com/whosonfirst/go-writer/v3 v3.1.1
github.com/whosonfirst/warning v0.1.1
)

require (
Expand Down
8 changes: 3 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v0.0.0-20171204182908-b7773ae21874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand All @@ -60,7 +58,6 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK
github.com/paulmach/orb v0.11.1 h1:3koVegMC4X/WeiXYz9iswopaTwMem53NzTJuTF20JzU=
github.com/paulmach/orb v0.11.1/go.mod h1:5mULz1xQfs3bmQm63QEJA6lNGujuRafwA5S/EnuLaLU=
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -93,8 +90,11 @@ github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/whosonfirst/go-ioutil v1.0.2 h1:+GJPfa42OFn5A+5yJSc5jQTQIkNV3/MhYyg4pavdrC8=
github.com/whosonfirst/go-ioutil v1.0.2/go.mod h1:2dS1vWdAIkiHDvDF8fYyjv6k2NISmwaIjJJeEDBEdvg=
github.com/whosonfirst/go-reader v1.0.1/go.mod h1:3RXON51sbyPw8Ca7sxxwOItVfx8SM+KFPR+l2azGxJA=
github.com/whosonfirst/go-reader v1.0.2 h1:eccnKKSMGR+X1SJyHUZN0/7qE7VbFQULqSVQU0Su3xs=
github.com/whosonfirst/go-reader v1.0.2/go.mod h1:2w9l/QusYZSiGuEof3RwCHUFnM492SSOF2H7UxS4YIE=
github.com/whosonfirst/go-reader-http v0.3.1 h1:05cfqxb0OCyoKmaJgyzyX81mXzPe1J29QV/4NqEUMRQ=
github.com/whosonfirst/go-reader-http v0.3.1/go.mod h1:F8MO2ti0CFmkf/MUAlkkgUklBhHYtgeh1bUAl6IzsgU=
github.com/whosonfirst/go-sanitize v0.1.0 h1:ygSqCnakwdzH/m8UEa15zXGDsoo5/JJeRkgmAjXZrBU=
github.com/whosonfirst/go-sanitize v0.1.0/go.mod h1:p/emgbafMM0p5iVAz2XWwecYPl06Tw4Jos9rhTKIrt8=
github.com/whosonfirst/go-whosonfirst-crawl v0.2.2 h1:7nwpNV/BFoPR0R7KMMy1iiYAer7wlHJBUOiL+NLzIFs=
Expand Down Expand Up @@ -127,8 +127,6 @@ github.com/whosonfirst/go-writer/v3 v3.1.1 h1:YFG/LUzqr8tNNV/rqqvACrMT2jaoXMEecj
github.com/whosonfirst/go-writer/v3 v3.1.1/go.mod h1:vK1dX0is3i0rw890qSsRXqsUkWlmZX5qiedwf1vSbkE=
github.com/whosonfirst/walk v0.0.2 h1:fA0xskpnorC8OTvA1IKVOwQ12joCW6R5k2+/qd9lS7k=
github.com/whosonfirst/walk v0.0.2/go.mod h1:HvRa/XX8jGtggvwAFMWMMuNy1M3+ZDqDX2hxiGR9b2o=
github.com/whosonfirst/warning v0.1.1 h1:h29zL3VNL9VUHztkAAndzblhrDHyik9z47OuUR2Vovw=
github.com/whosonfirst/warning v0.1.1/go.mod h1:/unEMzhB9YaMeEwTJpzLN3kM5LiSxdJhKEsf/OQhn6s=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
Expand Down
10 changes: 5 additions & 5 deletions hierarchy/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log/slog"
"time"

"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/sfomuseum/go-sfomuseum-mapshaper"
Expand Down Expand Up @@ -156,10 +156,10 @@ func (t *PointInPolygonHierarchyResolver) PointInPolygon(ctx context.Context, in

t1 := time.Now()

defer func(){
defer func() {
logger.Debug("Time to PIP", "final", time.Since(t1))
}()

centroid, err := t.PointInPolygonCentroid(ctx, body)

if err != nil {
Expand Down Expand Up @@ -253,11 +253,11 @@ func (t *PointInPolygonHierarchyResolver) PointInPolygon(ctx context.Context, in
logger.Debug("Perform point in polygon with placetype filter", "placetype", pt_name)

t2 := time.Now()

rsp, err := t.Database.PointInPolygon(ctx, coord, spr_filter)

logger = logger.With(pt_name, time.Since(t2))

if err != nil {
return nil, fmt.Errorf("Failed to point in polygon for %v, %v", coord, err)
}
Expand Down
11 changes: 11 additions & 0 deletions vendor/github.com/whosonfirst/go-reader-http/.gitignore

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

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

9 changes: 9 additions & 0 deletions vendor/github.com/whosonfirst/go-reader-http/README.md

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

110 changes: 110 additions & 0 deletions vendor/github.com/whosonfirst/go-reader-http/http.go

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

Empty file.
25 changes: 0 additions & 25 deletions vendor/github.com/whosonfirst/warning/README.md

This file was deleted.

Loading

0 comments on commit ebab458

Please sign in to comment.