zxc20041 commented on code in PR #60170:
URL: https://github.com/apache/doris/pull/60170#discussion_r2741747227
##########
be/src/geo/geo_types.cpp:
##########
@@ -1630,5 +1632,361 @@ std::string GeoShape::as_binary(GeoShape* rhs) {
return res;
}
+// Helper function to compute distance from a point to a line segment
+double distance_point_to_segment(const S2Point& point, const S2Point&
line_start,
+ const S2Point& line_end) {
+ S1Angle angle = S2::GetDistance(point, line_start, line_end);
+ return S2Earth::ToMeters(angle);
+}
+
+// Helper function to compute distance from a point to a polyline
+double distance_point_to_polyline(const S2Point& point, const S2Polyline*
polyline) {
+ if (polyline->num_vertices() == 0) {
+ return std::numeric_limits<double>::max();
+ }
+ if (polyline->num_vertices() == 1) {
+ return S2Earth::GetDistanceMeters(point, polyline->vertex(0));
+ }
+
+ S1Angle min_angle = S1Angle::Infinity();
+ for (int i = 0; i < polyline->num_vertices() - 1; ++i) {
+ const S2Point& p1 = polyline->vertex(i);
+ const S2Point& p2 = polyline->vertex(i + 1);
+
+ S1Angle dist = S2::GetDistance(point, p1, p2);
+ if (dist < min_angle) {
+ min_angle = dist;
+ }
+ }
+
+ return S2Earth::ToMeters(min_angle);
+}
+
+// Helper function to compute distance from a point to a polygon
+double distance_point_to_polygon(const S2Point& point, const S2Polygon*
polygon) {
+ // Check if point is inside polygon
+ if (polygon->Contains(point)) {
+ return 0.0;
+ }
+
+ // Find minimum distance to polygon boundary
+ S1Angle min_angle = S1Angle::Infinity();
+
+ for (int i = 0; i < polygon->num_loops(); ++i) {
+ const S2Loop* loop = polygon->loop(i);
+
+ for (int j = 0; j < loop->num_vertices(); ++j) {
+ const S2Point& p1 = loop->vertex(j);
+ const S2Point& p2 = loop->vertex((j + 1) % loop->num_vertices());
+
+ S1Angle dist = S2::GetDistance(point, p1, p2);
+ if (dist < min_angle) {
+ min_angle = dist;
+ }
+ }
+ }
+
+ return S2Earth::ToMeters(min_angle);
+}
+
+double GeoPoint::Length() const {
+ // Point has no length
+ return 0.0;
+}
+
+double GeoLine::Length() const {
+ // GeoLine is always valid with at least 2 vertices (guaranteed by
constructor)
+ double total_length = 0.0;
+ for (int i = 0; i < _polyline->num_vertices() - 1; ++i) {
+ const S2Point& p1 = _polyline->vertex(i);
+ const S2Point& p2 = _polyline->vertex(i + 1);
+
+ S2LatLng lat_lng1(p1);
+ S2LatLng lat_lng2(p2);
+
+ // Calculate distance in meters using S2Earth
+ double distance = S2Earth::GetDistanceMeters(lat_lng1, lat_lng2);
+ total_length += distance;
+ }
+
+ return total_length;
+}
+
+double GeoPolygon::Length() const {
+ // GeoPolygon is always valid with at least one loop (guaranteed by
constructor)
+ double perimeter = 0.0;
+
+ for (int loop_idx = 0; loop_idx < _polygon->num_loops(); ++loop_idx) {
+ const S2Loop* loop = _polygon->loop(loop_idx);
+ for (int i = 0; i < loop->num_vertices(); ++i) {
+ const S2Point& p1 = loop->vertex(i);
+ const S2Point& p2 = loop->vertex((i + 1) % loop->num_vertices());
+
+ S2LatLng lat_lng1(p1);
+ S2LatLng lat_lng2(p2);
+
+ // Calculate distance in meters using S2Earth
+ double distance = S2Earth::GetDistanceMeters(lat_lng1, lat_lng2);
+ perimeter += distance;
+ }
+ }
+
+ return perimeter;
+}
+
+double GeoMultiPolygon::Length() const {
+ double total_length = 0.0;
+
+ // Calculate the perimeter of all polygons
+ for (const auto& polygon : _polygons) {
+ total_length += polygon->Length();
+ }
+
+ return total_length;
+}
+
+double GeoCircle::Length() const {
+ // GeoCircle is always valid (guaranteed by constructor)
+ // Get the radius in meters
+ double radius_meters = S2Earth::ToMeters(_cap->radius());
+
+ // Calculate circumference: 2 * pi * r
+ return 2.0 * M_PI * radius_meters;
+}
+
+double GeoPoint::Distance(const GeoShape* rhs) const {
Review Comment:
reuse and remove duplicated implementations
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]