PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_distance_uncached()

Datum geography_distance_uncached ( PG_FUNCTION_ARGS  )

Definition at line 140 of file geography_measurement.c.

141 {
142  LWGEOM *lwgeom1 = NULL;
143  LWGEOM *lwgeom2 = NULL;
144  GSERIALIZED *g1 = PG_GETARG_GSERIALIZED_P(0);
145  GSERIALIZED *g2 = PG_GETARG_GSERIALIZED_P(1);
146  double distance;
147  double tolerance = FP_TOLERANCE;
148  bool use_spheroid = true;
149  SPHEROID s;
150 
151  /* Read our tolerance value. */
152  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
153  tolerance = PG_GETARG_FLOAT8(2);
154 
155  /* Read our calculation type. */
156  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
157  use_spheroid = PG_GETARG_BOOL(3);
158 
159  gserialized_error_if_srid_mismatch(g1, g2, __func__);
160 
161  /* Initialize spheroid */
162  spheroid_init_from_srid(gserialized_get_srid(g1), &s);
163 
164  /* Set to sphere if requested */
165  if ( ! use_spheroid )
166  s.a = s.b = s.radius;
167 
168  lwgeom1 = lwgeom_from_gserialized(g1);
169  lwgeom2 = lwgeom_from_gserialized(g2);
170 
171  /* Return NULL on empty arguments. */
172  if ( !lwgeom1 || !lwgeom2 || lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
173  {
174  PG_FREE_IF_COPY(g1, 0);
175  PG_FREE_IF_COPY(g2, 1);
176  PG_RETURN_NULL();
177  }
178 
179  /* Make sure we have boxes attached */
180  lwgeom_add_bbox_deep(lwgeom1, NULL);
181  lwgeom_add_bbox_deep(lwgeom2, NULL);
182 
183  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
184 
185  /* Clean up */
186  lwgeom_free(lwgeom1);
187  lwgeom_free(lwgeom2);
188  PG_FREE_IF_COPY(g1, 0);
189  PG_FREE_IF_COPY(g2, 1);
190 
191  /* Something went wrong, negative return... should already be eloged, return NULL */
192  if ( distance < 0.0 )
193  {
194  PG_RETURN_NULL();
195  }
196 
197  PG_RETURN_FLOAT8(distance);
198 }
char * s
Definition: cu_in_wkt.c:23
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
Definition: gserialized.c:403
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)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
Calculate the geodetic distance from lwgeom1 to lwgeom2 on the spheroid.
Definition: lwgeodetic.c:2204
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
void lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox)
Compute a box for geom and all sub-geometries, if not already computed.
Definition: lwgeom.c:714
#define FP_TOLERANCE
Floating point comparators.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032

References distance(), FP_TOLERANCE, gserialized_error_if_srid_mismatch(), gserialized_get_srid(), lwgeom_add_bbox_deep(), lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), and s.

Here is the call graph for this function: