ClickHouse

Форк
0
/
polygonConvexHull.cpp 
110 строк · 2.7 Кб
1
#include <Functions/FunctionFactory.h>
2
#include <Functions/geometryConverters.h>
3

4
#include <boost/geometry.hpp>
5
#include <boost/geometry/geometries/point_xy.hpp>
6
#include <boost/geometry/geometries/polygon.hpp>
7

8
#include <Common/logger_useful.h>
9

10
#include <Columns/ColumnArray.h>
11
#include <Columns/ColumnTuple.h>
12
#include <Columns/ColumnsNumber.h>
13
#include <DataTypes/DataTypeArray.h>
14
#include <DataTypes/DataTypeTuple.h>
15
#include <DataTypes/DataTypeCustomGeo.h>
16

17
#include <memory>
18
#include <string>
19

20
namespace DB
21
{
22

23
namespace ErrorCodes
24
{
25
    extern const int BAD_ARGUMENTS;
26
}
27

28
namespace
29
{
30

31
template <typename Point>
32
class FunctionPolygonConvexHull : public IFunction
33
{
34
public:
35
    static const char * name;
36

37
    explicit FunctionPolygonConvexHull() = default;
38

39
    static FunctionPtr create(ContextPtr)
40
    {
41
        return std::make_shared<FunctionPolygonConvexHull>();
42
    }
43

44
    String getName() const override
45
    {
46
        return name;
47
    }
48

49
    bool isVariadic() const override
50
    {
51
        return false;
52
    }
53

54
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
55

56
    size_t getNumberOfArguments() const override
57
    {
58
        return 1;
59
    }
60

61
    DataTypePtr getReturnTypeImpl(const DataTypes &) const override
62
    {
63
        return DataTypeFactory::instance().get("Polygon");
64
    }
65

66
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
67
    {
68
        PolygonSerializer<Point> serializer;
69

70
        callOnGeometryDataType<Point>(arguments[0].type, [&] (const auto & type)
71
        {
72
            using TypeConverter = std::decay_t<decltype(type)>;
73
            using Converter = typename TypeConverter::Type;
74

75
            if constexpr (std::is_same_v<Converter, ColumnToPointsConverter<Point>>)
76
                throw Exception(ErrorCodes::BAD_ARGUMENTS, "The argument of function {} must not be a Point", getName());
77
            else
78
            {
79
                auto geometries = Converter::convert(arguments[0].column->convertToFullColumnIfConst());
80

81
                for (size_t i = 0; i < input_rows_count; ++i)
82
                {
83
                    Polygon<Point> convex_hull{};
84
                    boost::geometry::convex_hull(geometries[i], convex_hull);
85
                    serializer.add(convex_hull);
86
                }
87
            }
88
        }
89
        );
90

91
        return serializer.finalize();
92
    }
93

94
    bool useDefaultImplementationForConstants() const override
95
    {
96
        return true;
97
    }
98
};
99

100
template <>
101
const char * FunctionPolygonConvexHull<CartesianPoint>::name = "polygonConvexHullCartesian";
102

103
}
104

105
REGISTER_FUNCTION(PolygonConvexHull)
106
{
107
    factory.registerFunction<FunctionPolygonConvexHull<CartesianPoint>>();
108
}
109

110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.