PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwmpoint_median()

LWPOINT* lwmpoint_median ( const LWMPOINT g,
double  tol,
uint32_t  max_iter,
char  fail_if_not_converged 
)

Definition at line 232 of file lwgeom_median.c.

233 {
234  /* m ordinate is considered weight, if defined */
235  uint32_t npoints = 0; /* we need to count this ourselves so we can exclude empties and weightless points */
236  uint32_t i;
237  int input_empty = LW_TRUE;
238  POINT3D median;
239  POINT4D* points = lwmpoint_extract_points_4d(g, &npoints, &input_empty);
240 
241  /* input validation failed, error reported already */
242  if (points == NULL) return NULL;
243 
244  if (npoints == 0)
245  {
246  lwfree(points);
247  if (input_empty)
248  {
249  return lwpoint_construct_empty(g->srid, 0, 0);
250  }
251  else
252  {
253  lwerror("Median failed to find non-empty input points with positive weight.");
254  return NULL;
255  }
256  }
257 
258  median = init_guess(points, npoints);
259 
260  i = iterate_4d(&median, points, npoints, max_iter, tol);
261 
262  lwfree(points);
263 
264  if (fail_if_not_converged && i >= max_iter)
265  {
266  lwerror("Median failed to converge within %g after %d iterations.", tol, max_iter);
267  return NULL;
268  }
269 
270  if (lwgeom_has_z((LWGEOM*) g))
271  {
272  return lwpoint_make3dz(g->srid, median.x, median.y, median.z);
273  }
274  else
275  {
276  return lwpoint_make2d(g->srid, median.x, median.y);
277  }
278 }
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
void lwfree(void *mem)
Definition: lwutil.c:248
LWPOINT * lwpoint_make3dz(int32_t srid, double x, double y, double z)
Definition: lwpoint.c:173
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
POINT4D * lwmpoint_extract_points_4d(const LWMPOINT *g, uint32_t *npoints, int *input_empty)
static POINT3D init_guess(const POINT4D *points, uint32_t npoints)
static uint32_t iterate_4d(POINT3D *curr, const POINT4D *points, const uint32_t npoints, const uint32_t max_iter, const double tol)
Definition: lwgeom_median.c:48
int32_t srid
Definition: liblwgeom.h:460
int32_t srid
Definition: liblwgeom.h:534
double z
Definition: liblwgeom.h:402
double x
Definition: liblwgeom.h:402
double y
Definition: liblwgeom.h:402

References init_guess(), iterate_4d(), LW_TRUE, lwerror(), lwfree(), lwgeom_has_z(), lwmpoint_extract_points_4d(), lwpoint_construct_empty(), lwpoint_make2d(), lwpoint_make3dz(), LWMPOINT::srid, POINT3D::x, POINT3D::y, and POINT3D::z.

Referenced by lwgeom_median().

Here is the call graph for this function:
Here is the caller graph for this function: