This commit is contained in:
2025-11-15 17:53:50 +01:00
parent 1bebded484
commit 5dc296805a
15 changed files with 634 additions and 688 deletions

View File

@@ -41,8 +41,8 @@ func LoadTripData(path string) (*TripData, error) {
err = ParseRoutes(f, func(r types.Route) {
tp.Routes[r.RouteID] = &r
if ag, ok := tp.Agencies[r.AgencyID]; ok {
r.Agency = ag
ag.AddRoute(&r)
r.SetAgency(ag)
// ag.AddRoute(&r)
}
})
case "stops":
@@ -88,7 +88,7 @@ func LoadTripData(path string) (*TripData, error) {
//transfers = append(transfers, tr)
stop, ok := tp.Stops[tr.FromStopId]
if ok {
stop.AddTransfer(&tr)
stop.AddTransfer(&tr, tp.Stops[tr.ToStopId])
} else {
log.Printf("stop %s not found for transfer", tr.FromStopId)
}

View File

@@ -6,12 +6,12 @@ type Agency struct {
AgencyURL string `json:"agency_url" csv:"agency_url"`
AgencyTimezone string `json:"agency_timezone" csv:"agency_timezone"`
AgencyLang string `json:"agency_lang" csv:"agency_lang"`
Routes map[string]*Route
//Routes map[string]*Route
}
func (a *Agency) AddRoute(route *Route) {
if a.Routes == nil {
a.Routes = make(map[string]*Route)
}
a.Routes[route.RouteID] = route
}
// func (a *Agency) AddRoute(route *Route) {
// if a.Routes == nil {
// a.Routes = make(map[string]*Route)
// }
// a.Routes[route.RouteID] = route
// }

View File

@@ -1,10 +1,10 @@
package types
type Route struct {
Agency *Agency `json:"agency" csv:"agency"`
Agency *Agency `json:"agency" csv:"-"`
Trips []*Trip `json:"trips" csv:"trips"`
RouteID string `json:"route_id" csv:"route_id"`
AgencyID string `json:"agency_id" csv:"agency_id"`
RouteId string `json:"route_id" csv:"route_id"`
AgencyId string `json:"agency_id" csv:"agency_id"`
RouteShortName string `json:"route_short_name" csv:"route_short_name"`
RouteLongName string `json:"route_long_name" csv:"route_long_name"`
RouteType int `json:"route_type" csv:"route_type"`

View File

@@ -16,7 +16,8 @@ type Stop struct {
Transfers []*Transfer `json:"-" csv:"transfers"`
}
func (s *Stop) AddTransfer(transfer *Transfer) {
func (s *Stop) AddTransfer(transfer *Transfer, toStop *Stop) {
transfer.ToStop = toStop
if s.Transfers == nil {
s.Transfers = make([]*Transfer, 0)
}
@@ -24,7 +25,6 @@ func (s *Stop) AddTransfer(transfer *Transfer) {
}
func (s *Stop) AddTrip(trip *Trip) {
s.Trips[trip.TripId] = trip
}
@@ -39,7 +39,7 @@ func (s *Stop) GetTripsAfter(when time.Time) iter.Seq[*TripWithDepartureTime] {
for _, trip := range s.Trips {
for _, stop := range trip.Stops {
if stop.StopId == s.StopId && stop.ArrivalTime >= startAfterMidnight {
if stop.StopId == s.StopId && stop.ArrivalTime >= startAfterMidnight && stop.PickupType == 0 {
if !yield(&TripWithDepartureTime{Trip: trip, DepartureTime: stop.DepartureTime}) {
return
@@ -86,21 +86,44 @@ func (s *Stop) GetUpcomingStops(start *StopTime) iter.Seq[*StopTime] {
func (s *Stop) GetStopsAfter(when time.Time) iter.Seq2[*StopTime, *StopTime] {
startAfterMidnight := AsSecondsAfterMidnight(when)
var first *StopTime
return func(yield func(start, stop *StopTime) bool) {
for _, trip := range s.Trips {
found := false
found := -1
var start *StopTime
for _, stop := range trip.Stops {
if stop.StopId == s.StopId && stop.ArrivalTime >= startAfterMidnight {
found = true
found = stop.StopSequence
start = stop
if first == nil || start.ArrivalTime < first.ArrivalTime {
first = start
}
}
if found {
if found != -1 && stop.StopSequence > found && stop.PickupType == 0 {
if !yield(start, stop) {
return
}
}
}
}
if first == nil {
return
}
for _, transfer := range s.Transfers {
if transfer.FromStopId == s.StopId {
if !yield(first, &StopTime{
Stop: transfer.ToStop,
TripId: "transfer",
ArrivalTime: startAfterMidnight + SecondsAfterMidnight(transfer.MinTransferTime),
DepartureTime: startAfterMidnight + SecondsAfterMidnight(transfer.MinTransferTime),
StopId: transfer.ToStopId,
}) {
return
}
}
}
}

View File

@@ -1,6 +1,7 @@
package types
type Transfer struct {
ToStop *Stop `json:"-" csv:"-"`
FromStopId string `json:"from_stop_id" csv:"from_stop_id"`
ToStopId string `json:"to_stop_id" csv:"to_stop_id"`
TransferType int `json:"transfer_type" csv:"transfer_type"`

View File

@@ -42,7 +42,28 @@ func (t *Trip) AddStopTime(stopTime *StopTime) {
if t.Stops == nil {
t.Stops = make([]*StopTime, 0)
}
t.Stops = append(t.Stops, stopTime)
// Find the index to insert based on StopSequence
idx := 0
for i, st := range t.Stops {
if stopTime.StopSequence < st.StopSequence {
idx = i
break
}
idx = i + 1
}
// Insert at the correct position
t.Stops = append(t.Stops[:idx], append([]*StopTime{stopTime}, t.Stops[idx:]...)...)
// Adjust times if necessary for next-day continuation
for i := idx; i < len(t.Stops)-1; i++ {
curr := t.Stops[i]
next := t.Stops[i+1]
if next.ArrivalTime < curr.ArrivalTime {
next.ArrivalTime += 86400
}
if next.DepartureTime < curr.DepartureTime {
next.DepartureTime += 86400
}
}
}
func (t *Trip) Has(stop *Stop) (*StopTime, bool) {