Skip to content

Commit 44d5d9c

Browse files
committed
PostGIS: fix ST_Intersects() with bounding box that is a point (follow-up of fixes #6181, fixes #6230) (fixes #6347 (comment))
1 parent 1ff7812 commit 44d5d9c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

mappostgis.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,37 +1666,44 @@ char *msPostGISBuildSQLBox(layerObj *layer, rectObj *rect, char *strSRID)
16661666

16671667
char *strBox = NULL;
16681668
size_t sz;
1669+
const int bIsPoint = rect->minx == rect->maxx && rect->miny == rect->maxy;
16691670

16701671
if (layer->debug) {
16711672
msDebug("msPostGISBuildSQLBox called.\n");
16721673
}
16731674

16741675
if ( strSRID ) {
1675-
static char *strBoxTemplate = "ST_GeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%s)";
1676+
static const char *strBoxTemplate = "ST_GeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%s)";
1677+
static const char *strBoxTemplatePoint = "ST_GeomFromText('POINT(%.15g %.15g)',%s)";
16761678
/* 10 doubles + 1 integer + template characters */
16771679
sz = 10 * 22 + strlen(strSRID) + strlen(strBoxTemplate);
16781680
strBox = (char*)msSmallMalloc(sz+1); /* add space for terminating NULL */
1679-
if ( sz <= snprintf(strBox, sz, strBoxTemplate,
1681+
if ( (bIsPoint && sz <= (size_t)(snprintf(strBox, sz, strBoxTemplatePoint,
1682+
rect->minx, rect->miny, strSRID))) ||
1683+
(!bIsPoint && sz <= (size_t)(snprintf(strBox, sz, strBoxTemplate,
16801684
rect->minx, rect->miny,
16811685
rect->minx, rect->maxy,
16821686
rect->maxx, rect->maxy,
16831687
rect->maxx, rect->miny,
16841688
rect->minx, rect->miny,
1685-
strSRID)) {
1689+
strSRID))) ) {
16861690
msSetError(MS_MISCERR,"Bounding box digits truncated.","msPostGISBuildSQLBox");
16871691
return NULL;
16881692
}
16891693
} else {
1690-
static char *strBoxTemplate = "ST_GeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))')";
1694+
static const char *strBoxTemplate = "ST_GeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))')";
1695+
static const char *strBoxTemplatePoint = "ST_GeomFromText('POINT(%.15g %.15g)')";
16911696
/* 10 doubles + template characters */
16921697
sz = 10 * 22 + strlen(strBoxTemplate);
16931698
strBox = (char*)msSmallMalloc(sz+1); /* add space for terminating NULL */
1694-
if ( sz <= snprintf(strBox, sz, strBoxTemplate,
1699+
if ( (bIsPoint && sz <= (size_t)(snprintf(strBox, sz, strBoxTemplatePoint,
1700+
rect->minx, rect->miny))) ||
1701+
(!bIsPoint && sz <= (size_t)(snprintf(strBox, sz, strBoxTemplate,
16951702
rect->minx, rect->miny,
16961703
rect->minx, rect->maxy,
16971704
rect->maxx, rect->maxy,
16981705
rect->maxx, rect->miny,
1699-
rect->minx, rect->miny) ) {
1706+
rect->minx, rect->miny))) ) {
17001707
msSetError(MS_MISCERR,"Bounding box digits truncated.","msPostGISBuildSQLBox");
17011708
return NULL;
17021709
}

0 commit comments

Comments
 (0)