some custom stuff

This commit is contained in:
2025-11-15 00:23:48 +01:00
parent 4dbbd30d4d
commit c189e3f59a
19 changed files with 1116 additions and 831 deletions

View File

@@ -1,9 +1,9 @@
package types
type FeedInfo struct {
FeedID string `json:"feed_id" csv:"feed_id"`
FeedPublisherName string `json:"feed_publisher_name" csv:"feed_publisher_name"`
FeedPublisherURL string `json:"feed_publisher_url" csv:"feed_publisher_url"`
FeedLang string `json:"feed_lang" csv:"feed_lang"`
FeedVersion string `json:"feed_version" csv:"feed_version"`
// FeedID string `json:"feed_id" csv:"feed_id"`
PublisherName string `json:"feed_publisher_name" csv:"feed_publisher_name"`
PublisherURL string `json:"feed_publisher_url" csv:"feed_publisher_url"`
Language string `json:"feed_lang" csv:"feed_lang"`
Version string `json:"feed_version" csv:"feed_version"`
}

View File

@@ -1,17 +1,18 @@
package types
import (
"iter"
"time"
)
type Stop struct {
trips map[string]*Trip
StopID string `json:"stop_id" csv:"stop_id"`
StopName string `json:"stop_name" csv:"stop_name"`
StopLat float64 `json:"stop_lat" csv:"stop_lat"`
StopLon float64 `json:"stop_lon" csv:"stop_lon"`
LocationType int `json:"location_type" csv:"location_type"`
Transfers []*Transfer `json:"transfers" csv:"transfers"`
Trips map[string]*Trip `json:"-" csv:"-"`
StopId string `json:"stop_id" csv:"stop_id"`
StopName string `json:"stop_name" csv:"stop_name"`
StopLat float64 `json:"stop_lat" csv:"stop_lat"`
StopLon float64 `json:"stop_lon" csv:"stop_lon"`
LocationType int `json:"location_type" csv:"location_type"`
Transfers []*Transfer `json:"-" csv:"transfers"`
}
func (s *Stop) AddTransfer(transfer *Transfer) {
@@ -22,21 +23,71 @@ func (s *Stop) AddTransfer(transfer *Transfer) {
}
func (s *Stop) AddTrip(trip *Trip) {
if s.trips == nil {
s.trips = make(map[string]*Trip)
}
s.trips[trip.TripID] = trip
s.Trips[trip.TripId] = trip
}
func (s *Stop) GetTripsAfter(time time.Time) []*Trip {
var trips []*Trip
for _, trip := range s.trips {
for _, stop := range trip.Stops {
if stop.StopID == s.StopID && stop.DepartsAfter(time) {
trips = append(trips, trip)
break
type TripWithDepartureTime struct {
*Trip
DepartureTime SecondsAfterMidnight
}
func (s *Stop) GetTripsAfter(when time.Time) iter.Seq[*TripWithDepartureTime] {
startAfterMidnight := AsSecondsAfterMidnight(when)
return func(yield func(*TripWithDepartureTime) bool) {
for _, trip := range s.Trips {
for _, stop := range trip.Stops {
if stop.StopId == s.StopId && stop.ArrivalTime >= startAfterMidnight {
if !yield(&TripWithDepartureTime{Trip: trip, DepartureTime: stop.DepartureTime}) {
return
}
break
}
}
}
}
}
func (s *Stop) GetUpcomingStops(start *StopTime) iter.Seq[*StopTime] {
return func(yield func(*StopTime) bool) {
found := false
for _, trip := range s.Trips {
for _, stop := range trip.Stops {
if !found {
if stop.StopId == start.StopId && stop.DepartureTime >= start.ArrivalTime {
found = true
}
} else {
if !yield(stop) {
return
}
}
}
}
}
return trips
}
func (s *Stop) GetStopsAfter(when time.Time) iter.Seq2[*StopTime, *StopTime] {
startAfterMidnight := AsSecondsAfterMidnight(when)
return func(yield func(start, stop *StopTime) bool) {
for _, trip := range s.Trips {
found := false
var start *StopTime
for _, stop := range trip.Stops {
if stop.StopId == s.StopId && stop.ArrivalTime >= startAfterMidnight {
found = true
start = stop
}
if found {
if !yield(start, stop) {
return
}
}
}
}
}
}

View File

@@ -1,24 +1,34 @@
package types
import (
"fmt"
"iter"
"strconv"
"strings"
"time"
)
type StopTime struct {
departureTime int
Stop *Stop `json:"stop"`
TripID string `json:"trip_id" csv:"trip_id"`
ArrivalTime string `json:"arrival_time" csv:"arrival_time"`
DepartureTime string `json:"departure_time" csv:"departure_time"`
StopID string `json:"stop_id" csv:"stop_id"`
StopSequence int `json:"stop_sequence" csv:"stop_sequence"`
PickupType int `json:"pickup_type" csv:"pickup_type"`
DropOffType int `json:"drop_off_type" csv:"drop_off_type"`
type SecondsAfterMidnight int
func AsTime(s SecondsAfterMidnight) string {
h := int(s) / 3600
m := (int(s) % 3600) / 60
sec := int(s) % 60
return fmt.Sprintf("%02d:%02d:%02d", h, m, sec)
}
func parseTime(s string) int {
type StopTime struct {
Stop *Stop `json:"stop"`
TripId string `json:"trip_id" csv:"trip_id"`
ArrivalTime SecondsAfterMidnight `json:"arrival_time" csv:"arrival_time"`
DepartureTime SecondsAfterMidnight `json:"departure_time" csv:"departure_time"`
StopId string `json:"stop_id" csv:"stop_id"`
StopSequence int `json:"stop_sequence" csv:"stop_sequence"`
PickupType int `json:"pickup_type" csv:"pickup_type"`
DropOffType int `json:"drop_off_type" csv:"drop_off_type"`
}
func ParseTimeString(s string) SecondsAfterMidnight {
parts := strings.Split(s, ":")
if len(parts) != 3 {
return 0
@@ -26,37 +36,29 @@ func parseTime(s string) int {
h, _ := strconv.Atoi(parts[0])
m, _ := strconv.Atoi(parts[1])
sec, _ := strconv.Atoi(parts[2])
return h*3600 + m*60 + sec
return SecondsAfterMidnight(h*3600 + m*60 + sec)
}
func (st *StopTime) DepartTimeAsSeconds() int {
if st.departureTime > 0 {
return st.departureTime
}
if st.DepartureTime == "" {
return 0
}
st.departureTime = parseTime(st.DepartureTime)
return st.departureTime
func AsSecondsAfterMidnight(when time.Time) SecondsAfterMidnight {
return SecondsAfterMidnight(when.Hour()*3600 + when.Minute()*60 + when.Second())
}
func (st *StopTime) DepartsAfter(when time.Time) bool {
secondsAfterMidnight := st.DepartTimeAsSeconds()
return secondsAfterMidnight >= when.Hour()*3600+when.Minute()*60+when.Second()
return st.DepartureTime >= AsSecondsAfterMidnight(when)
}
// func (st *StopTime) GetPossibleTrips() iter.Seq[*Trip] {
// return func(yield func(*Trip) bool) {
// for _, trip := range st.Stop.trips {
// if trip.TripID != st.TripID {
// if !yield(trip) {
// return
// }
// }
// }
// }
// }
func (st *StopTime) GetPossibleTrips() iter.Seq[*Trip] {
return func(yield func(*Trip) bool) {
for _, trip := range st.Stop.Trips {
if trip.TripId != st.TripId {
if !yield(trip) {
return
}
}
}
}
}
func (st *StopTime) SetStop(stop *Stop) {
st.Stop = stop

View File

@@ -1,10 +1,10 @@
package types
type Transfer struct {
FromStopID string `json:"from_stop_id" csv:"from_stop_id"`
ToStopID string `json:"to_stop_id" csv:"to_stop_id"`
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"`
MinTransferTime int `json:"min_transfer_time" csv:"min_transfer_time"`
FromTripID string `json:"from_trip_id" csv:"from_trip_id"`
ToTripID string `json:"to_trip_id" csv:"to_trip_id"`
FromTripId string `json:"from_trip_id" csv:"from_trip_id"`
ToTripId string `json:"to_trip_id" csv:"to_trip_id"`
}

View File

@@ -8,9 +8,9 @@ import (
type Trip struct {
*Route
Stops []*StopTime `json:"stops" csv:"stops"`
RouteID string `json:"route_id" csv:"route_id"`
ServiceID string `json:"service_id" csv:"service_id"`
TripID string `json:"trip_id" csv:"trip_id"`
RouteId string `json:"route_id" csv:"route_id"`
ServiceId string `json:"service_id" csv:"service_id"`
TripId string `json:"trip_id" csv:"trip_id"`
TripHeadsign string `json:"trip_headsign" csv:"trip_headsign"`
TripShortName string `json:"trip_short_name" csv:"trip_short_name"`
}
@@ -20,7 +20,7 @@ func (t *Trip) GetDirectPossibleDestinations(stop *Stop, when time.Time) iter.Se
started := false
for _, st := range t.Stops {
if !started {
if st.StopID == stop.StopID && st.PickupType == 0 {
if st.StopId == stop.StopId && st.PickupType == 0 {
started = true
}
continue
@@ -44,3 +44,12 @@ func (t *Trip) AddStopTime(stopTime *StopTime) {
}
t.Stops = append(t.Stops, stopTime)
}
func (t *Trip) Has(stop *Stop) (*StopTime, bool) {
for _, st := range t.Stops {
if st.StopId == stop.StopId {
return st, true
}
}
return nil, false
}