2006-10-19 00:44:37 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-10-19 00:44:37 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2006-10-19 00:44:37 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-04-08 02:45:01 +02:00
|
|
|
// mapnik
|
2006-10-19 00:44:37 +02:00
|
|
|
#include <mapnik/distance.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/ellipsoid.hpp>
|
2012-04-08 02:45:01 +02:00
|
|
|
|
|
|
|
// stl
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2006-10-19 00:44:37 +02:00
|
|
|
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
|
|
|
}
|
2006-10-19 00:44:37 +02:00
|
|
|
}
|