All Products
Search
Document Center

PolarDB:ST_AsProtobuf

Last Updated:Mar 28, 2026

Serializes a trajectory object into Protocol Buffers (Protobuf) binary format for efficient data exchange and display.

Syntax

bytea ST_AsProtobuf(trajectory traj);

Parameters

ParameterDescription
trajThe trajectory to serialize.

Return value

Returns a bytea value containing the trajectory data in Protobuf binary format.

The Protobuf schema is defined as follows:

syntax = "proto2";
option optimize_for = LITE_RUNTIME;

message Trajectory {
  required uint32 srid = 1 [default = 4326]; // Srid
  repeated Field fields = 2; // Fields definitions
  repeated Point points = 3; // All trajectory Point
  repeated Event events = 4; // Events

  message Field {
    required string name = 11; // Field Name
    required FieldType type = 12; // Field Type
    required uint32 length = 13;  // Field length
    required bool nullable = 14 [default = false]; // Field Nullable
  }

  message Value {
    required bool is_null = 21 [default = false]; // Is null
    oneof value_type {
      string string_value = 22; // string value
      double double_value = 23; // double value
      float float_value = 24; // float value
      sint32 int32_value = 25; // int32 value, int8 and int16 will in this value
      sint64 int64_value = 26; // int64 value
      bool bool_value = 27; // bool value
      uint64 timestamp_value = 28; // timestamp value
    }
  }

  enum FieldType {
    string_type = 31;
    double_type = 32;
    integer_type = 33;
    bool_type = 34;
    timestamp_type = 35;
  }

  message Point {
    required double x = 41; // x coordinate
    required double y = 42; // y coordinate
    optional double z = 43; // z value, optional
    optional double m = 44; // m value, optional
    required uint64 timestamp = 45; // timestamp

    repeated Value attributes = 46; // all attribute values
  }

  message Event {
    required int32 type = 51; // event type
    required uint64 timestamp = 52; // timestamp
  }
}

Example

The following example converts a trajectory built with ST_MakeTrajectory into Protobuf format. The trajectory contains three points along a LINESTRING geometry (SRID 4326), five attribute fields, and three events.

SELECT ST_AsProtobuf(
  ST_MakeTrajectory(
    'STPOINT'::leaftype,
    st_geomfromtext('LINESTRING (114 35, 115 36, 116 37)', 4326),
    '[2010-01-01 14:30, 2010-01-01 15:30)'::tsrange,
    '{"leafcount":3,"attributes":{"velocity": {"type": "integer", "length": 2,"nullable" : true,"value": [120, 130, 140]}, "accuracy": {"type": "float", "length": 4, "nullable" : false,"value": [120, 130, 140]}, "bearing": {"type": "float", "length": 8, "nullable" : false,"value": [120, 130, 140]}, "vesname": {"type": "string", "length": 20, "nullable" : true,"value": ["adsf", "sdf", "sdfff"]}, "active": {"type": "timestamp", "nullable" : false,"value": ["Fri Jan 01 14:30:00 2010", "Fri Jan 01 15:00:00 2010", "Fri Jan 01 15:30:00 2010"]}}, "events": [{"1" : "Fri Jan 01 14:30:00 2010"}, {"2" : "Fri Jan 01 15:00:00 2010"}, {"3" : "Fri Jan 01 15:30:00 2010"}]}'
  )
);

Expected output:

      st_asprotobuf
---------------------
 \x08e62112105a0876656c6f63...