Skip to content

Commit

Permalink
feat:implement local station finder based on google:s2
Browse files Browse the repository at this point in the history
issue:#240
  • Loading branch information
CodeBear801 committed Apr 13, 2020
1 parent a8a8f5d commit 10eeb70
Show file tree
Hide file tree
Showing 29 changed files with 460 additions and 62 deletions.
4 changes: 3 additions & 1 deletion integration/cmd/oasis/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var flags struct {
tnSearchEndpoint string
tnSearchAPIKey string
tnSearchAPISignature string
localDataPath string
}

func init() {
Expand All @@ -19,5 +20,6 @@ func init() {
flag.StringVar(&flags.finderType, "finder", "", "Specify search finder to search for nearby charge stations for given location, use TNSearchFinder or LocalIndexerFinder")
flag.StringVar(&flags.tnSearchEndpoint, "search", "", "TN-Search-backend endpoint")
flag.StringVar(&flags.tnSearchAPIKey, "searchApiKey", "", "API key for TN-Search-backend")
flag.StringVar(&flags.tnSearchAPISignature, "searchApiSignature", "", "API Signature for TN-Search-backend")
flag.StringVar(&flags.tnSearchAPISignature, "searchApiSignature", "", "API Signature for TN-Search-backend")
flag.StringVar(&flags.localDataPath, "datapath", "", "Local data path for index data")
}
3 changes: 2 additions & 1 deletion integration/cmd/oasis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func main() {
defer glog.Flush()
mux := http.NewServeMux()

oasisService, err := oasis.New(flags.osrmBackendEndpoint, flags.finderType, flags.tnSearchEndpoint, flags.tnSearchAPIKey, flags.tnSearchAPISignature)
oasisService, err := oasis.New(flags.osrmBackendEndpoint, flags.finderType, flags.tnSearchEndpoint,
flags.tnSearchAPIKey, flags.tnSearchAPISignature, flags.localDataPath)
if err != nil {
glog.Errorf("Failed to create oasis handler due to err %+v.\n", err)
return
Expand Down
4 changes: 2 additions & 2 deletions integration/service/oasis/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type Handler struct {
}

// New creates new Handler object
func New(osrmBackend, finderType, searchEndpoint, apiKey, apiSignature string) (*Handler, error) {
func New(osrmBackend, finderType, searchEndpoint, apiKey, apiSignature, dataFolderPath string) (*Handler, error) {
// @todo: need make sure connectivity is on and continues available
// simple request to guarantee server is alive after init
if len(osrmBackend) == 0 {
err := fmt.Errorf("empty osrmBackend end point")
return nil, err
}

finder, err := stationfinder.CreateStationsFinder(finderType, searchEndpoint, apiKey, apiSignature)
finder, err := stationfinder.CreateStationsFinder(finderType, searchEndpoint, apiKey, apiSignature, dataFolderPath)
if err != nil {
glog.Errorf("Failed in Handler's New() when try to call CreateStationsFinder(), met error = %+v\n", err)
return nil, err
Expand Down
11 changes: 6 additions & 5 deletions integration/service/oasis/spatialindexer/interface_mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spatialindexer

var mockPlaceInfo1 = []*PointInfo{
// MockPlaceInfo1 contains 10 PointInfo items
var MockPlaceInfo1 = []*PointInfo{
&PointInfo{
ID: 1,
Location: Location{
Expand Down Expand Up @@ -78,9 +79,9 @@ type MockFinder struct {
}

// FindNearByPointIDs returns mock result
// It returns 10 places defined in mockPlaceInfo1
// It returns 10 places defined in MockPlaceInfo1
func (finder *MockFinder) FindNearByPointIDs(center Location, radius float64, limitCount int) []*PointInfo {
return mockPlaceInfo1
return MockPlaceInfo1
}

// MockPointsIterator implements PointsIterator's interface
Expand All @@ -89,10 +90,10 @@ type MockPointsIterator struct {

// IteratePoints() iterate places with mock data
func (iterator *MockPointsIterator) IteratePoints() <-chan PointInfo {
pointInfoC := make(chan PointInfo, len(mockPlaceInfo1))
pointInfoC := make(chan PointInfo, len(MockPlaceInfo1))

go func() {
for _, item := range mockPlaceInfo1 {
for _, item := range MockPlaceInfo1 {
pointInfoC <- *item
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (bf *basicFinder) getNearbyChargeStations(req *nearbychargestation.Request)
bf.searchRespLock.Unlock()
}

func (bf *basicFinder) IterateNearbyStations() <-chan stationfindertype.ChargeStationInfo {
func (bf *basicFinder) IterateNearbyStations() <-chan *stationfindertype.ChargeStationInfo {
if bf.searchResp == nil || len(bf.searchResp.Results) == 0 {
c := make(chan stationfindertype.ChargeStationInfo)
c := make(chan *stationfindertype.ChargeStationInfo)
go func() {
defer close(c)
}()
Expand All @@ -52,7 +52,7 @@ func (bf *basicFinder) IterateNearbyStations() <-chan stationfindertype.ChargeSt
copy(results, bf.searchResp.Results)
bf.searchRespLock.RUnlock()

c := make(chan stationfindertype.ChargeStationInfo, size)
c := make(chan *stationfindertype.ChargeStationInfo, size)
go func() {
defer close(c)
for _, result := range results {
Expand All @@ -65,7 +65,7 @@ func (bf *basicFinder) IterateNearbyStations() <-chan stationfindertype.ChargeSt
Lat: result.Place.Address[0].GeoCoordinate.Latitude,
Lon: result.Place.Address[0].GeoCoordinate.Longitude},
}
c <- station
c <- &station
}
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ import (
"github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype"
)

var mockChargeStationInfo1 []stationfindertype.ChargeStationInfo = []stationfindertype.ChargeStationInfo{
stationfindertype.ChargeStationInfo{
var mockChargeStationInfo1 = []*stationfindertype.ChargeStationInfo{
{
ID: "station1",
Location: nav.Location{
Lat: 32.333,
Lon: 122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station2",
Location: nav.Location{
Lat: -32.333,
Lon: -122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station3",
Location: nav.Location{
Lat: 32.333,
Lon: -122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station4",
Location: nav.Location{
Lat: -32.333,
Expand All @@ -41,22 +41,22 @@ var mockChargeStationInfo1 []stationfindertype.ChargeStationInfo = []stationfind
},
}

var mockChargeStationInfo2 []stationfindertype.ChargeStationInfo = []stationfindertype.ChargeStationInfo{
stationfindertype.ChargeStationInfo{
var mockChargeStationInfo2 = []*stationfindertype.ChargeStationInfo{
{
ID: "station1",
Location: nav.Location{
Lat: 32.333,
Lon: 122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station2",
Location: nav.Location{
Lat: -32.333,
Lon: -122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station5",
Location: nav.Location{
Lat: -12.333,
Expand All @@ -65,15 +65,15 @@ var mockChargeStationInfo2 []stationfindertype.ChargeStationInfo = []stationfind
},
}

var mockChargeStationInfo3 []stationfindertype.ChargeStationInfo = []stationfindertype.ChargeStationInfo{
stationfindertype.ChargeStationInfo{
var mockChargeStationInfo3 = []*stationfindertype.ChargeStationInfo{
{
ID: "station6",
Location: nav.Location{
Lat: 30.333,
Lon: 122.333,
},
},
stationfindertype.ChargeStationInfo{
{
ID: "station7",
Location: nav.Location{
Lat: -10.333,
Expand All @@ -85,7 +85,7 @@ var mockChargeStationInfo3 []stationfindertype.ChargeStationInfo = []stationfind
func TestBasicFinderCorrectness(t *testing.T) {
cases := []struct {
input []*nearbychargestation.Result
expect []stationfindertype.ChargeStationInfo
expect []*stationfindertype.ChargeStationInfo
}{
{
nearbychargestation.MockSearchResponse1.Results,
Expand All @@ -105,7 +105,7 @@ func TestBasicFinderCorrectness(t *testing.T) {
wg.Add(1)
defer wg.Done()

var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo
for item := range c {
r = append(r, item)
}
Expand All @@ -122,7 +122,7 @@ func TestBasicFinderAsync(t *testing.T) {
cases := []struct {
input []*nearbychargestation.Result
inputLock *sync.RWMutex
expect []stationfindertype.ChargeStationInfo
expect []*stationfindertype.ChargeStationInfo
}{
{
nearbychargestation.MockSearchResponse1.Results,
Expand All @@ -145,7 +145,7 @@ func TestBasicFinderAsync(t *testing.T) {
c := bf.IterateNearbyStations()
go func(wg *sync.WaitGroup) {
defer wg.Done()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo
for item := range c {
r = append(r, item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func (sf *destStationFinder) prepare() {
return
}

func (sf *destStationFinder) IterateNearbyStations() <-chan stationfindertype.ChargeStationInfo {
func (sf *destStationFinder) IterateNearbyStations() <-chan *stationfindertype.ChargeStationInfo {
return sf.bf.IterateNearbyStations()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestDestStationFinderIterator(t *testing.T) {
sf := CreateMockDestStationFinder1()
c := sf.IterateNearbyStations()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo
go func() {
for item := range c {
r = append(r, item)
Expand Down
4 changes: 4 additions & 0 deletions integration/service/oasis/stationfinder/cloudfinder/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package cloudfinder provides spatial query based on Telenav's cloud web service.
*/
package cloudfinder
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype"
)

const lowEnergyLocationCandidateNumber = 20
// LowEnergyLocationCandidateNumber indicates how much charge station to be searched for low energy point
const LowEnergyLocationCandidateNumber = 20

type lowEnergyLocationStationFinder struct {
location *nav.Location
Expand All @@ -29,12 +30,12 @@ func (sf *lowEnergyLocationStationFinder) prepare() {
searchcoordinate.Coordinate{
Lat: sf.location.Lat,
Lon: sf.location.Lon},
lowEnergyLocationCandidateNumber,
LowEnergyLocationCandidateNumber,
-1)
sf.bf.getNearbyChargeStations(req)
return
}

func (sf *lowEnergyLocationStationFinder) IterateNearbyStations() <-chan stationfindertype.ChargeStationInfo {
func (sf *lowEnergyLocationStationFinder) IterateNearbyStations() <-chan *stationfindertype.ChargeStationInfo {
return sf.bf.IterateNearbyStations()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestLowEnergyLocationStationFinderIterator1(t *testing.T) {
defer wg.Done()

c := sf.IterateNearbyStations()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo

for item := range c {
r = append(r, item)
Expand All @@ -37,7 +37,7 @@ func TestLowEnergyLocationStationFinderIterator2(t *testing.T) {
defer wg.Done()

c := sf.IterateNearbyStations()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo
for item := range c {
r = append(r, item)
}
Expand All @@ -56,7 +56,7 @@ func TestLowEnergyLocationStationFinderIterator3(t *testing.T) {
defer wg.Done()

c := sf.IterateNearbyStations()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo
for item := range c {
r = append(r, item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func (sf *origStationFinder) prepare() {
return
}

func (sf *origStationFinder) IterateNearbyStations() <-chan stationfindertype.ChargeStationInfo {
func (sf *origStationFinder) IterateNearbyStations() <-chan *stationfindertype.ChargeStationInfo {
return sf.bf.IterateNearbyStations()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestOrigStationFinderIterator(t *testing.T) {
sf := CreateMockOrigStationFinder1()
c := sf.IterateNearbyStations()
var r []stationfindertype.ChargeStationInfo
var r []*stationfindertype.ChargeStationInfo

isdoneC := make(chan bool)
go func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
"github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype"
)

type tnSearchStationFinder struct {
type cloudStationFinder struct {
sc *searchconnector.TNSearchConnector
}

// New creates finder based on telenav search web service
func New(sc *searchconnector.TNSearchConnector) *tnSearchStationFinder {
return &tnSearchStationFinder{
func New(sc *searchconnector.TNSearchConnector) *cloudStationFinder {
return &cloudStationFinder{
sc: sc,
}
}

// NewOrigStationFinder creates finder to search for nearby charge stations near orig based on telenav search
func (finder *tnSearchStationFinder) NewOrigStationFinder(oasisReq *oasis.Request) stationfindertype.NearbyStationsIterator {
func (finder *cloudStationFinder) NewOrigStationFinder(oasisReq *oasis.Request) stationfindertype.NearbyStationsIterator {
return NewOrigStationFinder(finder.sc, oasisReq)
}

// NewDestStationFinder creates finder to search for nearby charge stations near destination based on telenav search
func (finder *tnSearchStationFinder) NewDestStationFinder(oasisReq *oasis.Request) stationfindertype.NearbyStationsIterator {
func (finder *cloudStationFinder) NewDestStationFinder(oasisReq *oasis.Request) stationfindertype.NearbyStationsIterator {
return NewDestStationFinder(finder.sc, oasisReq)
}

// NewLowEnergyLocationStationFinder creates finder to search for nearby charge stations when energy is low based on telenav search
func (finder *tnSearchStationFinder) NewLowEnergyLocationStationFinder(location *nav.Location) stationfindertype.NearbyStationsIterator {
func (finder *cloudStationFinder) NewLowEnergyLocationStationFinder(location *nav.Location) stationfindertype.NearbyStationsIterator {
return NewLowEnergyLocationStationFinder(finder.sc, location)
}
2 changes: 1 addition & 1 deletion integration/service/oasis/stationfinder/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StationFinder interface {
type Algorithm interface {
// FindOverlapBetweenStations finds overlap charge stations based on two iterator
FindOverlapBetweenStations(iterF stationfindertype.NearbyStationsIterator,
iterS stationfindertype.NearbyStationsIterator) []stationfindertype.ChargeStationInfo
iterS stationfindertype.NearbyStationsIterator) []*stationfindertype.ChargeStationInfo

// CalcWeightBetweenChargeStationsPair accepts two iterators and calculates weights between each pair of iterators
CalcWeightBetweenChargeStationsPair(from stationfindertype.NearbyStationsIterator,
Expand Down
Loading

0 comments on commit 10eeb70

Please sign in to comment.