我想检索存储在多边形中的点
Postgres数据库
数据库的内容是:

 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))

顶点列的类型为多边形。

我正在使用C++的libpqxx库。

假设我要检索和访问“顶点”列中的点,
我将在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..)

    }

抱歉,我是libpqxx的新手。我非常了解libpqxx
可以,但是我对多边形类型感到困惑。我们实际上只需要一个简单的
在Postgres中存储多边形的方式,但是我不确定如何访问它们
使用libpqxx。

最佳答案

同时,我将使用以下方法对字符串进行标记:

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;
   }

关于c++ - 如何使用Libpqxx访问存储在Postgres中的多边形的点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1089991/

10-11 18:02