Merge pull request #33479 from bharatnc/ncb/h3-misc-funcs

add h3 misc functions - part 1
This commit is contained in:
Maksim Kita 2022-01-10 23:44:42 +03:00 committed by GitHub
commit 4ddac56787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 755 additions and 0 deletions

View File

@ -380,6 +380,42 @@ Result:
└──────┘
```
## h3HexAreaKm2 {#h3hexareakm2}
Returns average hexagon area in square kilometers at the given resolution.
**Syntax**
``` sql
h3HexAreaKm2(resolution)
```
**Parameter**
- `resolution` — Index resolution. Range: `[0, 15]`. Type: [UInt8](../../../sql-reference/data-types/int-uint.md).
**Returned value**
- Area in square kilometers.
Type: [Float64](../../../sql-reference/data-types/float.md).
**Example**
Query:
``` sql
SELECT h3HexAreaKm2(13) AS area;
```
Result:
``` text
┌──────area─┐
│ 0.0000439 │
└───────────┘
```
## h3IndexesAreNeighbors {#h3indexesareneighbors}
Returns whether or not the provided [H3](#h3index) indexes are neighbors.
@ -704,4 +740,144 @@ Result:
└───────┘
```
## h3DegsToRads {#h3degstorads}
Converts degrees to radians.
**Syntax**
``` sql
h3DegsToRads(degrees)
```
**Parameter**
- `degrees` — Input in degrees. Type: [Float64](../../../sql-reference/data-types/float.md).
**Returned values**
- Radians. Type: [Float64](../../../sql-reference/data-types/float.md).
**Example**
Query:
``` sql
SELECT h3DegsToRads(180.0) AS radians;
```
Result:
``` text
┌───────────radians─┐
│ 3.141592653589793 │
└───────────────────┘
```
## h3RadsToDegs {#h3radstodegs}
Converts radians to degrees.
**Syntax**
``` sql
h3RadsToDegs(radians)
```
**Parameter**
- `radians` — Input in radians. Type: [Float64](../../../sql-reference/data-types/float.md).
**Returned values**
- Degrees. Type: [Float64](../../../sql-reference/data-types/float.md).
**Example**
Query:
``` sql
SELECT h3RadsToDegs(3.141592653589793) AS degrees;
```
Result:
``` text
┌─degrees─┐
│ 180 │
└─────────┘
```
## h3CellAreaM2 {#h3cellaream2}
Returns the exact area of a specific cell in square meters corresponding to the given input H3 index.
**Syntax**
``` sql
h3CellAreaM2(index)
```
**Parameter**
- `index` — Hexagon index number. Type: [UInt64](../../../sql-reference/data-types/int-uint.md).
**Returned value**
- Cell area in square meters.
Type: [Float64](../../../sql-reference/data-types/float.md).
**Example**
Query:
``` sql
SELECT h3CellAreaM2(579205133326352383) AS area;
```
Result:
``` text
┌───────────────area─┐
│ 4106166334463.9233 │
└────────────────────┘
```
## h3CellAreaRads2 {#h3cellarearads2}
Returns the exact area of a specific cell in square radians corresponding to the given input H3 index.
**Syntax**
``` sql
h3CellAreaRads2(index)
```
**Parameter**
- `index` — Hexagon index number. Type: [UInt64](../../../sql-reference/data-types/int-uint.md).
**Returned value**
- Cell area in square radians.
Type: [Float64](../../../sql-reference/data-types/float.md).
**Example**
Query:
``` sql
SELECT h3CellAreaRads2(579205133326352383) AS area;
```
Result:
``` text
┌────────────────area─┐
│ 0.10116268528089567 │
└─────────────────────┘
```
[Original article](https://clickhouse.com/docs/en/sql-reference/functions/geo/h3) <!--hide-->

View File

@ -0,0 +1,81 @@
#include "config_functions.h"
#if USE_H3
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <IO/WriteHelpers.h>
#include <Common/typeid_cast.h>
#include <base/range.h>
#include <constants.h>
#include <h3api.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
class FunctionH3CellAreaM2 : public IFunction
{
public:
static constexpr auto name = "h3CellAreaM2";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3CellAreaM2>(); }
std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto * arg = arguments[0].get();
if (!WhichDataType(arg).isUInt64())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}. Must be UInt64",
arg->getName(), 1, getName());
return std::make_shared<DataTypeFloat64>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto * column = checkAndGetColumn<ColumnUInt64>(arguments[0].column.get());
const auto & data = column->getData();
auto dst = ColumnVector<Float64>::create();
auto & dst_data = dst->getData();
dst_data.resize(input_rows_count);
for (size_t row = 0; row < input_rows_count; ++row)
{
const UInt64 resolution = data[row];
Float64 res = cellAreaM2(resolution);
dst_data[row] = res;
}
return dst;
}
};
}
void registerFunctionH3CellAreaM2(FunctionFactory & factory)
{
factory.registerFunction<FunctionH3CellAreaM2>();
}
}
#endif

