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 2106 of file ptarray.c.

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