PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ gserialized_gist_distance_2d()

Datum gserialized_gist_distance_2d ( PG_FUNCTION_ARGS  )

Definition at line 1267 of file gserialized_gist_2d.c.

1268 {
1269  GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
1270  BOX2DF query_box;
1271  BOX2DF *entry_box;
1272  StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
1273  double distance;
1274 #if POSTGIS_PGSQL_VERSION >= 95
1275  bool *recheck = (bool *) PG_GETARG_POINTER(4);
1276 #endif
1277 
1278  POSTGIS_DEBUG(4, "[GIST] 'distance' function called");
1279 
1280  /* We are using '13' as the gist true-distance <-> strategy number
1281  * and '14' as the gist distance-between-boxes <#> strategy number */
1282  if ( strategy != 13 && strategy != 14 ) {
1283  elog(ERROR, "unrecognized strategy number: %d", strategy);
1284  PG_RETURN_FLOAT8(FLT_MAX);
1285  }
1286 
1287  /* Null box should never make this far. */
1288  if ( gserialized_datum_get_box2df_p(PG_GETARG_DATUM(1), &query_box) == LW_FAILURE )
1289  {
1290  POSTGIS_DEBUG(4, "[GIST] null query_gbox_index!");
1291  PG_RETURN_FLOAT8(FLT_MAX);
1292  }
1293 
1294  /* Get the entry box */
1295  entry_box = (BOX2DF*)DatumGetPointer(entry->key);
1296 
1297 #if POSTGIS_PGSQL_VERSION >= 95
1298 
1299  /* Box-style distance test */
1300  if ( strategy == 14 ) /* operator <#> */
1301  {
1302  distance = box2df_distance(entry_box, &query_box);
1303  }
1304  /* True distance test (formerly centroid distance) */
1305  else if ( strategy == 13 ) /* operator <-> */
1306  {
1307  /* In all cases, since we only have keys (boxes) we'll return */
1308  /* the minimum possible distance, which is the box2df_distance */
1309  /* and let the recheck sort things out in the case of leaves */
1310  distance = box2df_distance(entry_box, &query_box);
1311 
1312  if (GIST_LEAF(entry))
1313  *recheck = true;
1314  }
1315  else
1316  {
1317  elog(ERROR, "%s: reached unreachable code", __func__);
1318  PG_RETURN_NULL();
1319  }
1320 #else
1321  /* Box-style distance test */
1322  if ( strategy == 14 )
1323  {
1324  distance = (double)box2df_distance(entry_box, &query_box);
1325  PG_RETURN_FLOAT8(distance);
1326  }
1327 
1328  /* Treat leaf node tests different from internal nodes */
1329  if (GIST_LEAF(entry))
1330  {
1331  /* Calculate distance to leaves */
1332  distance = (double)box2df_distance_leaf_centroid(entry_box, &query_box);
1333  }
1334  else
1335  {
1336  /* Calculate distance for internal nodes */
1337  distance = (double)box2df_distance_node_centroid(entry_box, &query_box);
1338  }
1339 #endif
1340 
1341  PG_RETURN_FLOAT8(distance);
1342 }
static double box2df_distance(const BOX2DF *a, const BOX2DF *b)
Calculate the box->box distance.
static double box2df_distance_leaf_centroid(const BOX2DF *a, const BOX2DF *b)
Calculate the centroid->centroid distance between the boxes.
int gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df)
Peak into a GSERIALIZED datum to find the bounding box.
#define LW_FAILURE
Definition: liblwgeom.h:79
Datum distance(PG_FUNCTION_ARGS)

References box2df_distance(), box2df_distance_leaf_centroid(), distance(), gserialized_datum_get_box2df_p(), and LW_FAILURE.

Here is the call graph for this function: