Skip to content

Commit a18c412

Browse files
committed
Disambiguate stops and platforms
In the GTFS reference a stop is different to a platform, based on whether the stop has a parent station.
1 parent d9cd987 commit a18c412

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

enums.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,15 @@ func (t RouteType) String() string {
242242
type StopType int32
243243

244244
const (
245-
StopType_Platform StopType = 0
245+
StopType_Stop StopType = 0
246246
StopType_Station StopType = 1
247247
StopType_EntranceOrExit StopType = 2
248248
StopType_GenericNode StopType = 3
249249
StopType_BoardingArea StopType = 4
250+
StopType_Platform StopType = 5
250251
)
251252

252-
func parseStopType(s string) StopType {
253+
func parseStopType(s string, hasParentStop bool) StopType {
253254
switch s {
254255
case "1":
255256
return StopType_Station
@@ -260,12 +261,18 @@ func parseStopType(s string) StopType {
260261
case "4":
261262
return StopType_BoardingArea
262263
default:
263-
return StopType_Platform
264+
if hasParentStop {
265+
return StopType_Platform
266+
} else {
267+
return StopType_Stop
268+
}
264269
}
265270
}
266271

267272
func (t StopType) String() string {
268273
switch t {
274+
case StopType_Stop:
275+
return "STOP"
269276
case StopType_Platform:
270277
return "PLATFORM"
271278
case StopType_Station:

realtime.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ func ParseRealtime(content []byte, opts *ParseRealtimeOptions) (*Realtime, error
269269
var ok bool
270270

271271
if tripUpdate := entity.TripUpdate; tripUpdate != nil {
272-
trip, vehicle, ok = parseTripUpdate(tripUpdate, opts, feedMessage.GetHeader().GetTimestamp())
272+
trip, vehicle, ok = parseTripUpdate(tripUpdate, opts)
273273
} else if vehiclePosition := entity.Vehicle; vehiclePosition != nil {
274-
trip, vehicle = parseVehicle(vehiclePosition, opts, feedMessage.GetHeader().GetTimestamp())
274+
trip, vehicle = parseVehicle(vehiclePosition, opts)
275275
ok = true
276276
} else if entityAlert := entity.Alert; entityAlert != nil {
277277
alert, alertTrips = parseAlert(entity.GetId(), entityAlert, opts)
@@ -337,7 +337,7 @@ func ParseRealtime(content []byte, opts *ParseRealtimeOptions) (*Realtime, error
337337
return &result, nil
338338
}
339339

340-
func parseTripUpdate(tripUpdate *gtfsrt.TripUpdate, opts *ParseRealtimeOptions, feedCreatedAt uint64) (*Trip, *Vehicle, bool) {
340+
func parseTripUpdate(tripUpdate *gtfsrt.TripUpdate, opts *ParseRealtimeOptions) (*Trip, *Vehicle, bool) {
341341
if tripUpdate.Trip == nil {
342342
return nil, nil, false
343343
}
@@ -375,19 +375,19 @@ func parseTripUpdate(tripUpdate *gtfsrt.TripUpdate, opts *ParseRealtimeOptions,
375375
return trip, nil, true
376376
}
377377
vehicle := &Vehicle{
378-
ID: parseVehicleDescriptor(tripUpdate.Vehicle, opts),
378+
ID: parseVehicleDescriptor(tripUpdate.Vehicle),
379379
IsEntityInMessage: false,
380380
}
381381
return trip, vehicle, true
382382
}
383383

384-
func parseVehicle(vehiclePosition *gtfsrt.VehiclePosition, opts *ParseRealtimeOptions, feedCreatedAt uint64) (*Trip, *Vehicle) {
384+
func parseVehicle(vehiclePosition *gtfsrt.VehiclePosition, opts *ParseRealtimeOptions) (*Trip, *Vehicle) {
385385
var congestionLevel = gtfsrt.VehiclePosition_UNKNOWN_CONGESTION_LEVEL
386386
if vehiclePosition.CongestionLevel != nil {
387387
congestionLevel = *vehiclePosition.CongestionLevel
388388
}
389389
vehicle := &Vehicle{
390-
ID: parseVehicleDescriptor(vehiclePosition.Vehicle, opts),
390+
ID: parseVehicleDescriptor(vehiclePosition.Vehicle),
391391
Position: convertVehiclePosition(vehiclePosition),
392392
CurrentStopSequence: vehiclePosition.CurrentStopSequence,
393393
StopID: vehiclePosition.StopId,
@@ -494,7 +494,7 @@ func parseStartDate(startDate *string, timezone *time.Location) (bool, time.Time
494494
return true, time.Date(y, time.Month(m), d, 0, 0, 0, 0, timezone)
495495
}
496496

497-
func parseVehicleDescriptor(vehicleDesc *gtfsrt.VehicleDescriptor, opts *ParseRealtimeOptions) *VehicleID {
497+
func parseVehicleDescriptor(vehicleDesc *gtfsrt.VehicleDescriptor) *VehicleID {
498498
if vehicleDesc == nil {
499499
return nil
500500
}

static.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,22 @@ func parseStops(csv *csv.File) []Stop {
432432
stopIdToIndex := map[string]int{}
433433
stopIdToParent := map[string]string{}
434434
for csv.NextRow() {
435+
stopID := idColumn.Read()
436+
hasParentStop := false
437+
if parentStopId := parentStationColumn.Read(); parentStopId != "" {
438+
stopIdToParent[stopID] = parentStopId
439+
hasParentStop = true
440+
}
435441
stop := Stop{
436-
Id: idColumn.Read(),
442+
Id: stopID,
437443
Code: codeColumn.Read(),
438444
Name: nameColumn.Read(),
439445
Description: descriptionColumn.Read(),
440446
ZoneId: zoneIdColumn.Read(),
441447
Longitude: parseFloat64(longitudeColumn.Read()),
442448
Latitude: parseFloat64(latitudeColumn.Read()),
443449
Url: urlColumn.Read(),
444-
Type: parseStopType(typeColumn.Read()),
450+
Type: parseStopType(typeColumn.Read(), hasParentStop),
445451
Timezone: timezoneColumn.Read(),
446452
WheelchairBoarding: parseWheelchairBoarding(wheelchairBoardingColumn.Read()),
447453
PlatformCode: platformCodeColumn.Read(),
@@ -451,9 +457,6 @@ func parseStops(csv *csv.File) []Stop {
451457
continue
452458
}
453459
stopIdToIndex[stop.Id] = len(stops)
454-
if parentStopId := parentStationColumn.Read(); parentStopId != "" {
455-
stopIdToParent[stop.Id] = parentStopId
456-
}
457460
stops = append(stops, stop)
458461
}
459462
for stopId, parentStopId := range stopIdToParent {

static_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ func TestParse(t *testing.T) {
227227
{
228228
Id: "a",
229229
Parent: &Stop{Id: "b"},
230+
Type: StopType_Platform,
230231
},
231232
{
232233
Id: "b",

0 commit comments

Comments
 (0)