Commit e0e67848 authored by dagal's avatar dagal
Browse files

Read all from database

No more reliance on random csv files!
parent a8017c5b
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
......@@ -49,8 +49,8 @@ func GetAllStops(c *gin.Context) {
}
func GetStop(c *gin.Context) {
idStr := c.Param("id")
s := stops[idStr].ToGeoJSON()
id := Atoi32(c.Param("id"), c)
s := stops[id].ToGeoJSON()
if s == nil {
c.JSON(404, gin.H{
......
package main
import (
"bufio"
"encoding/csv"
"fmt"
"os"
"os/exec"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"time"
)
func ReadCSV() {
file, err := os.Open("data/madrid_emt/stops.txt")
func ReadStops(db *sqlx.DB) {
stopSlice := []Stop{}
err := db.Select(&stopSlice, "SELECT ctr_id, name, lat, lon FROM stop WHERE ctr_id IS NOT NULL ORDER BY ctr_id")
LogAndQuit(err)
defer file.Close()
csvReader := csv.NewReader(bufio.NewReader(file))
records, err := csvReader.ReadAll()
LogAndQuit(err)
header := make(map[string]int, len(records[0]))
for i, h := range records[0] {
header[h] = i
}
fmt.Println(header)
for _, r := range records[1:] {
fmt.Println(r)
fmt.Println(r[header["stop_name"]])
for _, s := range stopSlice {
stops[s.Id] = s
}
}
func ReadTime() {
file, err := os.Open("data/time.txt")
LogAndQuit(err)
defer file.Close()
lineScanner := bufio.NewScanner(file)
if lineScanner.Scan() {
start_time, err = time.Parse("2006-01-02", lineScanner.Text())
LogAndQuit(err)
}
if lineScanner.Scan() {
end_time, err = time.Parse("2006-01-02", lineScanner.Text())
LogAndQuit(err)
}
}
func ReadTime(db *sqlx.DB) {
var strTime string
func ReadStopId() {
file, err := os.Open("data/stops.txt")
err := db.Get(&strTime, "SELECT min(start_date) FROM journey")
LogAndQuit(err)
defer file.Close()
lineScanner := bufio.NewScanner(file)
var i uint32 = 1
for lineScanner.Scan() {
stop_id_to_ctr[lineScanner.Text()] = i
i += 1
}
}
func InitDB(gtfs []string) {
//_ = os.Remove("data/trippy.db")
initCmd := exec.Command("sqlite3", "-init", "data/gtfs_SQL_importer/gtfs_tables.sqlite", "data/trippy.db")
defer initCmd.Wait()
initIn, _ := initCmd.StdinPipe()
defer initIn.Close()
err := initCmd.Start()
println(strTime)
start_time, err = time.Parse(time.RFC3339[0:19], strTime)
LogAndQuit(err)
println(start_time.Format("2006-01-02"))
for _, f := range gtfs {
// Now we want to call a lib written in a real language
insertCmd := exec.Command("python", "data/gtfs_SQL_importer/import_gtfs_to_sql.py", f, "nocopy")
out, err := insertCmd.Output()
LogAndQuit(err)
_ = insertCmd.Wait()
_, err = initIn.Write(out)
LogAndQuit(err)
_, _ = initIn.Write([]byte("\n"))
}
err = db.Get(&strTime, "SELECT max(start_date) FROM journey")
LogAndQuit(err)
println(strTime)
end_time, err = time.Parse(time.RFC3339[0:19], strTime)
LogAndQuit(err)
println(end_time.Format("2006-01-02"))
}
......@@ -8,8 +8,7 @@ import (
"time"
)
var stops = map[string]Stop{}
var stop_id_to_ctr = map[string]uint32{}
var stops = map[uint32]Stop{}
var start_time = time.Now()
var end_time = time.Now()
......@@ -18,15 +17,10 @@ func main() {
flag.Var(&gtfsFlags, "gtfs", "path to GTFS")
flag.Parse()
if len(gtfsFlags) > 0 {
InitDB(gtfsFlags)
}
db := connectDB()
defer db.Close()
readStops(db)
ReadStopId()
ReadTime()
ReadStops(db)
ReadTime(db)
CTRLoad()
defer CTRFree()
router := gin.Default()
......@@ -52,12 +46,3 @@ func connectDB() *sqlx.DB {
LogAndQuit(err)
return db
}
func readStops(db *sqlx.DB) {
stopSlice := []Stop{}
err := db.Select(&stopSlice, "SELECT stop_id, stop_name, stop_lat, stop_lon FROM gtfs_stops ORDER BY stop_id")
LogAndQuit(err)
for _, s := range stopSlice {
stops[s.Id] = s
}
}
......@@ -5,10 +5,10 @@ import (
)
type Stop struct {
Id string `db:"stop_id"`
Name string `db:"stop_name"`
Lat float64 `db:"stop_lat"`
Lon float64 `db:"stop_lon"`
Id uint32 `db:"ctr_id"`
Name string `db:"name"`
Lat float64 `db:"lat"`
Lon float64 `db:"lon"`
}
type Geometry interface {
......@@ -17,13 +17,7 @@ type Geometry interface {
func (s Stop) ToGeoJSON() *geojson.Feature {
f := geojson.NewPointFeature([]float64{s.Lon, s.Lat})
if id, ok := stop_id_to_ctr[s.Id]; ok {
f.Properties["id"] = id
} else {
return nil
}
f.Properties["id"] = s.Id
f.Properties["name"] = s.Name
return f
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment