PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ptarray_grid_in_place()

void ptarray_grid_in_place ( POINTARRAY pa,
const gridspec grid 
)

Snap to grid.

Definition at line 2091 of file ptarray.c.

2092 {
2093  uint32_t j = 0;
2094  POINT4D *p, *p_out = NULL;
2095  double x, y, z = 0, m = 0;
2096  uint32_t ndims = FLAGS_NDIMS(pa->flags);
2097  uint32_t has_z = FLAGS_GET_Z(pa->flags);
2098  uint32_t has_m = FLAGS_GET_M(pa->flags);
2099 
2100  for (uint32_t i = 0; i < pa->npoints; i++)
2101  {
2102  /* Look straight into the abyss */
2103  p = (POINT4D *)(getPoint_internal(pa, i));
2104  x = p->x;
2105  y = p->y;
2106  if (ndims > 2)
2107  z = p->z;
2108  if (ndims > 3)
2109  m = p->m;
2110 
2111  if (grid->xsize > 0)
2112  x = rint((x - grid->ipx) / grid->xsize) * grid->xsize + grid->ipx;
2113 
2114  if (grid->ysize > 0)
2115  y = rint((y - grid->ipy) / grid->ysize) * grid->ysize + grid->ipy;
2116 
2117  /* Read and round this point */
2118  /* Z is always in third position */
2119  if (has_z && grid->zsize > 0)
2120  z = rint((z - grid->ipz) / grid->zsize) * grid->zsize + grid->ipz;
2121 
2122  /* M might be in 3rd or 4th position */
2123  if (has_m && grid->msize > 0)
2124  {
2125  /* In POINT ZM, M is in 4th position, in POINT M, M is in 3rd position which is Z in POINT4D */
2126  if (has_z)
2127  m = rint((m - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2128  else
2129  z = rint((z - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2130  }
2131 
2132  /* Skip duplicates */
2133  if (p_out &&
2134  p_out->x == x &&
2135  p_out->y == y &&
2136  (ndims > 2 ? p_out->z == z : 1) &&
2137  (ndims > 3 ? p_out->m == m : 1))
2138  {
2139  continue;
2140  }
2141 
2142  /* Write rounded values into the next available point */
2143  p_out = (POINT4D *)(getPoint_internal(pa, j++));
2144  p_out->x = x;
2145  p_out->y = y;
2146  if (ndims > 2)
2147  p_out->z = z;
2148  if (ndims > 3)
2149  p_out->m = m;
2150  }
2151 
2152  /* Update output ptarray length */
2153  pa->npoints = j;
2154  return;
2155 }
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:179
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:166
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: lwinline.h:77
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t npoints
Definition: liblwgeom.h:427
double ipm
Definition: liblwgeom.h:1379
double zsize
Definition: liblwgeom.h:1382
double ysize
Definition: liblwgeom.h:1381
double xsize
Definition: liblwgeom.h:1380
double ipx
Definition: liblwgeom.h:1376
double msize
Definition: liblwgeom.h:1383
double ipy
Definition: liblwgeom.h:1377
double ipz
Definition: liblwgeom.h:1378

References POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, FLAGS_NDIMS, getPoint_internal(), gridspec_t::ipm, gridspec_t::ipx, gridspec_t::ipy, gridspec_t::ipz, POINT4D::m, gridspec_t::msize, POINTARRAY::npoints, POINT4D::x, pixval::x, gridspec_t::xsize, POINT4D::y, pixval::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: