PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwgeom_simplify_in_place()

void lwgeom_simplify_in_place ( LWGEOM igeom,
double  dist,
int  preserve_collapsed 
)

Definition at line 1750 of file lwgeom.c.

1751 {
1752  switch (geom->type)
1753  {
1754  /* No-op! Cannot simplify points or triangles */
1755  case POINTTYPE:
1756  return;
1757  case TRIANGLETYPE:
1758  {
1759  if (preserve_collapsed)
1760  return;
1761  LWTRIANGLE *t = lwgeom_as_lwtriangle(geom);
1762  POINTARRAY *pa = t->points;
1763  ptarray_simplify_in_place(pa, epsilon, 0);
1764  if (pa->npoints < 3)
1765  {
1766  pa->npoints = 0;
1767  }
1768  }
1769  case LINETYPE:
1770  {
1771  LWLINE *g = (LWLINE*)(geom);
1772  POINTARRAY *pa = g->points;
1773  ptarray_simplify_in_place(pa, epsilon, 2);
1774  /* Invalid output */
1775  if (pa->npoints == 1 && pa->maxpoints > 1)
1776  {
1777  /* Use first point as last point */
1778  if (preserve_collapsed)
1779  {
1780  pa->npoints = 2;
1781  ptarray_copy_point(pa, 0, 1);
1782  }
1783  /* Finish the collapse process */
1784  else
1785  {
1786  pa->npoints = 0;
1787  }
1788  }
1789  /* Duped output, force collapse */
1790  if (pa->npoints == 2 && !preserve_collapsed)
1791  {
1792  if (p2d_same(getPoint2d_cp(pa, 0), getPoint2d_cp(pa, 1)))
1793  pa->npoints = 0;
1794  }
1795  break;
1796  }
1797  case POLYGONTYPE:
1798  {
1799  uint32_t i, j = 0;
1800  LWPOLY *g = (LWPOLY*)(geom);
1801  for (i = 0; i < g->nrings; i++)
1802  {
1803  POINTARRAY *pa = g->rings[i];
1804  /* Only stop collapse on first ring */
1805  int minpoints = (preserve_collapsed && i == 0) ? 4 : 0;
1806  /* Skip zero'ed out rings */
1807  if(!pa)
1808  continue;
1809  ptarray_simplify_in_place(pa, epsilon, minpoints);
1810  /* Drop collapsed rings */
1811  if(pa->npoints < 4)
1812  {
1813  ptarray_free(pa);
1814  continue;
1815  }
1816  g->rings[j++] = pa;
1817  }
1818  /* Update ring count */
1819  g->nrings = j;
1820  break;
1821  }
1822  /* Can process all multi* types as generic collection */
1823  case MULTIPOINTTYPE:
1824  case MULTILINETYPE:
1825  case MULTIPOLYGONTYPE:
1826  case TINTYPE:
1827  case COLLECTIONTYPE:
1828  {
1829  uint32_t i, j = 0;
1830  LWCOLLECTION *col = (LWCOLLECTION*)geom;
1831  for (i = 0; i < col->ngeoms; i++)
1832  {
1833  LWGEOM *g = col->geoms[i];
1834  if (!g) continue;
1835  lwgeom_simplify_in_place(g, epsilon, preserve_collapsed);
1836  /* Drop zero'ed out geometries */
1837  if(lwgeom_is_empty(g))
1838  {
1839  lwgeom_free(g);
1840  continue;
1841  }
1842  col->geoms[j++] = g;
1843  }
1844  /* Update geometry count */
1845  col->ngeoms = j;
1846  break;
1847  }
1848  default:
1849  {
1850  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1851  break;
1852  }
1853  }
1854  return;
1855 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
#define MULTILINETYPE
Definition: liblwgeom.h:89
#define LINETYPE
Definition: liblwgeom.h:86
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
#define TINTYPE
Definition: liblwgeom.h:99
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
#define POLYGONTYPE
Definition: liblwgeom.h:87
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
#define TRIANGLETYPE
Definition: liblwgeom.h:98
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:374
void ptarray_simplify_in_place(POINTARRAY *pa, double epsilon, uint32_t minpts)
Definition: ptarray.c:1576
void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: lwgeom_api.c:460
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
void lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1750
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
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
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1144
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
uint32_t ngeoms
Definition: liblwgeom.h:510
LWGEOM ** geoms
Definition: liblwgeom.h:512
POINTARRAY * points
Definition: liblwgeom.h:425
POINTARRAY ** rings
Definition: liblwgeom.h:460
uint32_t nrings
Definition: liblwgeom.h:458
POINTARRAY * points
Definition: liblwgeom.h:436
uint32_t maxpoints
Definition: liblwgeom.h:375
uint32_t npoints
Definition: liblwgeom.h:374
unsigned int uint32_t
Definition: uthash.h:78

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

Referenced by lwgeom_simplify(), lwgeom_simplify_in_place(), lwgeom_subdivide_recursive(), and mvt_geom().

Here is the call graph for this function:
Here is the caller graph for this function: