PostGIS  2.2.7dev-r@@SVN_REVISION@@
LWPOLY* lwpoly_simplify ( const LWPOLY ipoly,
double  dist,
int  preserve_collapsed 
)

Definition at line 351 of file lwpoly.c.

References LWPOLY::flags, FLAGS_GET_M, FLAGS_GET_Z, LW_FAILURE, LWDEBUGF, lwpoly_add_ring(), lwpoly_construct_empty(), lwpoly_free(), lwpoly_is_empty(), POINTARRAY::npoints, LWPOLY::nrings, ptarray_free(), ptarray_simplify(), LWPOLY::rings, LWPOLY::srid, and LWPOLY::type.

Referenced by lwgeom_simplify().

352 {
353  int i;
354  LWPOLY *opoly = lwpoly_construct_empty(ipoly->srid, FLAGS_GET_Z(ipoly->flags), FLAGS_GET_M(ipoly->flags));
355 
356  LWDEBUGF(2, "%s: simplifying polygon with %d rings", __func__, ipoly->nrings);
357 
358  if ( lwpoly_is_empty(ipoly) )
359  {
360  lwpoly_free(opoly);
361  return NULL;
362  }
363 
364  for ( i = 0; i < ipoly->nrings; i++ )
365  {
366  POINTARRAY *opts;
367  int minvertices = 0;
368 
369  /* We'll still let holes collapse, but if we're preserving */
370  /* and this is a shell, we ensure it is kept */
371  if ( preserve_collapsed && i == 0 )
372  minvertices = 4;
373 
374  opts = ptarray_simplify(ipoly->rings[i], dist, minvertices);
375 
376  LWDEBUGF(3, "ring%d simplified from %d to %d points", i, ipoly->rings[i]->npoints, opts->npoints);
377 
378  /* Less points than are needed to form a closed ring, we can't use this */
379  if ( opts->npoints < 4 )
380  {
381  LWDEBUGF(3, "ring%d skipped (% pts)", i, opts->npoints);
382  ptarray_free(opts);
383  if ( i ) continue;
384  else break; /* Don't scan holes if shell is collapsed */
385  }
386 
387  /* Add ring to simplified polygon */
388  if( lwpoly_add_ring(opoly, opts) == LW_FAILURE )
389  {
390  lwpoly_free(opoly);
391  return NULL;
392  }
393  }
394 
395  LWDEBUGF(3, "simplified polygon with %d rings", ipoly->nrings);
396  opoly->type = ipoly->type;
397 
398  if( lwpoly_is_empty(opoly) )
399  {
400  lwpoly_free(opoly);
401  return NULL;
402  }
403 
404  return opoly;
405 }
int npoints
Definition: liblwgeom.h:355
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:330
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring to a polygon.
Definition: lwpoly.c:154
#define LW_FAILURE
Definition: liblwgeom.h:64
uint8_t type
Definition: liblwgeom.h:435
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:66
POINTARRAY ** rings
Definition: liblwgeom.h:441
int nrings
Definition: liblwgeom.h:439
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
POINTARRAY * ptarray_simplify(POINTARRAY *inpts, double epsilon, unsigned int minpts)
Definition: ptarray.c:1554
int32_t srid
Definition: liblwgeom.h:438
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:125
uint8_t flags
Definition: liblwgeom.h:436
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
int lwpoly_is_empty(const LWPOLY *poly)
Definition: lwpoly.c:332

Here is the call graph for this function:

Here is the caller graph for this function: