PostGIS  2.5.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 231 of file lwgeom_median.c.

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

Referenced by lwgeom_median().

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