【问题标题】:How can I access the points of the polygon stored in Postgres using Libpqxx?如何使用 Libpqxx 访问存储在 Postgres 中的多边形的点?
【发布时间】:2010-11-08 13:36:02
【问题描述】:

我想检索存储在 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))

顶点列的类型是多边形。

我正在使用 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。

【问题讨论】:

    标签: c++ postgresql polygon libpqxx


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      要访问线串中的第 N 个点,有 ST_PointN

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 您好,感谢您的评论。我们实际上可以使用 r[1].c_str() 将其作为字符串检索。然后我们解析字符串以获取值。我们得到的字符串是这样的:((3,3),(4,4),(5,5))。从那里我们可以解析它,但我把它作为我最后的手段。如果这是唯一的方法,那么解析就是答案。但是由于 libpqxx 的资源不多,所以我不确定这是否是最好的方法....
        【解决方案4】:

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

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

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

        【讨论】:

        • 您好,感谢您的信息。可以进行字符串操作,但我正在考虑将 Postgres 多边形/点类型转换为 C++/libpqxx 可以轻松使用的某种类型的容器。这就是我要找的。如果没有,那么我将不得不求助于字符串操作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 2012-06-23
        相关资源
        最近更新 更多