ClickHouse/src/Functions/polygonsDistance.cpp

104 lines
2.7 KiB
C++
Raw Normal View History

2020-06-17 15:29:08 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/geometryConverters.h>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
2020-06-21 14:54:13 +00:00
#include <common/logger_useful.h>
2020-06-17 15:29:08 +00:00
#include <Columns/ColumnArray.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypesNumber.h>
2020-06-17 15:29:08 +00:00
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeCustomGeo.h>
#include <memory>
#include <utility>
2020-06-17 15:29:08 +00:00
namespace DB
{
2021-01-19 14:52:53 +00:00
template <typename Point>
class FunctionPolygonsDistance : public IFunction
2020-06-17 15:29:08 +00:00
{
public:
2021-01-19 14:52:53 +00:00
static inline const char * name;
2020-06-17 15:29:08 +00:00
2021-01-19 14:52:53 +00:00
explicit FunctionPolygonsDistance() = default;
2020-06-17 15:29:08 +00:00
static FunctionPtr create(const Context &)
{
2021-01-19 14:52:53 +00:00
return std::make_shared<FunctionPolygonsDistance>();
2020-06-17 15:29:08 +00:00
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return false;
}
size_t getNumberOfArguments() const override
{
return 2;
}
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
return std::make_shared<DataTypeFloat64>();
2020-06-17 15:29:08 +00:00
}
2021-01-19 14:52:53 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
2020-06-17 15:29:08 +00:00
{
2021-01-19 14:52:53 +00:00
auto first_parser = makeGeometryFromColumnParser<Point>(arguments[0]);
2020-06-17 15:29:08 +00:00
auto first_container = createContainer(first_parser);
2021-01-19 14:52:53 +00:00
auto second_parser = makeGeometryFromColumnParser<Point>(arguments[1]);
2020-06-17 15:29:08 +00:00
auto second_container = createContainer(second_parser);
auto res_column = ColumnFloat64::create();
2020-06-17 15:29:08 +00:00
for (size_t i = 0; i < input_rows_count; i++)
{
2021-01-19 14:52:53 +00:00
get(first_parser, first_container, i);
get(second_parser, second_container, i);
2020-06-22 17:36:40 +00:00
Float64 distance = boost::geometry::distance(
2021-01-19 14:52:53 +00:00
boost::get<MultiPolygon<Point>>(first_container),
boost::get<MultiPolygon<Point>>(second_container));
2020-06-17 15:29:08 +00:00
res_column->insertValue(distance);
2020-06-17 15:29:08 +00:00
}
2021-01-18 23:51:34 +00:00
return res_column;
2020-06-21 14:54:13 +00:00
}
2021-01-19 14:52:53 +00:00
bool useDefaultImplementationForConstants() const override
2020-06-21 14:54:13 +00:00
{
2021-01-19 14:52:53 +00:00
return true;
2020-06-17 15:29:08 +00:00
}
};
2021-01-19 14:52:53 +00:00
template <>
const char * FunctionPolygonsDistance<CartesianPoint>::name = "polygonsDistanceCartesian";
2020-06-17 15:29:08 +00:00
2021-01-19 14:52:53 +00:00
template <>
const char * FunctionPolygonsDistance<GeographicPoint>::name = "polygonsDistanceGeographic";
2021-01-18 23:51:34 +00:00
2021-01-19 14:52:53 +00:00
void registerFunctionPolygonsDistance(FunctionFactory & factory)
2020-06-17 15:29:08 +00:00
{
2021-01-19 14:52:53 +00:00
factory.registerFunction<FunctionPolygonsDistance<CartesianPoint>>();
factory.registerFunction<FunctionPolygonsDistance<GeographicPoint>>();
2020-06-17 15:29:08 +00:00
}
2021-01-19 14:52:53 +00:00
2020-06-17 15:29:08 +00:00
}