PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ lwgeom_simplify_in_place()

void lwgeom_simplify_in_place ( LWGEOM geom,
double  epsilon,
int  preserve_collapsed 
)

Definition at line 1748 of file lwgeom.c.

References COLLECTIONTYPE, LWCOLLECTION::geoms, getPoint2d_cp(), LINETYPE, lwerror(), lwgeom_free(), lwgeom_is_empty(), lwtype_name(), POINTARRAY::maxpoints, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, p2d_same(), LWLINE::points, POINTTYPE, POLYGONTYPE, ptarray_copy_point(), ptarray_free(), ptarray_simplify_in_place(), LWPOLY::rings, and LWGEOM::type.

Referenced by lwgeom_clip_by_rect(), lwgeom_simplify(), and mvt_geom().

1749 {
1750  switch (geom->type)
1751  {
1752  /* No-op! Cannot simplify points */
1753  case POINTTYPE:
1754  return;
1755  case LINETYPE:
1756  {
1757  LWLINE *g = (LWLINE*)(geom);
1758  POINTARRAY *pa = g->points;
1759  ptarray_simplify_in_place(pa, epsilon, 2);
1760  /* Invalid output */
1761  if (pa->npoints == 1 && pa->maxpoints > 1)
1762  {
1763  /* Use first point as last point */
1764  if (preserve_collapsed)
1765  {
1766  pa->npoints = 2;
1767  ptarray_copy_point(pa, 0, 1);
1768  }
1769  /* Finish the collapse process */
1770  else
1771  {
1772  pa->npoints = 0;
1773  }
1774  }
1775  /* Duped output, force collapse */
1776  if (pa->npoints == 2 && !preserve_collapsed)
1777  {
1778  if (p2d_same(getPoint2d_cp(pa, 0), getPoint2d_cp(pa, 1)))
1779  pa->npoints = 0;
1780  }
1781  break;
1782  }
1783  case POLYGONTYPE:
1784  {
1785  uint32_t i, j = 0;
1786  LWPOLY *g = (LWPOLY*)(geom);
1787  for (i = 0; i < g->nrings; i++)
1788  {
1789  POINTARRAY *pa = g->rings[i];
1790  /* Only stop collapse on first ring */
1791  int minpoints = (preserve_collapsed && i == 0) ? 4 : 0;
1792  /* Skip zero'ed out rings */
1793  if(!pa)
1794  continue;
1795  ptarray_simplify_in_place(pa, epsilon, minpoints);
1796  /* Drop collapsed rings */
1797  if(pa->npoints < 4)
1798  {
1799  ptarray_free(pa);
1800  continue;
1801  }
1802  g->rings[j++] = pa;
1803  }
1804  /* Update ring count */
1805  g->nrings = j;
1806  break;
1807  }
1808  /* Can process all multi* types as generic collection */
1809  case MULTIPOINTTYPE:
1810  case MULTILINETYPE:
1811  case MULTIPOLYGONTYPE:
1812  case COLLECTIONTYPE:
1813  {
1814  uint32_t i, j = 0;
1815  LWCOLLECTION *col = (LWCOLLECTION*)geom;
1816  for (i = 0; i < col->ngeoms; i++)
1817  {
1818  LWGEOM *g = col->geoms[i];
1819  if (!g) continue;
1820  lwgeom_simplify_in_place(g, epsilon, preserve_collapsed);
1821  /* Drop zero'ed out geometries */
1822  if(lwgeom_is_empty(g))
1823  {
1824  lwgeom_free(g);
1825  continue;
1826  }
1827  col->geoms[j++] = g;
1828  }
1829  /* Update geometry count */
1830  col->ngeoms = j;
1831  break;
1832  }
1833  default:
1834  {
1835  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1836  break;
1837  }
1838  }
1839  return;
1840 }
#define LINETYPE
Definition: liblwgeom.h:85
#define POLYGONTYPE
Definition: liblwgeom.h:86
void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: lwgeom_api.c:450
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
uint32_t maxpoints
Definition: liblwgeom.h:374
uint32_t ngeoms
Definition: liblwgeom.h:509
uint32_t nrings
Definition: liblwgeom.h:457
void ptarray_simplify_in_place(POINTARRAY *pa, double epsilon, uint32_t minpts)
Definition: ptarray.c:1569
unsigned int uint32_t
Definition: uthash.h:78
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWGEOM ** geoms
Definition: liblwgeom.h:511
POINTARRAY ** rings
Definition: liblwgeom.h:459
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
void lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1748
uint8_t type
Definition: liblwgeom.h:398
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1144
#define MULTILINETYPE
Definition: liblwgeom.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
POINTARRAY * points
Definition: liblwgeom.h:424
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: