LTP GCOV extension - code coverage report
Current view: directory - ogr/ogrsf_frmts/pgeo - ogrpgeotablelayer.cpp
Test: gdal_filtered.info
Date: 2010-07-12 Instrumented lines: 126
Code covered: 0.0 % Executed lines: 0

       1                 : /******************************************************************************
       2                 :  * $Id: ogrpgeotablelayer.cpp 17755 2009-10-04 21:04:10Z rouault $
       3                 :  *
       4                 :  * Project:  OpenGIS Simple Features Reference Implementation
       5                 :  * Purpose:  Implements OGRPGeoTableLayer class, access to an existing table.
       6                 :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2005, Frank Warmerdam
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      12                 :  * copy of this software and associated documentation files (the "Software"),
      13                 :  * to deal in the Software without restriction, including without limitation
      14                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15                 :  * and/or sell copies of the Software, and to permit persons to whom the
      16                 :  * Software is furnished to do so, subject to the following conditions:
      17                 :  *
      18                 :  * The above copyright notice and this permission notice shall be included
      19                 :  * in all copies or substantial portions of the Software.
      20                 :  *
      21                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27                 :  * DEALINGS IN THE SOFTWARE.
      28                 :  ****************************************************************************/
      29                 : 
      30                 : #include "cpl_conv.h"
      31                 : #include "ogr_pgeo.h"
      32                 : 
      33                 : CPL_CVSID("$Id: ogrpgeotablelayer.cpp 17755 2009-10-04 21:04:10Z rouault $");
      34                 : 
      35                 : /************************************************************************/
      36                 : /*                          OGRPGeoTableLayer()                         */
      37                 : /************************************************************************/
      38                 : 
      39               0 : OGRPGeoTableLayer::OGRPGeoTableLayer( OGRPGeoDataSource *poDSIn )
      40                 : 
      41                 : {
      42               0 :     poDS = poDSIn;
      43               0 :     pszQuery = NULL;
      44               0 :     bUpdateAccess = TRUE;
      45               0 :     iNextShapeId = 0;
      46               0 :     nSRSId = -1;
      47               0 :     poFeatureDefn = NULL;
      48               0 :     memset( &sExtent, 0, sizeof(sExtent) );
      49               0 : }
      50                 : 
      51                 : /************************************************************************/
      52                 : /*                          ~OGRPGeoTableLayer()                          */
      53                 : /************************************************************************/
      54                 : 
      55               0 : OGRPGeoTableLayer::~OGRPGeoTableLayer()
      56                 : 
      57                 : {
      58               0 :     CPLFree( pszQuery );
      59               0 :     ClearStatement();
      60               0 : }
      61                 : 
      62                 : /************************************************************************/
      63                 : /*                             Initialize()                             */
      64                 : /************************************************************************/
      65                 : 
      66                 : CPLErr OGRPGeoTableLayer::Initialize( const char *pszTableName, 
      67                 :                                       const char *pszGeomCol,
      68                 :                                       int nShapeType, 
      69                 :                                       double dfExtentLeft,
      70                 :                                       double dfExtentRight,
      71                 :                                       double dfExtentBottom,
      72                 :                                       double dfExtentTop,
      73                 :                                       int nSRID,
      74               0 :                                       int bHasZ )
      75                 : 
      76                 : 
      77                 : {
      78               0 :     CPLODBCSession *poSession = poDS->GetSession();
      79                 : 
      80               0 :     CPLFree( pszGeomColumn );
      81               0 :     if( pszGeomCol == NULL )
      82               0 :         pszGeomColumn = NULL;
      83                 :     else
      84               0 :         pszGeomColumn = CPLStrdup( pszGeomCol );
      85                 : 
      86               0 :     CPLFree( pszFIDColumn );
      87               0 :     pszFIDColumn = NULL;
      88                 : 
      89               0 :     sExtent.MinX = dfExtentLeft;
      90               0 :     sExtent.MaxX = dfExtentRight;
      91               0 :     sExtent.MinY = dfExtentBottom;
      92               0 :     sExtent.MaxY = dfExtentTop;
      93                 : 
      94               0 :     LookupSRID( nSRID );
      95                 : 
      96                 : /* -------------------------------------------------------------------- */
      97                 : /*      Setup geometry type.                                            */
      98                 : /* -------------------------------------------------------------------- */
      99                 :     OGRwkbGeometryType  eOGRType;
     100                 : 
     101               0 :     switch( nShapeType )
     102                 :     {
     103                 :         case SHPT_POINT:
     104                 :         case SHPT_POINTM:
     105                 :         case SHPT_POINTZ:
     106                 :         case SHPT_POINTZM:
     107               0 :             eOGRType = wkbPoint;
     108               0 :             break;
     109                 : 
     110                 :         case SHPT_ARC:
     111                 :         case SHPT_ARCZ:
     112                 :         case SHPT_ARCM:
     113                 :         case SHPT_ARCZM:
     114               0 :             eOGRType = wkbLineString;
     115               0 :             break;
     116                 :             
     117                 :         case SHPT_MULTIPOINT:
     118                 :         case SHPT_MULTIPOINTZ:
     119                 :         case SHPT_MULTIPOINTM:
     120                 :         case SHPT_MULTIPOINTZM:
     121               0 :             eOGRType = wkbMultiPoint;
     122               0 :             break;
     123                 : 
     124                 :         default:
     125               0 :             eOGRType = wkbUnknown;
     126                 :             break;
     127                 :     }
     128                 : 
     129               0 :     if( eOGRType != wkbUnknown && bHasZ )
     130               0 :         eOGRType = (OGRwkbGeometryType)(((int) eOGRType) | wkb25DBit);
     131                 : 
     132                 : /* -------------------------------------------------------------------- */
     133                 : /*      Do we have a simple primary key?                                */
     134                 : /* -------------------------------------------------------------------- */
     135               0 :     CPLODBCStatement oGetKey( poSession );
     136                 :     
     137               0 :     if( oGetKey.GetPrimaryKeys( pszTableName ) && oGetKey.Fetch() )
     138                 :     {
     139               0 :         pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 ));
     140                 :         
     141               0 :         if( oGetKey.Fetch() ) // more than one field in key! 
     142                 :         {
     143               0 :             CPLFree( pszFIDColumn );
     144               0 :             pszFIDColumn = NULL;
     145                 :             CPLDebug( "PGeo", "%s: Compound primary key, ignoring.",
     146               0 :                       pszTableName );
     147                 :         }
     148                 :         else
     149                 :             CPLDebug( "PGeo", 
     150                 :                       "%s: Got primary key %s.",
     151               0 :                       pszTableName, pszFIDColumn );
     152                 :     }
     153                 :     else
     154               0 :         CPLDebug( "PGeo", "%s: no primary key", pszTableName );
     155                 : 
     156                 : /* -------------------------------------------------------------------- */
     157                 : /*      Get the column definitions for this table.                      */
     158                 : /* -------------------------------------------------------------------- */
     159               0 :     CPLODBCStatement oGetCol( poSession );
     160                 :     CPLErr eErr;
     161                 : 
     162               0 :     if( !oGetCol.GetColumns( pszTableName ) )
     163                 :     {
     164                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     165                 :                   "GetColumns() failed on %s.\n%s",
     166               0 :                   pszTableName, poSession->GetLastError() );
     167               0 :         return CE_Failure;
     168                 :     }
     169                 : 
     170               0 :     eErr = BuildFeatureDefn( pszTableName, &oGetCol );
     171               0 :     if( eErr != CE_None )
     172               0 :         return eErr;
     173                 : 
     174               0 :     if( poFeatureDefn->GetFieldCount() == 0 )
     175                 :     {
     176                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     177                 :                   "No column definitions found for table '%s', layer not usable.", 
     178               0 :                   pszTableName );
     179               0 :         return CE_Failure;
     180                 :     }
     181                 : 
     182                 : /* -------------------------------------------------------------------- */
     183                 : /*      Set geometry type.                                              */
     184                 : /*                                                                      */
     185                 : /*      NOTE: per reports from Craig Miller, it seems we cannot really  */
     186                 : /*      trust the ShapeType value.  At the very least "line" tables     */
     187                 : /*      sometimes have multilinestrings.  So for now we just always     */
     188                 : /*      return wkbUnknown.                                              */
     189                 : /*                                                                      */
     190                 : /*      TODO - mloskot: Similar issue has been reported in Ticket #1484 */
     191                 : /* -------------------------------------------------------------------- */
     192                 : #ifdef notdef
     193                 :     poFeatureDefn->SetGeomType( eOGRType );
     194                 : #endif
     195                 : 
     196               0 :     return CE_None;
     197                 : }
     198                 : 
     199                 : /************************************************************************/
     200                 : /*                           ClearStatement()                           */
     201                 : /************************************************************************/
     202                 : 
     203               0 : void OGRPGeoTableLayer::ClearStatement()
     204                 : 
     205                 : {
     206               0 :     if( poStmt != NULL )
     207                 :     {
     208               0 :         delete poStmt;
     209               0 :         poStmt = NULL;
     210                 :     }
     211               0 : }
     212                 : 
     213                 : /************************************************************************/
     214                 : /*                            GetStatement()                            */
     215                 : /************************************************************************/
     216                 : 
     217               0 : CPLODBCStatement *OGRPGeoTableLayer::GetStatement()
     218                 : 
     219                 : {
     220               0 :     if( poStmt == NULL )
     221               0 :         ResetStatement();
     222                 : 
     223               0 :     return poStmt;
     224                 : }
     225                 : 
     226                 : /************************************************************************/
     227                 : /*                           ResetStatement()                           */
     228                 : /************************************************************************/
     229                 : 
     230               0 : OGRErr OGRPGeoTableLayer::ResetStatement()
     231                 : 
     232                 : {
     233               0 :     ClearStatement();
     234                 : 
     235               0 :     iNextShapeId = 0;
     236                 : 
     237               0 :     poStmt = new CPLODBCStatement( poDS->GetSession() );
     238               0 :     poStmt->Append( "SELECT * FROM " );
     239               0 :     poStmt->Append( poFeatureDefn->GetName() );
     240               0 :     if( pszQuery != NULL )
     241               0 :         poStmt->Appendf( " WHERE %s", pszQuery );
     242                 : 
     243               0 :     if( poStmt->ExecuteSQL() )
     244               0 :         return OGRERR_NONE;
     245                 :     else
     246                 :     {
     247               0 :         delete poStmt;
     248               0 :         poStmt = NULL;
     249               0 :         return OGRERR_FAILURE;
     250                 :     }
     251                 : }
     252                 : 
     253                 : /************************************************************************/
     254                 : /*                            ResetReading()                            */
     255                 : /************************************************************************/
     256                 : 
     257               0 : void OGRPGeoTableLayer::ResetReading()
     258                 : 
     259                 : {
     260               0 :     ClearStatement();
     261               0 :     OGRPGeoLayer::ResetReading();
     262               0 : }
     263                 : 
     264                 : /************************************************************************/
     265                 : /*                             GetFeature()                             */
     266                 : /************************************************************************/
     267                 : 
     268               0 : OGRFeature *OGRPGeoTableLayer::GetFeature( long nFeatureId )
     269                 : 
     270                 : {
     271               0 :     if( pszFIDColumn == NULL )
     272               0 :         return OGRPGeoLayer::GetFeature( nFeatureId );
     273                 : 
     274               0 :     ClearStatement();
     275                 : 
     276               0 :     iNextShapeId = nFeatureId;
     277                 : 
     278               0 :     poStmt = new CPLODBCStatement( poDS->GetSession() );
     279               0 :     poStmt->Append( "SELECT * FROM " );
     280               0 :     poStmt->Append( poFeatureDefn->GetName() );
     281               0 :     poStmt->Appendf( " WHERE %s = %d", pszFIDColumn, nFeatureId );
     282                 : 
     283               0 :     if( !poStmt->ExecuteSQL() )
     284                 :     {
     285               0 :         delete poStmt;
     286               0 :         poStmt = NULL;
     287               0 :         return NULL;
     288                 :     }
     289                 : 
     290               0 :     return GetNextRawFeature();
     291                 : }
     292                 : 
     293                 : /************************************************************************/
     294                 : /*                         SetAttributeFilter()                         */
     295                 : /************************************************************************/
     296                 : 
     297               0 : OGRErr OGRPGeoTableLayer::SetAttributeFilter( const char *pszQuery )
     298                 : 
     299                 : {
     300               0 :     if( (pszQuery == NULL && this->pszQuery == NULL)
     301                 :         || (pszQuery != NULL && this->pszQuery != NULL 
     302                 :             && EQUAL(pszQuery,this->pszQuery)) )
     303               0 :         return OGRERR_NONE;
     304                 : 
     305               0 :     CPLFree( this->pszQuery );
     306               0 :     this->pszQuery = CPLStrdup( pszQuery );
     307                 : 
     308               0 :     ClearStatement();
     309                 : 
     310               0 :     return OGRERR_NONE;
     311                 : }
     312                 : 
     313                 : 
     314                 : /************************************************************************/
     315                 : /*                           TestCapability()                           */
     316                 : /************************************************************************/
     317                 : 
     318               0 : int OGRPGeoTableLayer::TestCapability( const char * pszCap )
     319                 : 
     320                 : {
     321               0 :     if( EQUAL(pszCap,OLCRandomRead) )
     322               0 :         return TRUE;
     323                 : 
     324               0 :     else if( EQUAL(pszCap,OLCFastFeatureCount) )
     325               0 :         return m_poFilterGeom == NULL;
     326                 : 
     327               0 :     else if( EQUAL(pszCap,OLCFastSpatialFilter) )
     328               0 :         return FALSE;
     329                 : 
     330                 :     else 
     331               0 :         return OGRPGeoLayer::TestCapability( pszCap );
     332                 : }
     333                 : 
     334                 : /************************************************************************/
     335                 : /*                          GetFeatureCount()                           */
     336                 : /*                                                                      */
     337                 : /*      If a spatial filter is in effect, we turn control over to       */
     338                 : /*      the generic counter.  Otherwise we return the total count.      */
     339                 : /*      Eventually we should consider implementing a more efficient     */
     340                 : /*      way of counting features matching a spatial query.              */
     341                 : /************************************************************************/
     342                 : 
     343               0 : int OGRPGeoTableLayer::GetFeatureCount( int bForce )
     344                 : 
     345                 : {
     346               0 :     if( m_poFilterGeom != NULL )
     347               0 :         return OGRPGeoLayer::GetFeatureCount( bForce );
     348                 : 
     349               0 :     CPLODBCStatement oStmt( poDS->GetSession() );
     350               0 :     oStmt.Append( "SELECT COUNT(*) FROM " );
     351               0 :     oStmt.Append( poFeatureDefn->GetName() );
     352                 : 
     353               0 :     if( pszQuery != NULL )
     354               0 :         oStmt.Appendf( " WHERE %s", pszQuery );
     355                 : 
     356               0 :     if( !oStmt.ExecuteSQL() || !oStmt.Fetch() )
     357                 :     {
     358                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     359                 :                   "GetFeatureCount() failed on query %s.\n%s",
     360               0 :                   oStmt.GetCommand(), poDS->GetSession()->GetLastError() );
     361               0 :         return OGRPGeoLayer::GetFeatureCount(bForce);
     362                 :     }
     363                 : 
     364               0 :     return atoi(oStmt.GetColData(0));
     365                 : }
     366                 : 
     367                 : /************************************************************************/
     368                 : /*                             GetExtent()                              */
     369                 : /************************************************************************/
     370                 : 
     371               0 : OGRErr OGRPGeoTableLayer::GetExtent( OGREnvelope *psExtent, int bForce )
     372                 : 
     373                 : {
     374               0 :     *psExtent = sExtent;
     375               0 :     return OGRERR_NONE;
     376                 : }

Generated by: LTP GCOV extension version 1.5