Skip to content
Merged
62 changes: 62 additions & 0 deletions java/example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mobilitydata</groupId>
<artifactId>gtfs-realtime-bindings-example</artifactId>
<version>1.0-SNAPSHOT</version>

<name>gtfs-realtime-bindings-example</name>
<description>Example showing how to use the gtfs-realtime-bindings Java library.</description>

<properties>
<!-- Change this to the desired release version, e.g. 0.1.0, to use a release build.
Keep the -SNAPSHOT suffix to use the latest snapshot. -->
<gtfs-realtime-bindings.version>0.1.0</gtfs-realtime-bindings.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mobilitydata</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>${gtfs-realtime-bindings.version}</version>
</dependency>
</dependencies>

<!-- Snapshot repository — only needed when using a -SNAPSHOT version above.
Safe to remove when pointing to a release. -->
<repositories>
<repository>
<id>central-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Run with: mvn exec:java -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>org.mobilitydata.example.GtfsRealtimeExample</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.mobilitydata.example;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


import com.google.transit.realtime.GtfsRealtime.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
* Minimal example demonstrating how to build, serialize, and parse a GTFS-Realtime feed
* using the gtfs-realtime-bindings Java library.
*
* <p>This example builds a feed message containing three entity types:
* <ul>
* <li>A vehicle position update</li>
* <li>A trip update with a stop time update</li>
* <li>A service alert</li>
* </ul>
*
* <p>Run with: {@code mvn exec:java} from the {@code example/} directory.
*/
public class GtfsRealtimeExample {

public static void main(String[] args) throws Exception {

// --- Build a FeedMessage ---

FeedMessage feed = FeedMessage.newBuilder()
.setHeader(FeedHeader.newBuilder()
.setGtfsRealtimeVersion("2.0")
.setIncrementality(FeedHeader.Incrementality.FULL_DATASET)
.setTimestamp(System.currentTimeMillis() / 1000))

// Entity 1: vehicle position
.addEntity(FeedEntity.newBuilder()
.setId("vehicle-1")
.setVehicle(VehiclePosition.newBuilder()
.setTrip(TripDescriptor.newBuilder()
.setTripId("trip-42")
.setRouteId("route-7"))
.setPosition(Position.newBuilder()
.setLatitude(45.5017f)
.setLongitude(-73.5673f)
.setBearing(180.0f)
.setSpeed(12.5f))
.setVehicle(VehicleDescriptor.newBuilder()
.setId("bus-101")
.setLabel("101"))
.setCurrentStatus(VehiclePosition.VehicleStopStatus.IN_TRANSIT_TO)
.setCurrentStopSequence(5)
.setOccupancyStatus(VehiclePosition.OccupancyStatus.MANY_SEATS_AVAILABLE)))

// Entity 2: trip update with a stop time update
.addEntity(FeedEntity.newBuilder()
.setId("trip-update-1")
.setTripUpdate(TripUpdate.newBuilder()
.setTrip(TripDescriptor.newBuilder()
.setTripId("trip-42")
.setRouteId("route-7"))
.addStopTimeUpdate(TripUpdate.StopTimeUpdate.newBuilder()
.setStopSequence(5)
.setStopId("stop-99")
.setArrival(TripUpdate.StopTimeEvent.newBuilder()
.setDelay(120)) // 2 minutes late
.setDeparture(TripUpdate.StopTimeEvent.newBuilder()
.setDelay(120)))))

// Entity 3: service alert
.addEntity(FeedEntity.newBuilder()
.setId("alert-1")
.setAlert(Alert.newBuilder()
.addInformedEntity(EntitySelector.newBuilder()
.setRouteId("route-7"))
.setHeaderText(TranslatedString.newBuilder()
.addTranslation(TranslatedString.Translation.newBuilder()
.setLanguage("en")
.setText("Route 7 delay")))
.setDescriptionText(TranslatedString.newBuilder()
.addTranslation(TranslatedString.Translation.newBuilder()
.setLanguage("en")
.setText("Route 7 is experiencing delays due to traffic.")))
.setCause(Alert.Cause.TECHNICAL_PROBLEM)
.setEffect(Alert.Effect.SIGNIFICANT_DELAYS)))

.build();

System.out.println("Built feed with " + feed.getEntityCount() + " entities.");

// --- Serialize to bytes (as you would when writing to a file or HTTP response) ---

ByteArrayOutputStream out = new ByteArrayOutputStream();
feed.writeTo(out);
byte[] bytes = out.toByteArray();
System.out.println("Serialized feed: " + bytes.length + " bytes.");

// --- Parse back from bytes (as a consumer would after fetching a GTFS-RT URL) ---

FeedMessage parsed = FeedMessage.parseFrom(new ByteArrayInputStream(bytes));
System.out.println("Parsed feed version : " + parsed.getHeader().getGtfsRealtimeVersion());
System.out.println("Parsed entity count : " + parsed.getEntityCount());

for (FeedEntity entity : parsed.getEntityList()) {
if (entity.hasVehicle()) {
VehiclePosition vp = entity.getVehicle();
System.out.printf(" Vehicle id=%-12s lat=%.4f lon=%.4f speed=%.1f m/s%n",
vp.getVehicle().getId(),
vp.getPosition().getLatitude(),
vp.getPosition().getLongitude(),
vp.getPosition().getSpeed());
} else if (entity.hasTripUpdate()) {
TripUpdate tu = entity.getTripUpdate();
System.out.printf(" TripUpd trip=%-10s stops=%d delay=%ds%n",
tu.getTrip().getTripId(),
tu.getStopTimeUpdateCount(),
tu.getStopTimeUpdate(0).getArrival().getDelay());
} else if (entity.hasAlert()) {
Alert a = entity.getAlert();
System.out.printf(" Alert cause=%-20s \"%s\"%n",
a.getCause(),
a.getHeaderText().getTranslation(0).getText());
}
}
}
}
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>org.mobilitydata</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>0.0.9-SNAPSHOT</version>
<version>0.1.0</version>

<name>gtfs-realtime-bindings</name>
<description>Java classes generated from the GTFS-realtime protocol buffer specification.</description>
Expand Down
Loading