View File

@ -0,0 +1,81 @@
#include "config_functions.h"
#if USE_H3
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <IO/WriteHelpers.h>
#include <Common/typeid_cast.h>
#include <base/range.h>
#include <constants.h>
#include <h3api.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
class FunctionH3CellAreaRads2 : public IFunction
{
public:
static constexpr auto name = "h3CellAreaRads2";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3CellAreaRads2>(); }
std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto * arg = arguments[0].get();
if (!WhichDataType(arg).isUInt64())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}. Must be UInt64",
arg->getName(), 1, getName());
return std::make_shared<DataTypeFloat64>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto * column = checkAndGetColumn<ColumnUInt64>(arguments[0].column.get());
const auto & data = column->getData();
auto dst = ColumnVector<Float64>::create();
auto & dst_data = dst->getData();
dst_data.resize(input_rows_count);
for (size_t row = 0; row < input_rows_count; ++row)
{
const UInt64 resolution = data[row];
Float64 res = cellAreaRads2(resolution);
dst_data[row] = res;
}
return dst;
}
};
}
void registerFunctionH3CellAreaRads2(FunctionFactory & factory)
{
factory.registerFunction<FunctionH3CellAreaRads2>();
}
}
#endif

View File

@ -0,0 +1,80 @@
#include "config_functions.h"
#if USE_H3
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnArray.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <Common/typeid_cast.h>
#include <h3api.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
class FunctionH3DegsToRads : public IFunction
{
public:
static constexpr auto name = "h3DegsToRads";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3DegsToRads>(); }
std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto * arg = arguments[0].get();
if (!WhichDataType(arg).isFloat64())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}. Must be Float64",
arg->getName(), 1, getName());
return std::make_shared<DataTypeFloat64>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto * column = checkAndGetColumn<ColumnFloat64>(arguments[0].column.get());
const auto & data = column->getData();
auto dst = ColumnVector<Float64>::create();
auto & dst_data = dst->getData();
dst_data.resize(input_rows_count);
for (size_t row = 0; row < input_rows_count; ++row)
{
const Float64 degrees = data[row];
auto res = degsToRads(degrees);
dst_data[row] = res;
}
return dst;
}
};
}
void registerFunctionH3DegsToRads(FunctionFactory & factory)
{
factory.registerFunction<FunctionH3DegsToRads>();
}
}
#endif

View File

@ -0,0 +1,90 @@
#include "config_functions.h"
#if USE_H3
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <IO/WriteHelpers.h>
#include <Common/typeid_cast.h>
#include <base/range.h>
#include <constants.h>
#include <h3api.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ARGUMENT_OUT_OF_BOUND;
}
namespace
{
class FunctionH3HexAreaKm2 : public IFunction
{
public:
static constexpr auto name = "h3HexAreaKm2";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3HexAreaKm2>(); }
std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto * arg = arguments[0].get();
if (!WhichDataType(arg).isUInt8())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}. Must be UInt8",
arg->getName(), 1, getName());
return std::make_shared<DataTypeFloat64>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto * column = checkAndGetColumn<ColumnUInt8>(arguments[0].column.get());
const auto & data = column->getData();
auto dst = ColumnVector<Float64>::create();
auto & dst_data = dst->getData();
dst_data.resize(input_rows_count);
for (size_t row = 0; row < input_rows_count; ++row)
{
const UInt64 resolution = data[row];
if (resolution > MAX_H3_RES)
throw Exception(
ErrorCodes::ARGUMENT_OUT_OF_BOUND,
"The argument 'resolution' ({}) of function {} is out of bounds because the maximum resolution in H3 library is ",
resolution,
getName(),
MAX_H3_RES);
Float64 res = getHexagonAreaAvgKm2(resolution);
dst_data[row] = res;
}
return dst;
}
};
}
void registerFunctionH3HexAreaKm2(FunctionFactory & factory)
{
factory.registerFunction<FunctionH3HexAreaKm2>();
}
}
#endif

View File

