ClickHouse/src/Functions/polygonsIntersection.cpp

99 lines
2.7 KiB
C++
Raw Normal View History

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>
#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>
#include <utility>
2020-06-17 15:29:08 +00:00
namespace DB
{
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();
}
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-01-19 14:52:53 +00:00
auto first_parser = makeGeometryFromColumnParser<CartesianPoint>(arguments[0]);
2020-06-17 15:29:08 +00:00
auto first_container = createContainer(first_parser);
2021-01-19 14:52:53 +00:00
auto second_parser = makeGeometryFromColumnParser<CartesianPoint>(arguments[1]);
2020-06-17 15:29:08 +00:00
auto second_container = createContainer(second_parser);
2021-01-19 14:52:53 +00:00
MultiPolygonSerializer<CartesianPoint> serializer;
2020-06-17 15:29:08 +00:00
for (size_t i = 0; i < input_rows_count; i++)
{
2021-01-19 12:02:56 +00:00
if (i == 0)
get(first_parser, first_container, i);
2021-01-19 12:02:56 +00:00
if (i == 0)
get(second_parser, second_container, i);
2020-06-17 15:29:08 +00:00
2021-01-18 23:51:34 +00:00
CartesianGeometry intersection = CartesianMultiPolygon({{{{}}}});
boost::geometry::intersection(
2021-01-18 23:51:34 +00:00
boost::get<CartesianMultiPolygon>(first_container),
boost::get<CartesianMultiPolygon>(second_container),
boost::get<CartesianMultiPolygon>(intersection));
2020-06-17 15:29:08 +00:00
2021-01-18 23:51:34 +00:00
boost::get<CartesianMultiPolygon>(intersection).erase(
boost::get<CartesianMultiPolygon>(intersection).begin());
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
}
};
void registerFunctionPolygonsIntersection(FunctionFactory & factory)
{
factory.registerFunction<FunctionPolygonsIntersection>();
}
}