Protobuf
Overview of protobuf, common use cases, and implementation details.
Tilebox uses Protocol Buffers, with a custom generation tool, combined with standard Go data structures.
Protocol Buffers (often referred to as protobuf
) is a schema definition language with an efficient binary format and native language support for lots of languages, including Go.
Protocol buffers are open source since 2008 and are maintained by Google.
tilebox-generate
Protobuf schemas are typically defined in a .proto
file, and then converted to a native Go struct using the protobuf compiler.
Tilebox datasets already define a protobuf schema as well, and automate the generation of Go structs for existing datasets through a quick tilebox-generate
command-line tool.
See Installation for more details on how to install tilebox-generate
.
The preceding command will generate a ./protogen/tilebox/v1/sentinel1_sar.pb.go
file. More flags can be set to change the default output folders, package name, etc.
This file contains everything needed to work with the Sentinel-1 SAR dataset. It’s recommended to check the generated files you use in your version control system.
If you open this file, you will see that it starts with // Code generated by protoc-gen-go. DO NOT EDIT.
.
It means that the file was generated by the protoc-gen-go
tool, which is part of the protobuf compiler.
After editing a dataset, you can call the generate command again to ensure that the changes are reflected in the generated file.
The file contains a Sentinel1Sar
struct, which is a Go struct that represents a datapoint in the dataset.
Notice that the fields are private (starting with a lowercase letter), so they are not accessible. Protobuf hides the fields and provides getters and setters to access them.
Protobuf 101
Initializing a message
Here is how to initialize a v1.Sentinel1Sar
message.
Protobuf fields are private and provides a builder pattern to create a message.
proto.String
is a helper function that converts string
to *string
.
This allows protobuf to differentiate between a field that is set to an empty string and a field that is not set (nil).
An exhaustive list of those helper functions can be found here.
Only primitives have a proto.XXX
helper function.
Complex types such as timestamps, durations, UUIDs, and geometries have a constructor function.
Getters and setters
Protobuf provides methods to get, set, clear and check if a field is set.
Getters for primitive types will return a Go native type (for example, int64, string, etc.). Getters for complex types such as timestamps, durations, UUIDs, and geometries can also be converted to more standard types using AsXXX methods.
Well known types
Beside Go primitives, Tilebox supports some well known types:
- Duration: A duration of time. See Duration for more information.
- Timestamp: A point in time. See Timestamp for more information.
- UUID: A universally unique identifier (UUID).
- Geometry: Geospatial geometries of type Point, LineString, Polygon or MultiPolygon.
They have a couple of useful methods to work with them.
Constructors
CheckValid
method
CheckValid
returns an error if the field is invalid.
IsValid
method
IsValid
reports whether the field is valid. It’s equivalent to CheckValid == nil
.
AsXXX
methods
AsXXX
methods convert the field to a more user friendly type.
AsUUID
will convert adatasetsv1.UUID
field to a uuid.UUID typeAsTime
will convert atimestamppb.Timestamp
field to a time.Time typeAsDuration
will convert adurationpb.Duration
field to a time.Duration typeAsGeometry
will convert adatasetsv1.Geometry
field to an orb.Geometry interface
Those methods performs conversion on a best-effort basis. Type validity must be checked beforehand using IsValid
or CheckValid
methods.
Common data operations
Datapoints are contained in a standard Go slice so all the usual slice operations and slice functions can be used.
The usual pattern to iterate over data in Go is by using a for
loop.
As an example, here is how to extract the copernicus_id
fields from the datapoints.
Here is an example of filtering out datapoints that have been published before January 2000 and are not from the Sentinel-1C platform.
Converting to JSON
Protobuf messages can be converted to JSON without loss of information. This is useful for interoperability with other systems that doesn’t use protobuf. A guide on protoJSON can be found format here: https://protobuf.dev/programming-guides/json/
It can also be converted back to a proto.Message
.