PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_distance_knn()

Datum geography_distance_knn ( PG_FUNCTION_ARGS  )

Definition at line 77 of file geography_measurement.c.

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