PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum ST_GeometricMedian ( PG_FUNCTION_ARGS  )

Definition at line 1147 of file lwgeom_functions_analytic.c.

References FP_MAX, FP_MIN, dumpnode::geom, geometry_serialize(), LW_FALSE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_get_bbox(), lwgeom_has_z(), lwgeom_median(), lwpoint_as_lwgeom(), GBOX::xmax, GBOX::xmin, GBOX::ymax, GBOX::ymin, GBOX::zmax, and GBOX::zmin.

1148 {
1149  GSERIALIZED* geom;
1150  GSERIALIZED* result;
1151  LWGEOM* input;
1152  LWPOINT* lwresult;
1153  static const double min_default_tolerance = 1e-8;
1154  double tolerance = min_default_tolerance;
1155  bool compute_tolerance_from_box;
1156  bool fail_if_not_converged;
1157  int max_iter;
1158 
1159  /* Read and validate our input arguments */
1160  if (PG_ARGISNULL(0))
1161  PG_RETURN_NULL();
1162 
1163  compute_tolerance_from_box = PG_ARGISNULL(1);
1164 
1165  if (!compute_tolerance_from_box)
1166  {
1167  tolerance = PG_GETARG_FLOAT8(1);
1168  if (tolerance < 0)
1169  {
1170  lwpgerror("Tolerance must be positive.");
1171  PG_RETURN_NULL();
1172  }
1173  }
1174 
1175  max_iter = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2);
1176  fail_if_not_converged = PG_ARGISNULL(3) ? LW_FALSE : PG_GETARG_BOOL(3);
1177 
1178  if (max_iter < 0)
1179  {
1180  lwpgerror("Maximum iterations must be positive.");
1181  PG_RETURN_NULL();
1182  }
1183 
1184  /* OK, inputs are valid. */
1185  geom = PG_GETARG_GSERIALIZED_P(0);
1186  input = lwgeom_from_gserialized(geom);
1187 
1188  if (compute_tolerance_from_box)
1189  {
1190  /* Compute a default tolerance based on the smallest dimension
1191  * of the geometry's bounding box.
1192  */
1193  static const double tolerance_coefficient = 1e-6;
1194  const GBOX* box = lwgeom_get_bbox(input);
1195 
1196  if (box)
1197  {
1198  double min_dim = FP_MIN(box->xmax - box->xmin, box->ymax - box->ymin);
1199  if (lwgeom_has_z(input))
1200  min_dim = FP_MIN(min_dim, box->zmax - box->zmin);
1201 
1202  /* Apply a lower bound to the computed default tolerance to
1203  * avoid a tolerance of zero in the case of collinear
1204  * points.
1205  */
1206  tolerance = FP_MAX(min_default_tolerance, tolerance_coefficient * min_dim);
1207  }
1208  }
1209 
1210  lwresult = lwgeom_median(input, tolerance, max_iter, fail_if_not_converged);
1211  lwgeom_free(input);
1212 
1213  if(!lwresult)
1214  {
1215  lwpgerror("Error computing geometric median.");
1216  PG_RETURN_NULL();
1217  }
1218 
1219  result = geometry_serialize(lwpoint_as_lwgeom(lwresult));
1220 
1221  PG_RETURN_POINTER(result);
1222 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double xmax
Definition: liblwgeom.h:292
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
#define FP_MIN(A, B)
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:923
double zmax
Definition: liblwgeom.h:296
double ymin
Definition: liblwgeom.h:293
LWGEOM * geom
double xmin
Definition: liblwgeom.h:291
#define LW_FALSE
Definition: liblwgeom.h:76
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:727
double ymax
Definition: liblwgeom.h:294
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double zmin
Definition: liblwgeom.h:295
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:334
LWPOINT * lwgeom_median(const LWGEOM *g, double tol, uint32_t maxiter, char fail_if_not_converged)
#define FP_MAX(A, B)

Here is the call graph for this function: