PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

LWMPOINT* lwpoly_to_points ( const LWPOLY lwpoly,
uint32_t  npoints 
)

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

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