MobilityDB/MobilityDB

Bug in nearestApproachInstant function

ninabel opened this issue · 3 comments

I want to find a point where the track is in the nearest approach to the given point.

But when I use given point as geometry without time the nearestApproachInstant returns second point of the track which is not nearest.

WITH l AS 
(SELECT '[POINT(0 1)@2022-05-12 03:27:23.522365+00, POINT(0.1 1)@2022-05-12 03:27:37+00, 
          POINT(0.2 1)@2022-05-12 03:27:54+00, POINT(0.3 1)@2022-05-12 03:28:06+00, 
          POINT(0.4 1)@2022-05-12 03:28:12+00, POINT(0.6 1)@2022-05-12 03:28:31+00, 
          POINT(0.65 1)@2022-05-12 03:28:38+00, POINT(0.7 1)@2022-05-12 03:28:47+00, 
          POINT(0.75 1)@2022-05-12 03:28:55+00, POINT(0.8 1)@2022-05-12 03:29:11+00, 
 		  POINT(0.82 1)@2022-05-12 03:29:28+00, POINT(0.85 1)@2022-05-12 03:29:40+00, 
          POINT(0.9 1)@2022-05-12 03:29:57+00, 
          POINT(1 1)@2022-05-12 03:40:01.602477+00]'::tgeompoint AS track)
SELECT  asText(nearestApproachInstant(track,'[POINT(0.35 1.1)@2022-05-12 03:27:23+00,POINT(0.35 1.1)@2022-05-12 03:50:23+00]'::tgeompoint)),
		asText(nearestApproachInstant(track,'POINT(0.35 1.1)'::geometry))
FROM l WHERE True;	
                          astext                          |               astext                
----------------------------------------------------------+-------------------------------------
 POINT(0.349999983333333 1)@2022-05-12 05:28:08.999999+02 | POINT(0.1 1)@2022-05-12 05:27:37+02
(1 row)

In this example the first column is the expected result, but I have to convert point to tgeompoint to get it.

MobilityDB 1.0.0, PostgreSQL 13.6 (Debian 13.6-1.pgdg100+1), PostGIS 3.2.1

Dear Nina
Many thanks for reporting this bug! You can replace the following lines
https://github.com/MobilityDB/MobilityDB/blob/master/src/point/tpoint_distance.c#L580-#L593
by the following lines

  if (fabsl(fraction) < MOBDB_EPSILON)
    *t = inst1->t;
  else if (fabsl(fraction - 1.0) < MOBDB_EPSILON)
    *t = inst2->t;
  else
  {
    double duration = (inst2->t - inst1->t);
    *t = inst1->t + (TimestampTz) (duration * fraction);
  }
  return dist;

and this will normally solve the problem. Could you please confirm ?
I am already doing this change in the current PR that I am working on, which will be available in the near future in the develop branch.
Regards
Esteban

Fixed in PR #223

Dear Esteban!
Thank you, it works now.