PostGIS  2.4.9dev-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 159 of file lwgeom_median.c.

References init_guess(), iterate_3d(), lwalloc(), lwerror(), lwfree(), lwgeom_has_z(), lwmpoint_extract_points_3d(), lwpoint_construct_empty(), lwpoint_make2d(), lwpoint_make3dz(), LWGEOM::srid, LWMPOINT::srid, POINT3D::x, POINT3D::y, and POINT3D::z.

Referenced by lwgeom_median().

160 {
161  uint32_t npoints; /* we need to count this ourselves so we can exclude empties */
162  uint32_t i;
163  double delta = DBL_MAX;
164  double* distances;
165  POINT3D* points = lwmpoint_extract_points_3d(g, &npoints);
166  POINT3D median;
167 
168  if (npoints == 0)
169  {
170  lwfree(points);
171  return lwpoint_construct_empty(g->srid, 0, 0);
172  }
173 
174  median = init_guess(points, npoints);
175 
176  distances = lwalloc(npoints * sizeof(double));
177 
178  for (i = 0; i < max_iter && delta > tol; i++)
179  {
180  delta = iterate_3d(&median, points, npoints, distances);
181  }
182 
183  lwfree(points);
184  lwfree(distances);
185 
186  if (fail_if_not_converged && delta > tol)
187  {
188  lwerror("Median failed to converge within %g after %d iterations.", tol, max_iter);
189  return NULL;
190  }
191 
192  if (lwgeom_has_z((LWGEOM*) g))
193  {
194  return lwpoint_make3dz(g->srid, median.x, median.y, median.z);
195  }
196  else
197  {
198  return lwpoint_make2d(g->srid, median.x, median.y);
199  }
200 }
void lwfree(void *mem)
Definition: lwutil.c:244
double y
Definition: liblwgeom.h:340
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
double x
Definition: liblwgeom.h:340
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:885
int32_t srid
Definition: liblwgeom.h:399
double z
Definition: liblwgeom.h:340
unsigned int uint32_t
Definition: uthash.h:78
static double iterate_3d(POINT3D *curr, const POINT3D *points, uint32_t npoints, double *distances)
Definition: lwgeom_median.c:41
LWPOINT * lwpoint_make3dz(int srid, double x, double y, double z)
Definition: lwpoint.c:173
static POINT3D * lwmpoint_extract_points_3d(const LWMPOINT *g, uint32_t *ngeoms)
void * lwalloc(size_t size)
Definition: lwutil.c:229
int32_t srid
Definition: liblwgeom.h:467
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static POINT3D init_guess(const POINT3D *points, uint32_t npoints)
Here is the call graph for this function:
Here is the caller graph for this function: