]> granicus.if.org Git - postgis/commitdiff
Short circuit on distance tests: only do full spheroidal calculation where the distan...
authorPaul Ramsey <pramsey@cleverelephant.ca>
Fri, 15 Jan 2010 18:41:41 +0000 (18:41 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Fri, 15 Jan 2010 18:41:41 +0000 (18:41 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@5133 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwgeodetic.c

index ebc1c31fb24d8bdb25f0fc398ee5a159695960b3..0bc74fd796fe651f8a5000ecec140299fd1496f6 100644 (file)
@@ -1521,11 +1521,15 @@ static double ptarray_distance_spheroid(const POINTARRAY *pa1, const POINTARRAY
                getPoint2d_p(pa2, 0, &p);
                geographic_point_init(p.x, p.y, &g2);
                /* Sphere special case, axes equal */
+               distance = s->radius * sphere_distance(&g1, &g2);
                if( use_sphere )
-                       distance = s->radius * sphere_distance(&g1, &g2);
+            return distance;
+        /* Below tolerance, actual distance isn't of interest */
+        else if( distance < 0.95 * tolerance )
+            return distance; 
+        /* Close or greater than tolerance, get the real answer to be sure */
                else
-                       distance = spheroid_distance(&g1, &g2, s);
-               return distance;                
+                   return spheroid_distance(&g1, &g2, s);
        }
 
        /* Handle point/line case here */
@@ -1575,9 +1579,14 @@ static double ptarray_distance_spheroid(const POINTARRAY *pa1, const POINTARRAY
                                /* Working on a sphere? The answer is correct, return */
                                if( use_sphere )
                                {
-                                       return distance;
+                                       return d;
                                }
-                               /* On a spheroid? Confirm that we are *actually* closer than tolerance */
+                               /* Far enough past the tolerance that the spheroid calculation won't change things */
+                               else if( d < tolerance * 0.95 )
+                               {
+                    return d;
+                           }
+                               /* On a spheroid and near the tolerance? Confirm that we are *actually* closer than tolerance */
                                else
                                {
                                        d = spheroid_distance(&g1, &nearest2, s);