I have the obtained the WKT string of a boost multipolygon which is constructed by union of boost polygons in a vector called vectorPolygons
std::vector<boost::geometry::Polygon> vectorPolygons;
After adding data in vectorPolygons below code computes its union and then extracts the WKT of the multi polygon
if (!vectorPolygons.empty())
{
multi_polygon boost_multipolygon; // Will store union of polygons
// Create the union of all the polygons
for (const boost::geometry::Polygon& p : vectorPolygons) {
// add another polygon each iteration
multi_polygon tmp_poly;
boost::geometry::union_(boost_multipolygon, p, tmp_poly);
boost_multipolygon= tmp_poly;
boost::geometry::clear(tmp_poly);
}
std::string validity_reason;
bool valid = boost::geometry::is_valid(boost_multipolygon, validity_reason);
if (!valid)
{
boost::geometry::correct(boost_multipolygon);
}
std::stringstream ss;
ss << boost::geometry::wkt(boost_multipolygon);
std::string wkt = ss.str();
}
The WKT string comes out as below
MULTIPOLYGON(((40000 30000,40000 -0,30000 -0,30000 10000,20000 10000,20000 20000,10000 20000,0 20000,0 30000,40000 30000)))
When i display this multipolygon by using the code example below then I am not sure why its display is always rotated upside down. In reality it should have been exactly upside down of what it displays.
I have tried to use boost::geometry::correct on the boost multipolygon before extracting the WKT also but still the display is always rotated. What is that i am doing wrong?
Here is the code which is trying to display the WKT generated above and the output is upside down
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/adapted/boost_polygon.hpp>
#include <vector>
namespace boost {
namespace geometry {
typedef model::d2::point_xy<double> point;
typedef model::polygon<point> polygon;
}
}
using multi_polygon = boost::geometry::model::multi_polygon<boost::geometry::polygon>;
namespace bg = boost::geometry;
int main()
{
// Specify the basic type
typedef boost::geometry::model::d2::point_xy<double> point_type;
multi_polygon b;
boost::geometry::read_wkt("MULTIPOLYGON(((40000 30000,40000 -0,30000 -0,30000 10000,20000 10000,20000 20000,10000 20000,0 20000,0 30000,40000 30000)))", b);
boost::geometry::correct(b);
// Declare a stream and an SVG mapper
std::ofstream svg("my_map.svg");
boost::geometry::svg_mapper<point_type> mapper(svg, 500, 500);
// Add geometries such that all these geometries fit on the map
mapper.add(b);
// Draw the geometries on the SVG map, using a specific SVG style
mapper.map(b, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:2");
// Destructor of map will be called - adding </svg>
// Destructor of stream will be called, closing the file
return 0;
}
There is no "right side up". Cartesian coordinate systems are just that: coordinate systems. How you map them on a visual projection is your choice.
Here's the output on my system:
Shows as
So you can see that axes grow down/to the right. This agrees with the first random online WKT renderer I could find:
By that count I'd rate the output as "correct".
Flipping It?
You could just manually flip the system:
Result
A more versatile approach is to use a transform that can scale, rotate and translate over arbitrary distances/amounts: https://www.boost.org/doc/libs/1_78_0/libs/geometry/doc/html/geometry/reference/algorithms/transform/transform_3_with_strategy.html
UPDATE
Taking it point by point: "Is it not possible to retain the input" - yes, it is always possible to retain invalid input, and you should expect to retain invalid output.
However, you didn't say what was being corrected. I ran my code with your data:
In a way, your data is not invalid, it's just invalid for the chosen geometry type! You could simply change the orientation for your polygon type:
Without any further changes you get: http://coliru.stacked-crooked.com/a/f02e56fc2402112d
Printing
Summary
Moral of the story: always check that your input data is valid.
Also, please note, NONE of these ever "rotated" the output. The output was simply undefined and not what you wanted (it was more akin to the "negative complement" of the shape you were looking for, but that was actually accidental and the result was unspecified because the input violated the pre-conditions).