Skip to content

Commit

Permalink
Improve transport type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronClaydon committed Nov 5, 2024
1 parent faeca2f commit e02f675
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
21 changes: 11 additions & 10 deletions pkg/ctdf/transporttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ type TransportType string

//goland:noinspection GoUnusedConst
const (
TransportTypeBus TransportType = "Bus"
TransportTypeCoach = "Coach"
TransportTypeTram = "Tram"
TransportTypeTaxi = "Taxi"
TransportTypeRail = "Rail"
TransportTypeMetro = "Metro"
TransportTypeFerry = "Ferry"
TransportTypeAirport = "Airport"
TransportTypeCableCar = "CableCar"
TransportTypeUnknown = "UNKNOWN"
TransportTypeBus TransportType = "Bus"
TransportTypeCoach = "Coach"
TransportTypeTram = "Tram"
TransportTypeTaxi = "Taxi"
TransportTypeRail = "Rail"
TransportTypeMetro = "Metro"
TransportTypeFerry = "Ferry"
TransportTypeAirport = "Airport"
TransportTypeCableCar = "CableCar"
TransportTypeFunicular = "Funicular"
TransportTypeUnknown = "UNKNOWN"
)
63 changes: 43 additions & 20 deletions pkg/dataimporter/formats/gtfs/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,6 @@ func (g *Schedule) Import(dataset datasets.DataSet, datasource *ctdf.DataSource)
}

// Routes / Services
routeTypeMapping := map[int]ctdf.TransportType{
0: ctdf.TransportTypeTram,
1: ctdf.TransportTypeMetro,
2: ctdf.TransportTypeRail,
3: ctdf.TransportTypeBus,
4: ctdf.TransportTypeFerry,
5: ctdf.TransportTypeTram,
6: ctdf.TransportTypeCableCar,
7: ctdf.TransportTypeUnknown, // Furnicular
11: ctdf.TransportTypeTram,
12: ctdf.TransportTypeUnknown, // Monorail
200: ctdf.TransportTypeCoach,
}

log.Info().Int("length", len(g.Routes)).Msg("Starting Services")
servicesQueue := NewDatabaseBatchProcessingQueue("services", 1*time.Second, 10*time.Second, 500)

Expand Down Expand Up @@ -243,11 +229,6 @@ func (g *Schedule) Import(dataset datasets.DataSet, datasource *ctdf.DataSource)
continue
}

transportType := routeTypeMapping[gtfsRoute.Type]
if transportType == "" {
transportType = ctdf.TransportTypeUnknown
}

ctdfService := &ctdf.Service{
PrimaryIdentifier: serviceID,
OtherIdentifiers: []string{
Expand All @@ -261,7 +242,7 @@ func (g *Schedule) Import(dataset datasets.DataSet, datasource *ctdf.DataSource)
Routes: []ctdf.Route{},
BrandColour: gtfsRoute.Colour,
SecondaryBrandColour: gtfsRoute.TextColour,
TransportType: transportType,
TransportType: convertTransportType(gtfsRoute.Type),
}

transforms.Transform(ctdfService, 1, "gb-dft-bods-gtfs-schedule")
Expand Down Expand Up @@ -504,6 +485,48 @@ func (g *Schedule) Import(dataset datasets.DataSet, datasource *ctdf.DataSource)
return nil
}

func convertTransportType(intType int) ctdf.TransportType {
routeTypeMapping := map[int]ctdf.TransportType{
0: ctdf.TransportTypeTram,
1: ctdf.TransportTypeMetro,
2: ctdf.TransportTypeRail,
3: ctdf.TransportTypeBus,
4: ctdf.TransportTypeFerry,
5: ctdf.TransportTypeTram,
6: ctdf.TransportTypeCableCar,
7: ctdf.TransportTypeFunicular,
11: ctdf.TransportTypeTram,
12: ctdf.TransportTypeRail, // Monorail
200: ctdf.TransportTypeCoach,
1000: ctdf.TransportTypeFerry,
// 1100: ctdf.TransportTypeAir,
1200: ctdf.TransportTypeFerry,
1400: ctdf.TransportTypeFunicular,
}

transportType := routeTypeMapping[intType]
if transportType != "" {
return transportType
}

// Extended https://developers.google.com/transit/gtfs/reference/extended-route-types
if intType >= 100 && intType <= 117 {
return ctdf.TransportTypeRail
} else if intType >= 200 && intType <= 209 {
return ctdf.TransportTypeCoach
} else if intType >= 400 && intType <= 405 {
return ctdf.TransportTypeMetro
} else if intType >= 700 && intType <= 716 {
return ctdf.TransportTypeBus
} else if intType >= 900 && intType <= 906 {
return ctdf.TransportTypeTram
} else if intType >= 1300 && intType <= 1307 {
return ctdf.TransportTypeCableCar
} else {
return ctdf.TransportTypeUnknown
}
}

func fixTimestamp(timestamp string) string {
splitTimestamp := strings.Split(timestamp, ":")

Expand Down

0 comments on commit e02f675

Please sign in to comment.