提高几何精度损失 [英] boost geometry precision loss

查看:90
本文介绍了提高几何精度损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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);
}

结果是:

POLYGON((45.4603 9.11463,45.4603 9.11963,45.4653 9.11963,45.4653 9.11463,45.4603 9.11463))

POLYGON((45.4603 9.11463,45.4603 9.11963,45.4653 9.11963,45.4653 9.11463,45.4603 9.11463))

对于地理坐标来说,精度太低了,我该怎么做才能保持相同的精度?

Is loosing too much precision for geo-coordinates, what I have to do to leave the same precision?

推荐答案

只需在流上设置输出精度,例如

Just set the output precision on the stream, e.g.

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

这要简单得多.您也不必做整个缓冲舞蹈!

This is a lot simpler. You don't have to do the whole buffer dance either!

在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))

这篇关于提高几何精度损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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