2013-12-24 03:09:50 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-12-24 03:09:50 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package geocode handles mapping user-entered locations into lat/long polygons.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package geocode // import "perkeep.org/pkg/geocode"
|
2013-12-24 03:09:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-26 09:05:38 +00:00
|
|
|
"context"
|
2013-12-24 03:09:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2013-12-24 21:43:25 +00:00
|
|
|
"log"
|
2013-12-24 03:09:50 +00:00
|
|
|
"net/url"
|
2013-12-24 20:43:19 +00:00
|
|
|
"sync"
|
2013-12-24 03:09:50 +00:00
|
|
|
|
2015-12-16 00:22:48 +00:00
|
|
|
"go4.org/ctxutil"
|
|
|
|
"go4.org/syncutil/singleflight"
|
2015-12-12 21:47:31 +00:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2013-12-24 03:09:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LatLong struct {
|
|
|
|
Lat float64 `json:"lat"`
|
|
|
|
Long float64 `json:"lng"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rect struct {
|
|
|
|
NorthEast LatLong `json:"northeast"`
|
|
|
|
SouthWest LatLong `json:"southwest"`
|
|
|
|
}
|
|
|
|
|
2016-11-08 17:43:30 +00:00
|
|
|
// AltLookupFn provides alternative geocode lookup in tests.
|
|
|
|
//
|
|
|
|
// If AltLookupFn is not nil, Lookup calls AltLookupFn first,
|
|
|
|
// and returns the results unless it is (nil, nil).
|
|
|
|
//
|
|
|
|
// Lookup performs its standard lookup using its cache
|
|
|
|
// and the Google geocoding serice if AltLookupFn is nil,
|
|
|
|
// or it returns (nil, nil) for the address being looked up.
|
|
|
|
//
|
|
|
|
// It's up to the caller to change AltLookupFn only
|
|
|
|
// when Lookup is not being called.
|
|
|
|
var AltLookupFn func(ctx context.Context, address string) ([]Rect, error)
|
|
|
|
|
2013-12-24 20:43:19 +00:00
|
|
|
var (
|
|
|
|
mu sync.RWMutex
|
|
|
|
cache = map[string][]Rect{}
|
|
|
|
|
|
|
|
sf singleflight.Group
|
|
|
|
)
|
|
|
|
|
2013-12-24 03:09:50 +00:00
|
|
|
// Lookup returns rectangles for the given address. Currently the only
|
|
|
|
// implementation is the Google geocoding service.
|
2015-12-12 21:47:31 +00:00
|
|
|
func Lookup(ctx context.Context, address string) ([]Rect, error) {
|
2016-11-08 17:43:30 +00:00
|
|
|
if AltLookupFn != nil {
|
|
|
|
r, err := AltLookupFn(ctx, address)
|
|
|
|
if r != nil || err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:19 +00:00
|
|
|
mu.RLock()
|
|
|
|
rects, ok := cache[address]
|
|
|
|
mu.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return rects, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rectsi, err := sf.Do(address, func() (interface{}, error) {
|
|
|
|
// TODO: static data files from OpenStreetMap, Wikipedia, etc?
|
|
|
|
urlStr := "https://maps.googleapis.com/maps/api/geocode/json?address=" + url.QueryEscape(address) + "&sensor=false"
|
2015-12-16 00:22:48 +00:00
|
|
|
res, err := ctxhttp.Get(ctx, ctxutil.Client(ctx), urlStr)
|
2013-12-24 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-24 03:02:10 +00:00
|
|
|
defer res.Body.Close()
|
2013-12-24 20:43:19 +00:00
|
|
|
rects, err := decodeGoogleResponse(res.Body)
|
2013-12-24 21:43:25 +00:00
|
|
|
log.Printf("Google geocode lookup (%q) = %#v, %v", address, rects, err)
|
2013-12-24 20:43:19 +00:00
|
|
|
if err == nil {
|
|
|
|
mu.Lock()
|
|
|
|
cache[address] = rects
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
return rects, err
|
|
|
|
})
|
2013-12-24 03:09:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-24 20:43:19 +00:00
|
|
|
return rectsi.([]Rect), nil
|
2013-12-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type googleResTop struct {
|
|
|
|
Results []*googleResult `json:"results"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type googleResult struct {
|
|
|
|
Geometry *googleGeometry `json:"geometry"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type googleGeometry struct {
|
2014-01-25 17:26:18 +00:00
|
|
|
Bounds *Rect `json:"bounds"`
|
|
|
|
Viewport *Rect `json:"viewport"`
|
2013-12-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func decodeGoogleResponse(r io.Reader) (rects []Rect, err error) {
|
|
|
|
var resTop googleResTop
|
|
|
|
if err := json.NewDecoder(r).Decode(&resTop); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, res := range resTop.Results {
|
|
|
|
if res.Geometry != nil && res.Geometry.Bounds != nil {
|
2014-01-25 17:26:18 +00:00
|
|
|
r := res.Geometry.Bounds
|
|
|
|
if r.NorthEast.Lat == 90 && r.NorthEast.Long == 180 &&
|
|
|
|
r.SouthWest.Lat == -90 && r.SouthWest.Long == -180 {
|
|
|
|
// Google sometimes returns a "whole world" rect for large addresses (like "USA")
|
|
|
|
// so instead use the viewport in that case.
|
|
|
|
if res.Geometry.Viewport != nil {
|
|
|
|
rects = append(rects, *res.Geometry.Viewport)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rects = append(rects, *r)
|
|
|
|
}
|
2013-12-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|