PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

LWMPOINT* lwpoly_to_points ( const LWPOLY poly,
uint32_t  npoints,
int32_t  seed 
)

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

1652 {
1653  double area, bbox_area, bbox_width, bbox_height;
1654  GBOX bbox;
1655  const LWGEOM* lwgeom = (LWGEOM*)lwpoly;
1656  uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1657  double sample_cell_size;
1658  uint32_t i, j, n;
1659  uint32_t iterations = 0;
1660  uint32_t npoints_generated = 0;
1661  uint32_t npoints_tested = 0;
1662  GEOSGeometry* g;
1663  const GEOSPreparedGeometry* gprep;
1664  GEOSGeometry* gpt;
1665  GEOSCoordSequence* gseq;
1666  LWMPOINT* mpt;
1667  int32_t srid = lwgeom_get_srid(lwgeom);
1668  int done = 0;
1669  int* cells;
1670  const size_t size = 2 * sizeof(int);
1671  char tmp[2 * sizeof(int)];
1672  const size_t stride = 2 * sizeof(int);
1673 
1674  if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
1675  {
1676  lwerror("%s: only polygons supported", __func__);
1677  return NULL;
1678  }
1679 
1680  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1681 
1682  if (!lwpoly->bbox)
1683  lwgeom_calculate_gbox(lwgeom, &bbox);
1684  else
1685  bbox = *(lwpoly->bbox);
1686 
1687  area = lwpoly_area(lwpoly);
1688  bbox_width = bbox.xmax - bbox.xmin;
1689  bbox_height = bbox.ymax - bbox.ymin;
1690  bbox_area = bbox_width * bbox_height;
1691 
1692  if (area == 0.0 || bbox_area == 0.0)
1693  {
1694  lwerror("%s: zero area input polygon, TBD", __func__);
1695  return NULL;
1696  }
1697 
1698  /* Gross up our test set a bit to increase odds of getting coverage in one pass */
1699  sample_npoints = npoints * bbox_area / area;
1700 
1701  /* We're going to generate points using a sample grid as described
1702  * http://lin-ear-th-inking.blogspot.ca/2010/05/more-random-points-in-jts.html to try and get a more uniform
1703  * "random" set of points. So we have to figure out how to stick a grid into our box */
1704  sample_sqrt = lround(sqrt(sample_npoints));
1705  if (sample_sqrt == 0)
1706  sample_sqrt = 1;
1707 
1708  /* Calculate the grids we're going to randomize within */
1709  if (bbox_width > bbox_height)
1710  {
1711  sample_width = sample_sqrt;
1712  sample_height = ceil((double)sample_npoints / (double)sample_width);
1713  sample_cell_size = bbox_width / sample_width;
1714  }
1715  else
1716  {
1717  sample_height = sample_sqrt;
1718  sample_width = ceil((double)sample_npoints / (double)sample_height);
1719  sample_cell_size = bbox_height / sample_height;
1720  }
1721 
1722  /* Prepare the polygon for fast true/false testing */
1723  initGEOS(lwnotice, lwgeom_geos_error);
1724  g = (GEOSGeometry*)LWGEOM2GEOS(lwgeom, 0);
1725  if (!g)
1726  {
1727  lwerror("%s: Geometry could not be converted to GEOS: %s", __func__, lwgeom_geos_errmsg);
1728  return NULL;
1729  }
1730  gprep = GEOSPrepare(g);
1731 
1732  /* Get an empty multi-point ready to return */
1733  mpt = lwmpoint_construct_empty(srid, 0, 0);
1734 
1735  /* Initiate random number generator.
1736  * Repeatable numbers are generated with seed values >= 1.
1737  * When seed is zero and has not previously been set, it is based on
1738  * Unix time (seconds) and process ID. */
1739  lwrandom_set_seed(seed);
1740 
1741  /* Now we fill in an array of cells, and then shuffle that array, */
1742  /* so we can visit the cells in random order to avoid visual ugliness */
1743  /* caused by visiting them sequentially */
1744  cells = lwalloc(2 * sizeof(int) * sample_height * sample_width);
1745  for (i = 0; i < sample_width; i++)
1746  {
1747  for (j = 0; j < sample_height; j++)
1748  {
1749  cells[2 * (i * sample_height + j)] = i;
1750  cells[2 * (i * sample_height + j) + 1] = j;
1751  }
1752  }
1753 
1754  /* Fisher-Yates shuffle */
1755  n = sample_height * sample_width;
1756  if (n > 1)
1757  {
1758  for (i = n - 1; i > 0; i--)
1759  {
1760  size_t j = (size_t)(lwrandom_uniform() * (i + 1));
1761 
1762  memcpy(tmp, (char *)cells + j * stride, size);
1763  memcpy((char *)cells + j * stride, (char *)cells + i * stride, size);
1764  memcpy((char *)cells + i * stride, tmp, size);
1765  }
1766  }
1767 
1768  /* Start testing points */
1769  while (npoints_generated < npoints)
1770  {
1771  iterations++;
1772  for (i = 0; i < sample_width * sample_height; i++)
1773  {
1774  int contains = 0;
1775  double y = bbox.ymin + cells[2 * i] * sample_cell_size;
1776  double x = bbox.xmin + cells[2 * i + 1] * sample_cell_size;
1777  x += lwrandom_uniform() * sample_cell_size;
1778  y += lwrandom_uniform() * sample_cell_size;
1779  if (x >= bbox.xmax || y >= bbox.ymax) continue;
1780 
1781  gseq = GEOSCoordSeq_create(1, 2);
1782 #if POSTGIS_GEOS_VERSION < 30800
1783  GEOSCoordSeq_setX(gseq, 0, x);
1784  GEOSCoordSeq_setY(gseq, 0, y);
1785 #else
1786  GEOSCoordSeq_setXY(gseq, 0, x, y);
1787 #endif
1788  gpt = GEOSGeom_createPoint(gseq);
1789 
1790  contains = GEOSPreparedIntersects(gprep, gpt);
1791 
1792  GEOSGeom_destroy(gpt);
1793 
1794  if (contains == 2)
1795  {
1796  GEOSPreparedGeom_destroy(gprep);
1797  GEOSGeom_destroy(g);
1798  lwerror("%s: GEOS exception on PreparedContains: %s", __func__, lwgeom_geos_errmsg);
1799  return NULL;
1800  }
1801  if (contains)
1802  {
1803  npoints_generated++;
1804  mpt = lwmpoint_add_lwpoint(mpt, lwpoint_make2d(srid, x, y));
1805  if (npoints_generated == npoints)
1806  {
1807  done = 1;
1808  break;
1809  }
1810  }
1811 
1812  /* Short-circuit check for ctrl-c occasionally */
1813  npoints_tested++;
1814  if (npoints_tested % 10000 == 0)
1815  LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g); return NULL);
1816 
1817  if (done) break;
1818  }
1819  if (done || iterations > 100) break;
1820  }
1821 
1822  GEOSPreparedGeom_destroy(gprep);
1823  GEOSGeom_destroy(g);
1824  lwfree(cells);
1825 
1826  return mpt;
1827 }
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
void lwgeom_geos_error(const char *fmt,...)
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:910
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
void lwfree(void *mem)
Definition: lwutil.c:242
#define POLYGONTYPE
Definition: liblwgeom.h:118
LWMPOINT * lwmpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwmpoint.c:39
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:738
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_ON_INTERRUPT(x)
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:434
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:145
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
void lwrandom_set_seed(int32_t seed)
Definition: lwrandom.c:48
double lwrandom_uniform(void)
Definition: lwrandom.c:94
Datum contains(PG_FUNCTION_ARGS)
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368

References LWPOLY::bbox, contains(), LW_ON_INTERRUPT, lwalloc(), lwerror(), lwfree(), LWGEOM2GEOS(), lwgeom_calculate_gbox(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_get_srid(), lwgeom_get_type(), lwgeom_is_empty(), lwmpoint_add_lwpoint(), lwmpoint_construct_empty(), lwnotice(), lwpoint_make2d(), lwpoly_area(), lwrandom_set_seed(), lwrandom_uniform(), POLYGONTYPE, pixval::x, GBOX::xmax, GBOX::xmin, pixval::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwgeom_to_points(), and lwmpoly_to_points().

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