|
| 1 | +package rt |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/interline-io/transitland-lib/rt/pb" |
| 8 | +) |
| 9 | + |
| 10 | +// createVehicleFeature creates a GeoJSON feature from a vehicle entity |
| 11 | +func createVehicleFeature(entity *pb.FeedEntity) map[string]any { |
| 12 | + vehicle := entity.Vehicle |
| 13 | + properties := map[string]any{ |
| 14 | + "id": entity.Id, |
| 15 | + } |
| 16 | + |
| 17 | + // Add vehicle descriptor properties |
| 18 | + if vehicle.Vehicle != nil { |
| 19 | + if vehicle.Vehicle.Id != nil { |
| 20 | + properties["vehicle_id"] = *vehicle.Vehicle.Id |
| 21 | + } |
| 22 | + if vehicle.Vehicle.Label != nil { |
| 23 | + properties["vehicle_label"] = *vehicle.Vehicle.Label |
| 24 | + } |
| 25 | + if vehicle.Vehicle.LicensePlate != nil { |
| 26 | + properties["vehicle_license_plate"] = *vehicle.Vehicle.LicensePlate |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Add trip information |
| 31 | + if vehicle.Trip != nil { |
| 32 | + if vehicle.Trip.TripId != nil { |
| 33 | + properties["trip_id"] = *vehicle.Trip.TripId |
| 34 | + } |
| 35 | + if vehicle.Trip.RouteId != nil { |
| 36 | + properties["route_id"] = *vehicle.Trip.RouteId |
| 37 | + } |
| 38 | + if vehicle.Trip.DirectionId != nil { |
| 39 | + properties["direction_id"] = *vehicle.Trip.DirectionId |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Add position and timestamp |
| 44 | + if vehicle.Position != nil { |
| 45 | + if vehicle.Position.Latitude != nil { |
| 46 | + properties["latitude"] = *vehicle.Position.Latitude |
| 47 | + } |
| 48 | + if vehicle.Position.Longitude != nil { |
| 49 | + properties["longitude"] = *vehicle.Position.Longitude |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if vehicle.Timestamp != nil { |
| 54 | + properties["timestamp"] = *vehicle.Timestamp |
| 55 | + } |
| 56 | + |
| 57 | + if vehicle.CurrentStopSequence != nil { |
| 58 | + properties["current_stop_sequence"] = *vehicle.CurrentStopSequence |
| 59 | + } |
| 60 | + |
| 61 | + if vehicle.StopId != nil { |
| 62 | + properties["stop_id"] = *vehicle.StopId |
| 63 | + } |
| 64 | + |
| 65 | + if vehicle.CurrentStatus != nil { |
| 66 | + properties["current_status"] = *vehicle.CurrentStatus |
| 67 | + } |
| 68 | + |
| 69 | + if vehicle.CongestionLevel != nil { |
| 70 | + properties["congestion_level"] = *vehicle.CongestionLevel |
| 71 | + } |
| 72 | + |
| 73 | + return map[string]any{ |
| 74 | + "type": "Feature", |
| 75 | + "properties": properties, |
| 76 | + "geometry": map[string]any{ |
| 77 | + "type": "Point", |
| 78 | + "coordinates": []float64{ |
| 79 | + float64(*vehicle.Position.Longitude), |
| 80 | + float64(*vehicle.Position.Latitude), |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// VehiclePositionsToGeoJSON converts vehicle position protobuf data to GeoJSON format |
| 87 | +func VehiclePositionsToGeoJSON(rtMsg *pb.FeedMessage, isGeoJSONL bool) ([]byte, error) { |
| 88 | + features := []map[string]any{} |
| 89 | + |
| 90 | + for _, entity := range rtMsg.Entity { |
| 91 | + if entity.Vehicle == nil || entity.Vehicle.Position == nil { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + feature := createVehicleFeature(entity) |
| 96 | + features = append(features, feature) |
| 97 | + } |
| 98 | + |
| 99 | + if isGeoJSONL { |
| 100 | + // Return GeoJSONL format (one feature per line) |
| 101 | + var result []byte |
| 102 | + for i, feature := range features { |
| 103 | + featureBytes, err := json.Marshal(feature) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + result = append(result, featureBytes...) |
| 108 | + if i < len(features)-1 { |
| 109 | + result = append(result, '\n') |
| 110 | + } |
| 111 | + } |
| 112 | + return result, nil |
| 113 | + } else { |
| 114 | + // Return standard GeoJSON format |
| 115 | + featureCollection := map[string]any{ |
| 116 | + "type": "FeatureCollection", |
| 117 | + "features": features, |
| 118 | + } |
| 119 | + return json.Marshal(featureCollection) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// VehiclePositionsToGeoJSONLStream streams vehicle position protobuf data to GeoJSONL format |
| 124 | +// This function writes features directly to the provided writer as they are processed, |
| 125 | +// reducing memory usage for large datasets. |
| 126 | +func VehiclePositionsToGeoJSONLStream(rtMsg *pb.FeedMessage, w io.Writer) error { |
| 127 | + encoder := json.NewEncoder(w) |
| 128 | + |
| 129 | + for _, entity := range rtMsg.Entity { |
| 130 | + if entity.Vehicle == nil || entity.Vehicle.Position == nil { |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + feature := createVehicleFeature(entity) |
| 135 | + |
| 136 | + // Encode and write the feature directly to the writer |
| 137 | + if err := encoder.Encode(feature); err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments