问题
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.
回答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:
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
- Split Self intersecting Polygon into non self intersecting polygon
- Area of self-intersecting polygon
来源:https://stackoverflow.com/questions/50179208/postgis-boostgeometry-c