PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ ptarray_grid_in_place()

void ptarray_grid_in_place ( POINTARRAY pa,
const gridspec grid 
)

Snap to grid.

Definition at line 1978 of file ptarray.c.

1979 {
1980  uint32_t i, j = 0;
1981  POINT4D *p, *p_out = NULL;
1982  int ndims = FLAGS_NDIMS(pa->flags);
1983  int has_z = FLAGS_GET_Z(pa->flags);
1984  int has_m = FLAGS_GET_M(pa->flags);
1985 
1986  LWDEBUGF(2, "%s called on %p", __func__, pa);
1987 
1988  for (i = 0; i < pa->npoints; i++)
1989  {
1990  /* Look straight into the abyss */
1991  p = (POINT4D*)(getPoint_internal(pa, i));
1992 
1993  if (grid->xsize > 0)
1994  {
1995  p->x = rint((p->x - grid->ipx)/grid->xsize) * grid->xsize + grid->ipx;
1996  }
1997 
1998  if (grid->ysize > 0)
1999  {
2000  p->y = rint((p->y - grid->ipy)/grid->ysize) * grid->ysize + grid->ipy;
2001  }
2002 
2003  /* Read and round this point */
2004  /* Z is always in third position */
2005  if (has_z)
2006  {
2007  if (grid->zsize > 0)
2008  p->z = rint((p->z - grid->ipz)/grid->zsize) * grid->zsize + grid->ipz;
2009  }
2010  /* M might be in 3rd or 4th position */
2011  if (has_m)
2012  {
2013  /* In POINT M, M is in 3rd position */
2014  if (grid->msize > 0 && !has_z)
2015  p->z = rint((p->z - grid->ipm)/grid->msize) * grid->msize + grid->ipm;
2016  /* In POINT ZM, M is in 4th position */
2017  if (grid->msize > 0 && has_z)
2018  p->m = rint((p->m - grid->ipm)/grid->msize) * grid->msize + grid->ipm;
2019  }
2020 
2021  /* Skip duplicates */
2022  if ( p_out && FP_EQUALS(p_out->x, p->x) && FP_EQUALS(p_out->y, p->y)
2023  && (ndims > 2 ? FP_EQUALS(p_out->z, p->z) : 1)
2024  && (ndims > 3 ? FP_EQUALS(p_out->m, p->m) : 1) )
2025  {
2026  continue;
2027  }
2028 
2029  /* Write rounded values into the next available point */
2030  p_out = (POINT4D*)(getPoint_internal(pa, j++));
2031  p_out->x = p->x;
2032  p_out->y = p->y;
2033  if (ndims > 2)
2034  p_out->z = p->z;
2035  if (ndims > 3)
2036  p_out->m = p->m;
2037  }
2038 
2039  /* Update output ptarray length */
2040  pa->npoints = j;
2041  return;
2042 }
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:179
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:193
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:180
#define FP_EQUALS(A, B)
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: lwinline.h:67
double m
Definition: liblwgeom.h:400
double x
Definition: liblwgeom.h:400
double z
Definition: liblwgeom.h:400
double y
Definition: liblwgeom.h:400
lwflags_t flags
Definition: liblwgeom.h:417
uint32_t npoints
Definition: liblwgeom.h:413
double ipm
Definition: liblwgeom.h:1345
double zsize
Definition: liblwgeom.h:1348
double ysize
Definition: liblwgeom.h:1347
double xsize
Definition: liblwgeom.h:1346
double ipx
Definition: liblwgeom.h:1342
double msize
Definition: liblwgeom.h:1349
double ipy
Definition: liblwgeom.h:1343
double ipz
Definition: liblwgeom.h:1344

References POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, FLAGS_NDIMS, FP_EQUALS, getPoint_internal(), gridspec_t::ipm, gridspec_t::ipx, gridspec_t::ipy, gridspec_t::ipz, LWDEBUGF, POINT4D::m, gridspec_t::msize, POINTARRAY::npoints, POINT4D::x, gridspec_t::xsize, POINT4D::y, gridspec_t::ysize, POINT4D::z, and gridspec_t::zsize.

Referenced by lwgeom_grid_in_place().

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