PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwgeom_simplify_in_place()

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

Definition at line 1851 of file lwgeom.c.

1852 {
1853  int modified = LW_FALSE;
1854  switch (geom->type)
1855  {
1856  /* No-op! Cannot simplify points or triangles */
1857  case POINTTYPE:
1858  return modified;
1859  case TRIANGLETYPE:
1860  {
1861  if (preserve_collapsed)
1862  return modified;
1863  LWTRIANGLE *t = lwgeom_as_lwtriangle(geom);
1864  POINTARRAY *pa = t->points;
1865  ptarray_simplify_in_place(pa, epsilon, 0);
1866  if (pa->npoints < 3)
1867  {
1868  pa->npoints = 0;
1869  modified = LW_TRUE;
1870  }
1871  break;
1872  }
1873  case LINETYPE:
1874  {
1875  LWLINE *g = (LWLINE*)(geom);
1876  POINTARRAY *pa = g->points;
1877  uint32_t in_npoints = pa->npoints;
1878  ptarray_simplify_in_place(pa, epsilon, 2);
1879  modified = in_npoints != pa->npoints;
1880  /* Invalid output */
1881  if (pa->npoints == 1 && pa->maxpoints > 1)
1882  {
1883  /* Use first point as last point */
1884  if (preserve_collapsed)
1885  {
1886  pa->npoints = 2;
1887  ptarray_copy_point(pa, 0, 1);
1888  }
1889  /* Finish the collapse process */
1890  else
1891  {
1892  pa->npoints = 0;
1893  }
1894  }
1895  /* Duped output, force collapse */
1896  if (pa->npoints == 2 && !preserve_collapsed)
1897  {
1898  if (p2d_same(getPoint2d_cp(pa, 0), getPoint2d_cp(pa, 1)))
1899  pa->npoints = 0;
1900  }
1901  break;
1902  }
1903  case POLYGONTYPE:
1904  {
1905  uint32_t i, j = 0;
1906  LWPOLY *g = (LWPOLY*)(geom);
1907  for (i = 0; i < g->nrings; i++)
1908  {
1909  POINTARRAY *pa = g->rings[i];
1910  /* Only stop collapse on first ring */
1911  int minpoints = (preserve_collapsed && i == 0) ? 4 : 0;
1912  /* Skip zero'ed out rings */
1913  if(!pa)
1914  continue;
1915  uint32_t in_npoints = pa->npoints;
1916  ptarray_simplify_in_place(pa, epsilon, minpoints);
1917  modified |= in_npoints != pa->npoints;
1918  /* Drop collapsed rings */
1919  if(pa->npoints < 4)
1920  {
1921  if (i == 0)
1922  {
1923  /* If the outer ring is dropped, all can be dropped */
1924  for (i = 0; i < g->nrings; i++)
1925  {
1926  pa = g->rings[i];
1927  ptarray_free(pa);
1928  }
1929  break;
1930  }
1931  else
1932  {
1933  /* Drop this inner ring only */
1934  ptarray_free(pa);
1935  continue;
1936  }
1937  }
1938  g->rings[j++] = pa;
1939  }
1940  /* Update ring count */
1941  g->nrings = j;
1942  break;
1943  }
1944  /* Can process all multi* types as generic collection */
1945  case MULTIPOINTTYPE:
1946  case MULTILINETYPE:
1947  case MULTIPOLYGONTYPE:
1948  case TINTYPE:
1949  case COLLECTIONTYPE:
1950  {
1951  uint32_t i, j = 0;
1952  LWCOLLECTION *col = (LWCOLLECTION*)geom;
1953  for (i = 0; i < col->ngeoms; i++)
1954  {
1955  LWGEOM *g = col->geoms[i];
1956  if (!g) continue;
1957  modified |= lwgeom_simplify_in_place(g, epsilon, preserve_collapsed);
1958  /* Drop zero'ed out geometries */
1959  if(lwgeom_is_empty(g))
1960  {
1961  lwgeom_free(g);
1962  continue;
1963  }
1964  col->geoms[j++] = g;
1965  }
1966  /* Update geometry count */
1967  col->ngeoms = j;
1968  break;
1969  }
1970  default:
1971  {
1972  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1973  break;
1974  }
1975  }
1976 
1977  if (modified)
1978  {
1979  lwgeom_drop_bbox(geom);
1980  }
1981  return modified;
1982 }
#define LW_FALSE
Definition: liblwgeom.h:94
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
#define MULTILINETYPE
Definition: liblwgeom.h:106
#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
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:327
#define TRIANGLETYPE
Definition: liblwgeom.h:115
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
void ptarray_simplify_in_place(POINTARRAY *pa, double tolerance, uint32_t minpts)
Definition: ptarray.c:1856
void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: lwgeom_api.c:394
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:57
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:710
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:252
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1246
int lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1851
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
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
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
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
POINTARRAY * points
Definition: liblwgeom.h:483
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
POINTARRAY * points
Definition: liblwgeom.h:495
uint32_t maxpoints
Definition: liblwgeom.h:428
uint32_t npoints
Definition: liblwgeom.h:427

References COLLECTIONTYPE, LWCOLLECTION::geoms, getPoint2d_cp(), LINETYPE, LW_FALSE, LW_TRUE, lwerror(), lwgeom_as_lwtriangle(), lwgeom_drop_bbox(), 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_simplify2d(), 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: