Reputation: 101
Basically what I'm trying to do is get the x
and y
coordinates from a ProtoBuf message that is similar to the SHPObject
type. I know that with SHPObject
I can do things like double* x = obj->padfX
and double* y = obj->padfY
. However, I'm not entirely sure how to extract this same information from my ProtoBuf (code is shown below). Thank you for your help!
This what I have tried so far:
myProject::protobuf::NewShape _NewShape;
auto obj = _NewShape.shape(0);
double* x = obj.polygon(0).point(0);
Gives the error:
cannot initialize a variable of the type 'double' with an rvalue of type 'unsigned int'
And, then I tried this which compiles but doesn't do anything (does not give me the desired output):
double x_coordinate = obj.polygon(0).point(0);
double *x_ptr = &x_coordinate;
Here's my ProtoBuf file:
newShape.proto
syntax = "proto2";
package myProject.protobuf;
message NewShape {
message Polygon
{
enum PolygonType {
POLY_TYPE_OUTER = 1;
POLY_TYPE_INNER = 2;
};
optional PolygonType type = 1 [default = POLY_TYPE_OUTER];
// x, y coordinates
repeated uint32 point = 2 [packed = true];
}
message Shape
{
repeated Polygon polygon = 1;
}
repeated Shape shape = 2;
}
Upvotes: 0
Views: 1053
Reputation: 14667
Given your format, you can access points from a properly populated and deserialized object like this:
// newShape is the deserialized object here
const auto s0p0p0 = newShape.shape(0).polygon(0).point(0);
const auto s0p0p1 = newShape.shape(0).polygon(0).point(1);
Similarly, shape(1)
would give you the second shape to access it points in the polygon object. You should check the *_size()
methods before accessing an index to ensure valid access e.g. newShape.shape_size()
, shape.polygon_size()
and polygon.point_size()
.
If you intend to modify the message, you can use mutable_*
methods to get the pointers to respective objects and then you can change those.
For example (change points of first polygon of first shape):
auto p = newShape2.mutable_shape(0);
p->mutable_polygon(0)->set_point(0, 123);
p->mutable_polygon(0)->set_point(1, 345);
Here's a complete working example of serialization and deserialization:
#include <iostream>
#include "newShape.pb.h"
int main()
{
// Serailization
::myProject::protobuf::NewShape newShape1;
auto shape1 = newShape1.add_shape();
auto polygon1 = shape1->add_polygon();
polygon1->add_point(1);
polygon1->add_point(2);
auto shape2 = newShape1.add_shape();
auto polygon2 = shape2->add_polygon();
polygon2->add_point(3);
polygon2->add_point(4);
const auto serializedData = newShape1.SerializeAsString();
std::cout << "Serialized Data Size: " << serializedData.size() << "\n\n";
// Send/Store Serialized Data
// Deserialization
::myProject::protobuf::NewShape newShape2;
if ( !newShape2.ParseFromString( serializedData ) )
{
std::cerr << "Deserialization failed!\n";
return -1;
}
std::cout << "Deserialized Data Size: " << newShape2.ByteSize() << "\n\n";
std::cout << "NewShape [Shapes: " << newShape2.shape_size() << "]\n";
for ( int i {0}; i < newShape2.shape_size(); ++i )
{
std::cout << " Shape # " << i << '\n';
const auto& shape = newShape2.shape( i );
for ( int j {0}; j < shape.polygon_size(); ++j )
{
std::cout << " Polygon # " << j << '\n';
const auto& polygon = shape.polygon( j );
for ( int k {0}; k < polygon.point_size(); ++k )
{
const auto& point = polygon.point( k );
std::cout << " Point # " << k << ": " << point << '\n';
}
}
}
return 0;
}
Output:
Serialized Data Size: 16
Deserialized Data Size: 16
NewShape [Shapes: 2]
Shape # 0
Polygon # 0
Point # 0: 1
Point # 1: 2
Shape # 1
Polygon # 0
Point # 0: 3
Point # 1: 4
Upvotes: 1