PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ lwpoly_to_points()

LWMPOINT* lwpoly_to_points ( const LWPOLY poly,
uint32_t  npoints 
)

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

1238 {
1239  double area, bbox_area, bbox_width, bbox_height;
1240  GBOX bbox;
1241  const LWGEOM* lwgeom = (LWGEOM*)lwpoly;
1242  uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1243  double sample_cell_size;
1244  uint32_t i, j, n;
1245  uint32_t iterations = 0;
1246  uint32_t npoints_generated = 0;
1247  uint32_t npoints_tested = 0;
1248  GEOSGeometry* g;
1249  const GEOSPreparedGeometry* gprep;
1250  GEOSGeometry* gpt;
1251  GEOSCoordSequence* gseq;
1252  LWMPOINT* mpt;
1253  int srid = lwgeom_get_srid(lwgeom);
1254  int done = 0;
1255  int* cells;
1256  const size_t size = 2 * sizeof(int);
1257  char tmp[2 * sizeof(int)];
1258  const size_t stride = 2 * sizeof(int);
1259 
1260  if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
1261  {
1262  lwerror("%s: only polygons supported", __func__);
1263  return NULL;
1264  }
1265 
1266  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1267 
1268  if (!lwpoly->bbox)
1269  lwgeom_calculate_gbox(lwgeom, &bbox);
1270  else
1271  bbox = *(lwpoly->bbox);
1272 
1273  area = lwpoly_area(lwpoly);
1274  bbox_width = bbox.xmax - bbox.xmin;
1275  bbox_height = bbox.ymax - bbox.ymin;
1276  bbox_area = bbox_width * bbox_height;
1277 
1278  if (area == 0.0 || bbox_area == 0.0)
1279  {
1280  lwerror("%s: zero area input polygon, TBD", __func__);
1281  return NULL;
1282  }
1283 
1284  /* Gross up our test set a bit to increase odds of getting coverage in one pass */
1285  sample_npoints = npoints * bbox_area / area;
1286 
1287  /* We're going to generate points using a sample grid as described
1288  * http://lin-ear-th-inking.blogspot.ca/2010/05/more-random-points-in-jts.html to try and get a more uniform
1289  * "random" set of points. So we have to figure out how to stick a grid into our box */
1290  sample_sqrt = lround(sqrt(sample_npoints));
1291  if (sample_sqrt == 0) sample_sqrt = 1;
1292 
1293  /* Calculate the grids we're going to randomize within */
1294  if (bbox_width > bbox_height)
1295  {
1296  sample_width = sample_sqrt;
1297  sample_height = ceil((double)sample_npoints / (double)sample_width);
1298  sample_cell_size = bbox_width / sample_width;
1299  }
1300  else
1301  {
1302  sample_height = sample_sqrt;
1303  sample_width = ceil((double)sample_npoints / (double)sample_height);
1304  sample_cell_size = bbox_height / sample_height;
1305  }
1306 
1307  /* Prepare the polygon for fast true/false testing */
1308  initGEOS(lwnotice, lwgeom_geos_error);
1309  g = (GEOSGeometry*)LWGEOM2GEOS(lwgeom, 0);
1310  if (!g)
1311  {
1312  lwerror("%s: Geometry could not be converted to GEOS: %s", __func__, lwgeom_geos_errmsg);
1313  return NULL;
1314  }
1315  gprep = GEOSPrepare(g);
1316 
1317  /* Get an empty multi-point ready to return */
1318  mpt = lwmpoint_construct_empty(srid, 0, 0);
1319 
1320  /* Init random number generator */
1321  srand(time(NULL));
1322 
1323  /* Now we fill in an array of cells, and then shuffle that array, */
1324  /* so we can visit the cells in random order to avoid visual ugliness */
1325  /* caused by visiting them sequentially */
1326  cells = lwalloc(2 * sizeof(int) * sample_height * sample_width);
1327  for (i = 0; i < sample_width; i++)
1328  {
1329  for (j = 0; j < sample_height; j++)
1330  {
1331  cells[2 * (i * sample_height + j)] = i;
1332  cells[2 * (i * sample_height + j) + 1] = j;
1333  }
1334  }
1335 
1336  /* shuffle */
1337  {
1338  n = sample_height * sample_width;
1339  if (n > 1)
1340  {
1341  for (i = 0; i < n - 1; ++i)
1342  {
1343  size_t rnd = (size_t)rand();
1344  size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
1345 
1346  memcpy(tmp, (char*)cells + j * stride, size);
1347  memcpy((char*)cells + j * stride, (char*)cells + i * stride, size);
1348  memcpy((char*)cells + i * stride, tmp, size);
1349  }
1350  }
1351  }
1352 
1353  /* Start testing points */
1354  while (npoints_generated < npoints)
1355  {
1356  iterations++;
1357  for (i = 0; i < sample_width * sample_height; i++)
1358  {
1359  int contains = 0;
1360  double y = bbox.ymin + cells[2 * i] * sample_cell_size;
1361  double x = bbox.xmin + cells[2 * i + 1] * sample_cell_size;
1362  x += rand() * sample_cell_size / RAND_MAX;
1363  y += rand() * sample_cell_size / RAND_MAX;
1364  if (x >= bbox.xmax || y >= bbox.ymax) continue;
1365 
1366  gseq = GEOSCoordSeq_create(1, 2);
1367  GEOSCoordSeq_setX(gseq, 0, x);
1368  GEOSCoordSeq_setY(gseq, 0, y);
1369  gpt = GEOSGeom_createPoint(gseq);
1370 
1371  contains = GEOSPreparedIntersects(gprep, gpt);
1372 
1373  GEOSGeom_destroy(gpt);
1374 
1375  if (contains == 2)
1376  {
1377  GEOSPreparedGeom_destroy(gprep);
1378  GEOSGeom_destroy(g);
1379  lwerror("%s: GEOS exception on PreparedContains: %s", __func__, lwgeom_geos_errmsg);
1380  return NULL;
1381  }
1382  if (contains)
1383  {
1384  npoints_generated++;
1385  mpt = lwmpoint_add_lwpoint(mpt, lwpoint_make2d(srid, x, y));
1386  if (npoints_generated == npoints)
1387  {
1388  done = 1;
1389  break;
1390  }
1391  }
1392 
1393  /* Short-circuit check for ctrl-c occasionally */
1394  npoints_tested++;
1395  if (npoints_tested % 10000 == 0)
1396  LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g); return NULL);
1397 
1398  if (done) break;
1399  }
1400  if (done || iterations > 100) break;
1401  }
1402 
1403  GEOSPreparedGeom_destroy(gprep);
1404  GEOSGeom_destroy(g);
1405  lwfree(cells);
1406 
1407  return mpt;
1408 }
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:922
#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:915
#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:745
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:1392
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: