2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2020-03-25 18:52:52 +00:00
|
|
|
#include <Functions/PolygonUtils.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2016-08-12 16:51:08 +00:00
|
|
|
|
2017-09-04 13:45:50 +00:00
|
|
|
#include <boost/geometry.hpp>
|
|
|
|
#include <boost/geometry/geometries/point_xy.hpp>
|
|
|
|
#include <boost/geometry/geometries/polygon.hpp>
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
2020-05-22 22:40:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2018-08-30 18:40:46 +00:00
|
|
|
#include <Common/ObjectPool.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/arithmeticOverflow.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2019-06-30 18:20:32 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2020-05-20 20:16:32 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-05-22 22:40:00 +00:00
|
|
|
#include <Interpreters/castColumn.h>
|
2019-04-09 19:19:30 +00:00
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2018-01-19 20:22:47 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event PolygonsAddedToPool;
|
|
|
|
extern const Event PolygonsInPoolAllocatedBytes;
|
|
|
|
}
|
2016-08-12 16:51:08 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-09-04 13:45:50 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2023-04-01 12:26:00 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-09-04 13:45:50 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2017-09-20 02:30:44 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-06-30 18:20:32 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-05-22 21:47:31 +00:00
|
|
|
using CoordinateType = Float64;
|
|
|
|
using Point = boost::geometry::model::d2::point_xy<CoordinateType>;
|
|
|
|
using Polygon = boost::geometry::model::polygon<Point, false>;
|
|
|
|
using Box = boost::geometry::model::box<Point>;
|
|
|
|
|
|
|
|
|
2020-05-22 23:14:14 +00:00
|
|
|
template <typename PointInConstPolygonImpl>
|
2017-09-20 02:30:44 +00:00
|
|
|
class FunctionPointInPolygon : public IFunction
|
|
|
|
{
|
2017-09-04 13:45:50 +00:00
|
|
|
public:
|
2020-03-25 10:13:34 +00:00
|
|
|
static inline const char * name = "pointInPolygon";
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2020-03-25 17:00:54 +00:00
|
|
|
explicit FunctionPointInPolygon(bool validate_) : validate(validate_) {}
|
2020-03-25 10:13:34 +00:00
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
2020-05-22 23:14:14 +00:00
|
|
|
return std::make_shared<FunctionPointInPolygon<PointInConstPolygonImpl>>(
|
2021-04-10 23:33:54 +00:00
|
|
|
context->getSettingsRef().validate_polygons);
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
{
|
2023-04-01 12:26:00 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires at least 2 arguments", getName());
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 19:15:29 +00:00
|
|
|
/** We allow function invocation in one of the following forms:
|
|
|
|
*
|
|
|
|
* pointInPolygon((x, y), [(x1, y1), (x2, y2), ...])
|
|
|
|
* - simple polygon
|
|
|
|
* pointInPolygon((x, y), [(x1, y1), (x2, y2), ...], [(x21, y21), (x22, y22), ...], ...)
|
|
|
|
* - polygon with a number of holes, each hole as a subsequent argument.
|
|
|
|
* pointInPolygon((x, y), [[(x1, y1), (x2, y2), ...], [(x21, y21), (x22, y22), ...], ...])
|
|
|
|
* - polygon with a number of holes, all as multidimensional array
|
|
|
|
*/
|
|
|
|
|
2020-05-02 16:48:36 +00:00
|
|
|
auto validate_tuple = [this](size_t i, const DataTypeTuple * tuple)
|
|
|
|
{
|
2017-09-04 13:45:50 +00:00
|
|
|
if (tuple == nullptr)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} must contain a tuple", getMessagePrefix(i));
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
const DataTypes & elements = tuple->getElements();
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
if (elements.size() != 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} must have exactly two elements", getMessagePrefix(i));
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto j : collections::range(0, elements.size()))
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
2019-05-24 12:11:03 +00:00
|
|
|
if (!isNativeNumber(elements[j]))
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} must contain numeric tuple at position {}",
|
|
|
|
getMessagePrefix(i), j + 1);
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 06:51:10 +00:00
|
|
|
};
|
|
|
|
|
2020-05-02 16:48:36 +00:00
|
|
|
validate_tuple(0, checkAndGetDataType<DataTypeTuple>(arguments[0].get()));
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-02 16:48:36 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-05-10 00:09:51 +00:00
|
|
|
const auto * array = checkAndGetDataType<DataTypeArray>(arguments[1].get());
|
2020-05-02 06:51:10 +00:00
|
|
|
if (array == nullptr)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} must contain an array of tuples or an array of arrays of tuples.", getMessagePrefix(1));
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-10 00:09:51 +00:00
|
|
|
const auto * nested_array = checkAndGetDataType<DataTypeArray>(array->getNestedType().get());
|
2020-05-02 16:48:36 +00:00
|
|
|
if (nested_array != nullptr)
|
|
|
|
{
|
2020-05-02 06:51:10 +00:00
|
|
|
array = nested_array;
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:48:36 +00:00
|
|
|
validate_tuple(1, checkAndGetDataType<DataTypeTuple>(array->getNestedType().get()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-20 12:55:07 +00:00
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
2020-05-02 16:48:36 +00:00
|
|
|
{
|
2020-05-10 00:09:51 +00:00
|
|
|
const auto * array = checkAndGetDataType<DataTypeArray>(arguments[i].get());
|
2020-05-02 06:51:10 +00:00
|
|
|
if (array == nullptr)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} must contain an array of tuples", getMessagePrefix(i));
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-02 16:48:36 +00:00
|
|
|
validate_tuple(i, checkAndGetDataType<DataTypeTuple>(array->getNestedType().get()));
|
2020-05-02 06:51:10 +00:00
|
|
|
}
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const IColumn * point_col = arguments[0].column.get();
|
2020-04-22 08:45:14 +00:00
|
|
|
const auto * const_tuple_col = checkAndGetColumn<ColumnConst>(point_col);
|
2017-09-20 02:30:44 +00:00
|
|
|
if (const_tuple_col)
|
|
|
|
point_col = &const_tuple_col->getDataColumn();
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2020-05-02 06:51:10 +00:00
|
|
|
const auto * tuple_col = checkAndGetColumn<ColumnTuple>(point_col);
|
2017-09-20 02:30:44 +00:00
|
|
|
if (!tuple_col)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "First argument for function {} must be constant array of tuples.",
|
|
|
|
getName());
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-03-25 17:29:26 +00:00
|
|
|
const auto & tuple_columns = tuple_col->getColumns();
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2022-05-27 20:51:37 +00:00
|
|
|
const ColumnWithTypeAndName & poly = arguments[1];
|
2020-05-22 21:23:49 +00:00
|
|
|
const IColumn * poly_col = poly.column.get();
|
2020-05-22 13:50:12 +00:00
|
|
|
const ColumnConst * const_poly_col = checkAndGetColumn<ColumnConst>(poly_col);
|
2020-05-02 06:51:10 +00:00
|
|
|
|
|
|
|
bool point_is_const = const_tuple_col != nullptr;
|
|
|
|
bool poly_is_const = const_poly_col != nullptr;
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// Two different algorithms are used for constant and non constant polygons.
|
|
|
|
/// Constant polygons are preprocessed to speed up matching.
|
|
|
|
/// For non-constant polygons, we cannot spend time for preprocessing
|
|
|
|
/// and have to quickly match on the fly without creating temporary data structures.
|
|
|
|
|
2020-05-22 14:21:49 +00:00
|
|
|
if (poly_is_const)
|
2020-05-02 16:48:36 +00:00
|
|
|
{
|
2020-05-22 14:21:49 +00:00
|
|
|
Polygon polygon;
|
2020-10-19 15:27:41 +00:00
|
|
|
parseConstPolygon(arguments, polygon);
|
2020-05-22 14:21:49 +00:00
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// Polygons are preprocessed and saved in cache.
|
|
|
|
/// Preprocessing can be computationally heavy but dramatically speeds up matching.
|
|
|
|
|
2020-05-22 19:37:56 +00:00
|
|
|
using Pool = ObjectPoolMap<PointInConstPolygonImpl, UInt128>;
|
2021-08-23 10:59:01 +00:00
|
|
|
/// C++11 has thread-safe function-local static.
|
2020-05-22 19:15:29 +00:00
|
|
|
static Pool known_polygons;
|
|
|
|
|
|
|
|
auto factory = [&polygon]()
|
|
|
|
{
|
|
|
|
auto ptr = std::make_unique<PointInConstPolygonImpl>(polygon);
|
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::PolygonsAddedToPool);
|
|
|
|
ProfileEvents::increment(ProfileEvents::PolygonsInPoolAllocatedBytes, ptr->getAllocatedBytes());
|
|
|
|
|
|
|
|
return ptr.release();
|
|
|
|
};
|
|
|
|
|
2020-05-22 19:37:56 +00:00
|
|
|
auto impl = known_polygons.get(sipHash128(polygon), factory);
|
2020-05-22 19:15:29 +00:00
|
|
|
|
2020-05-22 14:21:49 +00:00
|
|
|
if (point_is_const)
|
2020-05-02 16:48:36 +00:00
|
|
|
{
|
2020-05-22 19:15:29 +00:00
|
|
|
bool is_in = impl->contains(tuple_columns[0]->getFloat64(0), tuple_columns[1]->getFloat64(0));
|
2020-10-19 15:27:41 +00:00
|
|
|
return result_type->createColumnConst(input_rows_count, is_in);
|
2020-05-02 06:51:10 +00:00
|
|
|
}
|
2020-05-22 14:21:49 +00:00
|
|
|
else
|
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
return pointInPolygon(*tuple_columns[0], *tuple_columns[1], *impl);
|
2020-05-22 14:21:49 +00:00
|
|
|
}
|
2020-05-22 13:50:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-22 21:23:49 +00:00
|
|
|
if (arguments.size() != 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Multi-argument version of function {} works only with const polygon",
|
|
|
|
getName());
|
2020-05-22 21:23:49 +00:00
|
|
|
|
2020-05-22 14:21:49 +00:00
|
|
|
auto res_column = ColumnVector<UInt8>::create(input_rows_count);
|
|
|
|
auto & data = res_column->getData();
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// A polygon, possibly with holes, is represented by 2d array:
|
|
|
|
/// [[(outer_x_1, outer_y_1, ...)], [(hole1_x_1, hole1_y_1), ...], ...]
|
|
|
|
///
|
|
|
|
/// Or, a polygon without holes can be represented by 1d array:
|
|
|
|
/// [(outer_x_1, outer_y_1, ...)]
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
if (isTwoDimensionalArray(*arguments[1].type))
|
2020-05-22 14:21:49 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
/// We cast everything to Float64 in advance (in batch fashion)
|
|
|
|
/// to avoid casting with virtual calls in a loop.
|
|
|
|
/// Note that if the type is already Float64, the operation in noop.
|
|
|
|
|
2020-05-22 22:40:00 +00:00
|
|
|
ColumnPtr polygon_column_float64 = castColumn(
|
2020-10-19 15:27:41 +00:00
|
|
|
arguments[1],
|
2020-05-22 22:40:00 +00:00
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeTuple>(DataTypes{
|
|
|
|
std::make_shared<DataTypeFloat64>(),
|
|
|
|
std::make_shared<DataTypeFloat64>()}))));
|
|
|
|
|
2020-05-22 21:23:49 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
size_t point_index = point_is_const ? 0 : i;
|
2020-05-23 10:57:30 +00:00
|
|
|
data[i] = isInsidePolygonWithHoles(
|
2020-05-22 23:14:14 +00:00
|
|
|
tuple_columns[0]->getFloat64(point_index),
|
|
|
|
tuple_columns[1]->getFloat64(point_index),
|
|
|
|
*polygon_column_float64,
|
|
|
|
i);
|
2020-05-22 21:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-22 22:40:00 +00:00
|
|
|
ColumnPtr polygon_column_float64 = castColumn(
|
2020-10-19 15:27:41 +00:00
|
|
|
arguments[1],
|
2020-05-22 22:40:00 +00:00
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeTuple>(DataTypes{
|
|
|
|
std::make_shared<DataTypeFloat64>(),
|
|
|
|
std::make_shared<DataTypeFloat64>()})));
|
|
|
|
|
2020-05-22 21:23:49 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
size_t point_index = point_is_const ? 0 : i;
|
2020-05-23 10:57:30 +00:00
|
|
|
data[i] = isInsidePolygonWithoutHoles(
|
2020-05-22 23:14:14 +00:00
|
|
|
tuple_columns[0]->getFloat64(point_index),
|
|
|
|
tuple_columns[1]->getFloat64(point_index),
|
|
|
|
*polygon_column_float64,
|
|
|
|
i);
|
2020-05-22 21:23:49 +00:00
|
|
|
}
|
2020-05-22 14:21:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return res_column;
|
2020-05-22 13:50:12 +00:00
|
|
|
}
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-25 10:13:34 +00:00
|
|
|
bool validate;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-05-02 06:51:10 +00:00
|
|
|
std::string getMessagePrefix(size_t i) const
|
|
|
|
{
|
|
|
|
return "Argument " + toString(i + 1) + " for function " + getName();
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
bool isTwoDimensionalArray(const IDataType & type) const
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
return WhichDataType(type).isArray()
|
|
|
|
&& WhichDataType(static_cast<const DataTypeArray &>(type).getNestedType()).isArray();
|
2020-05-22 20:29:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// Implementation methods to check point-in-polygon on the fly (for non-const polygons).
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-23 10:41:49 +00:00
|
|
|
bool isInsideRing(
|
2020-05-22 23:14:14 +00:00
|
|
|
Float64 point_x,
|
|
|
|
Float64 point_y,
|
|
|
|
const Float64 * ring_x_data,
|
|
|
|
const Float64 * ring_y_data,
|
|
|
|
size_t ring_begin,
|
|
|
|
size_t ring_end) const
|
|
|
|
{
|
|
|
|
size_t size = ring_end - ring_begin;
|
|
|
|
|
|
|
|
if (size < 2)
|
|
|
|
return false;
|
|
|
|
|
2020-05-23 10:41:49 +00:00
|
|
|
/** This is the algorithm by W. Randolph Franklin
|
|
|
|
* https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
|
|
|
*
|
|
|
|
* Basically it works like this:
|
|
|
|
* From the point, cast a horizontal ray to the right
|
2020-05-23 11:03:21 +00:00
|
|
|
* and count the number of intersections with polygon edges
|
|
|
|
* (every edge is considered semi-closed, e.g. includes the first vertex and does not include the last)
|
2020-05-23 10:41:49 +00:00
|
|
|
*
|
|
|
|
* Advantages:
|
|
|
|
* - works regardless to the orientation;
|
|
|
|
* - for polygon without holes:
|
|
|
|
* works regardless to whether the polygon is closed by last vertex equals to first vertex or not;
|
|
|
|
* (no need to preprocess polygon in any way)
|
|
|
|
* - easy to apply for polygons with holes and for multi-polygons;
|
|
|
|
* - it even works for polygons with self-intersections in a reasonable way;
|
|
|
|
* - simplicity and performance;
|
|
|
|
* - can be additionally speed up with loop unrolling and/or binary search for possible intersecting edges.
|
|
|
|
*
|
2020-05-23 11:26:40 +00:00
|
|
|
* Drawbacks:
|
|
|
|
* - it's unspecified whether a point of the edge is inside or outside of a polygon
|
|
|
|
* (looks like it's inside for "left" edges and outside for "right" edges)
|
|
|
|
*
|
2020-05-23 10:41:49 +00:00
|
|
|
* Why not to apply the same algorithm available in boost::geometry?
|
|
|
|
* It will require to move data from columns to temporary containers.
|
2020-05-23 11:03:21 +00:00
|
|
|
* Despite the fact that the boost library is template based and allows arbitrary containers and points,
|
2020-05-23 10:41:49 +00:00
|
|
|
* it's diffucult to use without data movement because
|
|
|
|
* we use structure-of-arrays for coordinates instead of arrays-of-structures.
|
|
|
|
*/
|
2020-05-22 23:14:14 +00:00
|
|
|
|
|
|
|
size_t vertex1_idx = ring_begin;
|
|
|
|
size_t vertex2_idx = ring_end - 1;
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
while (vertex1_idx < ring_end)
|
|
|
|
{
|
2020-05-23 11:04:29 +00:00
|
|
|
/// First condition checks that the point is inside horizontal row between edge top and bottom y-coordinate.
|
2020-05-23 10:41:49 +00:00
|
|
|
/// Second condition checks for intersection with the edge.
|
|
|
|
|
2020-05-22 23:14:14 +00:00
|
|
|
if (((ring_y_data[vertex1_idx] > point_y) != (ring_y_data[vertex2_idx] > point_y))
|
|
|
|
&& (point_x < (ring_x_data[vertex2_idx] - ring_x_data[vertex1_idx])
|
|
|
|
* (point_y - ring_y_data[vertex1_idx]) / (ring_y_data[vertex2_idx] - ring_y_data[vertex1_idx])
|
|
|
|
+ ring_x_data[vertex1_idx]))
|
2020-05-23 10:41:49 +00:00
|
|
|
{
|
2020-05-22 23:14:14 +00:00
|
|
|
res = !res;
|
2020-05-23 10:41:49 +00:00
|
|
|
}
|
2020-05-22 23:14:14 +00:00
|
|
|
|
|
|
|
vertex2_idx = vertex1_idx;
|
|
|
|
++vertex1_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
bool isInsidePolygonWithoutHoles(
|
2020-05-22 23:14:14 +00:00
|
|
|
Float64 point_x,
|
|
|
|
Float64 point_y,
|
|
|
|
const IColumn & polygon_column,
|
|
|
|
size_t i) const
|
|
|
|
{
|
|
|
|
const auto & array_col = static_cast<const ColumnArray &>(polygon_column);
|
|
|
|
|
|
|
|
size_t begin = array_col.getOffsets()[i - 1];
|
|
|
|
size_t end = array_col.getOffsets()[i];
|
|
|
|
size_t size = end - begin;
|
|
|
|
|
|
|
|
if (size < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto & tuple_columns = static_cast<const ColumnTuple &>(array_col.getData()).getColumns();
|
|
|
|
const auto * x_data = static_cast<const ColumnFloat64 &>(*tuple_columns[0]).getData().data();
|
|
|
|
const auto * y_data = static_cast<const ColumnFloat64 &>(*tuple_columns[1]).getData().data();
|
|
|
|
|
2020-05-23 10:41:49 +00:00
|
|
|
return isInsideRing(point_x, point_y, x_data, y_data, begin, end);
|
2020-05-22 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
bool isInsidePolygonWithHoles(
|
|
|
|
Float64 point_x,
|
|
|
|
Float64 point_y,
|
|
|
|
const IColumn & polygon_column,
|
|
|
|
size_t i) const
|
2020-05-22 21:23:49 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
const auto & array_col = static_cast<const ColumnArray &>(polygon_column);
|
2020-05-22 21:23:49 +00:00
|
|
|
size_t rings_begin = array_col.getOffsets()[i - 1];
|
|
|
|
size_t rings_end = array_col.getOffsets()[i];
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-22 22:40:00 +00:00
|
|
|
const auto & nested_array_col = static_cast<const ColumnArray &>(array_col.getData());
|
|
|
|
const auto & tuple_columns = static_cast<const ColumnTuple &>(nested_array_col.getData()).getColumns();
|
|
|
|
const auto * x_data = static_cast<const ColumnFloat64 &>(*tuple_columns[0]).getData().data();
|
|
|
|
const auto * y_data = static_cast<const ColumnFloat64 &>(*tuple_columns[1]).getData().data();
|
|
|
|
|
2020-05-22 21:23:49 +00:00
|
|
|
for (size_t j = rings_begin; j < rings_end; ++j)
|
2020-05-02 16:48:36 +00:00
|
|
|
{
|
2020-05-22 21:23:49 +00:00
|
|
|
size_t begin = nested_array_col.getOffsets()[j - 1];
|
|
|
|
size_t end = nested_array_col.getOffsets()[j];
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
if (j == rings_begin)
|
2020-05-02 16:48:36 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
if (!isInsideRing(point_x, point_y, x_data, y_data, begin, end))
|
|
|
|
return false;
|
2020-05-22 21:23:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
if (isInsideRing(point_x, point_y, x_data, y_data, begin, end))
|
|
|
|
return false;
|
2020-05-02 06:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 10:57:30 +00:00
|
|
|
|
|
|
|
return true;
|
2020-05-02 06:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// Implementation methods to create boost::geometry::polygon for subsequent preprocessing.
|
|
|
|
/// They are used to optimize matching for constant polygons. Preprocessing may take significant amount of time.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void parseRing(
|
|
|
|
const Float64 * x_data,
|
|
|
|
const Float64 * y_data,
|
|
|
|
size_t begin,
|
|
|
|
size_t end,
|
|
|
|
T & out_container) const
|
2020-05-22 23:14:14 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
out_container.reserve(end - begin);
|
2021-03-11 14:20:06 +00:00
|
|
|
for (size_t i = begin; i < end; ++i)
|
|
|
|
{
|
2021-03-11 12:19:15 +00:00
|
|
|
Int64 result = 0;
|
|
|
|
if (common::mulOverflow(static_cast<Int64>(x_data[i]), static_cast<Int64>(y_data[i]), result))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "The coordinates of the point are such that "
|
|
|
|
"subsequent calculations cannot be performed correctly. "
|
|
|
|
"Most likely they are very large in modulus.");
|
2021-03-11 12:19:15 +00:00
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
out_container.emplace_back(x_data[i], y_data[i]);
|
2021-03-11 12:19:15 +00:00
|
|
|
}
|
2020-05-23 10:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parseConstPolygonWithoutHolesFromSingleColumn(const IColumn & column, size_t i, Polygon & out_polygon) const
|
|
|
|
{
|
|
|
|
const auto & array_col = static_cast<const ColumnArray &>(column);
|
|
|
|
size_t begin = array_col.getOffsets()[i - 1];
|
|
|
|
size_t end = array_col.getOffsets()[i];
|
|
|
|
|
|
|
|
const auto & tuple_columns = static_cast<const ColumnTuple &>(array_col.getData()).getColumns();
|
|
|
|
const auto * x_data = static_cast<const ColumnFloat64 &>(*tuple_columns[0]).getData().data();
|
|
|
|
const auto * y_data = static_cast<const ColumnFloat64 &>(*tuple_columns[1]).getData().data();
|
|
|
|
|
|
|
|
parseRing(x_data, y_data, begin, end, out_polygon.outer());
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseConstPolygonWithHolesFromSingleColumn(const IColumn & column, size_t i, Polygon & out_polygon) const
|
|
|
|
{
|
|
|
|
const auto & array_col = static_cast<const ColumnArray &>(column);
|
2020-05-22 23:14:14 +00:00
|
|
|
size_t rings_begin = array_col.getOffsets()[i - 1];
|
|
|
|
size_t rings_end = array_col.getOffsets()[i];
|
|
|
|
|
|
|
|
const auto & nested_array_col = static_cast<const ColumnArray &>(array_col.getData());
|
|
|
|
const auto & tuple_columns = static_cast<const ColumnTuple &>(nested_array_col.getData()).getColumns();
|
|
|
|
const auto * x_data = static_cast<const ColumnFloat64 &>(*tuple_columns[0]).getData().data();
|
|
|
|
const auto * y_data = static_cast<const ColumnFloat64 &>(*tuple_columns[1]).getData().data();
|
|
|
|
|
|
|
|
for (size_t j = rings_begin; j < rings_end; ++j)
|
|
|
|
{
|
|
|
|
size_t begin = nested_array_col.getOffsets()[j - 1];
|
|
|
|
size_t end = nested_array_col.getOffsets()[j];
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
if (out_polygon.outer().empty())
|
2020-05-22 23:14:14 +00:00
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
parseRing(x_data, y_data, begin, end, out_polygon.outer());
|
2020-05-22 23:14:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-23 10:57:30 +00:00
|
|
|
out_polygon.inners().emplace_back();
|
|
|
|
parseRing(x_data, y_data, begin, end, out_polygon.inners().back());
|
2020-05-22 23:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
void parseConstPolygonWithHolesFromMultipleColumns(const ColumnsWithTypeAndName & arguments, Polygon & out_polygon) const
|
2020-05-02 06:51:10 +00:00
|
|
|
{
|
2017-09-20 02:30:44 +00:00
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
2017-09-04 13:45:50 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const auto * const_col = checkAndGetColumn<ColumnConst>(arguments[i].column.get());
|
2020-05-02 06:51:10 +00:00
|
|
|
if (!const_col)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Multi-argument version of function {} works only with const polygon",
|
|
|
|
getName());
|
2020-05-02 06:51:10 +00:00
|
|
|
|
|
|
|
const auto * array_col = checkAndGetColumn<ColumnArray>(&const_col->getDataColumn());
|
2020-04-22 08:45:14 +00:00
|
|
|
const auto * tuple_col = array_col ? checkAndGetColumn<ColumnTuple>(&array_col->getData()) : nullptr;
|
2017-09-04 20:21:55 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
if (!tuple_col)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "{} must be constant array of tuples", getMessagePrefix(i));
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2017-12-08 00:50:25 +00:00
|
|
|
const auto & tuple_columns = tuple_col->getColumns();
|
|
|
|
const auto & column_x = tuple_columns[0];
|
|
|
|
const auto & column_y = tuple_columns[1];
|
2017-09-04 20:21:55 +00:00
|
|
|
|
2020-05-22 14:07:17 +00:00
|
|
|
if (!out_polygon.outer().empty())
|
|
|
|
out_polygon.inners().emplace_back();
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2020-05-22 14:07:17 +00:00
|
|
|
auto & container = out_polygon.outer().empty() ? out_polygon.outer() : out_polygon.inners().back();
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
auto size = column_x->size();
|
2017-09-04 13:45:50 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
if (size == 0)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "{} shouldn't be empty.", getMessagePrefix(i));
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto j : collections::range(0, size))
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2020-03-25 17:29:26 +00:00
|
|
|
CoordinateType x_coord = column_x->getFloat64(j);
|
|
|
|
CoordinateType y_coord = column_y->getFloat64(j);
|
|
|
|
container.push_back(Point(x_coord, y_coord));
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
2020-03-25 10:13:34 +00:00
|
|
|
}
|
2020-05-02 06:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
void parseConstPolygonFromSingleColumn(const ColumnsWithTypeAndName & arguments, Polygon & out_polygon) const
|
2020-05-02 06:51:10 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
if (isTwoDimensionalArray(*arguments[1].type))
|
2020-08-25 22:13:47 +00:00
|
|
|
{
|
|
|
|
ColumnPtr polygon_column_float64 = castColumn(
|
2020-10-19 15:27:41 +00:00
|
|
|
arguments[1],
|
2020-08-25 22:13:47 +00:00
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeTuple>(DataTypes{
|
|
|
|
std::make_shared<DataTypeFloat64>(),
|
|
|
|
std::make_shared<DataTypeFloat64>()}))));
|
2020-05-23 10:57:30 +00:00
|
|
|
|
2020-08-25 22:13:47 +00:00
|
|
|
const ColumnConst & column_const = typeid_cast<const ColumnConst &>(*polygon_column_float64);
|
|
|
|
const IColumn & column_const_data = column_const.getDataColumn();
|
2020-05-23 10:57:30 +00:00
|
|
|
|
|
|
|
parseConstPolygonWithHolesFromSingleColumn(column_const_data, 0, out_polygon);
|
2020-08-25 22:13:47 +00:00
|
|
|
}
|
2020-05-23 10:57:30 +00:00
|
|
|
else
|
2020-08-25 22:13:47 +00:00
|
|
|
{
|
|
|
|
ColumnPtr polygon_column_float64 = castColumn(
|
2020-10-19 15:27:41 +00:00
|
|
|
arguments[1],
|
2020-08-25 22:13:47 +00:00
|
|
|
std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeTuple>(DataTypes{
|
|
|
|
std::make_shared<DataTypeFloat64>(),
|
|
|
|
std::make_shared<DataTypeFloat64>()})));
|
|
|
|
|
|
|
|
const ColumnConst & column_const = typeid_cast<const ColumnConst &>(*polygon_column_float64);
|
|
|
|
const IColumn & column_const_data = column_const.getDataColumn();
|
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
parseConstPolygonWithoutHolesFromSingleColumn(column_const_data, 0, out_polygon);
|
2020-08-25 22:13:47 +00:00
|
|
|
}
|
2020-05-22 21:23:49 +00:00
|
|
|
}
|
2020-05-22 14:07:17 +00:00
|
|
|
|
2021-02-16 19:50:34 +00:00
|
|
|
void NO_SANITIZE_UNDEFINED parseConstPolygon(const ColumnsWithTypeAndName & arguments, Polygon & out_polygon) const
|
2020-05-22 21:23:49 +00:00
|
|
|
{
|
2020-05-02 16:48:36 +00:00
|
|
|
if (arguments.size() == 2)
|
2020-10-19 15:27:41 +00:00
|
|
|
parseConstPolygonFromSingleColumn(arguments, out_polygon);
|
2020-05-02 16:48:36 +00:00
|
|
|
else
|
2020-10-19 15:27:41 +00:00
|
|
|
parseConstPolygonWithHolesFromMultipleColumns(arguments, out_polygon);
|
2020-05-02 06:51:10 +00:00
|
|
|
|
2020-05-23 10:57:30 +00:00
|
|
|
/// Fix orientation and close rings. It's required for subsequent processing.
|
2020-05-22 14:07:17 +00:00
|
|
|
boost::geometry::correct(out_polygon);
|
2020-03-25 10:13:34 +00:00
|
|
|
|
2021-02-17 17:43:15 +00:00
|
|
|
#if !defined(__clang_analyzer__) /// It does not like boost.
|
2020-05-22 21:23:49 +00:00
|
|
|
if (validate)
|
2020-03-25 10:13:34 +00:00
|
|
|
{
|
|
|
|
std::string failure_message;
|
2020-05-22 14:07:17 +00:00
|
|
|
auto is_valid = boost::geometry::is_valid(out_polygon, failure_message);
|
2020-03-25 10:13:34 +00:00
|
|
|
if (!is_valid)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Polygon is not valid: {}", failure_message);
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
2020-03-25 17:00:54 +00:00
|
|
|
#endif
|
2017-09-04 13:45:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2019-04-09 19:19:30 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(PointInPolygon)
|
2016-08-12 16:51:08 +00:00
|
|
|
{
|
2020-05-22 23:14:14 +00:00
|
|
|
factory.registerFunction<FunctionPointInPolygon<PointInPolygonWithGrid<Float64>>>();
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|
2019-06-30 18:20:32 +00:00
|
|
|
|
2016-08-12 16:51:08 +00:00
|
|
|
}
|