1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
|
/******************************************************************************
* $Id: ogrsqliteviewlayer.cpp 25388 2012-12-29 16:47:45Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements OGRSQLiteViewLayer class, access to an existing spatialite view.
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
*
******************************************************************************
* Copyright (c) 2011, Even Rouault, <even dot rouault at mines dash paris dot org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_sqlite.h"
#include <string>
CPL_CVSID("$Id: ogrsqliteviewlayer.cpp 25388 2012-12-29 16:47:45Z rouault $");
/************************************************************************/
/* OGRSQLiteViewLayer() */
/************************************************************************/
OGRSQLiteViewLayer::OGRSQLiteViewLayer( OGRSQLiteDataSource *poDSIn )
{
poDS = poDSIn;
iNextShapeId = 0;
poFeatureDefn = NULL;
pszViewName = NULL;
pszEscapedTableName = NULL;
pszEscapedUnderlyingTableName = NULL;
bHasCheckedSpatialIndexTable = FALSE;
bLayerDefnError = FALSE;
}
/************************************************************************/
/* ~OGRSQLiteViewLayer() */
/************************************************************************/
OGRSQLiteViewLayer::~OGRSQLiteViewLayer()
{
ClearStatement();
CPLFree(pszViewName);
CPLFree(pszEscapedTableName);
CPLFree(pszEscapedUnderlyingTableName);
}
/************************************************************************/
/* Initialize() */
/************************************************************************/
CPLErr OGRSQLiteViewLayer::Initialize( const char *pszViewName,
const char *pszViewGeometry,
const char *pszViewRowid,
const char *pszUnderlyingTableName,
const char *pszUnderlyingGeometryColumn)
{
this->pszViewName = CPLStrdup(pszViewName);
osGeomColumn = pszViewGeometry;
eGeomFormat = OSGF_SpatiaLite;
CPLFree( pszFIDColumn );
pszFIDColumn = CPLStrdup( pszViewRowid );
osUnderlyingTableName = pszUnderlyingTableName;
osUnderlyingGeometryColumn = pszUnderlyingGeometryColumn;
poUnderlyingLayer = NULL;
//this->bHasM = bHasM;
pszEscapedTableName = CPLStrdup(OGRSQLiteEscape(pszViewName));
pszEscapedUnderlyingTableName = CPLStrdup(OGRSQLiteEscape(pszUnderlyingTableName));
return CE_None;
}
/************************************************************************/
/* GetLayerDefn() */
/************************************************************************/
OGRFeatureDefn* OGRSQLiteViewLayer::GetLayerDefn()
{
if (poFeatureDefn)
return poFeatureDefn;
EstablishFeatureDefn();
if (poFeatureDefn == NULL)
{
bLayerDefnError = TRUE;
poFeatureDefn = new OGRFeatureDefn( pszViewName );
poFeatureDefn->Reference();
}
return poFeatureDefn;
}
/************************************************************************/
/* GetUnderlyingLayer() */
/************************************************************************/
OGRSQLiteLayer* OGRSQLiteViewLayer::GetUnderlyingLayer()
{
if( poUnderlyingLayer == NULL )
{
if( strchr(osUnderlyingTableName, '(') == NULL )
{
CPLString osNewUnderlyingTableName;
osNewUnderlyingTableName.Printf("%s(%s)",
osUnderlyingTableName.c_str(),
osUnderlyingGeometryColumn.c_str());
poUnderlyingLayer =
(OGRSQLiteLayer*) poDS->GetLayerByName(osNewUnderlyingTableName);
}
if( poUnderlyingLayer == NULL )
poUnderlyingLayer =
(OGRSQLiteLayer*) poDS->GetLayerByName(osUnderlyingTableName);
}
return poUnderlyingLayer;
}
/************************************************************************/
/* GetGeomType() */
/************************************************************************/
OGRwkbGeometryType OGRSQLiteViewLayer::GetGeomType()
{
if (poFeatureDefn)
return poFeatureDefn->GetGeomType();
OGRSQLiteLayer* poUnderlyingLayer = GetUnderlyingLayer();
if (poUnderlyingLayer)
return poUnderlyingLayer->GetGeomType();
return wkbUnknown;
}
/************************************************************************/
/* EstablishFeatureDefn() */
/************************************************************************/
CPLErr OGRSQLiteViewLayer::EstablishFeatureDefn()
{
int rc;
sqlite3 *hDB = poDS->GetDB();
sqlite3_stmt *hColStmt = NULL;
const char *pszSQL;
OGRSQLiteLayer* poUnderlyingLayer = GetUnderlyingLayer();
if (poUnderlyingLayer == NULL)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot find underlying layer %s for view %s",
osUnderlyingTableName.c_str(), pszViewName);
return CE_Failure;
}
if ( !poUnderlyingLayer->IsTableLayer() )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Underlying layer %s for view %s is not a regular table",
osUnderlyingTableName.c_str(), pszViewName);
return CE_Failure;
}
const char* pszRealUnderlyingGeometryColumn = poUnderlyingLayer->GetGeometryColumn();
if ( pszRealUnderlyingGeometryColumn == NULL ||
!EQUAL(pszRealUnderlyingGeometryColumn, osUnderlyingGeometryColumn.c_str()) )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Underlying layer %s for view %s has not expected geometry column name (%s instead of %s)",
osUnderlyingTableName.c_str(), pszViewName,
pszRealUnderlyingGeometryColumn ? pszRealUnderlyingGeometryColumn : "(null)",
osUnderlyingGeometryColumn.c_str());
return CE_Failure;
}
poSRS = poUnderlyingLayer->GetSpatialRef();
if (poSRS)
poSRS->Reference();
this->bHasSpatialIndex = poUnderlyingLayer->HasSpatialIndex();
/* -------------------------------------------------------------------- */
/* Get the column definitions for this table. */
/* -------------------------------------------------------------------- */
hColStmt = NULL;
pszSQL = CPLSPrintf( "SELECT \"%s\", * FROM '%s' LIMIT 1",
OGRSQLiteEscapeName(pszFIDColumn).c_str(),
pszEscapedTableName );
rc = sqlite3_prepare( hDB, pszSQL, strlen(pszSQL), &hColStmt, NULL );
if( rc != SQLITE_OK )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unable to query table %s for column definitions : %s.",
pszViewName, sqlite3_errmsg(hDB) );
return CE_Failure;
}
rc = sqlite3_step( hColStmt );
if ( rc != SQLITE_DONE && rc != SQLITE_ROW )
{
CPLError( CE_Failure, CPLE_AppDefined,
"In Initialize(): sqlite3_step(%s):\n %s",
pszSQL, sqlite3_errmsg(hDB) );
sqlite3_finalize( hColStmt );
return CE_Failure;
}
/* -------------------------------------------------------------------- */
/* Collect the rest of the fields. */
/* -------------------------------------------------------------------- */
std::set<CPLString> aosEmpty;
BuildFeatureDefn( pszViewName, hColStmt, aosEmpty );
sqlite3_finalize( hColStmt );
/* -------------------------------------------------------------------- */
/* Set the geometry type if we know it. */
/* -------------------------------------------------------------------- */
poFeatureDefn->SetGeomType( poUnderlyingLayer->GetGeomType() );
return CE_None;
}
/************************************************************************/
/* ResetStatement() */
/************************************************************************/
OGRErr OGRSQLiteViewLayer::ResetStatement()
{
int rc;
CPLString osSQL;
ClearStatement();
iNextShapeId = 0;
osSQL.Printf( "SELECT \"%s\", * FROM '%s' %s",
OGRSQLiteEscapeName(pszFIDColumn).c_str(),
pszEscapedTableName,
osWHERE.c_str() );
rc = sqlite3_prepare( poDS->GetDB(), osSQL, osSQL.size(),
&hStmt, NULL );
if( rc == SQLITE_OK )
{
return OGRERR_NONE;
}
else
{
CPLError( CE_Failure, CPLE_AppDefined,
"In ResetStatement(): sqlite3_prepare(%s):\n %s",
osSQL.c_str(), sqlite3_errmsg(poDS->GetDB()) );
hStmt = NULL;
return OGRERR_FAILURE;
}
}
/************************************************************************/
/* GetSpatialRef() */
/************************************************************************/
OGRSpatialReference *OGRSQLiteViewLayer::GetSpatialRef()
{
if (HasLayerDefnError())
return NULL;
return poSRS;
}
/************************************************************************/
/* GetNextFeature() */
/************************************************************************/
OGRFeature *OGRSQLiteViewLayer::GetNextFeature()
{
if (HasLayerDefnError())
return NULL;
return OGRSQLiteLayer::GetNextFeature();
}
/************************************************************************/
/* GetFeature() */
/************************************************************************/
OGRFeature *OGRSQLiteViewLayer::GetFeature( long nFeatureId )
{
if (HasLayerDefnError())
return NULL;
/* -------------------------------------------------------------------- */
/* If we don't have an explicit FID column, just read through */
/* the result set iteratively to find our target. */
/* -------------------------------------------------------------------- */
if( pszFIDColumn == NULL )
return OGRSQLiteLayer::GetFeature( nFeatureId );
/* -------------------------------------------------------------------- */
/* Setup explicit query statement to fetch the record we want. */
/* -------------------------------------------------------------------- */
CPLString osSQL;
int rc;
ClearStatement();
iNextShapeId = nFeatureId;
osSQL.Printf( "SELECT \"%s\", * FROM '%s' WHERE \"%s\" = %d",
OGRSQLiteEscapeName(pszFIDColumn).c_str(),
pszEscapedTableName,
OGRSQLiteEscapeName(pszFIDColumn).c_str(),
(int) nFeatureId );
CPLDebug( "OGR_SQLITE", "exec(%s)", osSQL.c_str() );
rc = sqlite3_prepare( poDS->GetDB(), osSQL, osSQL.size(),
&hStmt, NULL );
if( rc != SQLITE_OK )
{
CPLError( CE_Failure, CPLE_AppDefined,
"In GetFeature(): sqlite3_prepare(%s):\n %s",
osSQL.c_str(), sqlite3_errmsg(poDS->GetDB()) );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Get the feature if possible. */
/* -------------------------------------------------------------------- */
OGRFeature *poFeature = NULL;
poFeature = GetNextRawFeature();
ResetReading();
return poFeature;
}
/************************************************************************/
/* SetAttributeFilter() */
/************************************************************************/
OGRErr OGRSQLiteViewLayer::SetAttributeFilter( const char *pszQuery )
{
if( pszQuery == NULL )
osQuery = "";
else
osQuery = pszQuery;
BuildWhere();
ResetReading();
return OGRERR_NONE;
}
/************************************************************************/
/* SetSpatialFilter() */
/************************************************************************/
void OGRSQLiteViewLayer::SetSpatialFilter( OGRGeometry * poGeomIn )
{
if( InstallFilter( poGeomIn ) )
{
BuildWhere();
ResetReading();
}
}
/************************************************************************/
/* GetSpatialWhere() */
/************************************************************************/
CPLString OGRSQLiteViewLayer::GetSpatialWhere(OGRGeometry* poFilterGeom)
{
CPLString osSpatialWHERE;
if (HasLayerDefnError())
return "";
if( poFilterGeom != NULL && bHasSpatialIndex )
{
OGREnvelope sEnvelope;
poFilterGeom->getEnvelope( &sEnvelope );
/* We first check that the spatial index table exists */
if (!bHasCheckedSpatialIndexTable)
{
bHasCheckedSpatialIndexTable = TRUE;
char **papszResult;
int nRowCount, nColCount;
char *pszErrMsg = NULL;
CPLString osSQL;
osSQL.Printf("SELECT name FROM sqlite_master "
"WHERE name='idx_%s_%s'",
pszEscapedUnderlyingTableName,
OGRSQLiteEscape(osUnderlyingGeometryColumn).c_str());
int rc = sqlite3_get_table( poDS->GetDB(), osSQL.c_str(),
&papszResult, &nRowCount,
&nColCount, &pszErrMsg );
if( rc != SQLITE_OK )
{
CPLError( CE_Failure, CPLE_AppDefined, "Error: %s",
pszErrMsg );
sqlite3_free( pszErrMsg );
bHasSpatialIndex = FALSE;
}
else
{
if (nRowCount != 1)
{
bHasSpatialIndex = FALSE;
}
sqlite3_free_table(papszResult);
}
}
if (bHasSpatialIndex)
{
osSpatialWHERE.Printf("\"%s\" IN ( SELECT pkid FROM 'idx_%s_%s' WHERE "
"xmax > %.12f AND xmin < %.12f AND ymax > %.12f AND ymin < %.12f)",
OGRSQLiteEscapeName(pszFIDColumn).c_str(),
pszEscapedUnderlyingTableName,
OGRSQLiteEscape(osUnderlyingGeometryColumn).c_str(),
sEnvelope.MinX - 1e-11, sEnvelope.MaxX + 1e-11,
sEnvelope.MinY - 1e-11, sEnvelope.MaxY + 1e-11);
}
else
{
CPLDebug("SQLITE", "Count not find idx_%s_%s layer. Disabling spatial index",
pszEscapedUnderlyingTableName, osUnderlyingGeometryColumn.c_str());
}
}
if( poFilterGeom != NULL && OGRSQLiteIsSpatialiteLoaded() && !bHasSpatialIndex )
{
OGREnvelope sEnvelope;
poFilterGeom->getEnvelope( &sEnvelope );
/* A bit inefficient but still faster than OGR filtering */
osSpatialWHERE.Printf("MBRIntersects(\"%s\", BuildMBR(%.12f, %.12f, %.12f, %.12f))",
OGRSQLiteEscapeName(osGeomColumn).c_str(),
sEnvelope.MinX - 1e-11, sEnvelope.MinY - 1e-11,
sEnvelope.MaxX + 1e-11, sEnvelope.MaxY + 1e-11);
}
return osSpatialWHERE;
}
/************************************************************************/
/* BuildWhere() */
/* */
/* Build the WHERE statement appropriate to the current set of */
/* criteria (spatial and attribute queries). */
/************************************************************************/
void OGRSQLiteViewLayer::BuildWhere()
{
osWHERE = "";
CPLString osSpatialWHERE = GetSpatialWhere(m_poFilterGeom);
if (osSpatialWHERE.size() != 0)
{
osWHERE = "WHERE ";
osWHERE += osSpatialWHERE;
}
if( osQuery.size() > 0 )
{
if( osWHERE.size() == 0 )
{
osWHERE = "WHERE ";
osWHERE += osQuery;
}
else
{
osWHERE += " AND (";
osWHERE += osQuery;
osWHERE += ")";
}
}
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRSQLiteViewLayer::TestCapability( const char * pszCap )
{
if (HasLayerDefnError())
return FALSE;
if (EQUAL(pszCap,OLCFastFeatureCount))
return m_poFilterGeom == NULL || osGeomColumn.size() == 0 ||
bHasSpatialIndex;
else if (EQUAL(pszCap,OLCFastSpatialFilter))
return bHasSpatialIndex;
else
return OGRSQLiteLayer::TestCapability( pszCap );
}
/************************************************************************/
/* GetFeatureCount() */
/* */
/* If a spatial filter is in effect, we turn control over to */
/* the generic counter. Otherwise we return the total count. */
/* Eventually we should consider implementing a more efficient */
/* way of counting features matching a spatial query. */
/************************************************************************/
int OGRSQLiteViewLayer::GetFeatureCount( int bForce )
{
if (HasLayerDefnError())
return 0;
if( !TestCapability(OLCFastFeatureCount) )
return OGRSQLiteLayer::GetFeatureCount( bForce );
/* -------------------------------------------------------------------- */
/* Form count SQL. */
/* -------------------------------------------------------------------- */
const char *pszSQL;
pszSQL = CPLSPrintf( "SELECT count(*) FROM '%s' %s",
pszEscapedTableName, osWHERE.c_str() );
/* -------------------------------------------------------------------- */
/* Execute. */
/* -------------------------------------------------------------------- */
char **papszResult, *pszErrMsg;
int nRowCount, nColCount;
int nResult = -1;
if( sqlite3_get_table( poDS->GetDB(), pszSQL, &papszResult,
&nColCount, &nRowCount, &pszErrMsg ) != SQLITE_OK )
return -1;
if( nRowCount == 1 && nColCount == 1 )
nResult = atoi(papszResult[1]);
sqlite3_free_table( papszResult );
return nResult;
}
|