-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjourneys.go
147 lines (114 loc) · 4.31 KB
/
journeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package navitia
import (
"net/url"
"time"
"github.com/govitia/navitia/types"
"github.com/govitia/navitia/utils"
)
const journeysEndpoint = "journeys"
// JourneyResults contains the results of a Journey request.
// Warning: types.Journey.From / types.Journey.To aren't guaranteed to be filled.
// Based on very basic inspection, it seems they aren't filled when there are sections...
type JourneyResults struct {
Journeys []types.Journey `json:"journeys"`
Paging Paging `json:"links"`
Logging `json:"-"`
session *Session
}
// Count returns the number of results available in a JourneyResults
func (jr *JourneyResults) Count() int {
return len(jr.Journeys)
}
// JourneyRequest contain the parameters needed to make a Journey request
type JourneyRequest struct {
// There must be at least one From or To parameter defined
// When used with just one of them, the resulting Journey won't have a populated Sections field.
From types.ID
To types.ID
// When do you want to depart ? Or is DateIsArrival when do you want to arrive at your destination.
Date time.Time
DateIsArrival bool
// The traveller's type
Traveler types.TravelerType
// Define the freshness of data to use to compute journeys
Freshness types.DataFreshness
// Forbidden public transport objects
Forbidden []types.ID
// Allowed public transport objects
// Note: This counstraint intersects with Forbidden
Allowed []types.ID
// Force the first section mode if it isn't a public transport mode
// Note: The parameter is inclusive, not exclusive. As such if you want to forbid a mode you have to include all modes except that one.
FirstSectionModes []string
// Same, but for the last section
LastSectionModes []string
// MaxDurationToPT is the maximum allowed duration to reach the public transport.
// Use this to limit the walking/biking part.
MaxDurationToPT time.Duration
// These four following parameters set the speed of each mode (Walking, Bike, BSS & car)
// In meters per second
WalkingSpeed float64
BikeSpeed float64
BikeShareSpeed float64
CarSpeed float64
// Minimum and maximum amounts of journeys suggested
MinJourneys uint
MaxJourneys uint
// Count fixes the amount of journeys to be returned, overriding minimum & maximum amount
// Note: if Count=0 then it isn't taken into account
Count uint
// Maximum number of transfers in each journey
MaxTransfers uint
// Maximum duration of a trip
MaxDuration time.Duration // To seconds
// Wheelchair restricts the answer to accessible public transports
Wheelchair bool
// Headsign If given, add a filter on the vehicle journeys that has the
// given value as headsign (on vehicle journey itself or at a stop time).
Headsign string
}
// toURL formats a journey request to url
// Should be refactored using a switch statement
func (req JourneyRequest) toURL() (url.Values, error) {
rb := utils.NewRequestBuilder()
// Encode the from and to
rb.AddString("from", string(req.From))
rb.AddString("to", string(req.To))
if !req.Date.IsZero() {
rb.AddDateTime("datetime", req.Date)
if req.DateIsArrival {
rb.AddString("datetime_represents", "arrival")
}
}
rb.AddString("traveler_type", string(req.Traveler))
rb.AddString("data_freshness", string(req.Freshness))
rb.AddIDSlice("forbidden_uris[]", req.Forbidden)
rb.AddIDSlice("allowed_id[]", req.Allowed)
rb.AddMode("first_section_mode[]", req.FirstSectionModes)
rb.AddMode("last_section_mode[]", req.LastSectionModes)
// max_duration_to_pt
rb.AddInt("max_duration_to_pt", int(req.MaxDurationToPT/time.Second))
// walking_speed, bike_speed, bss_speed & car_speed
rb.AddFloat64("walking_speed", req.WalkingSpeed)
rb.AddFloat64("bike_speed", req.BikeSpeed)
rb.AddFloat64("bss_speed", req.BikeShareSpeed)
rb.AddFloat64("car_speed", req.CarSpeed)
// If count is defined don't bother with the minimimal and maximum amount of items to return
if req.Count != 0 {
rb.AddUInt("count", req.Count)
} else {
rb.AddUInt("min_nb_journeys", req.MinJourneys)
rb.AddUInt("max_nb_journeys", req.MaxJourneys)
}
// max_nb_transfers
rb.AddUInt("max_nb_transfers", req.MaxTransfers)
// max_duration
rb.AddInt("max_duration", int(req.MaxDuration/time.Second))
// headsign
rb.AddString("headsign", req.Headsign)
// wheelchair
if req.Wheelchair {
rb.AddString("wheelchair", "true")
}
return rb.Values(), nil
}