PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_distance_knn()

Datum geography_distance_knn ( PG_FUNCTION_ARGS  )

Definition at line 56 of file geography_measurement.c.

References SPHEROID::a, SPHEROID::b, distance(), error_if_srid_mismatch(), FP_TOLERANCE, geography_distance_uncached(), gserialized_get_srid(), lwgeom_add_bbox_deep(), lwgeom_distance_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), PG_FUNCTION_INFO_V1(), SPHEROID::radius, and s.

57 {
58  LWGEOM *lwgeom1 = NULL;
59  LWGEOM *lwgeom2 = NULL;
60  GSERIALIZED *g1 = NULL;
61  GSERIALIZED *g2 = NULL;
62  double distance;
63  double tolerance = FP_TOLERANCE;
64  bool use_spheroid = false; /* must use sphere, can't get index to harmonize with spheroid */
65  SPHEROID s;
66 
67  /* Get our geometry objects loaded into memory. */
68  g1 = PG_GETARG_GSERIALIZED_P(0);
69  g2 = PG_GETARG_GSERIALIZED_P(1);
70 
71  /* Initialize spheroid */
72  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
73 
75 
76  /* Set to sphere if requested */
77  if ( ! use_spheroid )
78  s.a = s.b = s.radius;
79 
80  lwgeom1 = lwgeom_from_gserialized(g1);
81  lwgeom2 = lwgeom_from_gserialized(g2);
82 
83  /* Return NULL on empty arguments. */
84  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
85  {
86  PG_FREE_IF_COPY(g1, 0);
87  PG_FREE_IF_COPY(g2, 1);
88  PG_RETURN_NULL();
89  }
90 
91  /* Make sure we have boxes attached */
92  lwgeom_add_bbox_deep(lwgeom1, NULL);
93  lwgeom_add_bbox_deep(lwgeom2, NULL);
94 
95  distance = lwgeom_distance_spheroid(lwgeom1, lwgeom2, &s, tolerance);
96 
97  POSTGIS_DEBUGF(2, "[GIST] '%s' got distance %g", __func__, distance);
98 
99  /* Clean up */
100  lwgeom_free(lwgeom1);
101  lwgeom_free(lwgeom2);
102  PG_FREE_IF_COPY(g1, 0);
103  PG_FREE_IF_COPY(g2, 1);
104 
105  /* Something went wrong, negative return... should already be eloged, return NULL */
106  if ( distance < 0.0 )
107  {
108  PG_RETURN_NULL();
109  }
110 
111  PG_RETURN_FLOAT8(distance);
112 }
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:611
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double b
Definition: liblwgeom.h:298
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
double radius
Definition: liblwgeom.h:302
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
#define FP_TOLERANCE
Floating point comparators.
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
double a
Definition: liblwgeom.h:297
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:2081
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1297
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:69
Here is the call graph for this function: