PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_GeometricMedian()

Datum ST_GeometricMedian ( PG_FUNCTION_ARGS  )

Definition at line 1232 of file lwgeom_functions_analytic.c.

1233 {
1234  GSERIALIZED* geom;
1236  LWGEOM* input;
1237  LWPOINT* lwresult;
1238  static const double min_default_tolerance = 1e-8;
1239  double tolerance = min_default_tolerance;
1240  bool compute_tolerance_from_box;
1241  bool fail_if_not_converged;
1242  int max_iter;
1243 
1244  /* Read and validate our input arguments */
1245  if (PG_ARGISNULL(0))
1246  PG_RETURN_NULL();
1247 
1248  compute_tolerance_from_box = PG_ARGISNULL(1);
1249 
1250  if (!compute_tolerance_from_box)
1251  {
1252  tolerance = PG_GETARG_FLOAT8(1);
1253  if (tolerance < 0)
1254  {
1255  lwpgerror("Tolerance must be positive.");
1256  PG_RETURN_NULL();
1257  }
1258  }
1259 
1260  max_iter = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2);
1261  fail_if_not_converged = PG_ARGISNULL(3) ? LW_FALSE : PG_GETARG_BOOL(3);
1262 
1263  if (max_iter < 0)
1264  {
1265  lwpgerror("Maximum iterations must be positive.");
1266  PG_RETURN_NULL();
1267  }
1268 
1269  /* OK, inputs are valid. */
1270  geom = PG_GETARG_GSERIALIZED_P(0);
1271  input = lwgeom_from_gserialized(geom);
1272 
1273  if (compute_tolerance_from_box)
1274  {
1275  /* Compute a default tolerance based on the smallest dimension
1276  * of the geometry's bounding box.
1277  */
1278  static const double tolerance_coefficient = 1e-6;
1279  const GBOX* box = lwgeom_get_bbox(input);
1280 
1281  if (box)
1282  {
1283  double min_dim = FP_MIN(box->xmax - box->xmin, box->ymax - box->ymin);
1284  if (lwgeom_has_z(input))
1285  min_dim = FP_MIN(min_dim, box->zmax - box->zmin);
1286 
1287  /* Apply a lower bound to the computed default tolerance to
1288  * avoid a tolerance of zero in the case of collinear
1289  * points.
1290  */
1291  tolerance = FP_MAX(min_default_tolerance, tolerance_coefficient * min_dim);
1292  }
1293  }
1294 
1295  lwresult = lwgeom_median(input, tolerance, max_iter, fail_if_not_converged);
1296  lwgeom_free(input);
1297 
1298  if(!lwresult)
1299  {
1300  lwpgerror("Error computing geometric median.");
1301  PG_RETURN_NULL();
1302  }
1303 
1304  result = geometry_serialize(lwpoint_as_lwgeom(lwresult));
1305 
1306  PG_RETURN_POINTER(result);
1307 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define LW_FALSE
Definition: liblwgeom.h:94
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
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:743
LWPOINT * lwgeom_median(const LWGEOM *g, double tol, uint32_t maxiter, char fail_if_not_converged)
#define FP_MAX(A, B)
#define FP_MIN(A, B)
double ymax
Definition: liblwgeom.h:357
double zmax
Definition: liblwgeom.h:359
double xmax
Definition: liblwgeom.h:355
double zmin
Definition: liblwgeom.h:358
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354

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

Here is the call graph for this function: