即時道路資料會提供所選路線的最新路況和事件資訊,支援動態監控和快速回應。這類資料與 BigQuery 中定期收集的資料不同,適用於需要即時資訊的案例。
這項資料會持續串流,包括:行程時間 (顯示路線上的交通時間) 和速度讀取間隔 (顯示路段密度)。
如要存取即時道路資料,合約必須包含「即時作業」套裝組合。
建立 Cloud Pub/Sub 訂閱
專案設定完成後,即可接收即時資料,專案也會提供專屬的 Google Cloud Pub/Sub 主題。您可以在這個 Pub/Sub 主題中,找到所有已建立路線的即時資料。
以下程式碼範例顯示主題網址的格式。
projects/maps-platform-roads-management/topics/rmi-roadsinformation-PROJECT_NUMBER
您必須訂閱系統提供的 Pub/Sub 主題,才能接收即時資料訊息。如要快速瞭解如何訂閱及調用 Cloud Pub/Sub 主題的訊息,請參閱「訂閱主題」。
即時路況資料訊息結構定義
每則即時資料訊息都會納入下列資料:
- 道路詳細資料,例如
travel_duration
和speed_reading_intervals
。 - 路徑 ID,例如
selected_route_id
和display_name
。
每則訊息都會按照下列 Protobuf 格式發布。
syntax = "proto3"; // Contains the road information like travel duration and speed reading // intervals for a selected route. message RoadsInformation { message TravelDuration { // The duration of travel through the route based on current road conditions. float duration_in_seconds = 1; // The duration of travel through the route without taking road conditions into consideration. float static_duration_in_seconds = 2; } message Timestamp { // Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. int64 seconds = 1; // Non-negative fractions of a second at nanosecond resolution. Negative // second values with fractions must still have non-negative nanos values // that count forward in time. Must be from 0 to 999,999,999 // inclusive. int32 nanos = 2; } // Represents the latitude and longitude of a coordinate // within speed reading interval. message LatLng { float latitude = 1; float longitude = 2; } message SpeedReadingInterval { // The coordinates on the polyline for the speed reading interval repeated LatLng interval_coordinates = 1; // Describes the categorized current speed of road conditions. Possible values are: // - "NORMAL": Road is flowing smoothly, no slowdown is detected. // - "SLOW": Slowdown detected, but no congestion formed. // - "TRAFFIC_JAM": Congestion detected. string speed = 2; } // Id for selected_route. string selected_route_id = 1; // User provided name for the route. string display_name = 2; // Intervals representing the road density across the route. repeated SpeedReadingInterval speed_reading_intervals = 3; // Travel time information. TravelDuration travel_duration = 4; // The time the road data was collected. Timestamp retrieval_time = 5; // Contains a geojson polyline representing the optimal route determined based // on user's input waypoints. string route_geometry = 6; }
使用 Pub/Sub 將路線資料串流至 BigQuery
您可以設定 Pub/Sub 訂閱項目,將道路資料直接串流至 BigQuery 資料表。這可確保資料儲存空間充足,並對提供的路線資訊進行強大的分析。設定這類訂閱項目之前,您必須在 BigQuery 專案中建立合適的資料集和資料表,以便寫入資料。
如需如何建立可將資料寫入 BigQuery 的 Pub/Sub 訂閱項目,請參閱「將資料串流至 BigQuery」一文。
BigQuery 資料表結構定義
發布至 Pub/Sub 主題的訊息 (也可以寫入 BigQuery 資料表) 會符合下列結構定義。建立目標 BigQuery 資料表時,請使用這個結構定義,確保資料表相容。
{ "mode": "NULLABLE", "name": "selected_route_id", "type": "STRING", "description": "Id for selected_route." }, { "mode": "NULLABLE", "name": "display_name", "type": "STRING", "description": "User provided name for the route." }, { "fields": [ { "mode": "NULLABLE", "name": "speed", "type": "STRING", "description": "Describes the categorized current speed of traffic. Possible values are: \"NORMAL\": Traffic is flowing smoothly, no slowdown is detected. \"SLOW\": Slowdown detected, but no traffic jam formed. \"TRAFFIC_JAM\": Traffic jam detected." }, { "fields": [ { "mode": "NULLABLE", "name": "latitude", "type": "NUMERIC" }, { "mode": "NULLABLE", "name": "longitude", "type": "NUMERIC" } ], "mode": "REPEATED", "name": "interval_coordinates", "type": "RECORD", "description": "The geometry for this interval" } ], "mode": "REPEATED", "name": "speed_reading_intervals", "type": "RECORD", "description": "Intervals representing the traffic density across the route." }, { "fields": [ { "mode": "NULLABLE", "name": "duration_in_seconds", "type": "FLOAT", "description": "The duration of travel through the route based on current traffic conditions." }, { "mode": "NULLABLE", "name": "static_duration_in_seconds", "type": "FLOAT", "description": "The duration of travel through the route without taking traffic conditions into consideration." } ], "mode": "NULLABLE", "name": "travel_duration", "type": "RECORD", "description": "Travel time information." }, { "fields": [ { "mode": "NULLABLE", "name": "seconds", "type": "INTEGER", "description": "Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive." }, { "mode": "NULLABLE", "name": "nanos", "type": "INTEGER", "description": "Non-negative fractions of a second at nanosecond resolution. Negative second values with fractions must still have non-negative nanos values that count forward in time. Must be from 0 to 999,999,999 inclusive." } ], "mode": "NULLABLE", "name": "retrieval_time", "type": "RECORD", "description": "The time the traffic data was collected." }, { "mode": "NULLABLE", "name": "route_geometry", "type": "STRING", "description": "Contains a geojson polyline representing the optimal route determined based on user's input waypoints" }