@ -0,0 +1,79 @@
#include "config_functions.h"
#if USE_H3
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnArray.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <Common/typeid_cast.h>
#include <h3api.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
class FunctionH3RadsToDegs : public IFunction
{
public:
static constexpr auto name = "h3RadsToDegs";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3RadsToDegs>(); }
std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto * arg = arguments[0].get();
if (!WhichDataType(arg).isFloat64())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}. Must be Float64",
arg->getName(), 1, getName());
return std::make_shared<DataTypeFloat64>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto * column = checkAndGetColumn<ColumnFloat64>(arguments[0].column.get());
const auto & col_rads = column->getData();
auto dst = ColumnVector<Float64>::create();
auto & dst_data = dst->getData();
dst_data.resize(input_rows_count);
for (size_t row = 0; row < input_rows_count; ++row)
{
const Float64 rads = col_rads[row];
auto res = radsToDegs(rads);
dst_data[row] = res;
}
return dst;
}
};
}
void registerFunctionH3RadsToDegs(FunctionFactory & factory)
{
factory.registerFunction<FunctionH3RadsToDegs>();
}
}
#endif

View File

@ -43,6 +43,12 @@ void registerFunctionH3HexAreaM2(FunctionFactory &);
void registerFunctionH3IsResClassIII(FunctionFactory &);
void registerFunctionH3IsPentagon(FunctionFactory &);
void registerFunctionH3GetFaces(FunctionFactory &);
void registerFunctionH3DegsToRads(FunctionFactory &);
void registerFunctionH3RadsToDegs(FunctionFactory &);
void registerFunctionH3HexAreaKm2(FunctionFactory &);
void registerFunctionH3CellAreaM2(FunctionFactory &);
void registerFunctionH3CellAreaRads2(FunctionFactory &);
#endif
#if USE_S2_GEOMETRY
@ -99,6 +105,11 @@ void registerFunctionsGeo(FunctionFactory & factory)
registerFunctionH3IsResClassIII(factory);
registerFunctionH3IsPentagon(factory);
registerFunctionH3GetFaces(factory);
registerFunctionH3DegsToRads(factory);
registerFunctionH3RadsToDegs(factory);
registerFunctionH3HexAreaKm2(factory);
registerFunctionH3CellAreaM2(factory);
registerFunctionH3CellAreaRads2(factory);
#endif
#if USE_S2_GEOMETRY

View File

@ -2,3 +2,4 @@
SELECT h3EdgeLengthM(100); -- { serverError 69 }
SELECT h3HexAreaM2(100); -- { serverError 69 }
SELECT h3HexAreaKm2(100); -- { serverError 69 }

View File

@ -0,0 +1,16 @@
4106166334463.9233
666617118882.2277
85294486110.07852
12781831077.715292
1730585103.2965515
302748289.6422262
30296673.089799587
4984621.68910725
644257.1047199412
113498.17901913072
16692.536464980716
2335.8824226249617
324.4496823479308
48.63220901355471
7.442732649761864
0.5977527784258132

View File

@ -0,0 +1,30 @@
-- Tags: no-fasttest
DROP TABLE IF EXISTS h3_indexes;
CREATE TABLE h3_indexes (h3_index UInt64) ENGINE = Memory;
-- Random geo coordinates were generated using the H3 tool: https://github.com/ClickHouse-Extras/h3/blob/master/src/apps/testapps/mkRandGeo.c at various resolutions from 0 to 15.
-- Corresponding H3 index values were in turn generated with those geo coordinates using `geoToH3(lon, lat, res)` ClickHouse function for the following test.
INSERT INTO h3_indexes VALUES (579205133326352383);
INSERT INTO h3_indexes VALUES (581263419093549055);
INSERT INTO h3_indexes VALUES (589753847883235327);
INSERT INTO h3_indexes VALUES (594082350283882495);
INSERT INTO h3_indexes VALUES (598372386957426687);
INSERT INTO h3_indexes VALUES (599542359671177215);
INSERT INTO h3_indexes VALUES (604296355086598143);
INSERT INTO h3_indexes VALUES (608785214872748031);
INSERT INTO h3_indexes VALUES (615732192485572607);
INSERT INTO h3_indexes VALUES (617056794467368959);
INSERT INTO h3_indexes VALUES (624586477873168383);
INSERT INTO h3_indexes VALUES (627882919484481535);
INSERT INTO h3_indexes VALUES (634600058503392255);
INSERT INTO h3_indexes VALUES (635544851677385791);
INSERT INTO h3_indexes VALUES (639763125756281263);
INSERT INTO h3_indexes VALUES (644178757620501158);
SELECT h3CellAreaM2(h3_index) FROM h3_indexes ORDER BY h3_index;
DROP TABLE h3_indexes;

