2

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

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

vertices 列的类型为 Polygon。

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

假设我想检索和访问 vertices 列中的点,我将在 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 的工作原理,但我被 Polygon 类型所困扰。实际上,我们只需要在 Postgres 中为我们的多边形存储一个简单的存储空间,但我不确定如何使用 libpqxx 访问它们。

4

4 回答 4

2

与此同时,我将使用以下方法标记字符串:

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;
   }
于 2009-07-10T01:45:04.823 回答
2

你不能用ST_X() ST_Y()吗?

为了访问线串中的第 N 个点,有ST_PointN.

于 2011-05-12T15:27:52.757 回答
0

您可以使用正则表达式来构建您的多边形: /(\d,\d)/

请注意,内部数字已分组,以便您可以通过匹配对象迭代检索它们。Ieach 迭代将 xy 对添加到多边形。而不是 \d 您应该使用可以匹配浮点数的正则表达式。

但是,这样您就无法在检索坐标之前正确验证字符串。

于 2009-07-07T11:37:25.190 回答
0

我也不熟悉 libpqxx,但在大多数客户端库中,结果都以字符串形式返回。因此,第一步尝试将字段转换为字符串并打印出来。然后,如果它看起来像 psql 显示的那样,它会自己将其解析为更易于使用的东西。

于 2009-07-07T09:27:06.763 回答