PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ST_GeometricMedian()

Datum ST_GeometricMedian ( PG_FUNCTION_ARGS  )

Definition at line 1192 of file lwgeom_functions_analytic.c.

1193 {
1194  GSERIALIZED* geom;
1195  GSERIALIZED* result;
1196  LWGEOM* input;
1197  LWPOINT* lwresult;
1198  static const double min_default_tolerance = 1e-8;
1199  double tolerance = min_default_tolerance;
1200  bool compute_tolerance_from_box;
1201  bool fail_if_not_converged;
1202  int max_iter;
1203 
1204  /* Read and validate our input arguments */
1205  if (PG_ARGISNULL(0))
1206  PG_RETURN_NULL();
1207 
1208  compute_tolerance_from_box = PG_ARGISNULL(1);
1209 
1210  if (!compute_tolerance_from_box)
1211  {
1212  tolerance = PG_GETARG_FLOAT8(1);
1213  if (tolerance < 0)
1214  {
1215  lwpgerror("Tolerance must be positive.");
1216  PG_RETURN_NULL();
1217  }
1218  }
1219 
1220  max_iter = PG_ARGISNULL(2) ? -1 : PG_GETARG_INT32(2);
1221  fail_if_not_converged = PG_ARGISNULL(3) ? LW_FALSE : PG_GETARG_BOOL(3);
1222 
1223  if (max_iter < 0)
1224  {
1225  lwpgerror("Maximum iterations must be positive.");
1226  PG_RETURN_NULL();
1227  }
1228 
1229  /* OK, inputs are valid. */
1230  geom = PG_GETARG_GSERIALIZED_P(0);
1231  input = lwgeom_from_gserialized(geom);
1232 
1233  if (compute_tolerance_from_box)
1234  {
1235  /* Compute a default tolerance based on the smallest dimension
1236  * of the geometry's bounding box.
1237  */
1238  static const double tolerance_coefficient = 1e-6;
1239  const GBOX* box = lwgeom_get_bbox(input);
1240 
1241  if (box)
1242  {
1243  double min_dim = FP_MIN(box->xmax - box->xmin, box->ymax - box->ymin);
1244  if (lwgeom_has_z(input))
1245  min_dim = FP_MIN(min_dim, box->zmax - box->zmin);
1246 
1247  /* Apply a lower bound to the computed default tolerance to
1248  * avoid a tolerance of zero in the case of collinear
1249  * points.
1250  */
1251  tolerance = FP_MAX(min_default_tolerance, tolerance_coefficient * min_dim);
1252  }
1253  }
1254 
1255  lwresult = lwgeom_median(input, tolerance, max_iter, fail_if_not_converged);
1256  lwgeom_free(input);
1257 
1258  if(!lwresult)
1259  {
1260  lwpgerror("Error computing geometric median.");
1261  PG_RETURN_NULL();
1262  }
1263 
1264  result = geometry_serialize(lwpoint_as_lwgeom(lwresult));
1265 
1266  PG_RETURN_POINTER(result);
1267 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define LW_FALSE
Definition: liblwgeom.h:77
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
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:734
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)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double ymax
Definition: liblwgeom.h:298
double zmax
Definition: liblwgeom.h:300
double xmax
Definition: liblwgeom.h:296
double zmin
Definition: liblwgeom.h:299
double ymin
Definition: liblwgeom.h:297
double xmin
Definition: liblwgeom.h:295

References FP_MAX, FP_MIN, 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.

Here is the call graph for this function: