PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ lwgeom_remove_repeated_points_in_place()

int lwgeom_remove_repeated_points_in_place ( LWGEOM in,
double  tolerance 
)
extern

Definition at line 1667 of file lwgeom.c.

1668{
1669 int geometry_modified = LW_FALSE;
1670 switch (geom->type)
1671 {
1672 /* No-op! Cannot remove points */
1673 case POINTTYPE:
1674 case TRIANGLETYPE:
1675 return geometry_modified;
1676 case LINETYPE: {
1677 LWLINE *g = (LWLINE *)(geom);
1678 POINTARRAY *pa = g->points;
1679 uint32_t npoints = pa->npoints;
1681 geometry_modified = npoints != pa->npoints;
1682 /* Invalid input, discard the collapsed line */
1683 if (pa->npoints < 2)
1684 {
1685 pa->npoints = 0;
1686 geometry_modified = LW_TRUE;
1687 }
1688 break;
1689 }
1690 case POLYGONTYPE: {
1691 uint32_t j = 0;
1692 LWPOLY *g = (LWPOLY *)(geom);
1693 for (uint32_t i = 0; i < g->nrings; i++)
1694 {
1695 POINTARRAY *pa = g->rings[i];
1696 uint32_t npoints = pa->npoints;
1698 geometry_modified |= npoints != pa->npoints;
1699 /* Drop collapsed rings */
1700 if (pa->npoints < 4)
1701 {
1702 geometry_modified = LW_TRUE;
1703 ptarray_free(pa);
1704 continue;
1705 }
1706 g->rings[j++] = pa;
1707 }
1708 /* Update ring count */
1709 g->nrings = j;
1710 break;
1711 }
1712 case MULTIPOINTTYPE: {
1713 double tolsq = tolerance * tolerance;
1714 LWMPOINT *mpt = (LWMPOINT *)geom;
1715
1716 for (uint8_t dim = 0; dim < 2; dim++)
1717 {
1718 /* sort by y, then by x - this way the result is sorted by x */
1719 qsort(mpt->geoms, mpt->ngeoms, sizeof(LWPOINT *), dim ? cmp_point_x : cmp_point_y);
1720 for (uint32_t i = 0; i < mpt->ngeoms; i++)
1721 {
1722 if (!mpt->geoms[i])
1723 continue;
1724
1725 const POINT2D *pti = getPoint2d_cp(mpt->geoms[i]->point, 0);
1726 if (!pti) continue;
1727
1728 /* check upcoming points if they're within tolerance of current one */
1729 for (uint32_t j = i + 1; j < mpt->ngeoms; j++)
1730 {
1731 if (!mpt->geoms[j])
1732 continue;
1733
1734 const POINT2D *ptj = getPoint2d_cp(mpt->geoms[j]->point, 0);
1735 if (!ptj) continue;
1736
1737 /* check that the point is in the strip of tolerance around the point */
1738 if ((dim ? ptj->x - pti->x : ptj->y - pti->y) > tolerance)
1739 break;
1740
1741 /* remove any upcoming point that is within tolerance circle */
1742 if (distance2d_sqr_pt_pt(pti, ptj) <= tolsq)
1743 {
1744 lwpoint_free(mpt->geoms[j]);
1745 mpt->geoms[j] = NULL;
1746 geometry_modified = LW_TRUE;
1747 }
1748 }
1749 }
1750
1751 /* null out empties */
1752 for (uint32_t j = 0; j < mpt->ngeoms; j++)
1753 {
1754 if (mpt->geoms[j] && lwpoint_is_empty(mpt->geoms[j]))
1755 {
1756 lwpoint_free(mpt->geoms[j]);
1757 mpt->geoms[j] = NULL;
1758 geometry_modified = LW_TRUE;
1759 }
1760 }
1761
1762 /* compactify array of points */
1763 uint32_t i = 0;
1764 for (uint32_t j = 0; j < mpt->ngeoms; j++)
1765 if (mpt->geoms[j])
1766 mpt->geoms[i++] = mpt->geoms[j];
1767 mpt->ngeoms = i;
1768 }
1769
1770 break;
1771 }
1772
1773 case CIRCSTRINGTYPE:
1774 /* Dunno how to handle these, will return untouched */
1775 return geometry_modified;
1776
1777 /* Can process most multi* types as generic collection */
1778 case MULTILINETYPE:
1779 case MULTIPOLYGONTYPE:
1780 case TINTYPE:
1781 case COLLECTIONTYPE:
1782 /* Curve types we mostly ignore, but allow the linear */
1783 /* portions to be processed by recursing into them */
1784 case MULTICURVETYPE:
1785 case CURVEPOLYTYPE:
1786 case MULTISURFACETYPE:
1787 case COMPOUNDTYPE: {
1788 uint32_t i, j = 0;
1789 LWCOLLECTION *col = (LWCOLLECTION *)(geom);
1790 for (i = 0; i < col->ngeoms; i++)
1791 {
1792 LWGEOM *g = col->geoms[i];
1793 if (!g)
1794 continue;
1795 geometry_modified |= lwgeom_remove_repeated_points_in_place(g, tolerance);
1796 /* Drop zero'ed out geometries */
1797 if (lwgeom_is_empty(g))
1798 {
1799 lwgeom_free(g);
1800 continue;
1801 }
1802 col->geoms[j++] = g;
1803 }
1804 /* Update geometry count */
1805 col->ngeoms = j;
1806 break;
1807 }
1808 default: {
1809 lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1810 break;
1811 }
1812 }
1813
1814 if (geometry_modified)
1815 lwgeom_drop_bbox(geom);
1816 return geometry_modified;
1817}
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition lwutil.c:216
#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
void ptarray_free(POINTARRAY *pa)
Definition ptarray.c:327
#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:1662
int lwpoint_is_empty(const LWPOINT *point)
static int cmp_point_x(const void *pa, const void *pb)
Definition lwgeom.c:1633
int lwgeom_remove_repeated_points_in_place(LWGEOM *geom, double tolerance)
Definition lwgeom.c:1667
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:1650
void lwgeom_free(LWGEOM *lwgeom)
Definition lwgeom.c:1218
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition lwinline.h:33
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:199
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:97
uint32_t ngeoms
Definition liblwgeom.h:580
LWGEOM ** geoms
Definition liblwgeom.h:575
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 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(), lwpoint_is_empty(), 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: