Skip to content

Instantly share code, notes, and snippets.

@timothyandrew
Created November 14, 2022 16:43
Show Gist options
  • Save timothyandrew/22d576e480fff62c8efc989b7f40c5df to your computer and use it in GitHub Desktop.
Save timothyandrew/22d576e480fff62c8efc989b7f40c5df to your computer and use it in GitHub Desktop.
Build London Bus Dataset
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
// Route,Run,Sequence,Stop_Code_LBSL,Bus_Stop_Code,Naptan_Atco,Stop_Name,Location_Easting,Location_Northing,Heading,Virtual_Bus_Stop
// CSV from: https://tfl.gov.uk/info-for/open-data-users/api-documentation
type Result struct {
Start, End, Route string
}
func main() {
f, _ := os.Open("/Users/tim/Downloads/bus-sequences.csv")
reader := csv.NewReader(f)
results := []Result{}
current := Result{}
reader.Read()
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if record[1] != "1" {
continue
}
if record[0] != current.Route {
results = append(results, current)
current = Result{Route: record[0], Start: record[6]}
}
current.End = record[6]
}
out, _ := os.Create("/Users/tim/Downloads/bus.json")
writer := json.NewEncoder(out)
err := writer.Encode(results)
fmt.Println(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment