1504 double area, bbox_area, bbox_width, bbox_height;
1507 uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1508 double sample_cell_size;
1510 uint32_t iterations = 0;
1511 uint32_t npoints_generated = 0;
1512 uint32_t npoints_tested = 0;
1514 const GEOSPreparedGeometry* gprep;
1516 GEOSCoordSequence* gseq;
1521 const size_t size = 2 *
sizeof(int);
1522 char tmp[2 *
sizeof(int)];
1523 const size_t stride = 2 *
sizeof(int);
1527 lwerror(
"%s: only polygons supported", __func__);
1536 bbox = *(lwpoly->
bbox);
1539 bbox_width = bbox.
xmax - bbox.
xmin;
1540 bbox_height = bbox.
ymax - bbox.
ymin;
1541 bbox_area = bbox_width * bbox_height;
1543 if (area == 0.0 || bbox_area == 0.0)
1545 lwerror(
"%s: zero area input polygon, TBD", __func__);
1550 sample_npoints = npoints * bbox_area / area;
1555 sample_sqrt = lround(sqrt(sample_npoints));
1556 if (sample_sqrt == 0)
1560 if (bbox_width > bbox_height)
1562 sample_width = sample_sqrt;
1563 sample_height = ceil((
double)sample_npoints / (
double)sample_width);
1564 sample_cell_size = bbox_width / sample_width;
1568 sample_height = sample_sqrt;
1569 sample_width = ceil((
double)sample_npoints / (
double)sample_height);
1570 sample_cell_size = bbox_height / sample_height;
1581 gprep = GEOSPrepare(g);
1595 cells =
lwalloc(2 *
sizeof(
int) * sample_height * sample_width);
1596 for (i = 0; i < sample_width; i++)
1598 for (j = 0; j < sample_height; j++)
1600 cells[2 * (i * sample_height + j)] = i;
1601 cells[2 * (i * sample_height + j) + 1] = j;
1606 n = sample_height * sample_width;
1609 for (i = n - 1; i > 0; i--)
1613 memcpy(tmp, (
char *)cells + j * stride, size);
1614 memcpy((
char *)cells + j * stride, (
char *)cells + i * stride, size);
1615 memcpy((
char *)cells + i * stride, tmp, size);
1620 while (npoints_generated < npoints)
1623 for (i = 0; i < sample_width * sample_height; i++)
1626 double y = bbox.
ymin + cells[2 * i] * sample_cell_size;
1627 double x = bbox.
xmin + cells[2 * i + 1] * sample_cell_size;
1630 if (
x >= bbox.
xmax ||
y >= bbox.
ymax)
continue;
1632 gseq = GEOSCoordSeq_create(1, 2);
1633 #if POSTGIS_GEOS_VERSION < 38
1634 GEOSCoordSeq_setX(gseq, 0,
x);
1635 GEOSCoordSeq_setY(gseq, 0,
y);
1637 GEOSCoordSeq_setXY(gseq, 0,
x,
y);
1639 gpt = GEOSGeom_createPoint(gseq);
1641 contains = GEOSPreparedIntersects(gprep, gpt);
1643 GEOSGeom_destroy(gpt);
1647 GEOSPreparedGeom_destroy(gprep);
1648 GEOSGeom_destroy(g);
1654 npoints_generated++;
1656 if (npoints_generated == npoints)
1665 if (npoints_tested % 10000 == 0)
1666 LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g);
return NULL);
1670 if (done || iterations > 100)
break;
1673 GEOSPreparedGeom_destroy(gprep);
1674 GEOSGeom_destroy(g);
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).
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
void lwnotice(const char *fmt,...)
Write a notice out to the notice 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)
Datum contains(PG_FUNCTION_ARGS)