View File

@ -0,0 +1,16 @@
0.10116268528089567
0.01642329421346843
0.002101380838405832
0.00031490306268786255
0.000042636031250655976
0.000007458740696242262
7.464122383736096e-7
1.2280498988731694e-7
1.587241563444197e-8
2.7962288004989136e-9
4.112502211061015e-10
5.754860352096175e-11
7.99339296836726e-12
1.1981406631437076e-12
1.8336491007639705e-13
1.4726699133479243e-14

View File

@ -0,0 +1,30 @@
-- Tags: no-fasttest
DROP TABLE IF EXISTS h3_indexes;
CREATE TABLE h3_indexes (h3_index UInt64) ENGINE = Memory;
-- Random geo coordinates were generated using the H3 tool: https://github.com/ClickHouse-Extras/h3/blob/master/src/apps/testapps/mkRandGeo.c at various resolutions from 0 to 15.
-- Corresponding H3 index values were in turn generated with those geo coordinates using `geoToH3(lon, lat, res)` ClickHouse function for the following test.
INSERT INTO h3_indexes VALUES (579205133326352383);
INSERT INTO h3_indexes VALUES (581263419093549055);
INSERT INTO h3_indexes VALUES (589753847883235327);
INSERT INTO h3_indexes VALUES (594082350283882495);
INSERT INTO h3_indexes VALUES (598372386957426687);
INSERT INTO h3_indexes VALUES (599542359671177215);
INSERT INTO h3_indexes VALUES (604296355086598143);
INSERT INTO h3_indexes VALUES (608785214872748031);
INSERT INTO h3_indexes VALUES (615732192485572607);
INSERT INTO h3_indexes VALUES (617056794467368959);
INSERT INTO h3_indexes VALUES (624586477873168383);
INSERT INTO h3_indexes VALUES (627882919484481535);
INSERT INTO h3_indexes VALUES (634600058503392255);
INSERT INTO h3_indexes VALUES (635544851677385791);
INSERT INTO h3_indexes VALUES (639763125756281263);
INSERT INTO h3_indexes VALUES (644178757620501158);
SELECT h3CellAreaRads2(h3_index) FROM h3_indexes ORDER BY h3_index;
DROP TABLE h3_indexes;

View File

@ -0,0 +1,16 @@
4250546.848
607220.9782
86745.85403
12392.26486
1770.323552
252.9033645
36.1290521
5.1612932
0.7373276
0.1053325
0.0150475
0.0021496
0.0003071
0.0000439
0.0000063
9e-7

View File

@ -0,0 +1,18 @@
-- Tags: no-fasttest
SELECT h3HexAreaKm2(0);
SELECT h3HexAreaKm2(1);
SELECT h3HexAreaKm2(2);
SELECT h3HexAreaKm2(3);
SELECT h3HexAreaKm2(4);
SELECT h3HexAreaKm2(5);
SELECT h3HexAreaKm2(6);
SELECT h3HexAreaKm2(7);
SELECT h3HexAreaKm2(8);
SELECT h3HexAreaKm2(9);
SELECT h3HexAreaKm2(10);
SELECT h3HexAreaKm2(11);
SELECT h3HexAreaKm2(12);
SELECT h3HexAreaKm2(13);
SELECT h3HexAreaKm2(14);
SELECT h3HexAreaKm2(15);

View File

@ -0,0 +1,9 @@
-360
-180.6
-180
-1
0
1
180
180.5
360

View File

@ -0,0 +1,21 @@
-- Tags: no-fasttest
DROP TABLE IF EXISTS h3_indexes;
CREATE TABLE h3_indexes (degrees Float64) ENGINE = Memory;
INSERT INTO h3_indexes VALUES (-1);
INSERT INTO h3_indexes VALUES (-180);
INSERT INTO h3_indexes VALUES (-180.6);
INSERT INTO h3_indexes VALUES (-360);
INSERT INTO h3_indexes VALUES (0);
INSERT INTO h3_indexes VALUES (1);
INSERT INTO h3_indexes VALUES (180);
INSERT INTO h3_indexes VALUES (180.5);
INSERT INTO h3_indexes VALUES (360);
select h3RadsToDegs(h3DegsToRads(degrees)) from h3_indexes order by degrees;
DROP TABLE h3_indexes;