From 5e3d39c62dfc0aa3822558029d46f1ac6e9937ca Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 10 Sep 2024 12:26:44 -0500 Subject: [PATCH] Full processing pipeline minus coordinate gathering --- .gitignore | 1 + main.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53752db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output diff --git a/main.go b/main.go index 67cfd06..4c7cdfa 100644 --- a/main.go +++ b/main.go @@ -96,6 +96,10 @@ type CombiLine struct { Color string `json:"color"` } +type CombiRouteContainer struct { + Routes []CombiRoute +} + type CombiRoute struct { Name string `json:"name"` Departure []float64 `json:"departure"` @@ -151,6 +155,8 @@ func main() { // Create a map from string to CombiLine combiLineMap := make(map[string]*CombiLine) + // Create a map for CombiRoute, grouped by CombiLine.Id + combiRoutesMap := make(map[int]*CombiRouteContainer) // Populate the map for _, combi := range combis { @@ -164,21 +170,66 @@ func main() { Color: combi.Color, } combiLineMap[combi.Company] = &combiLine + + // create the route container + combiRoutesMap[combi.Id] = &CombiRouteContainer{ + Routes: make([]CombiRoute, 0), + } } } + // Convert each CombiData into a CombiRoute and store it + for _, combi := range combis { + combiRoute := CombiRoute{ + Name: combi.Ref, + Departure: make([]float64, 0), + Return: make([]float64, 0), + } + combiLineSlice := combiRoutesMap[combi.Id] + combiLineSlice.Routes = append(combiLineSlice.Routes, combiRoute) + } + + writeOutput(combiLineMap, combiRoutesMap) +} + +func writeOutput(lines map[string]*CombiLine, routes map[int]*CombiRouteContainer) { + // Create output folder + os.MkdirAll("output", os.ModePerm) + + // + // Write the lines JSON file + // + // convert the map into an array combiLineSlice := make([]*CombiLine, 0) - for _, combiLine := range combiLineMap { + for _, combiLine := range lines { combiLineSlice = append(combiLineSlice, combiLine) } // print JSON - jsonStr, err := json.Marshal(combiLineSlice) + jsonBytes, err := json.Marshal(combiLineSlice) if err != nil { panic(err) } - fmt.Print(string(jsonStr[:])) + os.WriteFile("output/lines.json", jsonBytes, 0644) + + // + // Write each JSON file for the routes + // + for lineId, routeContainer := range routes { + outFilename := fmt.Sprintf("output/routes_%d.json", lineId) + jsonBytes, err := json.Marshal(routeContainer.Routes) + if err != nil { + panic(nil) + } + // write + err = os.WriteFile(outFilename, jsonBytes, 0644) + if err != nil { + panic(err) + } + } + + log.Print("JSON files written to output/") } func parseCombiData(member OsmRelation) CombiData {