zxc20041 commented on code in PR #60170:
URL: https://github.com/apache/doris/pull/60170#discussion_r2730480308
##########
be/src/geo/geo_types.cpp:
##########
@@ -1630,5 +1632,355 @@ 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;
+}
+
Review Comment:
polygon will sum all loops
--
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]