-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
119 lines (105 loc) · 2.21 KB
/
create.go
File metadata and controls
119 lines (105 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"time"
"github.com/MichaelOfCourse/file"
"github.com/geops/gtfsparser"
)
func getLine(a string, datas jsonData) *busLine {
for _, b := range datas.BusLines {
if b.Name == a {
return b
}
}
return nil
}
func directionExists(dir int, line busLine) bool {
return line.Path[dir] != nil
}
func getStop(all []*stop, id string) *stop {
for _, v := range all {
if v.ID == id {
return v
}
}
return nil
}
type stop struct {
ID string
Code string
Lat float32
Lon float32
Name string
}
type linePath struct {
Dir string
Stops []*stop
}
type busLine struct {
ID string
Name string
Color string
TextColor string
Path [2]*linePath
}
type jsonData struct {
Date time.Time
BusLines []*busLine
}
func order(feed *gtfsparser.Feed, jsonData *jsonData) {
var newStop *stop
var allStops []*stop
var line *busLine
for _, v := range feed.Stops {
newStop = new(stop)
newStop.ID = v.Id
newStop.Code = v.Code
newStop.Name = v.Name
newStop.Lat = v.Lat
newStop.Lon = v.Lon
allStops = append(allStops, newStop)
}
for _, v := range feed.Trips {
line = getLine(v.Route.Short_name, *jsonData)
if line == nil {
line = new(busLine)
line.Name = v.Route.Short_name
line.ID = v.Route.Id
line.Color = v.Route.Color
line.TextColor = v.Route.Text_color
jsonData.BusLines = append(jsonData.BusLines, line)
} else if directionExists(v.Direction_id, *line) {
continue
}
line.Path[v.Direction_id] = new(linePath)
line.Path[v.Direction_id].Dir = v.Headsign
for _, j := range v.StopTimes {
line.Path[v.Direction_id].Stops = append(line.Path[v.Direction_id].Stops, getStop(allStops, j.Stop.Id))
}
}
}
// Create provide a file containing json infos
func Create() error {
feed := gtfsparser.NewFeed()
jsonData := new(jsonData)
if err := feed.Parse("gtfs/starGtfs.zip"); err != nil {
return err
}
jsonData.Date = time.Now()
order(feed, jsonData)
b, err := json.Marshal(*jsonData)
if err != nil {
return err
}
f, err := file.Create("jsonData.json")
if err != nil {
return err
}
if nb, err := f.Write(b); err != nil || nb != len(b) {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}