mapnik/src/distance.cpp

60 lines
1.8 KiB
C++
Raw Normal View History

/*****************************************************************************
2012-02-02 02:53:35 +01:00
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
#include <mapnik/distance.hpp>
2007-10-08 19:42:41 +02:00
#include <mapnik/ellipsoid.hpp>
// stl
2007-10-08 19:42:41 +02:00
#include <cmath>
namespace mapnik {
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
using std::atan2;
using std::cos;
using std::pow;
using std::sin;
using std::sqrt;
2009-01-16 00:12:56 +01:00
2010-06-02 13:03:30 +02:00
static const double deg2rad = 0.0174532925199432958;
static const double R = 6372795.0; // average great-circle radius of the earth
2012-02-02 02:53:35 +01:00
double great_circle_distance::operator() (coord2d const& pt0,
2010-06-02 13:03:30 +02:00
coord2d const& pt1) const
{
double lon0 = pt0.x * deg2rad;
double lat0 = pt0.y * deg2rad;
double lon1 = pt1.x * deg2rad;
double lat1 = pt1.y * deg2rad;
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
double dlat = lat1 - lat0;
double dlon = lon1 - lon0;
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
double sin_dlat = sin(0.5 * dlat);
double sin_dlon = sin(0.5 * dlon);
2012-02-02 02:53:35 +01:00
2010-06-02 13:03:30 +02:00
double a = pow(sin_dlat,2.0) + cos(lat0)*cos(lat1)*pow(sin_dlon,2.0);
double c = 2 * atan2(sqrt(a),sqrt(1 - a));
2012-02-02 02:53:35 +01:00
return R * c;
2010-06-02 13:03:30 +02:00
}
}