From: Paul Ramsey Date: Fri, 15 Jan 2010 18:41:41 +0000 (+0000) Subject: Short circuit on distance tests: only do full spheroidal calculation where the distan... X-Git-Tag: 1.5.0rc1~14 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=721f1bafc2c6b6f2983167b3395ba833aab24e03;p=postgis Short circuit on distance tests: only do full spheroidal calculation where the distance is near or greater than the tolerance. This will make large st_dwithin() radius searches much faster since points that are well within the radius will not have their full geodetic calculation run, only those that are close to the radius boundary. git-svn-id: http://svn.osgeo.org/postgis/trunk@5133 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/lwgeodetic.c b/liblwgeom/lwgeodetic.c index ebc1c31fb..0bc74fd79 100644 --- a/liblwgeom/lwgeodetic.c +++ b/liblwgeom/lwgeodetic.c @@ -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);