forked from laprice/miamidadetransitgeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (73 loc) · 2.05 KB
/
main.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
package main
import "net/http"
import "io/ioutil"
import "os"
import "encoding/xml"
import "log"
import "fmt"
import (
gj "github.com/kpawlik/geojson"
)
//Record is a single update of a single bus location
type Record struct {
BusID string `xml:"Record>BusID"`
BusName string `xml:"Record>BusName"`
Latitude float64 `xml:"Record>Latitude"`
Longitude float64 `xml:"Record>Longitude"`
RouteID string `xml:"Record>RouteID"`
TripID string `xml:"Record>TripID"`
Direction string `xml:"Record>Direction"`
ServiceDirection string `xml:"Record>ServiceDirection"`
Service string `xml:"Record>Service"`
ServiceName string `xml:"Record>ServiceName"`
TripHeadsign string `xml:"Record>TripHeadsign"`
LocationUpdated string `xml:"Record>LocationUpdated"`
}
func parserecord(r **Record, body []byte) {
err := xml.Unmarshal(body, r)
if err != nil {
log.Fatal("Unmarshall failed", err)
}
}
func makeFeaturefromRecord(r *Record) string {
var f *gj.Feature
lon := gj.Coord(r.Longitude)
lat := gj.Coord(r.Latitude)
p := gj.NewPoint(gj.Coordinate{lon, lat})
id := r.BusID
props := make(map[string]interface{})
props["BusName"] = r.BusName
props["RouteID"] = r.RouteID
props["TripID"] = r.TripID
props["TripHeadsign"] = r.TripHeadsign
props["Service"] = r.Service
props["LocationUpdated"] = r.LocationUpdated
f = gj.NewFeature(p, props, id)
gjstr, err := gj.Marshal(f)
if err != nil {
log.Fatal("geojson marshall failed", err)
}
return gjstr
}
func init() {
log.SetOutput(os.Stderr)
}
func main() {
log.Println("starting")
resp, err := http.Get("http://www.miamidade.gov/transit/WebServices/Buses/?RouteID=8")
if err != nil {
log.Fatal("error fetching url", err)
}
log.Println(resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("could not read Response Body")
}
resp.Body.Close()
r := new(Record)
parserecord(&r, body)
log.Println(r.BusName)
log.Printf("POINT(%f %f)", r.Longitude, r.Latitude)
j := makeFeaturefromRecord(r)
fmt.Println(j)
}