views:

174

answers:

3

I want to retrieve the points of a polygon that are stored in the postgres db. The contents of the db are:

 polygonid |vertices
-----------+---------------------------------------------------------------------
         2 |((1,0),(1.5,-1),(2,-1),(2,1),(1,1),(0,0),(0,2),(3,2),(3,-2),(1,-2))
         4 | ((3,3),(4,4),(5,5))

The vertices column is of type Polygon.

I'm using libpqxx library for C++.

Suppose I want to retrieve and access the points in the vertices column, I would execute these statements in C++:

    result R = W.exec ("select * from polygon_tbl");
    for (result::const_iterator r = R.begin();
         r != R.end();
         ++r)
    {
       int x = 0;
       cout << "Polygon ID: " << r[0].to(x) << endl;

       //Suppose i would like to print the first point of every polygon,
       //how would i access it?
       cout << "First vertex: " << r[1][0] << endl;    ???

       //Or suppose i would like to print the first x coordinate of
       //every polygon, how would i access it?
       cout << "First x coordinate: " << r[1][0][0] << endl; //???? (am just guessing here..)

    }

Sorry I'm very new to libpqxx. I've pretty much understood how the libpqxx works but I'm stuck with the Polygon types. We actually just need a simple storage for our polygons in Postgres but I'm not sure how to access them using libpqxx.

A: 

I'm not familiar with libpqxx neither but in most of client libs the results are returned as strings. So as a first step try to convert the field to string and print it. Then if it looks the way psql show it parse it yourself to something easier to work with.

Milen A. Radev
Hi thanks for your comment.We can actually retrieve it as a string using the r[1].c_str(). Then we parse the string to get the values. The string we get is something like this: ((3,3),(4,4),(5,5)). From that we can parse it but I'm holding this as my last resort. If that is the only way to do it then parsing is the answer. But since there is not much resource for libpqxx then I'm not sure if that is the best way to go....
Mark Gabriel A. Paylaga
A: 

You could use a regex to build your poylgon: /(\d,\d)/

Notice that the inner numbers are grouped so that you can retrieve them iterating over the match object. Ieach iteration adds a xy pair to the polygon. Instead of \d you sould use a regex which can match floating numbers.

However, with this you can't validate the String properly before retrieving the coordinates.

Hi thanks for the info. String manipulation can be done but I was thinking of casting the Postgres polygon/points type to a container of some type that C++/libpqxx can readily use. This is what I'm looking for. If there's none then I will have to resort to the string manipulation.
Mark Gabriel A. Paylaga
+1  A: 

For the meantime I'll just tokenize the string using this:

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(",()");
tokenizer tokens(str_coordinates, sep);
for (tokenizer::iterator tok_iter = tokens.begin();
   tok_iter != tokens.end(); ++tok_iter)
   {
       std::cout << "x:<" << *tok_iter << "> ";
       ++tok_iter;
       std::cout << "y:<" << *tok_iter << "> " << endl;
   }
Mark Gabriel A. Paylaga