ClickHouse/src/Functions/polygonPerimeter.cpp

141 lines
3.7 KiB
C++
Raw Normal View History

2020-06-22 16:36:21 +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>
#include <common/logger_useful.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeCustomGeo.h>
#include <memory>
#include <string>
namespace DB
{
namespace ErrorCodes
{
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
extern const int BAD_ARGUMENTS;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
}
2021-01-18 23:51:34 +00:00
template <typename Derived>
class FunctionPolygonPerimeterBase : public IFunction
2020-06-22 16:36:21 +00:00
{
public:
2021-01-18 23:51:34 +00:00
static inline const char * name = Derived::name;
2020-06-22 16:36:21 +00:00
2021-01-18 23:51:34 +00:00
explicit FunctionPolygonPerimeterBase() = default;
2020-06-22 16:36:21 +00:00
static FunctionPtr create(const Context &)
{
2021-01-18 23:51:34 +00:00
return std::make_shared<FunctionPolygonPerimeterBase>();
2020-06-22 16:36:21 +00:00
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return false;
}
size_t getNumberOfArguments() const override
{
return 1;
}
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
return std::make_shared<DataTypeFloat64>();
}
2021-01-18 23:51:34 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
return static_cast<const Derived*>(this)->executeImplementation(arguments, result_type, input_rows_count);
}
bool useDefaultImplementationForConstants() const override
2020-06-22 16:36:21 +00:00
{
2021-01-18 23:51:34 +00:00
return true;
}
};
2020-06-22 16:36:21 +00:00
2021-01-18 23:51:34 +00:00
class FunctionPolygonPerimeterCartesian : public FunctionPolygonPerimeterBase<FunctionPolygonPerimeterCartesian>
{
public:
static inline const char * name = "polygonPerimeterCartesian";
ColumnPtr executeImplementation(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const
{
auto parser = makeCartesianGeometryFromColumnParser(arguments[0]);
2020-06-22 16:36:21 +00:00
auto container = createContainer(parser);
auto res_column = ColumnFloat64::create();
for (size_t i = 0; i < input_rows_count; i++)
{
get(parser, container, i);
Float64 perimeter = boost::geometry::perimeter(
2021-01-18 23:51:34 +00:00
boost::get<CartesianMultiPolygon>(container));
2020-06-22 16:36:21 +00:00
res_column->insertValue(perimeter);
}
2021-01-18 23:51:34 +00:00
return res_column;
2020-06-22 16:36:21 +00:00
}
2021-01-18 23:51:34 +00:00
};
2020-06-22 16:36:21 +00:00
2021-01-18 23:51:34 +00:00
class FunctionPolygonPerimeterGeographic : public FunctionPolygonPerimeterBase<FunctionPolygonPerimeterGeographic>
{
public:
static inline const char * name = "polygonPerimeterGeographic";
ColumnPtr executeImplementation(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const
2020-06-22 16:36:21 +00:00
{
2021-01-18 23:51:34 +00:00
auto parser = makeGeographicGeometryFromColumnParser(arguments[0]);
auto container = createContainer(parser);
auto res_column = ColumnFloat64::create();
for (size_t i = 0; i < input_rows_count; i++)
{
get(parser, container, i);
Float64 perimeter = boost::geometry::perimeter(
boost::get<GeographicMultiPolygon>(container));
res_column->insertValue(perimeter);
}
return res_column;
2020-06-22 16:36:21 +00:00
}
};
2021-01-18 23:51:34 +00:00
void registerFunctionPolygonPerimeterCartesian(FunctionFactory & factory)
{
factory.registerFunction<FunctionPolygonPerimeterCartesian>();
}
void registerFunctionPolygonPerimeterGeographic(FunctionFactory & factory)
2020-06-22 16:36:21 +00:00
{
2021-01-18 23:51:34 +00:00
factory.registerFunction<FunctionPolygonPerimeterGeographic>();
2020-06-22 16:36:21 +00:00
}
}