PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwmpoly_to_points()

LWMPOINT* lwmpoly_to_points ( const LWMPOLY mpoly,
int  npoints 
)

Definition at line 1872 of file liblwgeom/lwgeom_geos.c.

References area(), LWMPOINT::geoms, LWMPOLY::geoms, lwerror(), lwfree(), lwgeom_area(), lwgeom_get_type(), lwgeom_is_empty(), lwgeom_release(), lwmpoint_add_lwpoint(), lwpoly_area(), lwpoly_to_points(), MULTIPOLYGONTYPE, LWMPOINT::ngeoms, and LWMPOLY::ngeoms.

Referenced by lwgeom_to_points().

1873 {
1874  const LWGEOM *lwgeom = (LWGEOM*)lwmpoly;
1875  double area;
1876  int i;
1877  LWMPOINT *mpt = NULL;
1878 
1879  if (lwgeom_get_type(lwgeom) != MULTIPOLYGONTYPE)
1880  {
1881  lwerror("%s: only multipolygons supported", __func__);
1882  return NULL;
1883  }
1884  if (npoints == 0 || lwgeom_is_empty(lwgeom))
1885  {
1886  return NULL;
1887  }
1888 
1889  area = lwgeom_area(lwgeom);
1890 
1891  for (i = 0; i < lwmpoly->ngeoms; i++)
1892  {
1893  double sub_area = lwpoly_area(lwmpoly->geoms[i]);
1894  int sub_npoints = lround(npoints * sub_area / area);
1895  if(sub_npoints > 0)
1896  {
1897  LWMPOINT *sub_mpt = lwpoly_to_points(lwmpoly->geoms[i], sub_npoints);
1898  if (!mpt)
1899  {
1900  mpt = sub_mpt;
1901  }
1902  else
1903  {
1904  int j;
1905  for (j = 0; j < sub_mpt->ngeoms; j++)
1906  {
1907  mpt = lwmpoint_add_lwpoint(mpt, sub_mpt->geoms[j]);
1908  }
1909  /* Just free the shell, leave the underlying lwpoints alone, as they
1910  are now owed by the returning multipoint */
1911  lwfree(sub_mpt->geoms);
1912  lwgeom_release((LWGEOM*)sub_mpt);
1913  }
1914  }
1915  }
1916 
1917  return mpt;
1918 }
void lwfree(void *mem)
Definition: lwutil.c:244
LWMPOINT * lwpoly_to_points(const LWPOLY *lwpoly, int npoints)
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:878
Datum area(PG_FUNCTION_ARGS)
LWPOINT ** geoms
Definition: liblwgeom.h:470
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:524
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1623
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:421
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:1346
int ngeoms
Definition: liblwgeom.h:468
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
Here is the call graph for this function:
Here is the caller graph for this function: