ClickHouse

Форк
0
/
h3EdgeAngle.cpp 
104 строки · 3.1 Кб
1
#include "config.h"
2

3
#if USE_H3
4

5
#include <Columns/ColumnsNumber.h>
6
#include <DataTypes/DataTypesNumber.h>
7
#include <Functions/FunctionFactory.h>
8
#include <Functions/IFunction.h>
9
#include <IO/WriteHelpers.h>
10
#include <Common/typeid_cast.h>
11

12
#include <constants.h>
13
#include <h3api.h>
14

15

16
namespace DB
17
{
18
namespace ErrorCodes
19
{
20
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
21
    extern const int ARGUMENT_OUT_OF_BOUND;
22
    extern const int ILLEGAL_COLUMN;
23
}
24

25
namespace
26
{
27

28
class FunctionH3EdgeAngle : public IFunction
29
{
30
public:
31
    static constexpr auto name = "h3EdgeAngle";
32

33
    static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3EdgeAngle>(); }
34

35
    std::string getName() const override { return name; }
36

37
    size_t getNumberOfArguments() const override { return 1; }
38
    bool useDefaultImplementationForConstants() const override { return true; }
39
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
40

41
    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
42
    {
43
        const auto * arg = arguments[0].get();
44
        if (!WhichDataType(arg).isUInt8())
45
            throw Exception(
46
                ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
47
                "Illegal type {} of argument {} of function {}. Must be UInt8",
48
                arg->getName(), 1, getName());
49

50
        return std::make_shared<DataTypeFloat64>();
51
    }
52

53
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
54
    {
55
        auto non_const_arguments = arguments;
56
        for (auto & argument : non_const_arguments)
57
            argument.column = argument.column->convertToFullColumnIfConst();
58

59
        const auto * column = checkAndGetColumn<ColumnUInt8>(non_const_arguments[0].column.get());
60
        if (!column)
61
            throw Exception(
62
                ErrorCodes::ILLEGAL_COLUMN,
63
                "Illegal type {} of argument {} of function {}. Must be UInt8.",
64
                arguments[0].type->getName(),
65
                1,
66
                getName());
67

68
        const auto & data = column->getData();
69

70
        auto dst = ColumnVector<Float64>::create();
71
        auto & dst_data = dst->getData();
72
        dst_data.resize(input_rows_count);
73

74
        for (size_t row = 0; row < input_rows_count; ++row)
75
        {
76
            const UInt8 resolution = data[row];
77
            if (resolution > MAX_H3_RES)
78
                throw Exception(
79
                    ErrorCodes::ARGUMENT_OUT_OF_BOUND,
80
                    "The argument 'resolution' ({}) of function {} is out of bounds because the maximum resolution in H3 library is {}",
81
                    toString(resolution),
82
                    getName(),
83
                    MAX_H3_RES);
84

85
            // Numerical constant is 180 degrees / pi / Earth radius, Earth radius is from h3 sources
86
            Float64 res = 8.99320592271288084e-6 * getHexagonEdgeLengthAvgM(resolution);
87

88
            dst_data[row] = res;
89
        }
90

91
        return dst;
92
    }
93
};
94

95
}
96

97
REGISTER_FUNCTION(H3EdgeAngle)
98
{
99
    factory.registerFunction<FunctionH3EdgeAngle>();
100
}
101

102
}
103

104
#endif
105

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

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

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

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