2016-08-12 16:51:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <DB/Functions/IFunction.h>
|
|
|
|
#include <ext/range.hpp>
|
|
|
|
#include <math.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <array>
|
2016-08-12 16:51:08 +00:00
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
#define DEGREES_IN_RADIANS (M_PI / 180.0)
|
2016-08-12 16:51:08 +00:00
|
|
|
|
2016-08-15 20:07:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2016-08-12 16:51:08 +00:00
|
|
|
|
2016-08-15 12:41:06 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
}
|
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
const Float64 EARTH_RADIUS_IN_METERS = 6372797.560856;
|
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
static inline Float64 degToRad(Float64 angle) { return angle * DEGREES_IN_RADIANS; }
|
|
|
|
static inline Float64 radToDeg(Float64 angle) { return angle / DEGREES_IN_RADIANS; }
|
2016-08-12 16:51:08 +00:00
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
/**
|
|
|
|
* The function calculates distance in meters between two points on Earth specified by longitude and latitude in degrees.
|
|
|
|
* The function uses great circle distance formula https://en.wikipedia.org/wiki/Great-circle_distance.
|
|
|
|
* Throws exception when one or several input values are not within reasonable bounds.
|
|
|
|
* Latitude must be in [-90, 90], longitude must be [-180, 180]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class FunctionGreatCircleDistance : public IFunction
|
2016-08-12 16:51:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
static constexpr auto name = "greatCircleDistance";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionGreatCircleDistance>(); }
|
2016-08-12 16:51:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum class instr_type : uint8_t
|
|
|
|
{
|
|
|
|
get_float_64,
|
|
|
|
get_const_float_64
|
|
|
|
};
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
using instr_t = std::pair<instr_type, const IColumn *>;
|
2016-08-15 12:41:06 +00:00
|
|
|
using instrs_t = std::array<instr_t, 4>;
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 4)
|
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + "doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 4",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (const auto arg_idx : ext::range(0, arguments.size()))
|
|
|
|
{
|
|
|
|
const auto arg = arguments[arg_idx].get();
|
|
|
|
if (!typeid_cast<const DataTypeFloat64 *>(arg))
|
|
|
|
throw Exception(
|
2016-08-14 12:18:11 +00:00
|
|
|
"Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function " + getName() + ". Must be Float64",
|
2016-08-12 16:51:08 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeFloat64>();
|
|
|
|
}
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
instrs_t getInstructions(const Block & block, const ColumnNumbers & arguments, bool & out_const)
|
|
|
|
{
|
2016-08-15 12:41:06 +00:00
|
|
|
instrs_t result;
|
2016-08-12 16:51:08 +00:00
|
|
|
out_const = true;
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-15 15:00:02 +00:00
|
|
|
for (const auto arg_idx : ext::range(0, arguments.size()))
|
2016-08-12 16:51:08 +00:00
|
|
|
{
|
2016-08-15 15:00:02 +00:00
|
|
|
const auto column = block.getByPosition(arguments[arg_idx]).column.get();
|
2016-08-12 16:51:08 +00:00
|
|
|
|
|
|
|
if (const auto col = typeid_cast<const ColumnVector<Float64> *>(column))
|
|
|
|
{
|
|
|
|
out_const = false;
|
2016-08-15 12:41:06 +00:00
|
|
|
result[arg_idx] = instr_t{instr_type::get_float_64, col};
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|
|
|
|
else if (const auto col = typeid_cast<const ColumnConst<Float64> *>(column))
|
|
|
|
{
|
2016-08-15 12:41:06 +00:00
|
|
|
result[arg_idx] = instr_t{instr_type::get_const_float_64, col};
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Illegal column " + column->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
/// https://en.wikipedia.org/wiki/Great-circle_distance
|
2016-08-15 20:07:31 +00:00
|
|
|
Float64 greatCircleDistance(Float64 lon1Deg, Float64 lat1Deg, Float64 lon2Deg, Float64 lat2Deg)
|
|
|
|
{
|
2016-08-14 12:18:11 +00:00
|
|
|
if (lon1Deg < -180 || lon1Deg > 180 ||
|
|
|
|
lon2Deg < -180 || lon2Deg > 180 ||
|
|
|
|
lat1Deg < -90 || lat1Deg > 90 ||
|
2016-08-15 20:07:31 +00:00
|
|
|
lat2Deg < -90 || lat2Deg > 90)
|
2016-08-14 12:18:11 +00:00
|
|
|
{
|
2016-08-15 12:41:06 +00:00
|
|
|
throw Exception("Arguments values out of bounds for function " + getName(), ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
2016-08-14 12:18:11 +00:00
|
|
|
}
|
2016-08-15 20:07:31 +00:00
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
Float64 lon1Rad = degToRad(lon1Deg);
|
|
|
|
Float64 lat1Rad = degToRad(lat1Deg);
|
|
|
|
Float64 lon2Rad = degToRad(lon2Deg);
|
|
|
|
Float64 lat2Rad = degToRad(lat2Deg);
|
|
|
|
Float64 u = sin((lat2Rad - lat1Rad) / 2);
|
|
|
|
Float64 v = sin((lon2Rad - lon1Rad) / 2);
|
|
|
|
return 2.0 * EARTH_RADIUS_IN_METERS * asin(sqrt(u * u + cos(lat1Rad) * cos(lat2Rad) * v * v));
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|
2016-08-15 20:07:31 +00:00
|
|
|
|
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
|
|
|
{
|
|
|
|
const auto size = block.rowsInFirstColumn();
|
|
|
|
|
|
|
|
bool result_is_const{};
|
|
|
|
auto instrs = getInstructions(block, arguments, result_is_const);
|
|
|
|
|
|
|
|
if (result_is_const)
|
|
|
|
{
|
|
|
|
const auto & colLon1 = static_cast<const ColumnConst<Float64> *>(block.getByPosition(arguments[0]).column.get())->getData();
|
|
|
|
const auto & colLat1 = static_cast<const ColumnConst<Float64> *>(block.getByPosition(arguments[1]).column.get())->getData();
|
|
|
|
const auto & colLon2 = static_cast<const ColumnConst<Float64> *>(block.getByPosition(arguments[2]).column.get())->getData();
|
|
|
|
const auto & colLat2 = static_cast<const ColumnConst<Float64> *>(block.getByPosition(arguments[3]).column.get())->getData();
|
|
|
|
|
2016-08-14 12:18:11 +00:00
|
|
|
Float64 res = greatCircleDistance(colLon1, colLat1, colLon2, colLat2);
|
2016-08-12 16:51:08 +00:00
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConst<Float64>>(size, res);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto dst = std::make_shared<ColumnVector<Float64>>();
|
|
|
|
block.getByPosition(result).column = dst;
|
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(size);
|
|
|
|
Float64 vals[instrs.size()];
|
|
|
|
for (const auto row : ext::range(0, size))
|
|
|
|
{
|
|
|
|
for (const auto idx : ext::range(0, instrs.size()))
|
|
|
|
{
|
|
|
|
if (instr_type::get_float_64 == instrs[idx].first)
|
2016-08-15 15:00:02 +00:00
|
|
|
vals[idx] = static_cast<const ColumnVector<Float64> *>(instrs[idx].second)->getData()[row];
|
2016-08-12 16:51:08 +00:00
|
|
|
else if (instr_type::get_const_float_64 == instrs[idx].first)
|
2016-08-15 15:00:02 +00:00
|
|
|
vals[idx] = static_cast<const ColumnConst<Float64> *>(instrs[idx].second)->getData();
|
2016-08-12 16:51:08 +00:00
|
|
|
else
|
|
|
|
throw std::logic_error{"unknown instr_type"};
|
|
|
|
}
|
2016-08-14 12:18:11 +00:00
|
|
|
dst_data[row] = greatCircleDistance(vals[0], vals[1], vals[2], vals[3]);
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-08-14 12:18:11 +00:00
|
|
|
|
|
|
|
#undef DEGREES_IN_RADIANS
|