PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

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

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

1705 {
1706  double area, bbox_area, bbox_width, bbox_height;
1707  GBOX bbox;
1708  const LWGEOM* lwgeom = (LWGEOM*)lwpoly;
1709  uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1710  double sample_cell_size;
1711  uint32_t i, j, n;
1712  uint32_t iterations = 0;
1713  uint32_t npoints_generated = 0;
1714  uint32_t npoints_tested = 0;
1715  GEOSGeometry* g;
1716  const GEOSPreparedGeometry* gprep;
1717  GEOSGeometry* gpt;
1718  GEOSCoordSequence* gseq;
1719  LWMPOINT* mpt;
1720  int32_t srid = lwgeom_get_srid(lwgeom);
1721  int done = 0;
1722  int* cells;
1723  const size_t size = 2 * sizeof(int);
1724  char tmp[2 * sizeof(int)];
1725  const size_t stride = 2 * sizeof(int);
1726 
1727  if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
1728  {
1729  lwerror("%s: only polygons supported", __func__);
1730  return NULL;
1731  }
1732 
1733  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1734 
1735  if (!lwpoly->bbox)
1736  lwgeom_calculate_gbox(lwgeom, &bbox);
1737  else
1738  bbox = *(lwpoly->bbox);
1739 
1740  area = lwpoly_area(lwpoly);
1741  bbox_width = bbox.xmax - bbox.xmin;
1742  bbox_height = bbox.ymax - bbox.ymin;
1743  bbox_area = bbox_width * bbox_height;
1744 
1745  if (area == 0.0 || bbox_area == 0.0)
1746  {
1747  lwerror("%s: zero area input polygon, TBD", __func__);
1748  return NULL;
1749  }
1750 
1751  /* Gross up our test set a bit to increase odds of getting coverage in one pass */
1752  sample_npoints = npoints * bbox_area / area;
1753 
1754  /* We're going to generate points using a sample grid as described
1755  * http://lin-ear-th-inking.blogspot.ca/2010/05/more-random-points-in-jts.html to try and get a more uniform
1756  * "random" set of points. So we have to figure out how to stick a grid into our box */
1757  sample_sqrt = lround(sqrt(sample_npoints));
1758  if (sample_sqrt == 0)
1759  sample_sqrt = 1;
1760 
1761  /* Calculate the grids we're going to randomize within */
1762  if (bbox_width > bbox_height)
1763  {
1764  sample_width = sample_sqrt;
1765  sample_height = ceil((double)sample_npoints / (double)sample_width);
1766  sample_cell_size = bbox_width / sample_width;
1767  }
1768  else
1769  {
1770  sample_height = sample_sqrt;
1771  sample_width = ceil((double)sample_npoints / (double)sample_height);
1772  sample_cell_size = bbox_height / sample_height;
1773  }
1774 
1775  /* Prepare the polygon for fast true/false testing */
1776  initGEOS(lwnotice, lwgeom_geos_error);
1777  g = (GEOSGeometry*)LWGEOM2GEOS(lwgeom, 0);
1778  if (!g)
1779  {
1780  lwerror("%s: Geometry could not be converted to GEOS: %s", __func__, lwgeom_geos_errmsg);
1781  return NULL;
1782  }
1783  gprep = GEOSPrepare(g);
1784 
1785  /* Get an empty multi-point ready to return */
1786  mpt = lwmpoint_construct_empty(srid, 0, 0);
1787 
1788  /* Initiate random number generator.
1789  * Repeatable numbers are generated with seed values >= 1.
1790  * When seed is zero and has not previously been set, it is based on
1791  * Unix time (seconds) and process ID. */
1792  lwrandom_set_seed(seed);
1793 
1794  /* Now we fill in an array of cells, and then shuffle that array, */
1795  /* so we can visit the cells in random order to avoid visual ugliness */
1796  /* caused by visiting them sequentially */
1797  cells = lwalloc(2 * sizeof(int) * sample_height * sample_width);
1798  for (i = 0; i < sample_width; i++)
1799  {
1800  for (j = 0; j < sample_height; j++)
1801  {
1802  cells[2 * (i * sample_height + j)] = i;
1803  cells[2 * (i * sample_height + j) + 1] = j;
1804  }
1805  }
1806 
1807  /* Fisher-Yates shuffle */
1808  n = sample_height * sample_width;
1809  if (n > 1)
1810  {
1811  for (i = n - 1; i > 0; i--)
1812  {
1813  size_t j = (size_t)(lwrandom_uniform() * (i + 1));
1814 
1815  memcpy(tmp, (char *)cells + j * stride, size);
1816  memcpy((char *)cells + j * stride, (char *)cells + i * stride, size);
1817  memcpy((char *)cells + i * stride, tmp, size);
1818  }
1819  }
1820 
1821  /* Start testing points */
1822  while (npoints_generated < npoints)
1823  {
1824  iterations++;
1825  for (i = 0; i < sample_width * sample_height; i++)
1826  {
1827  int contains = 0;
1828  double y = bbox.ymin + cells[2 * i] * sample_cell_size;
1829  double x = bbox.xmin + cells[2 * i + 1] * sample_cell_size;
1830  x += lwrandom_uniform() * sample_cell_size;
1831  y += lwrandom_uniform() * sample_cell_size;
1832  if (x >= bbox.xmax || y >= bbox.ymax) continue;
1833 
1834  gseq = GEOSCoordSeq_create(1, 2);
1835 #if POSTGIS_GEOS_VERSION < 30800
1836  GEOSCoordSeq_setX(gseq, 0, x);
1837  GEOSCoordSeq_setY(gseq, 0, y);
1838 #else
1839  GEOSCoordSeq_setXY(gseq, 0, x, y);
1840 #endif
1841  gpt = GEOSGeom_createPoint(gseq);
1842 
1843  contains = GEOSPreparedIntersects(gprep, gpt);
1844 
1845  GEOSGeom_destroy(gpt);
1846 
1847  if (contains == 2)
1848  {
1849  GEOSPreparedGeom_destroy(gprep);
1850  GEOSGeom_destroy(g);
1851  lwerror("%s: GEOS exception on PreparedContains: %s", __func__, lwgeom_geos_errmsg);
1852  return NULL;
1853  }
1854  if (contains)
1855  {
1856  npoints_generated++;
1857  mpt = lwmpoint_add_lwpoint(mpt, lwpoint_make2d(srid, x, y));
1858  if (npoints_generated == npoints)
1859  {
1860  done = 1;
1861  break;
1862  }
1863  }
1864 
1865  /* Short-circuit check for ctrl-c occasionally */
1866  npoints_tested++;
1867  if (npoints_tested % 10000 == 0)
1868  LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g); return NULL);
1869 
1870  if (done) break;
1871  }
1872  if (done || iterations > 100) break;
1873  }
1874 
1875  GEOSPreparedGeom_destroy(gprep);
1876  GEOSGeom_destroy(g);
1877  lwfree(cells);
1878 
1879  return mpt;
1880 }
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:927
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:104
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:755
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:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354

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: