structs.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package traffic
  2. import (
  3. "apiote.xyz/p/szczanieckiej/gtfs_rt"
  4. "image/color"
  5. "strings"
  6. "time"
  7. "github.com/dhconnelly/rtreego"
  8. )
  9. type DeparturesType uint8
  10. const (
  11. DEPARTURES_HYBRID DeparturesType = iota
  12. DEPARTURES_FULL
  13. )
  14. type Position struct {
  15. Lat float64
  16. Lon float64
  17. }
  18. // todo(BAF10) change IDs to type ID, string to type OLC
  19. type Agency struct {
  20. ID ID
  21. Name string
  22. Website string
  23. Timezone string
  24. PhoneNumber string // todo(BAF10) PhoneNumber
  25. Language string // todo(BAF10) language.Tag
  26. Email string
  27. FareWebsite string
  28. }
  29. type FeedInfo struct {
  30. Name string
  31. Website string
  32. Language string // todo(BAF10) language.Tag
  33. }
  34. type Vehicle struct { // todo(BAF10) two types of vehicles — descriptions ID-Capabilities, and an actual vehicle that runs on a trip
  35. Id ID
  36. Capabilities uint16
  37. Coordinates Position
  38. Speed float32
  39. Delay int32 // todo(BAF31)
  40. LineName string
  41. Headsign string
  42. // Status
  43. // Occupancy
  44. // Congestion
  45. }
  46. func (v Vehicle) Location() Position {
  47. return v.Coordinates
  48. }
  49. type Departure struct {
  50. StopSeq int
  51. Time uint
  52. Pickup uint // todo(BAF10) enum
  53. Dropoff uint // todo(BAF10) enum
  54. StopOffset uint64
  55. }
  56. type DepartureRealtime struct {
  57. Departure Departure
  58. Headsign string
  59. LineName string
  60. Order StopOrder
  61. Update gtfs_rt.Update
  62. }
  63. type Trip struct {
  64. ID string
  65. Headsign string
  66. Direction uint
  67. LineName string
  68. ScheduleID string
  69. ShapeID ID // todo(BAF11)
  70. Departures []Departure
  71. }
  72. type LineGraph struct {
  73. StopCodes []string
  74. NextNodes map[int][]int
  75. PrevNodes map[int][]int
  76. }
  77. func (g LineGraph) LastNodes() []int {
  78. lastNodes := []int{}
  79. for i, nextNodes := range g.NextNodes {
  80. for _, node := range nextNodes {
  81. if node == -1 {
  82. lastNodes = append(lastNodes, i)
  83. break
  84. }
  85. }
  86. }
  87. return lastNodes
  88. }
  89. type Line struct {
  90. ID string
  91. Name string
  92. Colour color.RGBA
  93. Type uint // todo(BAF10) enum
  94. AgencyID ID
  95. GraphThere LineGraph
  96. GraphBack LineGraph
  97. }
  98. type LineStub struct {
  99. Name string
  100. Colour string
  101. Type string
  102. }
  103. func (l Line) IsItem() {}
  104. type Shape struct { // todo(BAF11)
  105. Points []string // OLC
  106. }
  107. type Schedule struct {
  108. ScheduleID string
  109. Weekdays uint8
  110. StartDate string
  111. EndDate string
  112. }
  113. type StopOrder struct {
  114. TripID string `bare:"-"`
  115. TripOffset uint
  116. Order int
  117. }
  118. type ChangeOption struct {
  119. LineID string `bare:"-"`
  120. LineName string
  121. Headsign string
  122. }
  123. type Item interface { // todo(BAF12) name: ‘query-able’ eg.
  124. IsItem()
  125. }
  126. type Locatable interface {
  127. Location() Position
  128. }
  129. type Stop struct {
  130. ID string
  131. Code string
  132. Name string
  133. ChangeOptions []ChangeOption
  134. Zone string
  135. Coordinates Position
  136. Order []StopOrder
  137. Timezone string
  138. }
  139. func (s Stop) Location() Position {
  140. return s.Coordinates
  141. }
  142. func (s Stop) Bounds() *rtreego.Rect {
  143. rect, err := rtreego.NewRectFromPoints(
  144. rtreego.Point{s.Coordinates.Lat, s.Coordinates.Lon},
  145. rtreego.Point{s.Coordinates.Lat, s.Coordinates.Lon},
  146. )
  147. if err != nil {
  148. panic(err.Error())
  149. }
  150. return rect
  151. }
  152. func (s Stop) IsItem() {}
  153. type TimedStopStub struct {
  154. StopStub
  155. Time uint
  156. }
  157. type StopStub struct {
  158. Code string
  159. Name string
  160. Zone string
  161. OnDemand bool
  162. }
  163. type Alert struct {
  164. Header string
  165. Description string
  166. URL string
  167. Cause int32 // todo(BAF10)
  168. Effect int32 // todo(BAF10)
  169. }
  170. type ID string
  171. type Validity string // 20060102_20060102
  172. func (v Validity) Start() string {
  173. return strings.Split(string(v), "_")[0]
  174. }
  175. func (v Validity) End() string {
  176. return strings.Split(string(v), "_")[1]
  177. }
  178. type CodeIndex map[ID]uint
  179. type FeedCodeIndex map[Validity]CodeIndex
  180. type GlobalCodeIndex map[string]FeedCodeIndex
  181. type NameOffset struct {
  182. Name string
  183. Offsets []uint
  184. }
  185. type NameIndex []NameOffset
  186. type FeedNameIndex map[Validity]NameIndex
  187. type GlobalNameIndex map[string]FeedNameIndex
  188. func (ix NameIndex) String(i int) string {
  189. return ix[i].Name
  190. }
  191. func (ix NameIndex) Len() int {
  192. return len(ix)
  193. }
  194. type FeedCalendar map[Validity][]Schedule
  195. type GlobalCalendar map[string]FeedCalendar
  196. type Vehicles map[ID]Vehicle
  197. type FeedVehicles map[Validity]Vehicles
  198. type GlobalVehicles map[string]FeedVehicles
  199. type FeedPositionIndex map[Validity]*rtreego.Rtree
  200. type GlobalPositionIndex map[string]FeedPositionIndex
  201. type Version struct {
  202. Link string
  203. ValidFrom time.Time
  204. ValidTill time.Time
  205. }
  206. func (v Version) String() string {
  207. return v.ValidFrom.Format("20060102") + "_" + v.ValidTill.Format("20060102")
  208. }
  209. type GlobalVersions map[string][]Version
  210. var DateFormat string = "2006-01-02"
  211. var ValidityFormat string = "20060102"
  212. type Traffic struct {
  213. CodeIndexes GlobalCodeIndex
  214. NameIndexes GlobalNameIndex
  215. LineIndexes GlobalNameIndex
  216. TripIndexes GlobalNameIndex
  217. PositionIndexes GlobalPositionIndex
  218. Versions GlobalVersions
  219. Calendars GlobalCalendar
  220. Vehicles GlobalVehicles
  221. Feeds map[string]Feed
  222. }
  223. type Context struct {
  224. DataHome string
  225. FeedName string
  226. Version Validity
  227. }