2020-06-17 15:29:08 +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>
|
|
|
|
|
2020-06-21 14:54:13 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
2020-06-17 15:29:08 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
2020-06-22 16:15:11 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2020-06-17 15:29:08 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <DataTypes/DataTypeCustomGeo.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2020-06-22 16:15:11 +00:00
|
|
|
#include <utility>
|
2020-06-17 15:29:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using CoordinateType = Float64;
|
|
|
|
using Point = boost::geometry::model::d2::point_xy<CoordinateType>;
|
|
|
|
using Polygon = boost::geometry::model::polygon<Point, false>;
|
|
|
|
using MultiPolygon = boost::geometry::model::multi_polygon<Float64Polygon>;
|
|
|
|
using Box = boost::geometry::model::box<Point>;
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionPolygonsIntersection : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static inline const char * name = "polygonsIntersection";
|
|
|
|
|
|
|
|
explicit FunctionPolygonsIntersection() = default;
|
|
|
|
|
|
|
|
static FunctionPtr create(const Context &)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionPolygonsIntersection>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
|
|
|
|
{
|
|
|
|
return DataTypeCustomMultiPolygonSerialization::nestedDataType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
|
|
|
|
{
|
2020-06-21 16:36:43 +00:00
|
|
|
auto get_parser = [&block, &arguments] (size_t i) {
|
2020-06-22 16:15:11 +00:00
|
|
|
const auto * const_col =
|
|
|
|
checkAndGetColumn<ColumnConst>(block.getByPosition(arguments[i]).column.get());
|
|
|
|
|
|
|
|
bool is_const = static_cast<bool>(const_col);
|
|
|
|
|
|
|
|
return std::pair<bool, GeometryFromColumnParser>{is_const, is_const ?
|
|
|
|
makeGeometryFromColumnParser(ColumnWithTypeAndName(const_col->getDataColumnPtr(), block.getByPosition(arguments[i]).type, block.getByPosition(arguments[i]).name)) :
|
|
|
|
makeGeometryFromColumnParser(block.getByPosition(arguments[i]))};
|
2020-06-21 16:36:43 +00:00
|
|
|
};
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2020-06-22 16:15:11 +00:00
|
|
|
auto [is_first_polygon_const, first_parser] = get_parser(0);
|
2020-06-17 15:29:08 +00:00
|
|
|
auto first_container = createContainer(first_parser);
|
|
|
|
|
2020-06-22 16:15:11 +00:00
|
|
|
auto [is_second_polygon_const, second_parser] = get_parser(1);
|
2020-06-17 15:29:08 +00:00
|
|
|
auto second_container = createContainer(second_parser);
|
|
|
|
|
2020-06-17 16:29:31 +00:00
|
|
|
Float64MultiPolygonSerializer serializer;
|
2020-06-17 15:29:08 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; i++)
|
|
|
|
{
|
2020-06-22 16:15:11 +00:00
|
|
|
if (!is_first_polygon_const || i == 0)
|
|
|
|
get(first_parser, first_container, i);
|
|
|
|
if (!is_second_polygon_const || i == 0)
|
|
|
|
get(second_parser, second_container, i);
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2020-06-21 14:54:13 +00:00
|
|
|
Float64Geometry intersection = Float64MultiPolygon({{{{}}}});
|
2020-06-17 16:29:31 +00:00
|
|
|
boost::geometry::intersection(
|
|
|
|
boost::get<Float64MultiPolygon>(first_container),
|
|
|
|
boost::get<Float64MultiPolygon>(second_container),
|
|
|
|
boost::get<Float64MultiPolygon>(intersection));
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2020-06-21 16:36:43 +00:00
|
|
|
boost::get<Float64MultiPolygon>(intersection).erase(
|
|
|
|
boost::get<Float64MultiPolygon>(intersection).begin());
|
|
|
|
|
2020-06-17 16:29:31 +00:00
|
|
|
serializer.add(intersection);
|
2020-06-17 15:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:29:31 +00:00
|
|
|
block.getByPosition(result).column = std::move(serializer.finalize());
|
2020-06-21 14:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override
|
|
|
|
{
|
|
|
|
return true;
|
2020-06-17 15:29:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionPolygonsIntersection(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionPolygonsIntersection>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|