c++ - 如何使用 Libpqxx 访问存储在 Postgres 中的多边形点?

标签 c++ postgresql polygon libpqxx

我想检索存储在中的多边形点 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/

相关文章:

c++ - 避免 'Buffer Overrun' C6386 警告

python Postgresql CREATE DATABASE IF NOT EXISTS 是错误的

arrays - 如何检查直线多边形是否相交

ruby-on-rails - 如何在不使用 EVAL 的情况下将字符串转换为散列

sql - 查询postgresql表的效率问题

algorithm - 两个凸多边形的交集

javascript - 错误检测到多边形中的点

c++ - 如何将 Python dict 转换为 Pybind11 中的 C++ 计数器部分?

c++ - 如何让 dirent 忽略当前目录?

c++ - 从我的 C/C++ 代码中获取等效的汇编代码 - x86 和 ARM