PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ geography_distance_knn()

Datum geography_distance_knn ( PG_FUNCTION_ARGS  )

Definition at line 71 of file geography_measurement.c.

72 {
73  LWGEOM *lwgeom1 = NULL;
74  LWGEOM *lwgeom2 = NULL;
75  GSERIALIZED *g1 = NULL;
76  GSERIALIZED *g2 = NULL;
77  double distance;
78  double tolerance = FP_TOLERANCE;
79  bool use_spheroid = false; /* must use sphere, can't get index to harmonize with spheroid */
80  SPHEROID s;
81 
82  /* Get our geometry objects loaded into memory. */
83  g1 = PG_GETARG_GSERIALIZED_P(0);
84  g2 = PG_GETARG_GSERIALIZED_P(1);
85 
86  gserialized_error_if_srid_mismatch(g1, g2, __func__);
87 
88  /* Initialize spheroid */
89  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
90 
91  /* Set to sphere if requested */
92  if ( ! use_spheroid )
93  s.a = s.b = s.radius;
94 
95  lwgeom1 = lwgeom_from_gserialized(g1);
96  lwgeom2 = lwgeom_from_gserialized(g2);
97 
98  /* Return NULL on empty arguments. */
99  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
100  {
101  PG_FREE_IF_COPY(g1, 0);
102  PG_FREE_IF_COPY(g2, 1);
103  PG_RETURN_NULL();
104  }
105 
106  /* Make sure we have boxes attached */
107  lwgeom_add_bbox_deep(lwgeom1, NULL);
108  lwgeom_add_bbox_deep(lwgeom2, NULL);
109 
110  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
111 
112  POSTGIS_DEBUGF(2, "[GIST] '%s' got distance %g", __func__, distance);
113 
114  /* Clean up */
115  lwgeom_free(lwgeom1);
116  lwgeom_free(lwgeom2);
117  PG_FREE_IF_COPY(g1, 0);
118  PG_FREE_IF_COPY(g2, 1);
119 
120  /* Something went wrong, negative return... should already be eloged, return NULL */
121  if ( distance < 0.0 )
122  {
123  PG_RETURN_NULL();
124  }
125 
126  PG_RETURN_FLOAT8(distance);
127 }
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:404
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:2187
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
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:696
#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:193
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: