mirror of
https://github.com/cjdenio/uta-trax-api.git
synced 2026-10-03 16:06:19 +00:00
direction/map/frontrunner
This commit is contained in:
4 files changed
+105
-11
No files matched your search
+19
-7
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
pb "github.com/cjdenio/uta-trax-api/proto"
|
pb "github.com/cjdenio/uta-trax-api/proto"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
@@ -41,7 +42,7 @@ func getVehicles() ([]*pb.VehiclePosition, error) {
|
|||||||
|
|
||||||
type TripInfo struct {
|
type TripInfo struct {
|
||||||
Line pb.VehicleFeed_Line
|
Line pb.VehicleFeed_Line
|
||||||
Direction int
|
Direction int32
|
||||||
}
|
}
|
||||||
|
|
||||||
var trips = make(map[string]*TripInfo)
|
var trips = make(map[string]*TripInfo)
|
||||||
@@ -61,12 +62,17 @@ func loadTrips() error {
|
|||||||
|
|
||||||
route_id := slices.Index(header, "route_id")
|
route_id := slices.Index(header, "route_id")
|
||||||
trip_id := slices.Index(header, "trip_id")
|
trip_id := slices.Index(header, "trip_id")
|
||||||
// direction_id := slices.Index(header, "direction_id")
|
direction_id := slices.Index(header, "direction_id")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if record, err := r.Read(); err == nil {
|
if record, err := r.Read(); err == nil {
|
||||||
trip_info := new(TripInfo)
|
trip_info := new(TripInfo)
|
||||||
|
|
||||||
|
direction, err := strconv.ParseInt(record[direction_id], 10, 32)
|
||||||
|
if err == nil {
|
||||||
|
trip_info.Direction = int32(direction)
|
||||||
|
}
|
||||||
|
|
||||||
switch record[route_id] {
|
switch record[route_id] {
|
||||||
case "8246":
|
case "8246":
|
||||||
trip_info.Line = pb.VehicleFeed_RED
|
trip_info.Line = pb.VehicleFeed_RED
|
||||||
@@ -80,6 +86,9 @@ func loadTrips() error {
|
|||||||
case "45389":
|
case "45389":
|
||||||
trip_info.Line = pb.VehicleFeed_STREETCAR
|
trip_info.Line = pb.VehicleFeed_STREETCAR
|
||||||
trips[record[trip_id]] = trip_info
|
trips[record[trip_id]] = trip_info
|
||||||
|
case "41065":
|
||||||
|
trip_info.Line = pb.VehicleFeed_FRONTRUNNER
|
||||||
|
trips[record[trip_id]] = trip_info
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -90,7 +99,6 @@ func loadTrips() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed {
|
func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed {
|
||||||
|
|
||||||
vehicle_feed := make([]*pb.VehicleFeed_Vehicle, 0, len(vehicles))
|
vehicle_feed := make([]*pb.VehicleFeed_Vehicle, 0, len(vehicles))
|
||||||
|
|
||||||
for _, vehicle := range vehicles {
|
for _, vehicle := range vehicles {
|
||||||
@@ -100,9 +108,11 @@ func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vehicle_feed = append(vehicle_feed, &pb.VehicleFeed_Vehicle{
|
vehicle_feed = append(vehicle_feed, &pb.VehicleFeed_Vehicle{
|
||||||
Lat: *vehicle.Position.Latitude,
|
Lat: *vehicle.Position.Latitude,
|
||||||
Lon: *vehicle.Position.Longitude,
|
Lon: *vehicle.Position.Longitude,
|
||||||
Line: trip.Line,
|
Line: trip.Line,
|
||||||
|
Id: *vehicle.Vehicle.Id,
|
||||||
|
Direction: trip.Direction,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +126,9 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||||
|
|
||||||
|
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||||
vehicles, err := getVehicles()
|
vehicles, err := getVehicles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
|||||||
+17
-4
@@ -29,6 +29,7 @@ const (
|
|||||||
VehicleFeed_RED VehicleFeed_Line = 2
|
VehicleFeed_RED VehicleFeed_Line = 2
|
||||||
VehicleFeed_BLUE VehicleFeed_Line = 3
|
VehicleFeed_BLUE VehicleFeed_Line = 3
|
||||||
VehicleFeed_STREETCAR VehicleFeed_Line = 4
|
VehicleFeed_STREETCAR VehicleFeed_Line = 4
|
||||||
|
VehicleFeed_FRONTRUNNER VehicleFeed_Line = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// Enum value maps for VehicleFeed_Line.
|
// Enum value maps for VehicleFeed_Line.
|
||||||
@@ -39,6 +40,7 @@ var (
|
|||||||
2: "RED",
|
2: "RED",
|
||||||
3: "BLUE",
|
3: "BLUE",
|
||||||
4: "STREETCAR",
|
4: "STREETCAR",
|
||||||
|
5: "FRONTRUNNER",
|
||||||
}
|
}
|
||||||
VehicleFeed_Line_value = map[string]int32{
|
VehicleFeed_Line_value = map[string]int32{
|
||||||
"LINE_UNSPECIFIED": 0,
|
"LINE_UNSPECIFIED": 0,
|
||||||
@@ -46,6 +48,7 @@ var (
|
|||||||
"RED": 2,
|
"RED": 2,
|
||||||
"BLUE": 3,
|
"BLUE": 3,
|
||||||
"STREETCAR": 4,
|
"STREETCAR": 4,
|
||||||
|
"FRONTRUNNER": 5,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -126,6 +129,7 @@ type VehicleFeed_Vehicle struct {
|
|||||||
Lon float32 `protobuf:"fixed32,2,opt,name=lon,proto3" json:"lon,omitempty"`
|
Lon float32 `protobuf:"fixed32,2,opt,name=lon,proto3" json:"lon,omitempty"`
|
||||||
Line VehicleFeed_Line `protobuf:"varint,3,opt,name=line,proto3,enum=VehicleFeed_Line" json:"line,omitempty"`
|
Line VehicleFeed_Line `protobuf:"varint,3,opt,name=line,proto3,enum=VehicleFeed_Line" json:"line,omitempty"`
|
||||||
Direction int32 `protobuf:"varint,4,opt,name=direction,proto3" json:"direction,omitempty"`
|
Direction int32 `protobuf:"varint,4,opt,name=direction,proto3" json:"direction,omitempty"`
|
||||||
|
Id string `protobuf:"bytes,5,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
}
|
}
|
||||||
@@ -188,24 +192,33 @@ func (x *VehicleFeed_Vehicle) GetDirection() int32 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *VehicleFeed_Vehicle) GetId() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Id
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
var File_proto_schema_proto protoreflect.FileDescriptor
|
var File_proto_schema_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
const file_proto_schema_proto_rawDesc = "" +
|
const file_proto_schema_proto_rawDesc = "" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"\x12proto/schema.proto\"\xfe\x01\n" +
|
"\x12proto/schema.proto\"\xa0\x02\n" +
|
||||||
"\vVehicleFeed\x120\n" +
|
"\vVehicleFeed\x120\n" +
|
||||||
"\bvehicles\x18\x01 \x03(\v2\x14.VehicleFeed.VehicleR\bvehicles\x1ar\n" +
|
"\bvehicles\x18\x01 \x03(\v2\x14.VehicleFeed.VehicleR\bvehicles\x1a\x82\x01\n" +
|
||||||
"\aVehicle\x12\x10\n" +
|
"\aVehicle\x12\x10\n" +
|
||||||
"\x03lat\x18\x01 \x01(\x02R\x03lat\x12\x10\n" +
|
"\x03lat\x18\x01 \x01(\x02R\x03lat\x12\x10\n" +
|
||||||
"\x03lon\x18\x02 \x01(\x02R\x03lon\x12%\n" +
|
"\x03lon\x18\x02 \x01(\x02R\x03lon\x12%\n" +
|
||||||
"\x04line\x18\x03 \x01(\x0e2\x11.VehicleFeed.LineR\x04line\x12\x1c\n" +
|
"\x04line\x18\x03 \x01(\x0e2\x11.VehicleFeed.LineR\x04line\x12\x1c\n" +
|
||||||
"\tdirection\x18\x04 \x01(\x05R\tdirection\"I\n" +
|
"\tdirection\x18\x04 \x01(\x05R\tdirection\x12\x0e\n" +
|
||||||
|
"\x02id\x18\x05 \x01(\tR\x02id\"Z\n" +
|
||||||
"\x04Line\x12\x14\n" +
|
"\x04Line\x12\x14\n" +
|
||||||
"\x10LINE_UNSPECIFIED\x10\x00\x12\t\n" +
|
"\x10LINE_UNSPECIFIED\x10\x00\x12\t\n" +
|
||||||
"\x05GREEN\x10\x01\x12\a\n" +
|
"\x05GREEN\x10\x01\x12\a\n" +
|
||||||
"\x03RED\x10\x02\x12\b\n" +
|
"\x03RED\x10\x02\x12\b\n" +
|
||||||
"\x04BLUE\x10\x03\x12\r\n" +
|
"\x04BLUE\x10\x03\x12\r\n" +
|
||||||
"\tSTREETCAR\x10\x04B'Z%github.com/cjdenio/uta-trax-api/protob\x06proto3"
|
"\tSTREETCAR\x10\x04\x12\x0f\n" +
|
||||||
|
"\vFRONTRUNNER\x10\x05B'Z%github.com/cjdenio/uta-trax-api/protob\x06proto3"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
file_proto_schema_proto_rawDescOnce sync.Once
|
file_proto_schema_proto_rawDescOnce sync.Once
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ message VehicleFeed {
|
|||||||
RED = 2;
|
RED = 2;
|
||||||
BLUE = 3;
|
BLUE = 3;
|
||||||
STREETCAR = 4;
|
STREETCAR = 4;
|
||||||
|
FRONTRUNNER = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Vehicle {
|
message Vehicle {
|
||||||
@@ -15,6 +16,7 @@ message VehicleFeed {
|
|||||||
float lon = 2;
|
float lon = 2;
|
||||||
Line line = 3;
|
Line line = 3;
|
||||||
int32 direction = 4;
|
int32 direction = 4;
|
||||||
|
string id = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
repeated Vehicle vehicles = 1;
|
repeated Vehicle vehicles = 1;
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
|
||||||
|
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
|
||||||
|
crossorigin=""
|
||||||
|
/>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
html,
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="map" style="height: 100vh"></div>
|
||||||
|
|
||||||
|
<script src="//cdn.jsdelivr.net/npm/protobufjs@7.X.X/dist/protobuf.min.js"></script>
|
||||||
|
<script
|
||||||
|
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
|
||||||
|
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
|
||||||
|
crossorigin=""
|
||||||
|
></script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const COLORS = {
|
||||||
|
1: "#2eb566",
|
||||||
|
2: "#be2036",
|
||||||
|
3: "#004a97",
|
||||||
|
4: "#77777a",
|
||||||
|
5: "#c227b9",
|
||||||
|
};
|
||||||
|
|
||||||
|
var map = L.map("map").setView([40.656734, -111.890818], 12);
|
||||||
|
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
|
||||||
|
maxZoom: 19,
|
||||||
|
attribution:
|
||||||
|
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
||||||
|
}).addTo(map);
|
||||||
|
|
||||||
|
protobuf
|
||||||
|
.load(
|
||||||
|
"https://raw.githubusercontent.com/cjdenio/uta-trax-api/refs/heads/main/proto/schema.proto"
|
||||||
|
)
|
||||||
|
.then(async (root) => {
|
||||||
|
const bin = await fetch("/api").then((r) => r.arrayBuffer());
|
||||||
|
const { vehicles } = root
|
||||||
|
.lookupType("VehicleFeed")
|
||||||
|
.decode(new Uint8Array(bin));
|
||||||
|
|
||||||
|
vehicles.forEach((vehicle) => {
|
||||||
|
L.circle([vehicle.lat, vehicle.lon], {
|
||||||
|
fillColor: COLORS[vehicle.line],
|
||||||
|
color: COLORS[vehicle.line],
|
||||||
|
fillOpacity: 0.5,
|
||||||
|
radius: 200,
|
||||||
|
}).addTo(map);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in new issue
Block a user