PostGIS  2.5.0rc1dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

LWMPOINT* lwpoly_to_points ( const LWPOLY lwpoly,
uint32_t  npoints 
)

Definition at line 1414 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().

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