PostGIS  2.1.10dev-r@@SVN_REVISION@@
LWPOLY * lwpoly_grid ( LWPOLY poly,
gridspec grid 
)

Definition at line 377 of file lwgeom_functions_analytic.c.

References getPoint2d_p(), lwpoly_construct(), POINTARRAY::npoints, LWPOLY::nrings, ptarray_grid(), LWPOLY::rings, SAMEPOINT, LWPOLY::srid, gridspec_t::xsize, and gridspec_t::ysize.

Referenced by lwgeom_grid().

378 {
379  LWPOLY *opoly;
380  int ri;
381  POINTARRAY **newrings = NULL;
382  int nrings = 0;
383 #if 0
384  /*
385  * TODO: control this assertion
386  * it is assumed that, since the grid size will be a pixel,
387  * a visible ring should show at least a white pixel inside,
388  * thus, for a square, that would be grid_xsize*grid_ysize
389  */
390  double minvisiblearea = grid->xsize * grid->ysize;
391 #endif
392 
393  nrings = 0;
394 
395  POSTGIS_DEBUGF(3, "grid_polygon3d: applying grid to polygon with %d rings",
396  poly->nrings);
397 
398  for (ri=0; ri<poly->nrings; ri++)
399  {
400  POINTARRAY *ring = poly->rings[ri];
401  POINTARRAY *newring;
402 
403 #if POSTGIS_DEBUG_LEVEL >= 4
404  POINT2D p1, p2;
405  getPoint2d_p(ring, 0, &p1);
406  getPoint2d_p(ring, ring->npoints-1, &p2);
407  if ( ! SAMEPOINT(&p1, &p2) )
408  POSTGIS_DEBUG(4, "Before gridding: first point != last point");
409 #endif
410 
411  newring = ptarray_grid(ring, grid);
412 
413  /* Skip ring if not composed by at least 4 pts (3 segments) */
414  if ( newring->npoints < 4 )
415  {
416  pfree(newring);
417 
418  POSTGIS_DEBUGF(3, "grid_polygon3d: ring%d skipped ( <4 pts )", ri);
419 
420  if ( ri ) continue;
421  else break; /* this is the external ring, no need to work on holes */
422  }
423 
424 #if POSTGIS_DEBUG_LEVEL >= 4
425  getPoint2d_p(newring, 0, &p1);
426  getPoint2d_p(newring, newring->npoints-1, &p2);
427  if ( ! SAMEPOINT(&p1, &p2) )
428  POSTGIS_DEBUG(4, "After gridding: first point != last point");
429 #endif
430 
431  POSTGIS_DEBUGF(3, "grid_polygon3d: ring%d simplified from %d to %d points", ri,
432  ring->npoints, newring->npoints);
433 
434  /*
435  * Add ring to simplified ring array
436  * (TODO: dinamic allocation of pts_per_ring)
437  */
438  if ( ! nrings )
439  {
440  newrings = palloc(sizeof(POINTARRAY *));
441  }
442  else
443  {
444  newrings = repalloc(newrings, sizeof(POINTARRAY *)*(nrings+1));
445  }
446  if ( ! newrings )
447  {
448  elog(ERROR, "Out of virtual memory");
449  return NULL;
450  }
451  newrings[nrings++] = newring;
452  }
453 
454  POSTGIS_DEBUGF(3, "grid_polygon3d: simplified polygon with %d rings", nrings);
455 
456  if ( ! nrings ) return NULL;
457 
458  opoly = lwpoly_construct(poly->srid, NULL, nrings, newrings);
459  return opoly;
460 }
POINTARRAY * ptarray_grid(POINTARRAY *pa, gridspec *grid)
int npoints
Definition: liblwgeom.h:327
#define SAMEPOINT(a, b)
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
POINTARRAY ** rings
Definition: liblwgeom.h:413
int nrings
Definition: liblwgeom.h:411
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:434
int32_t srid
Definition: liblwgeom.h:410

Here is the call graph for this function:

Here is the caller graph for this function: