Skip to content

Commit

Permalink
spatialindex for sqlite sources in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
WouterVisscher authored and github-actions[bot] committed Jul 8, 2021
1 parent d304099 commit 955d14e
Showing 1 changed file with 62 additions and 60 deletions.
122 changes: 62 additions & 60 deletions mapogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2272,66 +2272,6 @@ static int msOGRFileWhichShapes(layerObj *layer, rectObj rect, msOGRFileInfo *ps

bool bSpatialiteOrGPKGAddOrderByFID = false;

if( psInfo->dialect && psInfo->pszMainTableName != NULL &&
( (EQUAL(psInfo->dialect, "Spatialite") && psInfo->bHasSpatialIndex)
|| EQUAL(psInfo->dialect, "GPKG") ) &&
bIsValidRect )
{
select = msStringConcatenate(select, " JOIN ");

char szSpatialIndexName[256];
snprintf( szSpatialIndexName, sizeof(szSpatialIndexName),
"%s_%s_%s",
EQUAL(psInfo->dialect, "Spatialite") ? "idx" : "rtree",
psInfo->pszSpatialFilterTableName,
psInfo->pszSpatialFilterGeometryColumn );
char* pszEscapedSpatialIndexName = msLayerEscapePropertyName(
layer, szSpatialIndexName);
select = msStringConcatenate(select, "\"");
select = msStringConcatenate(select, pszEscapedSpatialIndexName);
msFree(pszEscapedSpatialIndexName);
select = msStringConcatenate(select, "\" ms_spat_idx ON \"");
char* pszEscapedMainTableName = msLayerEscapePropertyName(
layer, psInfo->pszMainTableName);
select = msStringConcatenate(select, pszEscapedMainTableName);
msFree(pszEscapedMainTableName);
select = msStringConcatenate(select, "\".");
if( psInfo->pszRowId )
{
char* pszEscapedRowId = msLayerEscapePropertyName(
layer, psInfo->pszRowId);
select = msStringConcatenate(select, "\"");
select = msStringConcatenate(select, pszEscapedRowId);
select = msStringConcatenate(select, "\"");
msFree(pszEscapedRowId);
}
else
select = msStringConcatenate(select, "ROWID");
if( EQUAL(psInfo->dialect, "Spatialite") )
select = msStringConcatenate(select, " = ms_spat_idx.pkid AND ");
else
select = msStringConcatenate(select, " = ms_spat_idx.id AND ");

char szCond[256];
if( EQUAL(psInfo->dialect, "Spatialite") )
{
snprintf(szCond, sizeof(szCond),
"ms_spat_idx.xmin <= %.15g AND ms_spat_idx.xmax >= %.15g AND "
"ms_spat_idx.ymin <= %.15g AND ms_spat_idx.ymax >= %.15g",
rect.maxx, rect.minx, rect.maxy, rect.miny);
}
else
{
snprintf(szCond, sizeof(szCond),
"ms_spat_idx.minx <= %.15g AND ms_spat_idx.maxx >= %.15g AND "
"ms_spat_idx.miny <= %.15g AND ms_spat_idx.maxy >= %.15g",
rect.maxx, rect.minx, rect.maxy, rect.miny);
}
select = msStringConcatenate(select, szCond);

bSpatialiteOrGPKGAddOrderByFID = true;
}

const char *sql = layer->filter.native_string;
if (psInfo->dialect && sql && *sql != '\0' &&
(EQUAL(psInfo->dialect, "Spatialite") ||
Expand Down Expand Up @@ -2373,6 +2313,68 @@ static int msOGRFileWhichShapes(layerObj *layer, rectObj rect, msOGRFileInfo *ps
EQUAL(psInfo->dialect, "GPKG")) &&
psInfo->pszMainTableName != NULL )
{

if( psInfo->dialect && psInfo->pszMainTableName != NULL &&
( (EQUAL(psInfo->dialect, "Spatialite") && psInfo->bHasSpatialIndex)
|| EQUAL(psInfo->dialect, "GPKG") ) &&
bIsValidRect )
{
char* pszEscapedMainTableName = msLayerEscapePropertyName(
layer, psInfo->pszMainTableName);
filter = msStringConcatenate(filter, "\"");
filter = msStringConcatenate(filter, pszEscapedMainTableName);
filter = msStringConcatenate(filter, "\".");
msFree(pszEscapedMainTableName);
filter = msStringConcatenate(filter, "ROWID");

filter = msStringConcatenate(filter, " IN ");
filter = msStringConcatenate(filter, "(");
filter = msStringConcatenate(filter, "SELECT ");

if( EQUAL(psInfo->dialect, "Spatialite") )
filter = msStringConcatenate(filter, "ms_spat_idx.pkid");
else
filter = msStringConcatenate(filter, "ms_spat_idx.id");

filter = msStringConcatenate(filter, " FROM ");

char szSpatialIndexName[256];
snprintf( szSpatialIndexName, sizeof(szSpatialIndexName),
"%s_%s_%s",
EQUAL(psInfo->dialect, "Spatialite") ? "idx" : "rtree",
psInfo->pszSpatialFilterTableName,
psInfo->pszSpatialFilterGeometryColumn );
char* pszEscapedSpatialIndexName = msLayerEscapePropertyName(
layer, szSpatialIndexName);

filter = msStringConcatenate(filter, "\"");
filter = msStringConcatenate(filter, pszEscapedSpatialIndexName);
msFree(pszEscapedSpatialIndexName);

filter = msStringConcatenate(filter, "\" ms_spat_idx WHERE ");

char szCond[256];
if( EQUAL(psInfo->dialect, "Spatialite") )
{
snprintf(szCond, sizeof(szCond),
"ms_spat_idx.xmin <= %.15g AND ms_spat_idx.xmax >= %.15g AND "
"ms_spat_idx.ymin <= %.15g AND ms_spat_idx.ymax >= %.15g",
rect.maxx, rect.minx, rect.maxy, rect.miny);
}
else
{
snprintf(szCond, sizeof(szCond),
"ms_spat_idx.minx <= %.15g AND ms_spat_idx.maxx >= %.15g AND "
"ms_spat_idx.miny <= %.15g AND ms_spat_idx.maxy >= %.15g",
rect.maxx, rect.minx, rect.maxy, rect.miny);
}
filter = msStringConcatenate(filter, szCond);

filter = msStringConcatenate(filter, ")");

bSpatialiteOrGPKGAddOrderByFID = true;
}

const bool isGPKG = EQUAL(psInfo->dialect, "GPKG");
if (filter) filter = msStringConcatenate(filter, " AND");
const char *col = OGR_L_GetGeometryColumn(psInfo->hLayer); // which geom field??
Expand Down

0 comments on commit 955d14e

Please sign in to comment.