PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ geography_distance()

Datum geography_distance ( PG_FUNCTION_ARGS  )

Definition at line 206 of file geography_measurement.c.

207{
208 GSERIALIZED* g1 = NULL;
209 GSERIALIZED* g2 = NULL;
210 double distance;
211 bool use_spheroid = true;
212 SPHEROID s;
213
214 /* Get our geometry objects loaded into memory. */
215 g1 = PG_GETARG_GSERIALIZED_P(0);
216 g2 = PG_GETARG_GSERIALIZED_P(1);
217
218 if (PG_NARGS() > 2)
219 use_spheroid = PG_GETARG_BOOL(2);
220
221 gserialized_error_if_srid_mismatch(g1, g2, __func__);
222
223 /* Initialize spheroid */
224 spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
225
226 /* Set to sphere if requested */
227 if ( ! use_spheroid )
228 s.a = s.b = s.radius;
229
230 /* Return NULL on empty arguments. */
232 {
233 PG_FREE_IF_COPY(g1, 0);
234 PG_FREE_IF_COPY(g2, 1);
235 PG_RETURN_NULL();
236 }
237
238 /* Do the brute force calculation if the cached calculation doesn't tick over */
239 if ( LW_FAILURE == geography_distance_cache(fcinfo, g1, g2, &s, &distance) )
240 {
241 /* default to using tree-based distance calculation at all times */
242 /* in standard distance call. */
244 /*
245 LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
246 LWGEOM* lwgeom2 = lwgeom_from_gserialized(g2);
247 distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
248 lwgeom_free(lwgeom1);
249 lwgeom_free(lwgeom2);
250 */
251 }
252
253 /* Clean up */
254 PG_FREE_IF_COPY(g1, 0);
255 PG_FREE_IF_COPY(g2, 1);
256
257 /* Knock off any funny business at the nanometer level, ticket #2168 */
259
260 /* Something went wrong, negative return... should already be eloged, return NULL */
261 if ( distance < 0.0 )
262 {
263 elog(ERROR, "distance returned negative!");
264 PG_RETURN_NULL();
265 }
266
267 PG_RETURN_FLOAT8(distance);
268}
char * s
Definition cu_in_wkt.c:23
#define INVMINDIST
int geography_tree_distance(const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
int geography_distance_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
#define LW_FAILURE
Definition liblwgeom.h:110
#define FP_TOLERANCE
Floating point comparators.
static double distance(double x1, double y1, double x2, double y2)
Definition lwtree.c:1032

References distance(), FP_TOLERANCE, geography_distance_cache(), geography_tree_distance(), gserialized_error_if_srid_mismatch(), gserialized_get_srid(), gserialized_is_empty(), INVMINDIST, LW_FAILURE, and s.

Here is the call graph for this function: