PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum gserialized_gist_distance_2d ( PG_FUNCTION_ARGS  )

Definition at line 1063 of file gserialized_gist_2d.c.

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

1064 {
1065  GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
1066  BOX2DF query_box;
1067  BOX2DF *entry_box;
1068  StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
1069  double distance;
1070 #if POSTGIS_PGSQL_VERSION >= 95
1071  bool *recheck = (bool *) PG_GETARG_POINTER(4);
1072 #endif
1073 
1074  POSTGIS_DEBUG(4, "[GIST] 'distance' function called");
1075 
1076  /* We are using '13' as the gist true-distance <-> strategy number
1077  * and '14' as the gist distance-between-boxes <#> strategy number */
1078  if ( strategy != 13 && strategy != 14 ) {
1079  elog(ERROR, "unrecognized strategy number: %d", strategy);
1080  PG_RETURN_FLOAT8(FLT_MAX);
1081  }
1082 
1083  /* Null box should never make this far. */
1084  if ( gserialized_datum_get_box2df_p(PG_GETARG_DATUM(1), &query_box) == LW_FAILURE )
1085  {
1086  POSTGIS_DEBUG(4, "[GIST] null query_gbox_index!");
1087  PG_RETURN_FLOAT8(FLT_MAX);
1088  }
1089 
1090  /* Get the entry box */
1091  entry_box = (BOX2DF*)DatumGetPointer(entry->key);
1092 
1093 #if POSTGIS_PGSQL_VERSION >= 95
1094 
1095  /* Box-style distance test */
1096  if ( strategy == 14 ) /* operator <#> */
1097  {
1098  distance = box2df_distance(entry_box, &query_box);
1099  }
1100  /* True distance test (formerly centroid distance) */
1101  else if ( strategy == 13 ) /* operator <-> */
1102  {
1103  /* In all cases, since we only have keys (boxes) we'll return */
1104  /* the minimum possible distance, which is the box2df_distance */
1105  /* and let the recheck sort things out in the case of leaves */
1106  distance = box2df_distance(entry_box, &query_box);
1107 
1108  if (GIST_LEAF(entry))
1109  *recheck = true;
1110  }
1111  else
1112  {
1113  elog(ERROR, "%s: reached unreachable code", __func__);
1114  PG_RETURN_NULL();
1115  }
1116 #else
1117  /* Box-style distance test */
1118  if ( strategy == 14 )
1119  {
1120  distance = (double)box2df_distance(entry_box, &query_box);
1121  PG_RETURN_FLOAT8(distance);
1122  }
1123 
1124  /* Treat leaf node tests different from internal nodes */
1125  if (GIST_LEAF(entry))
1126  {
1127  /* Calculate distance to leaves */
1128  distance = (double)box2df_distance_leaf_centroid(entry_box, &query_box);
1129  }
1130  else
1131  {
1132  /* Calculate distance for internal nodes */
1133  distance = (double)box2df_distance_node_centroid(entry_box, &query_box);
1134  }
1135 #endif
1136 
1137  PG_RETURN_FLOAT8(distance);
1138 }
static double box2df_distance_node_centroid(const BOX2DF *node, const BOX2DF *query)
Calculate the The node_box_edge->query_centroid distance between the boxes.
#define LW_FAILURE
Definition: liblwgeom.h:64
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.
static int gserialized_datum_get_box2df_p(Datum gsdatum, BOX2DF *box2df)
Peak into a GSERIALIZED datum to find the bounding box.
Datum distance(PG_FUNCTION_ARGS)

Here is the call graph for this function: