PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ ST_GeometricMedian()

Datum ST_GeometricMedian ( PG_FUNCTION_ARGS  )

Definition at line 1189 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(), PG_FUNCTION_INFO_V1(), ST_IsPolygonCW(), GBOX::xmax, GBOX::xmin, GBOX::ymax, GBOX::ymin, GBOX::zmax, and GBOX::zmin.

Referenced by ST_MinimumBoundingCircle().

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