@opentripplanner/core-utils 9.0.0-alpha.3 → 9.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/itinerary.js +4 -3
- package/esm/itinerary.js.map +1 -1
- package/esm/query-gen.js +62 -16
- package/esm/query-gen.js.map +1 -1
- package/lib/itinerary.d.ts +7 -7
- package/lib/itinerary.d.ts.map +1 -1
- package/lib/itinerary.js +4 -3
- package/lib/itinerary.js.map +1 -1
- package/lib/query-gen.d.ts +25 -3
- package/lib/query-gen.d.ts.map +1 -1
- package/lib/query-gen.js +55 -14
- package/lib/query-gen.js.map +1 -1
- package/package.json +3 -4
- package/src/__tests__/query-params.js +61 -2
- package/src/itinerary.ts +14 -8
- package/src/planQuery.graphql +39 -1
- package/src/query-gen.ts +101 -47
- package/tsconfig.tsbuildinfo +1 -1
package/src/itinerary.ts
CHANGED
|
@@ -4,9 +4,10 @@ import {
|
|
|
4
4
|
Config,
|
|
5
5
|
ElevationProfile,
|
|
6
6
|
FlexBookingInfo,
|
|
7
|
-
|
|
7
|
+
ItineraryOnlyLegsRequired,
|
|
8
8
|
LatLngArray,
|
|
9
9
|
Leg,
|
|
10
|
+
MassUnitOption,
|
|
10
11
|
Money,
|
|
11
12
|
Place,
|
|
12
13
|
Step,
|
|
@@ -212,7 +213,9 @@ export function getCompanyFromLeg(leg: Leg): string {
|
|
|
212
213
|
return null;
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
export function getItineraryBounds(
|
|
216
|
+
export function getItineraryBounds(
|
|
217
|
+
itinerary: ItineraryOnlyLegsRequired
|
|
218
|
+
): LatLngArray[] {
|
|
216
219
|
let coords = [];
|
|
217
220
|
itinerary.legs.forEach(leg => {
|
|
218
221
|
const legCoords = polyline
|
|
@@ -407,7 +410,7 @@ export function getTNCLocation(leg: Leg, type: string): string {
|
|
|
407
410
|
}
|
|
408
411
|
|
|
409
412
|
export function calculatePhysicalActivity(
|
|
410
|
-
itinerary:
|
|
413
|
+
itinerary: ItineraryOnlyLegsRequired
|
|
411
414
|
): {
|
|
412
415
|
bikeDuration: number;
|
|
413
416
|
caloriesBurned: number;
|
|
@@ -433,7 +436,9 @@ export function calculatePhysicalActivity(
|
|
|
433
436
|
* these values and currency info.
|
|
434
437
|
* It is assumed that the same currency is used for all TNC legs.
|
|
435
438
|
*/
|
|
436
|
-
export function calculateTncFares(
|
|
439
|
+
export function calculateTncFares(
|
|
440
|
+
itinerary: ItineraryOnlyLegsRequired
|
|
441
|
+
): TncFare {
|
|
437
442
|
return itinerary.legs
|
|
438
443
|
.filter(leg => leg.mode === "CAR" && leg.hailedCar && leg.tncData)
|
|
439
444
|
.reduce(
|
|
@@ -501,15 +506,16 @@ const CARBON_INTENSITY_DEFAULTS = {
|
|
|
501
506
|
};
|
|
502
507
|
|
|
503
508
|
/**
|
|
504
|
-
* @param
|
|
505
|
-
* @param
|
|
509
|
+
* @param {itinerary} itinerary OTP trip itinierary, only legs is required.
|
|
510
|
+
* @param {carbonIntensity} carbonIntensity carbon intensity by mode in grams/meter
|
|
506
511
|
* @param {units} units units to be used in return value
|
|
507
512
|
* @return Amount of carbon in chosen unit
|
|
508
513
|
*/
|
|
509
514
|
export function calculateEmissions(
|
|
510
|
-
|
|
515
|
+
// This type makes all the properties from Itinerary optional except legs.
|
|
516
|
+
itinerary: ItineraryOnlyLegsRequired,
|
|
511
517
|
carbonIntensity: Record<string, number> = {},
|
|
512
|
-
units?:
|
|
518
|
+
units?: MassUnitOption
|
|
513
519
|
): number {
|
|
514
520
|
// Apply defaults for any values that we don't have.
|
|
515
521
|
const carbonIntensityWithDefaults = {
|
package/src/planQuery.graphql
CHANGED
|
@@ -46,11 +46,21 @@ query PlanQuery(
|
|
|
46
46
|
realtimeState
|
|
47
47
|
startTime
|
|
48
48
|
transitLeg
|
|
49
|
+
trip {
|
|
50
|
+
id
|
|
51
|
+
gtfsId
|
|
52
|
+
}
|
|
49
53
|
agency {
|
|
50
54
|
name
|
|
51
55
|
id
|
|
52
56
|
timezone
|
|
53
57
|
url
|
|
58
|
+
alerts {
|
|
59
|
+
alertHeaderText
|
|
60
|
+
alertDescriptionText
|
|
61
|
+
alertUrl
|
|
62
|
+
effectiveStartDate
|
|
63
|
+
}
|
|
54
64
|
}
|
|
55
65
|
legGeometry {
|
|
56
66
|
length
|
|
@@ -70,6 +80,12 @@ query PlanQuery(
|
|
|
70
80
|
textColor
|
|
71
81
|
id
|
|
72
82
|
type
|
|
83
|
+
alerts {
|
|
84
|
+
alertHeaderText
|
|
85
|
+
alertDescriptionText
|
|
86
|
+
alertUrl
|
|
87
|
+
effectiveStartDate
|
|
88
|
+
}
|
|
73
89
|
}
|
|
74
90
|
from {
|
|
75
91
|
lat
|
|
@@ -79,7 +95,12 @@ query PlanQuery(
|
|
|
79
95
|
stop {
|
|
80
96
|
id
|
|
81
97
|
code
|
|
82
|
-
|
|
98
|
+
alerts {
|
|
99
|
+
alertHeaderText
|
|
100
|
+
alertDescriptionText
|
|
101
|
+
alertUrl
|
|
102
|
+
effectiveStartDate
|
|
103
|
+
}
|
|
83
104
|
}
|
|
84
105
|
}
|
|
85
106
|
to {
|
|
@@ -90,12 +111,29 @@ query PlanQuery(
|
|
|
90
111
|
stop {
|
|
91
112
|
id
|
|
92
113
|
code
|
|
114
|
+
alerts {
|
|
115
|
+
alertHeaderText
|
|
116
|
+
alertDescriptionText
|
|
117
|
+
alertUrl
|
|
118
|
+
effectiveStartDate
|
|
119
|
+
}
|
|
93
120
|
}
|
|
94
121
|
}
|
|
95
122
|
steps {
|
|
96
123
|
distance
|
|
97
124
|
lat
|
|
98
125
|
lon
|
|
126
|
+
relativeDirection
|
|
127
|
+
absoluteDirection
|
|
128
|
+
stayOn
|
|
129
|
+
streetName
|
|
130
|
+
area
|
|
131
|
+
alerts{
|
|
132
|
+
alertHeaderText
|
|
133
|
+
alertDescriptionText
|
|
134
|
+
alertUrl
|
|
135
|
+
effectiveStartDate
|
|
136
|
+
}
|
|
99
137
|
elevationProfile {
|
|
100
138
|
distance
|
|
101
139
|
elevation
|
package/src/query-gen.ts
CHANGED
|
@@ -9,12 +9,52 @@ import { LonLatOutput } from "@conveyal/lonlat";
|
|
|
9
9
|
import PlanQuery from "./planQuery.graphql";
|
|
10
10
|
|
|
11
11
|
type OTPQueryParams = {
|
|
12
|
-
to: LonLatOutput;
|
|
13
|
-
from: LonLatOutput;
|
|
12
|
+
to: LonLatOutput & { name?: string };
|
|
13
|
+
from: LonLatOutput & { name?: string };
|
|
14
14
|
modes: Array<TransportMode>;
|
|
15
15
|
modeSettings: ModeSetting[];
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
type GraphQLQuery = {
|
|
19
|
+
query: string;
|
|
20
|
+
variables: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Mode Settings can contain additional modes to add to the query,
|
|
25
|
+
* this function extracts those additional modes from the settings
|
|
26
|
+
* and returns them in an array.
|
|
27
|
+
* @param modeSettings List of mode settings with values populated
|
|
28
|
+
* @returns Additional transport modes to add to query
|
|
29
|
+
*/
|
|
30
|
+
export function extractAdditionalModes(
|
|
31
|
+
modeSettings: ModeSetting[],
|
|
32
|
+
enabledModes: TransportMode[]
|
|
33
|
+
): Array<TransportMode> {
|
|
34
|
+
return modeSettings.reduce<TransportMode[]>((prev, cur) => {
|
|
35
|
+
// First, ensure that the mode associated with this setting is even enabled
|
|
36
|
+
if (!enabledModes.map(m => m.mode).includes(cur.applicableMode)) {
|
|
37
|
+
return prev;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// In checkboxes, mode must be enabled and have a transport mode in it
|
|
41
|
+
if (cur.type === "CHECKBOX" && cur.addTransportMode && cur.value) {
|
|
42
|
+
return [...prev, cur.addTransportMode];
|
|
43
|
+
}
|
|
44
|
+
if (cur.type === "DROPDOWN") {
|
|
45
|
+
const transportMode = cur.options.find(o => o.value === cur.value)
|
|
46
|
+
.addTransportMode;
|
|
47
|
+
if (transportMode) {
|
|
48
|
+
return [
|
|
49
|
+
...prev,
|
|
50
|
+
cur.options.find(o => o.value === cur.value).addTransportMode
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return prev;
|
|
55
|
+
}, []);
|
|
56
|
+
}
|
|
57
|
+
|
|
18
58
|
/**
|
|
19
59
|
* Generates every possible mathematical subset of the input TransportModes.
|
|
20
60
|
* Uses code from:
|
|
@@ -32,6 +72,7 @@ function combinations(array: TransportMode[]): TransportMode[][] {
|
|
|
32
72
|
.map((e1, i) => array.filter((e2, j) => i & (1 << j)))
|
|
33
73
|
);
|
|
34
74
|
}
|
|
75
|
+
|
|
35
76
|
export const SIMPLIFICATIONS = {
|
|
36
77
|
AIRPLANE: "TRANSIT",
|
|
37
78
|
BICYCLE: "PERSONAL",
|
|
@@ -57,6 +98,12 @@ export const TRANSIT_SUBMODES_AND_TRANSIT = Object.keys(SIMPLIFICATIONS).filter(
|
|
|
57
98
|
mode => SIMPLIFICATIONS[mode] === "TRANSIT"
|
|
58
99
|
);
|
|
59
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Generates list of queries for OTP to get a comprehensive
|
|
103
|
+
* set of results based on the modes input.
|
|
104
|
+
* @param params OTP Query Params
|
|
105
|
+
* @returns Set of parameters to generate queries
|
|
106
|
+
*/
|
|
60
107
|
export function generateCombinations(params: OTPQueryParams): OTPQueryParams[] {
|
|
61
108
|
const VALID_COMBOS = [
|
|
62
109
|
["WALK"],
|
|
@@ -70,56 +117,65 @@ export function generateCombinations(params: OTPQueryParams): OTPQueryParams[] {
|
|
|
70
117
|
|
|
71
118
|
const BANNED_TOGETHER = ["SCOOTER", "BICYCLE"];
|
|
72
119
|
|
|
73
|
-
|
|
74
|
-
|
|
120
|
+
const completeModeList = [
|
|
121
|
+
...extractAdditionalModes(params.modeSettings, params.modes),
|
|
122
|
+
...params.modes
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
// List of the transit *submodes* that are included in the input params
|
|
126
|
+
const queryTransitSubmodes = completeModeList
|
|
75
127
|
.filter(mode => TRANSIT_SUBMODES.includes(mode.mode))
|
|
76
128
|
.map(mode => mode.mode);
|
|
77
129
|
|
|
78
|
-
return (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
return prev;
|
|
99
|
-
}, queryTransitSubmodes.length) !== 0
|
|
100
|
-
) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
130
|
+
return combinations(completeModeList)
|
|
131
|
+
.filter(combo => {
|
|
132
|
+
if (combo.length === 0) return false;
|
|
133
|
+
|
|
134
|
+
// All current qualifiers currently simplify to "SHARED"
|
|
135
|
+
const simplifiedModes = Array.from(
|
|
136
|
+
new Set(
|
|
137
|
+
combo.map(c => (c.qualifier ? "SHARED" : SIMPLIFICATIONS[c.mode]))
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// Ensure that if we have one transit mode, then we include ALL transit modes
|
|
142
|
+
if (simplifiedModes.includes("TRANSIT")) {
|
|
143
|
+
// Don't allow TRANSIT along with any other submodes
|
|
144
|
+
if (
|
|
145
|
+
queryTransitSubmodes.length &&
|
|
146
|
+
combo.find(c => c.mode === "TRANSIT")
|
|
147
|
+
) {
|
|
148
|
+
return false;
|
|
103
149
|
}
|
|
104
150
|
|
|
105
|
-
|
|
106
|
-
|
|
151
|
+
if (
|
|
152
|
+
combo.reduce((prev, cur) => {
|
|
153
|
+
if (queryTransitSubmodes.includes(cur.mode)) {
|
|
154
|
+
return prev - 1;
|
|
155
|
+
}
|
|
156
|
+
return prev;
|
|
157
|
+
}, queryTransitSubmodes.length) !== 0
|
|
158
|
+
) {
|
|
107
159
|
return false;
|
|
160
|
+
}
|
|
161
|
+
// Continue to the other checks
|
|
162
|
+
}
|
|
108
163
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
164
|
+
// OTP doesn't support multiple non-walk modes
|
|
165
|
+
if (BANNED_TOGETHER.every(m => combo.find(c => c.mode === m)))
|
|
166
|
+
return false;
|
|
167
|
+
|
|
168
|
+
return !!VALID_COMBOS.find(
|
|
169
|
+
vc =>
|
|
170
|
+
simplifiedModes.every(m => vc.includes(m)) &&
|
|
171
|
+
vc.every(m => simplifiedModes.includes(m))
|
|
172
|
+
);
|
|
173
|
+
})
|
|
174
|
+
.map(combo => ({ ...params, modes: combo }));
|
|
119
175
|
}
|
|
120
176
|
|
|
121
177
|
// eslint-disable-next-line import/prefer-default-export
|
|
122
|
-
export function generateOtp2Query(params: OTPQueryParams):
|
|
178
|
+
export function generateOtp2Query(params: OTPQueryParams): GraphQLQuery {
|
|
123
179
|
const { to, from, modeSettings } = params;
|
|
124
180
|
|
|
125
181
|
// This extracts the values from the mode settings to key value pairs
|
|
@@ -132,17 +188,15 @@ export function generateOtp2Query(params: OTPQueryParams): any {
|
|
|
132
188
|
walkReluctance,
|
|
133
189
|
wheelchair,
|
|
134
190
|
bikeReluctance,
|
|
135
|
-
carReluctance
|
|
136
|
-
allowBikeRental
|
|
191
|
+
carReluctance
|
|
137
192
|
} = modeSettingValues;
|
|
138
193
|
|
|
139
194
|
return {
|
|
140
195
|
query: print(PlanQuery),
|
|
141
196
|
variables: {
|
|
142
|
-
fromPlace:
|
|
143
|
-
toPlace:
|
|
197
|
+
fromPlace: `${from.name}::${from.lat},${from.lon}}`,
|
|
198
|
+
toPlace: `${to.name}::${to.lat},${to.lon}}`,
|
|
144
199
|
modes: params.modes,
|
|
145
|
-
allowBikeRental,
|
|
146
200
|
walkReluctance,
|
|
147
201
|
wheelchair,
|
|
148
202
|
bikeReluctance,
|