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

Definition at line 422 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().

423 {
424  int i;
425  LWPOLY *opoly = lwpoly_construct_empty(ipoly->srid, FLAGS_GET_Z(ipoly->flags), FLAGS_GET_M(ipoly->flags));
426 
427  LWDEBUGF(2, "%s: simplifying polygon with %d rings", __func__, ipoly->nrings);
428 
429  if ( lwpoly_is_empty(ipoly) )
430  {
431  lwpoly_free(opoly);
432  return NULL;
433  }
434 
435  for ( i = 0; i < ipoly->nrings; i++ )
436  {
437  POINTARRAY *opts;
438  int minvertices = 0;
439 
440  /* We'll still let holes collapse, but if we're preserving */
441  /* and this is a shell, we ensure it is kept */
442  if ( preserve_collapsed && i == 0 )
443  minvertices = 4;
444 
445  opts = ptarray_simplify(ipoly->rings[i], dist, minvertices);
446 
447  LWDEBUGF(3, "ring%d simplified from %d to %d points", i, ipoly->rings[i]->npoints, opts->npoints);
448 
449  /* Less points than are needed to form a closed ring, we can't use this */
450  if ( opts->npoints < 4 )
451  {
452  LWDEBUGF(3, "ring%d skipped (% pts)", i, opts->npoints);
453  ptarray_free(opts);
454  if ( i ) continue;
455  else break; /* Don't scan holes if shell is collapsed */
456  }
457 
458  /* Add ring to simplified polygon */
459  if( lwpoly_add_ring(opoly, opts) == LW_FAILURE )
460  {
461  lwpoly_free(opoly);
462  return NULL;
463  }
464  }
465 
466  LWDEBUGF(3, "simplified polygon with %d rings", ipoly->nrings);
467  opoly->type = ipoly->type;
468 
469  if( lwpoly_is_empty(opoly) )
470  {
471  lwpoly_free(opoly);
472  return NULL;
473  }
474 
475  return opoly;
476 }
int npoints
Definition: liblwgeom.h:370
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:225
#define LW_FAILURE
Definition: liblwgeom.h:78
uint8_t type
Definition: liblwgeom.h:450
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:137
POINTARRAY ** rings
Definition: liblwgeom.h:456
int nrings
Definition: liblwgeom.h:454
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
POINTARRAY * ptarray_simplify(POINTARRAY *inpts, double epsilon, unsigned int minpts)
Definition: ptarray.c:1554
int32_t srid
Definition: liblwgeom.h:453
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:150
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
uint8_t flags
Definition: liblwgeom.h:451
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
int lwpoly_is_empty(const LWPOLY *poly)
Definition: lwpoly.c:403

Here is the call graph for this function:

Here is the caller graph for this function: