2015-01-22 3 views
0
#include <iostream> 
#include <vector> 
#include <boost/geometry.hpp> 
#include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp> 
#include <boost/geometry/geometries/point_xy.hpp> 
#include <boost/geometry/geometries/polygon.hpp> 
#include <boost/geometry/geometries/linestring.hpp> 
#include <boost/geometry/multi/geometries/multi_polygon.hpp> 
#include <boost/geometry/io/wkt/wkt.hpp> 
#include <boost/foreach.hpp> 

int main(int argc, char *argv[]) 
{ 
    typedef boost::geometry::model::d2::point_xy<double> point; 
    typedef boost::geometry::model::polygon<point> polygon; 
    std::stringstream ss; 
    std::string sstring; 
    char *poly1 = 
     "POLYGON((45.4602851 9.1146293,45.4602851 9.1196293,45.4652851 9.1196293,45.4652851 9.1146293,45.4602851 9.1146293))"; 
    char *buffer = NULL; 
    int poly1StrLen; 
    double tmp; 
    polygon poly, newPoly; 
    point p1; 

    boost::geometry::read_wkt(poly1, poly); 
    boost::geometry::correct(poly); 
    BOOST_FOREACH(point const & p, boost::geometry::exterior_ring(poly)) 
    { 
     //  ss << boost::geometry::wkt(p); 
     //  p1.x(p.y()); 
     //  p1.y(p.x()); 
     boost::geometry::append(boost::geometry::exterior_ring(newPoly), p); 
    } 
    ss << boost::geometry::wkt(newPoly); 
    sstring = ss.str(); 
    buffer = (char *)malloc(sstring.length()); 
    buffer = strcpy(buffer, sstring.c_str()); 
    printf("%s\n", buffer); 
    free(buffer); 
} 

Результат:увеличение потери геометрии точность

ПОЛИГОН ((45,4603 9.11463,45.4603 9.11963,45.4653 9.11963,45.4653 9.11463,45.4603 9,11463))

теряет слишком много точности для гео-координат, что я должен сделать, чтобы оставить такую ​​же точность?

+0

вы можете просто 'станд :: соиЬ << WKT (newPoly)' или 'станд :: соиЬ << ss.rdbuf () 'или' std :: cout << ss.str() 'или даже' printf ("% s \ n", ss.str(). c_str()) '. Использование 'malloc' в C++ всегда очень странный знак – sehe

ответ

2

Просто установите выходную точность в потоке, например.

std::cout << std::setprecision(12) << boost::geometry::wkt(newPoly); 

Это намного проще. Вам также не нужно делать весь буферный танец!

Live On Coliru

#include <iostream> 
#include <vector> 
#include <boost/geometry.hpp> 
#include <boost/geometry/geometries/point_xy.hpp> 
#include <boost/geometry/geometries/polygon.hpp> 
#include <boost/geometry/io/wkt/wkt.hpp> 
#include <boost/foreach.hpp> 

int main() { 
    typedef boost::geometry::model::d2::point_xy<double> point; 
    typedef boost::geometry::model::polygon<point> polygon; 
    char const *poly1 = "POLYGON((45.4602851 9.1146293,45.4602851 9.1196293,45.4652851 9.1196293,45.4652851 9.1146293,45.4602851 9.1146293))"; 
    polygon poly, newPoly; 

    boost::geometry::read_wkt(poly1, poly); 
    boost::geometry::correct(poly); 

    BOOST_FOREACH(point const & p, boost::geometry::exterior_ring(poly)) { 
     boost::geometry::append(boost::geometry::exterior_ring(newPoly), p); 
    } 

    std::cout << std::setprecision(12) << boost::geometry::wkt(newPoly); 
} 

Печать

POLYGON((45.4602851 9.1146293,45.4602851 9.1196293,45.4652851 9.1196293,45.4652851 9.1146293,45.4602851 9.1146293)) 
+0

спасибо! setprecision (x) - это решение! –

+0

@GiovanniCampagnoli Добро пожаловать в SO. Обязательно проверьте http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work :) – sehe