From 3e42e356884225c8790b5cb5630a7585aea19355 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Jul 2021 17:03:41 +0200 Subject: [PATCH] PostGIS: fix ST_Intersects() with collections with PostGIS < 2.5. Fixes https://github.com/MapServer/MapServer/pull/6355#issuecomment-877290417 --- mappostgis.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mappostgis.c b/mappostgis.c index 4077fdbc5a..c48f867535 100644 --- a/mappostgis.c +++ b/mappostgis.c @@ -2081,9 +2081,20 @@ char *msPostGISBuildSQLWhere(layerObj *layer, rectObj *rect, long *uid, rectObj // otherwise if find_srid() would return 0, ST_Intersects() would not // work at all, which breaks the msautotest/query/query_postgis.map // tests, releated to bdry_counpy2 layer that has no SRID - static const char *strRectTemplate = "ST_Intersects(\"%s\", %s)"; - strRect = (char*)msSmallMalloc(strlen(strRectTemplate) + strBoxLength + strlen(layerinfo->geomcolumn) +1 ); - sprintf(strRect, strRectTemplate, layerinfo->geomcolumn, strBox); + if( layerinfo->version >= 20500 ) + { + static const char *strRectTemplate = "ST_Intersects(\"%s\", %s)"; + strRect = (char*)msSmallMalloc(strlen(strRectTemplate) + strBoxLength + strlen(layerinfo->geomcolumn) +1 ); + sprintf(strRect, strRectTemplate, layerinfo->geomcolumn, strBox); + } + else + { + // ST_Intersects() before PostGIS 2.5 doesn't support collections + // See https://github.com/MapServer/MapServer/pull/6355#issuecomment-877355007 + static const char *strRectTemplate = "(\"%s\" && %s) AND ST_Distance(\"%s\", %s) = 0"; + strRect = (char*)msSmallMalloc(strlen(strRectTemplate) + 2 * (strBoxLength + strlen(layerinfo->geomcolumn)) +1 ); + sprintf(strRect, strRectTemplate, layerinfo->geomcolumn, strBox, layerinfo->geomcolumn, strBox); + } } else {