PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwgeom_remove_repeated_points_in_place()

int lwgeom_remove_repeated_points_in_place ( LWGEOM geom,
double  tolerance 
)

Definition at line 1594 of file lwgeom.c.

1595 {
1596  int geometry_modified = LW_FALSE;
1597  switch (geom->type)
1598  {
1599  /* No-op! Cannot remove points */
1600  case POINTTYPE:
1601  case TRIANGLETYPE:
1602  return geometry_modified;
1603  case LINETYPE: {
1604  LWLINE *g = (LWLINE *)(geom);
1605  POINTARRAY *pa = g->points;
1606  uint32_t npoints = pa->npoints;
1607  ptarray_remove_repeated_points_in_place(pa, tolerance, 2);
1608  geometry_modified = npoints != pa->npoints;
1609  /* Invalid input, discard the collapsed line */
1610  if (pa->npoints < 2)
1611  {
1612  pa->npoints = 0;
1613  geometry_modified = LW_TRUE;
1614  }
1615  break;
1616  }
1617  case POLYGONTYPE: {
1618  uint32_t j = 0;
1619  LWPOLY *g = (LWPOLY *)(geom);
1620  for (uint32_t i = 0; i < g->nrings; i++)
1621  {
1622  POINTARRAY *pa = g->rings[i];
1623  uint32_t npoints = pa->npoints;
1624  ptarray_remove_repeated_points_in_place(pa, tolerance, 4);
1625  geometry_modified |= npoints != pa->npoints;
1626  /* Drop collapsed rings */
1627  if (pa->npoints < 4)
1628  {
1629  geometry_modified = LW_TRUE;
1630  ptarray_free(pa);
1631  continue;
1632  }
1633  g->rings[j++] = pa;
1634  }
1635  /* Update ring count */
1636  g->nrings = j;
1637  break;
1638  }
1639  case MULTIPOINTTYPE: {
1640  double tolsq = tolerance * tolerance;
1641  LWMPOINT *mpt = (LWMPOINT *)geom;
1642 
1643  for (uint8_t dim = 0; dim < 2; dim++)
1644  {
1645  /* sort by y, then by x - this way the result is sorted by x */
1646  qsort(mpt->geoms, mpt->ngeoms, sizeof(LWPOINT *), dim ? cmp_point_x : cmp_point_y);
1647  for (uint32_t i = 0; i < mpt->ngeoms; i++)
1648  {
1649  if (!mpt->geoms[i])
1650  continue;
1651 
1652  const POINT2D *pti = getPoint2d_cp(mpt->geoms[i]->point, 0);
1653 
1654  /* check upcoming points if they're within tolerance of current one */
1655  for (uint32_t j = i + 1; j < mpt->ngeoms; j++)
1656  {
1657  if (!mpt->geoms[j])
1658  continue;
1659 
1660  const POINT2D *ptj = getPoint2d_cp(mpt->geoms[j]->point, 0);
1661 
1662  /* check that the point is in the strip of tolerance around the point */
1663  if ((dim ? ptj->x - pti->x : ptj->y - pti->y) > tolerance)
1664  break;
1665 
1666  /* remove any upcoming point that is within tolerance circle */
1667  if (distance2d_sqr_pt_pt(pti, ptj) <= tolsq)
1668  {
1669  lwpoint_free(mpt->geoms[j]);
1670  mpt->geoms[j] = NULL;
1671  geometry_modified = LW_TRUE;
1672  }
1673  }
1674  }
1675 
1676  /* compactify array of points */
1677  uint32_t i = 0;
1678  for (uint32_t j = 0; j < mpt->ngeoms; j++)
1679  if (mpt->geoms[j])
1680  mpt->geoms[i++] = mpt->geoms[j];
1681  mpt->ngeoms = i;
1682  }
1683 
1684  break;
1685  }
1686 
1687  case CIRCSTRINGTYPE:
1688  /* Dunno how to handle these, will return untouched */
1689  return geometry_modified;
1690 
1691  /* Can process most multi* types as generic collection */
1692  case MULTILINETYPE:
1693  case MULTIPOLYGONTYPE:
1694  case TINTYPE:
1695  case COLLECTIONTYPE:
1696  /* Curve types we mostly ignore, but allow the linear */
1697  /* portions to be processed by recursing into them */
1698  case MULTICURVETYPE:
1699  case CURVEPOLYTYPE:
1700  case MULTISURFACETYPE:
1701  case COMPOUNDTYPE: {
1702  uint32_t i, j = 0;
1703  LWCOLLECTION *col = (LWCOLLECTION *)(geom);
1704  for (i = 0; i < col->ngeoms; i++)
1705  {
1706  LWGEOM *g = col->geoms[i];
1707  if (!g)
1708  continue;
1709  geometry_modified |= lwgeom_remove_repeated_points_in_place(g, tolerance);
1710  /* Drop zero'ed out geometries */
1711  if (lwgeom_is_empty(g))
1712  {
1713  lwgeom_free(g);
1714  continue;
1715  }
1716  col->geoms[j++] = g;
1717  }
1718  /* Update geometry count */
1719  col->ngeoms = j;
1720  break;
1721  }
1722  default: {
1723  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1724  break;
1725  }
1726  }
1727 
1728  if (geometry_modified)
1729  lwgeom_drop_bbox(geom);
1730  return geometry_modified;
1731 }
#define LW_FALSE
Definition: liblwgeom.h:94
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
#define COMPOUNDTYPE
Definition: liblwgeom.h:110
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define CURVEPOLYTYPE
Definition: liblwgeom.h:111
#define MULTILINETYPE
Definition: liblwgeom.h:106
#define MULTISURFACETYPE
Definition: liblwgeom.h:113
#define LINETYPE
Definition: liblwgeom.h:103
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
#define TINTYPE
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:109
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:319
#define MULTICURVETYPE
Definition: liblwgeom.h:112
#define TRIANGLETYPE
Definition: liblwgeom.h:115
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
void ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, uint32_t min_points)
Definition: ptarray.c:1535
static int cmp_point_x(const void *pa, const void *pb)
Definition: lwgeom.c:1570
int lwgeom_remove_repeated_points_in_place(LWGEOM *geom, double tolerance)
Definition: lwgeom.c:1594
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:682
static int cmp_point_y(const void *pa, const void *pb)
Definition: lwgeom.c:1582
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1155
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
uint8_t type
Definition: liblwgeom.h:462
POINTARRAY * points
Definition: liblwgeom.h:483
uint32_t ngeoms
Definition: liblwgeom.h:538
LWPOINT ** geoms
Definition: liblwgeom.h:533
POINTARRAY * point
Definition: liblwgeom.h:471
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References CIRCSTRINGTYPE, cmp_point_x(), cmp_point_y(), COLLECTIONTYPE, COMPOUNDTYPE, CURVEPOLYTYPE, distance2d_sqr_pt_pt(), LWMPOINT::geoms, LWCOLLECTION::geoms, getPoint2d_cp(), LINETYPE, LW_FALSE, LW_TRUE, lwerror(), lwgeom_drop_bbox(), lwgeom_free(), lwgeom_is_empty(), lwgeom_remove_repeated_points_in_place(), lwpoint_free(), lwtype_name(), MULTICURVETYPE, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, MULTISURFACETYPE, LWMPOINT::ngeoms, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, LWPOINT::point, LWLINE::points, POINTTYPE, POLYGONTYPE, ptarray_free(), ptarray_remove_repeated_points_in_place(), LWPOLY::rings, TINTYPE, TRIANGLETYPE, LWGEOM::type, POINT2D::x, and POINT2D::y.

Referenced by lwgeom_remove_repeated_points(), lwgeom_remove_repeated_points_in_place(), mvt_geom(), ST_RemoveRepeatedPoints(), and test_lwgeom_remove_repeated_points().

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