2

I have the following wtk:

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

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

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

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

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.

sergio campo
  • 190
  • 2
  • 13

1 Answers1

1

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:

enter image description here

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

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

And note the output:

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • thank you and Yes I know what you mean, but i would like to know how to use geometry operation with POSTGIS when poligons are no contained in DATA BASE. I would like an example. – sergio campo May 05 '18 at 06:57
  • You should probably ask a separate question then. In your questing you specifically _only_ ask for `st_makevalid` (and why use an invalid example polygon? That distracts from your question) – sehe May 05 '18 at 08:09
  • "ST_MakeValid — Attempts to make an invalid geometry valid without losing vertices." – sergio campo May 05 '18 at 14:22