Postgis + boost :: geometry + C ++ [英] Postgis + boost::geometry + C++

查看:84
本文介绍了Postgis + boost :: geometry + C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下wtk:

POLYGON((0 0, 10 0, 10 11, 11 10, 0 10))

在boost :: geometry中,我们可以使用

In boost::geometry we can get this polygon representation using

boost::geometry::dsv(polygon," "," "," ");

如何将postgis st_makevalid函数应用于boost :: geometry多边形?我想是这样的:

How I can apply postgis st_makevalid function to a boost::geometry polygon? I suppose it something like:

#include "libpq/libpq-fs.h"
#include "../libpq-fe.h"
.
.
.
std::string geom = "POLYGON" + boost::geometry::dsv(multipoly," "," "," ");

res = PQexecParams(conn, "SELECT st_makevalid(geom) FROM ....

我不想连接到数据库,我只想使用postgis函数st_makevalid修复多边形或多面体.

I do not want to connect to a data base, I just want to repair a polygon or multipolygon with the postgis function st_makevalid.

推荐答案

我怀疑您需要PostGIS进行此操作.

I doubt you need PostGIS for this operation.

我也怀疑是否有一种方法可以使其有效".因为多边形具有清晰的自交点:

I also doubt there is a way to "make it valid". Because the polygon has a clear self intersection:

以下是您在Boost Geometry本身中进行验证和校正的方法:

Here's how you do the validation and correction in Boost Geometry itself:

在Coliru上直播

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/algorithms/equals.hpp>
#include <iostream>

namespace bg = boost::geometry;
namespace bgm = boost::geometry::model;

template<typename G>
bool check(G const& g) {
    std::string reason;
    bool valid = bg::is_valid(g, reason);

    if (valid) std::cout << "Valid (dsv): " << bg::dsv(g) << "\n";
    else       std::cout << "Invalid: " << reason << "\n";

    return valid;
}

int main() {
    using pt = bgm::d2::point_xy<double>;
    using poly = bgm::polygon<pt>;

    poly p;
    bg::read_wkt("POLYGON((0 0, 10 0, 10 11, 11 10, 0 10))", p);

    while (!check(p)) {
        auto same = p;
        bg::correct(p);

        if (bg::equals(p, same)) {
            std::cout << "Out of ideas\n";
            break;
        }
    }
}

并注意输出:

Invalid: Geometry is defined as closed but is open
Invalid: Geometry has invalid self-intersections. A self-intersection point was found at (10, 10); method: i; operations: u/i; segment IDs {source, multi, ring, segment}: {0, -1, -1, 1}/{0, -1, -1, 3}
Out of ideas

如果您的来源实际上包含这样的自相交,则很难说出您想要的内容.也许您想看看

If your source actually contains self-intersections like that, it's hard to tell what you'd like. Perhaps you want to look at

这篇关于Postgis + boost :: geometry + C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