-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
133 lines (104 loc) · 2.84 KB
/
load.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package bifrost
import (
"encoding/json"
"fmt"
"github.com/klauspost/compress/zstd"
"os"
"path/filepath"
"time"
)
type LoadOptions struct {
OsmPaths []string // paths to osm pbf files
GtfsPaths []string // path to GTFS zip files
BifrostPath string // path to bifrost cache
}
// LoadData loads the data from a given bifrost cache if it exists. Otherwise it will generate the data from given GTFS
// and street CSV files. After generating the data it will write the data to a bifrost cache.
func (b *Bifrost) LoadData(load *LoadOptions) error {
cacheExists := true
_, err := os.Stat(load.BifrostPath)
if os.IsNotExist(err) {
cacheExists = false
} else if err != nil {
return fmt.Errorf("error checking for bifrost cache: %w", err)
}
if cacheExists {
b.AddBifrostData(load.BifrostPath)
b.Data.RebuildVertexTree()
return nil
}
t := time.Now()
fmt.Println("reading gtfs data")
for _, gtfsPath := range load.GtfsPaths {
fmt.Println("reading gtfs data from", gtfsPath)
localT := time.Now()
err = b.AddGtfs(gtfsPath)
if err != nil {
return fmt.Errorf("error reading gtfs data: %w", err)
}
fmt.Println("reading gtfs data from", gtfsPath, "took", time.Since(localT))
}
fmt.Println("reading gtfs data took", time.Since(t))
for _, streetPath := range load.OsmPaths {
fmt.Println("reading street data from", streetPath)
localT := time.Now()
err = b.AddOSM(streetPath)
if err != nil {
return fmt.Errorf("error reading street data: %w", err)
}
fmt.Println("reading street data from", streetPath, "took", time.Since(localT))
}
fmt.Println("reading all files", time.Since(t))
t = time.Now()
b.ConnectStopsToVertices()
fmt.Println("connecting stops to vertices took", time.Since(t))
fmt.Println("writing to bifrost cache")
t = time.Now()
b.WriteBifrostData(load.BifrostPath)
fmt.Println("writing raptor data took", time.Since(t))
return nil
}
// AddBifrostData Adds cached bifrost data file to the Bifrost data. Used by LoadOptions, generated by WriteBifrostData
func (b *Bifrost) AddBifrostData(fileName string) {
f, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer f.Close()
read, err := zstd.NewReader(f)
if err != nil {
panic(err)
}
defer read.Close()
r := &RoutingData{}
decoder := json.NewDecoder(read)
err = decoder.Decode(r)
if err != nil {
panic(err)
}
b.Data = MergeData(b.Data, r)
}
func (b *Bifrost) WriteBifrostData(fileName string) {
// create directory if not exists
directory := filepath.Dir(fileName)
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
panic(err)
}
f, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer f.Close()
write, err := zstd.NewWriter(f)
if err != nil {
panic(err)
}
defer write.Flush()
defer write.Close()
encoder := json.NewEncoder(write)
err = encoder.Encode(b.Data)
if err != nil {
panic(err)
}
}