PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

LWMPOINT* lwpoly_to_points ( const LWPOLY poly,
uint32_t  npoints 
)

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

References area(), 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(), POLYGONTYPE, pixval::x, GBOX::xmax, GBOX::xmin, pixval::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwgeom_to_points(), and lwmpoly_to_points().

1434 {
1435  double area, bbox_area, bbox_width, bbox_height;
1436  GBOX bbox;
1437  const LWGEOM* lwgeom = (LWGEOM*)lwpoly;
1438  uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1439  double sample_cell_size;
1440  uint32_t i, j, n;
1441  uint32_t iterations = 0;
1442  uint32_t npoints_generated = 0;
1443  uint32_t npoints_tested = 0;
1444  GEOSGeometry* g;
1445  const GEOSPreparedGeometry* gprep;
1446  GEOSGeometry* gpt;
1447  GEOSCoordSequence* gseq;
1448  LWMPOINT* mpt;
1449  int srid = lwgeom_get_srid(lwgeom);
1450  int done = 0;
1451  int* cells;
1452  const size_t size = 2 * sizeof(int);
1453  char tmp[2 * sizeof(int)];
1454  const size_t stride = 2 * sizeof(int);
1455 
1456  if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
1457  {
1458  lwerror("%s: only polygons supported", __func__);
1459  return NULL;
1460  }
1461 
1462  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1463 
1464  if (!lwpoly->bbox)
1465  lwgeom_calculate_gbox(lwgeom, &bbox);
1466  else
1467  bbox = *(lwpoly->bbox);
1468 
1469  area = lwpoly_area(lwpoly);
1470  bbox_width = bbox.xmax - bbox.xmin;
1471  bbox_height = bbox.ymax - bbox.ymin;
1472  bbox_area = bbox_width * bbox_height;
1473 
1474  if (area == 0.0 || bbox_area == 0.0)
1475  {
1476  lwerror("%s: zero area input polygon, TBD", __func__);
1477  return NULL;
1478  }
1479 
1480  /* Gross up our test set a bit to increase odds of getting coverage in one pass */
1481  sample_npoints = npoints * bbox_area / area;
1482 
1483  /* We're going to generate points using a sample grid as described
1484  * http://lin-ear-th-inking.blogspot.ca/2010/05/more-random-points-in-jts.html to try and get a more uniform
1485  * "random" set of points. So we have to figure out how to stick a grid into our box */
1486  sample_sqrt = lround(sqrt(sample_npoints));
1487  if (sample_sqrt == 0) sample_sqrt = 1;
1488 
1489  /* Calculate the grids we're going to randomize within */
1490  if (bbox_width > bbox_height)
1491  {
1492  sample_width = sample_sqrt;
1493  sample_height = ceil((double)sample_npoints / (double)sample_width);
1494  sample_cell_size = bbox_width / sample_width;
1495  }
1496  else
1497  {
1498  sample_height = sample_sqrt;
1499  sample_width = ceil((double)sample_npoints / (double)sample_height);
1500  sample_cell_size = bbox_height / sample_height;
1501  }
1502 
1503  /* Prepare the polygon for fast true/false testing */
1504  initGEOS(lwnotice, lwgeom_geos_error);
1505  g = (GEOSGeometry*)LWGEOM2GEOS(lwgeom, 0);
1506  if (!g)
1507  {
1508  lwerror("%s: Geometry could not be converted to GEOS: %s", __func__, lwgeom_geos_errmsg);
1509  return NULL;
1510  }
1511  gprep = GEOSPrepare(g);
1512 
1513  /* Get an empty multi-point ready to return */
1514  mpt = lwmpoint_construct_empty(srid, 0, 0);
1515 
1516  /* Init random number generator */
1517  srand(time(NULL));
1518 
1519  /* Now we fill in an array of cells, and then shuffle that array, */
1520  /* so we can visit the cells in random order to avoid visual ugliness */
1521  /* caused by visiting them sequentially */
1522  cells = lwalloc(2 * sizeof(int) * sample_height * sample_width);
1523  for (i = 0; i < sample_width; i++)
1524  {
1525  for (j = 0; j < sample_height; j++)
1526  {
1527  cells[2 * (i * sample_height + j)] = i;
1528  cells[2 * (i * sample_height + j) + 1] = j;
1529  }
1530  }
1531 
1532  /* shuffle */
1533  n = sample_height * sample_width;
1534  if (n > 1)
1535  {
1536  for (i = 0; i < n - 1; ++i)
1537  {
1538  size_t rnd = (size_t)rand();
1539  size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
1540 
1541  memcpy(tmp, (char *)cells + j * stride, size);
1542  memcpy((char *)cells + j * stride, (char *)cells + i * stride, size);
1543  memcpy((char *)cells + i * stride, tmp, size);
1544  }
1545  }
1546 
1547  /* Start testing points */
1548  while (npoints_generated < npoints)
1549  {
1550  iterations++;
1551  for (i = 0; i < sample_width * sample_height; i++)
1552  {
1553  int contains = 0;
1554  double y = bbox.ymin + cells[2 * i] * sample_cell_size;
1555  double x = bbox.xmin + cells[2 * i + 1] * sample_cell_size;
1556  x += rand() * sample_cell_size / RAND_MAX;
1557  y += rand() * sample_cell_size / RAND_MAX;
1558  if (x >= bbox.xmax || y >= bbox.ymax) continue;
1559 
1560  gseq = GEOSCoordSeq_create(1, 2);
1561  GEOSCoordSeq_setX(gseq, 0, x);
1562  GEOSCoordSeq_setY(gseq, 0, y);
1563  gpt = GEOSGeom_createPoint(gseq);
1564 
1565  contains = GEOSPreparedIntersects(gprep, gpt);
1566 
1567  GEOSGeom_destroy(gpt);
1568 
1569  if (contains == 2)
1570  {
1571  GEOSPreparedGeom_destroy(gprep);
1572  GEOSGeom_destroy(g);
1573  lwerror("%s: GEOS exception on PreparedContains: %s", __func__, lwgeom_geos_errmsg);
1574  return NULL;
1575  }
1576  if (contains)
1577  {
1578  npoints_generated++;
1579  mpt = lwmpoint_add_lwpoint(mpt, lwpoint_make2d(srid, x, y));
1580  if (npoints_generated == npoints)
1581  {
1582  done = 1;
1583  break;
1584  }
1585  }
1586 
1587  /* Short-circuit check for ctrl-c occasionally */
1588  npoints_tested++;
1589  if (npoints_tested % 10000 == 0)
1590  LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g); return NULL);
1591 
1592  if (done) break;
1593  }
1594  if (done || iterations > 100) break;
1595  }
1596 
1597  GEOSPreparedGeom_destroy(gprep);
1598  GEOSGeom_destroy(g);
1599  lwfree(cells);
1600 
1601  return mpt;
1602 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
void lwfree(void *mem)
Definition: lwutil.c:244
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
#define POLYGONTYPE
Definition: liblwgeom.h:86
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
Datum area(PG_FUNCTION_ARGS)
double xmax
Definition: liblwgeom.h:295
Datum contains(PG_FUNCTION_ARGS)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:916
#define LW_ON_INTERRUPT(x)
unsigned int uint32_t
Definition: uthash.h:78
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:746
double ymin
Definition: liblwgeom.h:296
double xmin
Definition: liblwgeom.h:294
void lwgeom_geos_error(const char *fmt,...)
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:441
LWMPOINT * lwmpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwmpoint.c:39
double ymax
Definition: liblwgeom.h:297
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
void * lwalloc(size_t size)
Definition: lwutil.c:229
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:1393
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: