1706 double area, bbox_area, bbox_width, bbox_height;
1709 uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1710 double sample_cell_size;
1712 uint32_t iterations = 0;
1713 uint32_t npoints_generated = 0;
1714 uint32_t npoints_tested = 0;
1716 const GEOSPreparedGeometry* gprep;
1718 GEOSCoordSequence* gseq;
1723 const size_t size = 2 *
sizeof(int);
1724 char tmp[2 *
sizeof(int)];
1725 const size_t stride = 2 *
sizeof(int);
1729 lwerror(
"%s: only polygons supported", __func__);
1738 bbox = *(lwpoly->
bbox);
1741 bbox_width = bbox.
xmax - bbox.
xmin;
1742 bbox_height = bbox.
ymax - bbox.
ymin;
1743 bbox_area = bbox_width * bbox_height;
1745 if (area == 0.0 || bbox_area == 0.0)
1747 lwerror(
"%s: zero area input polygon, TBD", __func__);
1752 sample_npoints = npoints * bbox_area / area;
1757 sample_sqrt = lround(sqrt(sample_npoints));
1758 if (sample_sqrt == 0)
1762 if (bbox_width > bbox_height)
1764 sample_width = sample_sqrt;
1765 sample_height = ceil((
double)sample_npoints / (
double)sample_width);
1766 sample_cell_size = bbox_width / sample_width;
1770 sample_height = sample_sqrt;
1771 sample_width = ceil((
double)sample_npoints / (
double)sample_height);
1772 sample_cell_size = bbox_height / sample_height;
1783 gprep = GEOSPrepare(g);
1797 cells =
lwalloc(2 *
sizeof(
int) * sample_height * sample_width);
1798 for (i = 0; i < sample_width; i++)
1800 for (j = 0; j < sample_height; j++)
1802 cells[2 * (i * sample_height + j)] = i;
1803 cells[2 * (i * sample_height + j) + 1] = j;
1808 n = sample_height * sample_width;
1811 for (i = n - 1; i > 0; i--)
1815 memcpy(tmp, (
char *)cells + j * stride, size);
1816 memcpy((
char *)cells + j * stride, (
char *)cells + i * stride, size);
1817 memcpy((
char *)cells + i * stride, tmp, size);
1822 while (npoints_generated < npoints)
1825 for (i = 0; i < sample_width * sample_height; i++)
1828 double y = bbox.
ymin + cells[2 * i] * sample_cell_size;
1829 double x = bbox.
xmin + cells[2 * i + 1] * sample_cell_size;
1832 if (
x >= bbox.
xmax ||
y >= bbox.
ymax)
continue;
1834 gseq = GEOSCoordSeq_create(1, 2);
1835 #if POSTGIS_GEOS_VERSION < 30800
1836 GEOSCoordSeq_setX(gseq, 0,
x);
1837 GEOSCoordSeq_setY(gseq, 0,
y);
1839 GEOSCoordSeq_setXY(gseq, 0,
x,
y);
1841 gpt = GEOSGeom_createPoint(gseq);
1843 contains = GEOSPreparedIntersects(gprep, gpt);
1845 GEOSGeom_destroy(gpt);
1849 GEOSPreparedGeom_destroy(gprep);
1850 GEOSGeom_destroy(g);
1856 npoints_generated++;
1858 if (npoints_generated == npoints)
1867 if (npoints_tested % 10000 == 0)
1868 LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g);
return NULL);
1872 if (done || iterations > 100)
break;
1875 GEOSPreparedGeom_destroy(gprep);
1876 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)