github-actions[bot] commented on code in PR #22933: URL: https://github.com/apache/doris/pull/22933#discussion_r1377255254
########## be/src/geo/geo_tojson.h: ########## @@ -0,0 +1,78 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <rapidjson/allocators.h> Review Comment: warning: 'rapidjson/allocators.h' file not found [clang-diagnostic-error] ```cpp #include <rapidjson/allocators.h> ^ ``` ########## be/src/geo/geojson_parse.h: ########## @@ -0,0 +1,78 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <memory> + +#include "geo/geo_common.h" +#include "geo/wkt_parse_type.h" +#include "rapidjson/document.h" Review Comment: warning: 'rapidjson/document.h' file not found [clang-diagnostic-error] ```cpp #include "rapidjson/document.h" ^ ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion const auto* point = (const GeoPoint*)rhs; ``` ########## be/src/geo/util/GeoCircle.cpp: ########## @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCircle.h" + +#include <s2/s2cap.h> Review Comment: warning: 's2/s2cap.h' file not found [clang-diagnostic-error] ```cpp #include <s2/s2cap.h> ^ ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion ++i) {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion YGON: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion ++i) {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); + res_collection->add_one_geometry(point); + } else if (multi_point->get_num_point() > 1) { + res_collection->add_one_geometry(multi_point.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_linestring->get_num_line() == 1) { + GeoLineString* linestring = (GeoLineString*)multi_linestring->get_geometries_n(0); + res_collection->add_one_geometry(linestring); + } else if (multi_linestring->get_num_line() > 1) { + res_collection->add_one_geometry(multi_linestring.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_polygon->get_num_polygon() == 1) { + GeoPolygon* polygon = (GeoPolygon*)multi_polygon->get_geometries_n(0); + res_collection->add_one_geometry(polygon); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion * } Polygon* polygon = (GeoPolygon*)multi_polygon->get_geometries_n(0);auto ``` ########## be/src/geo/geo_tobinary.cpp: ########## @@ -69,29 +81,56 @@ bool toBinary::writeGeoPoint(GeoPoint* point, ToBinaryContext* ctx) { writeByteOrder(ctx); writeGeometryType(wkbType::wkbPoint, ctx); - GeoCoordinateList p = point->to_coords(); - - writeCoordinateList(p, false, ctx); + if (point->is_empty()) { + GeoCoordinate c(DoubleNotANumber, DoubleNotANumber); + writeCoordinate(c, ctx); + } else { + GeoCoordinates p = point->to_coords(); + writeCoordinateList(p, false, ctx); + } return true; } -bool toBinary::writeGeoLine(GeoLine* line, ToBinaryContext* ctx) { +bool toBinary::writeGeoLine(GeoLineString* linestring, ToBinaryContext* ctx) { writeByteOrder(ctx); writeGeometryType(wkbType::wkbLine, ctx); - GeoCoordinateList p = line->to_coords(); - - writeCoordinateList(p, true, ctx); + if (linestring->is_empty()) { + writeInt(0, ctx); + } else { + GeoCoordinates p = linestring->to_coords(); + writeCoordinateList(p, true, ctx); + } return true; } bool toBinary::writeGeoPolygon(doris::GeoPolygon* polygon, ToBinaryContext* ctx) { writeByteOrder(ctx); writeGeometryType(wkbType::wkbPolygon, ctx); - writeInt(polygon->numLoops(), ctx); - std::unique_ptr<GeoCoordinateListList> coordss(polygon->to_coords()); + if (polygon->is_empty()) { + writeInt(0, ctx); + } else { + writeInt(polygon->num_loops(), ctx); + std::unique_ptr<GeoCoordinateLists> coords_list(polygon->to_coords()); + for (int i = 0; i < coords_list->coords_list.size(); ++i) { + writeCoordinateList(*coords_list->coords_list[i], true, ctx); Review Comment: warning: use range-based for loop instead [modernize-loop-convert] ```suggestion for (auto & i : coords_list->coords_list) { writeCoordinateList(*i, true, ctx); ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion const auto* polygon = (const GeoPolygon*)rhs; ``` ########## be/src/geo/geo_tobinary.cpp: ########## @@ -69,29 +81,56 @@ bool toBinary::write(GeoShape* shape, ToBinaryContext* ctx) { bool toBinary::writeGeoPoint(GeoPoint* point, ToBinaryContext* ctx) { writeByteOrder(ctx); writeGeometryType(wkbType::wkbPoint, ctx); - GeoCoordinateList p = point->to_coords(); - - writeCoordinateList(p, false, ctx); + if (point->is_empty()) { + GeoCoordinate c(DoubleNotANumber, DoubleNotANumber); Review Comment: warning: no matching constructor for initialization of 'doris::GeoCoordinate' [clang-diagnostic-error] ```cpp GeoCoordinate c(DoubleNotANumber, DoubleNotANumber); ^ ``` <details> <summary>Additional context</summary> **be/src/geo/wkt_parse_type.h:24:** candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided ```cpp struct GeoCoordinate { ^ ``` **be/src/geo/wkt_parse_type.h:24:** candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided ```cpp struct GeoCoordinate { ^ ``` **be/src/geo/wkt_parse_type.h:24:** candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 2 were provided ```cpp struct GeoCoordinate { ^ ``` </details> ########## be/src/geo/geojson_parse.cpp: ########## @@ -0,0 +1,259 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "geojson_parse.h" + +#include <stdint.h> Review Comment: warning: inclusion of deprecated C++ header 'stdint.h'; consider using 'cstdint' instead [modernize-deprecated-headers] ```suggestion #include <cstdint> ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion const auto* collection = (const GeoCollection*)rhs; ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion const auto* line = (const GeoLineString*)rhs; ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion RING: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); + res_collection->add_one_geometry(point); + } else if (multi_point->get_num_point() > 1) { + res_collection->add_one_geometry(multi_point.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_linestring->get_num_line() == 1) { + GeoLineString* linestring = (GeoLineString*)multi_linestring->get_geometries_n(0); + res_collection->add_one_geometry(linestring); + } else if (multi_linestring->get_num_line() > 1) { + res_collection->add_one_geometry(multi_linestring.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_polygon->get_num_polygon() == 1) { + GeoPolygon* polygon = (GeoPolygon*)multi_polygon->get_geometries_n(0); + res_collection->add_one_geometry(polygon); + } else if (multi_polygon->get_num_polygon() > 1) { + res_collection->add_one_geometry(multi_polygon.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + return res_collection; +} + +bool GeoCollection::add_to_s2shape_index(MutableS2ShapeIndex& S2shape_index) const { + if (is_empty()) return false; + for (const auto& g : geometries) { + if (g->is_empty() || !g->is_valid()) continue; + switch (g->type()) { + case GEO_SHAPE_POINT: { + GeoPoint* point = dynamic_cast<GeoPoint*>(g.get()); + if (point->is_empty()) continue; + if (!point->add_to_s2shape_index(S2shape_index)) { + return false; + } + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + if (linestring->is_empty()) continue; + if (!linestring->add_to_s2shape_index(S2shape_index)) { + return false; + } + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + if (polygon->is_empty()) continue; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion _SHAPE_POLYGON: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); + res_collection->add_one_geometry(point); + } else if (multi_point->get_num_point() > 1) { + res_collection->add_one_geometry(multi_point.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_linestring->get_num_line() == 1) { + GeoLineString* linestring = (GeoLineString*)multi_linestring->get_geometries_n(0); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion if (multi_linestring->get_num_line() == 1) {auto ``` ########## be/src/geo/util/GeoCircle.cpp: ########## @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCircle.h" + +#include <s2/s2cap.h> +#include <s2/s2earth.h> +#include <s2/util/coding/coder.h> + +namespace doris { + +GeoCircle::GeoCircle() = default; +GeoCircle::~GeoCircle() = default; + +GeoParseStatus GeoCircle::to_s2cap(double lng, double lat, double radius_meter) { + S2Point center; + auto status = GeoPoint::to_s2point(lng, lat, ¢er); + if (status != GEO_PARSE_OK) { + return status; + } + S1Angle radius = S2Earth::ToAngle(util::units::Meters(radius_meter)); + _cap.reset(new S2Cap(center, radius)); + if (!_cap->is_valid()) { + return GEO_PARSE_CIRCLE_INVALID; + } + return GEO_PARSE_OK; +} + +bool GeoCircle::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion const auto* point = (const GeoPoint*)rhs; ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion RING: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion OINT: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion ++i) {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion YGON: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion nt->get_num_point() == 1) {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); + res_collection->add_one_geometry(point); + } else if (multi_point->get_num_point() > 1) { + res_collection->add_one_geometry(multi_point.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_linestring->get_num_line() == 1) { + GeoLineString* linestring = (GeoLineString*)multi_linestring->get_geometries_n(0); + res_collection->add_one_geometry(linestring); + } else if (multi_linestring->get_num_line() > 1) { + res_collection->add_one_geometry(multi_linestring.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_polygon->get_num_polygon() == 1) { + GeoPolygon* polygon = (GeoPolygon*)multi_polygon->get_geometries_n(0); + res_collection->add_one_geometry(polygon); + } else if (multi_polygon->get_num_polygon() > 1) { + res_collection->add_one_geometry(multi_polygon.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + return res_collection; +} + +bool GeoCollection::add_to_s2shape_index(MutableS2ShapeIndex& S2shape_index) const { + if (is_empty()) return false; + for (const auto& g : geometries) { + if (g->is_empty() || !g->is_valid()) continue; + switch (g->type()) { + case GEO_SHAPE_POINT: { + GeoPoint* point = dynamic_cast<GeoPoint*>(g.get()); + if (point->is_empty()) continue; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion case GEO_SHAPE_POINT: {auto ``` ########## be/src/geo/util/GeoCollection.cpp: ########## @@ -0,0 +1,352 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "GeoCollection.h" + +#include <sstream> + +#include "GeoMultiLineString.h" +#include "GeoMultiPoint.h" +#include "GeoMultiPolygon.h" + +namespace doris { + +GeoCollection::GeoCollection() = default; +GeoCollection::~GeoCollection() = default; + +bool GeoCollection::is_valid() const { + for (const auto& g : geometries) { + if (!g->is_valid()) { + return false; + } + } + return true; +} + +bool GeoCollection::is_closed() const { + for (const auto& g : geometries) { + if (!g->is_closed()) { + return false; + } + } + return true; +} + +GeoParseStatus GeoCollection::add_one_geometry(GeoShape* shape) { + geometries.emplace_back(std::unique_ptr<GeoShape>(shape)); + return GEO_PARSE_OK; +} + +std::size_t GeoCollection::get_num_geometries() const { + return geometries.size(); +} + +GeoShape* GeoCollection::get_geometries_n(std::size_t n) const { + return geometries[n].get(); +} + +std::string GeoCollection::as_wkt() const { + std::stringstream ss; + ss << "GEOMETRYCOLLECTION "; + + if (is_empty()) { + ss << "EMPTY"; + return ss.str(); + } + ss << "("; + + for (int i = 0; i < geometries.size(); ++i) { + ss << geometries[i]->as_wkt(); + if (i != geometries.size() - 1) { + ss << ","; + } + } + ss << ")"; + return ss.str(); +} + +bool GeoCollection::contains(const GeoShape* rhs) const { + switch (rhs->type()) { + case GEO_SHAPE_POINT: { + const GeoPoint* point = (const GeoPoint*)rhs; + for (const auto& g : geometries) { + if (!g->contains(point)) { + return false; + } + } + return true; + } + case GEO_SHAPE_LINE_STRING: { + const GeoLineString* line = (const GeoLineString*)rhs; + for (const auto& g : geometries) { + if (!g->contains(line)) { + return false; + } + } + return true; + } + case GEO_SHAPE_POLYGON: { + const GeoPolygon* polygon = (const GeoPolygon*)rhs; + for (const auto& g : geometries) { + if (!g->contains(polygon)) { + return false; + } + } + return true; + } + case GEO_SHAPE_MULTI_POINT: + case GEO_SHAPE_MULTI_LINE_STRING: + case GEO_SHAPE_MULTI_POLYGON: + case GEO_SHAPE_GEOMETRY_COLLECTION: { + const GeoCollection* collection = (const GeoCollection*)rhs; + for (const auto& g1 : collection->geometries) { + for (const auto& g2 : geometries) { + if (!g2->contains(g1.get())) { + return false; + } + } + } + return true; + } + default: + return false; + } +} + +std::size_t GeoCollection::get_num_point() const { + if (is_empty()) return 0; + std::size_t num_point = 0; + for (const auto& g : geometries) { + num_point += g->get_num_point(); + } + return num_point; +} + +std::unique_ptr<GeoShape> GeoCollection::boundary() const { + std::unique_ptr<GeoCollection> collection = GeoCollection::create_unique(); + if (is_empty()) { + collection->set_empty(); + return collection; + } + + for (const auto& g : geometries) { + std::unique_ptr<GeoShape> shape = g->boundary(); + if (!shape->is_empty()) collection->add_one_geometry(shape.release()); + } + if (collection->get_num_geometries() == 0) collection->set_empty(); + return collection; + + //to_homogenize 还有问题 + //return collection->to_homogenize(); +} + +std::unique_ptr<GeoCollection> GeoCollection::to_homogenize() { + std::unique_ptr<GeoMultiPoint> multi_point = GeoMultiPoint::create_unique(); + std::unique_ptr<GeoMultiLineString> multi_linestring = GeoMultiLineString::create_unique(); + std::unique_ptr<GeoMultiPolygon> multi_polygon = GeoMultiPolygon::create_unique(); + + for (const auto& g : geometries) { + switch (g->type()) { + case GEO_SHAPE_POINT: { + multi_point->add_one_geometry(g.get()); + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + multi_linestring->add_one_geometry(linestring); + break; + } + case GEO_SHAPE_POLYGON: { + GeoPolygon* polygon = dynamic_cast<GeoPolygon*>(g.get()); + multi_polygon->add_one_geometry(polygon); + break; + } + case GEO_SHAPE_MULTI_POINT: { + const GeoMultiPoint* multi_point_tmp = dynamic_cast<const GeoMultiPoint*>(g.get()); + for (int i = 0; i < multi_point_tmp->get_num_point(); ++i) { + GeoPoint* point = dynamic_cast<GeoPoint*>(multi_point_tmp->get_geometries_n(i)); + multi_point->add_one_geometry(point); + } + break; + } + case GEO_SHAPE_MULTI_LINE_STRING: { + const GeoMultiLineString* multi_linestring_tmp = + dynamic_cast<const GeoMultiLineString*>(g.get()); + for (int i = 0; i < multi_linestring_tmp->get_num_line(); ++i) { + GeoLineString* linestring = + dynamic_cast<GeoLineString*>(multi_linestring_tmp->get_geometries_n(i)); + multi_linestring->add_one_geometry(linestring); + } + break; + } + case GEO_SHAPE_MULTI_POLYGON: { + const GeoMultiPolygon* multi_polygon_tmp = + dynamic_cast<const GeoMultiPolygon*>(g.get()); + for (int i = 0; i < multi_polygon_tmp->get_num_polygon(); ++i) { + GeoPolygon* polygon = + dynamic_cast<GeoPolygon*>(multi_polygon_tmp->get_geometries_n(i)); + multi_polygon->add_one_geometry(polygon); + } + break; + } + default: { + //这里应该输出告警日志 + return nullptr; + } + } + } + + std::unique_ptr<GeoCollection> res_collection = GeoCollection::create_unique(); + + if (multi_point->get_num_point() == 1) { + GeoPoint* point = (GeoPoint*)multi_point->get_geometries_n(0); + res_collection->add_one_geometry(point); + } else if (multi_point->get_num_point() > 1) { + res_collection->add_one_geometry(multi_point.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_linestring->get_num_line() == 1) { + GeoLineString* linestring = (GeoLineString*)multi_linestring->get_geometries_n(0); + res_collection->add_one_geometry(linestring); + } else if (multi_linestring->get_num_line() > 1) { + res_collection->add_one_geometry(multi_linestring.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + if (multi_polygon->get_num_polygon() == 1) { + GeoPolygon* polygon = (GeoPolygon*)multi_polygon->get_geometries_n(0); + res_collection->add_one_geometry(polygon); + } else if (multi_polygon->get_num_polygon() > 1) { + res_collection->add_one_geometry(multi_polygon.release()); + } + /* + * else { + * 这里应该输出告警日志 + * } + */ + + return res_collection; +} + +bool GeoCollection::add_to_s2shape_index(MutableS2ShapeIndex& S2shape_index) const { + if (is_empty()) return false; + for (const auto& g : geometries) { + if (g->is_empty() || !g->is_valid()) continue; + switch (g->type()) { + case GEO_SHAPE_POINT: { + GeoPoint* point = dynamic_cast<GeoPoint*>(g.get()); + if (point->is_empty()) continue; + if (!point->add_to_s2shape_index(S2shape_index)) { + return false; + } + break; + } + case GEO_SHAPE_LINE_STRING: { + GeoLineString* linestring = dynamic_cast<GeoLineString*>(g.get()); + if (linestring->is_empty()) continue; Review Comment: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] ```suggestion TRING: {auto ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org