1418 typedef struct CellCorner {
1424 uint32_t num_cells = 0;
1426 double area, bbox_area, bbox_width, bbox_height, area_ratio;
1429 uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1430 double sample_cell_size;
1432 uint32_t iterations = 0;
1433 uint32_t npoints_generated = 0;
1434 uint32_t npoints_tested = 0;
1436 const GEOSPreparedGeometry* gprep;
1443 lwerror(
"%s: only polygons supported", __func__);
1452 bbox = *(lwpoly->bbox);
1455 bbox_width = bbox.
xmax - bbox.
xmin;
1456 bbox_height = bbox.
ymax - bbox.
ymin;
1457 bbox_area = bbox_width * bbox_height;
1459 if (area == 0.0 || bbox_area == 0.0)
1461 lwerror(
"%s: zero area input polygon, TBD", __func__);
1471 area_ratio =
FP_MIN(bbox_area / area, 10000.0);
1472 sample_npoints = npoints * area_ratio;
1478 sample_sqrt = ceil(sqrt(sample_npoints));
1481 if (bbox_width > bbox_height)
1483 sample_width = sample_sqrt;
1484 sample_height = ceil((
double)sample_npoints / (
double)sample_width);
1485 sample_cell_size = bbox_width / sample_width;
1489 sample_height = sample_sqrt;
1490 sample_width = ceil((
double)sample_npoints / (
double)sample_height);
1491 sample_cell_size = bbox_height / sample_height;
1502 gprep = GEOSPrepare(g);
1510 cells =
lwalloc(
sizeof(CellCorner) * sample_height * sample_width);
1512 for (i = 0; i < sample_width; i++)
1514 for (j = 0; j < sample_height; j++)
1517 double xmin = bbox.
xmin + i * sample_cell_size;
1518 double ymin = bbox.
ymin + j * sample_cell_size;
1520 intersects = GEOSPreparedIntersects(gprep, gcell);
1521 GEOSGeom_destroy(gcell);
1522 if (intersects == 2)
1524 GEOSPreparedGeom_destroy(gprep);
1525 GEOSGeom_destroy(g);
1529 if (intersects == 1)
1531 cells[num_cells].x = xmin;
1532 cells[num_cells].y = ymin;
1550 for (i = n - 1; i > 0; i--)
1554 memcpy( &tmp, cells + j,
sizeof(CellCorner));
1555 memcpy(cells + j, cells + i,
sizeof(CellCorner));
1556 memcpy(cells + i, &tmp,
sizeof(CellCorner));
1564 while (npoints_generated < npoints)
1567 for (i = 0; i < num_cells; i++)
1570 double y = cells[i].y;
1571 double x = cells[i].x;
1574 if (
x >= bbox.
xmax ||
y >= bbox.
ymax)
continue;
1576 #if POSTGIS_GEOS_VERSION >= 31200
1577 contains = GEOSPreparedIntersectsXY(gprep,
x,
y);
1581 GEOSCoordSequence *gseq = GEOSCoordSeq_create(1, 2);;
1582 GEOSCoordSeq_setXY(gseq, 0,
x,
y);
1583 gpt = GEOSGeom_createPoint(gseq);
1584 contains = GEOSPreparedIntersects(gprep, gpt);
1585 GEOSGeom_destroy(gpt);
1590 GEOSPreparedGeom_destroy(gprep);
1591 GEOSGeom_destroy(g);
1598 npoints_generated++;
1599 if (npoints_generated == npoints)
1608 if (npoints_tested % 10000 == 0)
1609 LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g);
return NULL);
1613 if (done || iterations > 100)
break;
1616 GEOSPreparedGeom_destroy(gprep);
1617 GEOSGeom_destroy(g);
static GEOSGeometry * lwpoly_to_points_make_cell(double xmin, double ymin, double cell_size)
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.
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
LWMPOINT * lwmpoint_construct_empty(int32_t srid, char hasz, char hasm)
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...
void * lwalloc(size_t size)
#define LW_ON_INTERRUPT(x)
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Datum contains(PG_FUNCTION_ARGS)
void lwnotice(const char *fmt,...) __attribute__((format(printf
Write a notice out to the notice handler.
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void lwrandom_set_seed(int32_t seed)
double lwrandom_uniform(void)