PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ptarray_grid_in_place()

void ptarray_grid_in_place ( POINTARRAY pa,
gridspec grid 
)

Snap to grid.

Definition at line 2099 of file ptarray.c.

2100 {
2101  uint32_t j = 0;
2102  POINT4D *p, *p_out = NULL;
2103  double x, y, z = 0, m = 0;
2104  uint32_t ndims = FLAGS_NDIMS(pa->flags);
2105  uint32_t has_z = FLAGS_GET_Z(pa->flags);
2106  uint32_t has_m = FLAGS_GET_M(pa->flags);
2107 
2108  for (uint32_t i = 0; i < pa->npoints; i++)
2109  {
2110  /* Look straight into the abyss */
2111  p = (POINT4D *)(getPoint_internal(pa, i));
2112  x = p->x;
2113  y = p->y;
2114  if (ndims > 2)
2115  z = p->z;
2116  if (ndims > 3)
2117  m = p->m;
2118 
2119  /*
2120  * See https://github.com/libgeos/geos/pull/956
2121  * We use scale for rounding when gridsize is < 1 and
2122  * gridsize for rounding when scale < 1.
2123  */
2124  if (grid->xsize > 0) {
2125  if (grid->xsize < 1)
2126  x = rint((x - grid->ipx) * grid->xscale) / grid->xscale + grid->ipx;
2127  else
2128  x = rint((x - grid->ipx) / grid->xsize) * grid->xsize + grid->ipx;
2129  }
2130 
2131  if (grid->ysize > 0) {
2132  if (grid->ysize < 1)
2133  y = rint((y - grid->ipy) * grid->yscale) / grid->yscale + grid->ipy;
2134  else
2135  y = rint((y - grid->ipy) / grid->ysize) * grid->ysize + grid->ipy;
2136  }
2137 
2138  /* Read and round this point */
2139  /* Z is always in third position */
2140  if (has_z && grid->zsize > 0)
2141  z = rint((z - grid->ipz) / grid->zsize) * grid->zsize + grid->ipz;
2142 
2143  /* M might be in 3rd or 4th position */
2144  if (has_m && grid->msize > 0)
2145  {
2146  /* In POINT ZM, M is in 4th position, in POINT M, M is in 3rd position which is Z in POINT4D */
2147  if (has_z)
2148  m = rint((m - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2149  else
2150  z = rint((z - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2151  }
2152 
2153  /* Skip duplicates */
2154  if (p_out &&
2155  p_out->x == x &&
2156  p_out->y == y &&
2157  (ndims > 2 ? p_out->z == z : 1) &&
2158  (ndims > 3 ? p_out->m == m : 1))
2159  {
2160  continue;
2161  }
2162 
2163  /* Write rounded values into the next available point */
2164  p_out = (POINT4D *)(getPoint_internal(pa, j++));
2165  p_out->x = x;
2166  p_out->y = y;
2167  if (ndims > 2)
2168  p_out->z = z;
2169  if (ndims > 3)
2170  p_out->m = m;
2171  }
2172 
2173  /* Update output ptarray length */
2174  pa->npoints = j;
2175  return;
2176 }
#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:75
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 xscale
Definition: liblwgeom.h:1407
double ipm
Definition: liblwgeom.h:1402
double zsize
Definition: liblwgeom.h:1405
double ysize
Definition: liblwgeom.h:1404
double xsize
Definition: liblwgeom.h:1403
double yscale
Definition: liblwgeom.h:1408
double ipx
Definition: liblwgeom.h:1399
double msize
Definition: liblwgeom.h:1406
double ipy
Definition: liblwgeom.h:1400
double ipz
Definition: liblwgeom.h:1401

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::xscale, gridspec_t::xsize, POINT4D::y, pixval::y, gridspec_t::yscale, 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: