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
|
|
|
|
{
|
2021-01-21 12:31:47 +00:00
|
|
|
|
|
|
|
template <typename Point>
|
2020-06-17 15:29:08 +00:00
|
|
|
class FunctionPolygonsIntersection : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2021-01-21 12:31:47 +00:00
|
|
|
static inline const char * name;
|
2020-06-17 15:29:08 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-01-18 23:51:34 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
|
2020-06-17 15:29:08 +00:00
|
|
|
{
|
2021-02-15 19:22:13 +00:00
|
|
|
checkColumnTypeOrThrow<Point, MultiPolygon>(arguments[0]);
|
|
|
|
auto first_parser = MultiPolygonFromColumnParser<Point>(std::move(arguments[0].column->convertToFullColumnIfConst()));
|
|
|
|
MultiPolygon<Point> first_container;
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2021-02-15 19:22:13 +00:00
|
|
|
checkColumnTypeOrThrow<Point, MultiPolygon>(arguments[1]);
|
|
|
|
auto second_parser = MultiPolygonFromColumnParser<Point>(std::move(arguments[1].column->convertToFullColumnIfConst()));
|
|
|
|
MultiPolygon<Point> second_container;
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2021-01-21 12:31:47 +00:00
|
|
|
MultiPolygonSerializer<Point> serializer;
|
2021-02-15 19:22:13 +00:00
|
|
|
MultiPolygon<Point> intersection{};
|
2020-06-17 15:29:08 +00:00
|
|
|
|
2021-01-21 12:31:47 +00:00
|
|
|
/// We are not interested in some pitfalls in third-party libraries
|
2021-01-20 23:12:47 +00:00
|
|
|
/// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Assign)
|
2021-02-15 19:22:13 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2020-06-17 15:29:08 +00:00
|
|
|
{
|
2021-02-15 19:22:13 +00:00
|
|
|
first_parser.get(first_container, i);
|
|
|
|
second_parser.get(second_container, i);
|
2021-01-21 12:31:47 +00:00
|
|
|
|
|
|
|
/// Orient the polygons correctly.
|
2021-02-15 19:22:13 +00:00
|
|
|
boost::geometry::correct(first_container);
|
|
|
|
boost::geometry::correct(second_container);
|
2021-01-21 12:31:47 +00:00
|
|
|
|
|
|
|
/// Main work here.
|
2021-02-15 19:22:13 +00:00
|
|
|
boost::geometry::intersection(first_container, second_container, intersection);
|
2020-06-21 16:36:43 +00:00
|
|
|
|
2020-06-17 16:29:31 +00:00
|
|
|
serializer.add(intersection);
|
2020-06-17 15:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 23:51:34 +00:00
|
|
|
return serializer.finalize();
|
2020-06-21 14:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override
|
|
|
|
{
|
|
|
|
return true;
|
2020-06-17 15:29:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-21 12:31:47 +00:00
|
|
|
template <>
|
|
|
|
const char * FunctionPolygonsIntersection<CartesianPoint>::name = "polygonsIntersectionCartesian";
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char * FunctionPolygonsIntersection<GeographicPoint>::name = "polygonsIntersectionGeographic";
|
|
|
|
|
|
|
|
|
2020-06-17 15:29:08 +00:00
|
|
|
void registerFunctionPolygonsIntersection(FunctionFactory & factory)
|
|
|
|
{
|
2021-01-21 12:31:47 +00:00
|
|
|
factory.registerFunction<FunctionPolygonsIntersection<CartesianPoint>>();
|
|
|
|
factory.registerFunction<FunctionPolygonsIntersection<GeographicPoint>>();
|
2020-06-17 15:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|