ogridbtablelayer.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,052 行 · 第 1/3 页

CPP
1,052
字号
        osFields += poFeatureDefn->GetFieldDefn(i)->GetNameRef();    }    CPLString sql;    sql.Printf( "SELECT %s FROM %s WHERE %s = %d",                osFields.c_str(), poFeatureDefn->GetName(),                pszFIDColumn, nFeatureId );    CPLDebug( "OGR_IDB", "ExecuteSQL(%s)", sql.c_str() );    if( !poCurr->Prepare( sql.c_str() ) ||        !poCurr->Open(ITCursor::ReadOnly) )    {        delete poCurr;        poCurr = NULL;        return NULL;    }    return GetNextRawFeature();}/************************************************************************//*                         SetAttributeFilter()                         *//************************************************************************/OGRErr OGRIDBTableLayer::SetAttributeFilter( const char *pszQuery ){    if( (pszQuery == NULL && this->pszQuery == NULL)        || (pszQuery != NULL && this->pszQuery != NULL             && EQUAL(pszQuery,this->pszQuery)) )        return OGRERR_NONE;    CPLFree( this->pszQuery );    this->pszQuery = CPLStrdup( pszQuery );    ClearQuery();    return OGRERR_NONE;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int OGRIDBTableLayer::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCSequentialWrite) ||        EQUAL(pszCap,OLCRandomWrite) )        return bUpdateAccess;    else if( EQUAL(pszCap,OLCRandomRead) )        return TRUE;    else        return OGRIDBLayer::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 OGRIDBTableLayer::GetFeatureCount( int bForce ){    return OGRIDBLayer::GetFeatureCount( bForce );}/************************************************************************//*                           GetSpatialRef()                            *//*                                                                      *//*      We override this to try and fetch the table SRID from the       *//*      geometry_columns table if the srsid is -2 (meaning we           *//*      haven't yet even looked for it).                                *//************************************************************************/OGRSpatialReference *OGRIDBTableLayer::GetSpatialRef(){    if( nSRSId == -2 )    {        nSRSId = -1;        if ( ! pszGeomColumn )            return NULL;        CPLString osCmd;        osCmd.Printf( " SELECT FIRST 1 srid, trim(srtext)"                      " FROM spatial_ref_sys, %s"                      " WHERE srid = ST_Srid(%s) ",                      poFeatureDefn->GetName(), pszGeomColumn );        ITCursor oSridCur( *poDS->GetConnection() );        if( oSridCur.Prepare( osCmd.c_str() )&&            oSridCur.Open( ITCursor::ReadOnly ) )        {            ITRow * row = static_cast<ITRow *>( oSridCur.NextRow() );            if ( row && ! row->IsNull() )            {                nSRSId = atoi(row->Column(0)->Printable());                const char * wkt = row->Column(1)->Printable();                if ( poSRS )                {                    // Hmm ... it should be null                    delete poSRS;                }                poSRS = new OGRSpatialReference();                if ( poSRS->importFromWkt( (char **)&wkt ) != OGRERR_NONE )                {                    CPLError( CE_Warning, CPLE_AppDefined,                              "Error parse srs wkt: %s", wkt );                    delete poSRS;                    poSRS = NULL;                }            }        }    }    return OGRIDBLayer::GetSpatialRef();}#if 0OGRErr OGRIDBTableLayer::SetFeature( OGRFeature *poFeature ){    OGRErr eErr(OGRERR_FAILURE);    if ( ! bUpdateAccess )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "Error update feature. Layer is read only." );        return eErr;    }    if( NULL == poFeature )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "NULL pointer to OGRFeature passed to SetFeature()." );        return eErr;    }    if( poFeature->GetFID() == OGRNullFID )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "FID required on features given to SetFeature()." );        return eErr;    }    ITStatement oQuery( *poDS->GetConnection() );    int bUpdateGeom = TRUE;    OGRwkbGeometryType nGeomType = poFeature->GetGeometryRef()->getGeometryType();    CPLString osGeomFunc;    int nSrid = 0; // FIXME Obtain geometry SRID    switch (nGeomType)    {        case wkbPoint:            osGeomFunc = "ST_PointFromText";            break;        case wkbLineString:            osGeomFunc = "ST_LineFromText";            break;        case wkbPolygon:            osGeomFunc = "ST_PolyFromText";            break;        case wkbMultiPoint:            osGeomFunc = "ST_MPointFromText";            break;        case wkbMultiLineString:            osGeomFunc = "ST_MLineFromText";            break;        case wkbMultiPolygon:            osGeomFunc = "ST_MPolyFromText";            break;        default:            bUpdateGeom = FALSE;            CPLDebug("OGR_IDB", "SetFeature(): Unknown geometry type. Geometry will not be updated.");    }    // Create query    CPLString osSql;    CPLString osFields;    if ( pszGeomColumn && bUpdateGeom )    {        osFields.Printf( "%s = %s( ?, %d )", pszGeomColumn, osGeomFunc.c_str(), nSrid );    }    for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        const char * pszFieldName = poFeatureDefn->GetFieldDefn(i)->GetNameRef();        // skip fid column from update        if ( EQUAL( pszFIDColumn, pszFieldName ) )            continue;        if ( ! osFields.empty() )        {            osFields += ",";        }        osFields += pszFieldName;        osFields += "=?";    }    osSql.Printf( "UPDATE %s SET %s WHERE %s = %d",                  poFeatureDefn->GetName(),                  osFields.c_str(),                  pszFIDColumn,                  poFeature->GetFID() );    if ( ! oQuery.Prepare( osSql.c_str() ) )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "Error prepare SQL.\n%s",osSql.c_str() );        return eErr;    }    int iParam = 0;    if ( pszGeomColumn && bUpdateGeom )    {        ITValue * par = oQuery.Param( iParam ); // it should be a geom value        if ( ! par )        {            CPLError( CE_Failure, CPLE_AppDefined,                      "Error prepare geom param");            return eErr;        }        OGRGeometry * poGeom = poFeature->GetGeometryRef();        char * wkt;        poGeom->exportToWkt( &wkt );        if( ! par->FromPrintable( wkt ) )        {            CPLError( CE_Failure, CPLE_AppDefined,                      "Error prepare geom param");            par->Release();            return eErr;        }        CPLFree( wkt );        par->Release();        iParam++;    }    for ( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        ITValue * par = oQuery.Param( iParam );        if ( ! par )        {            CPLError( CE_Failure, CPLE_AppDefined,                      "Error prepare param %d", iParam);            return eErr;        }        if ( ! poFeature->IsFieldSet( i ) )        {            if ( ! par->SetNull() )            {                CPLError( CE_Failure, CPLE_AppDefined,                        "Error set param %d to NULL", iParam);                par->Release();                return eErr;            }            par->Release();            continue;        }        ITConversions * cv = 0;        bool res = FALSE;        if ( par->QueryInterface( ITConversionsIID, (void **) &cv) !=             IT_QUERYINTERFACE_SUCCESS )        {            CPLError( CE_Failure, CPLE_AppDefined,                    "Error prepare param %d", iParam);            par->Release();            return eErr;        }        switch ( poFeatureDefn->GetFieldDefn( i )->GetType() )        {            case OFTInteger:                res = cv->ConvertFrom( poFeature->GetFieldAsInteger( i ) );                break;            case OFTReal:                res = cv->ConvertFrom( poFeature->GetFieldAsDouble( i ) );                break;            case OFTIntegerList:            case OFTRealList:            case OFTStringList:            case OFTWideStringList:                // FIXME Prepare array of values field                //cv->ConvertFrom( poFeature->GetFieldAsStringList( i ) );                //break;            case OFTBinary:                // FIXME Prepare binary field            case OFTString:            case OFTWideString:            case OFTDate:            case OFTTime:            case OFTDateTime:                res = cv->ConvertFrom( poFeature->GetFieldAsString( i ) );                break;            default:                CPLError( CE_Failure, CPLE_AppDefined,                        "Error prepare param %d. Unknown data type.", iParam);                cv->Release();                par->Release();                return eErr;        }        if ( res != TRUE )            CPLError( CE_Failure, CPLE_AppDefined,                      "Error prepare param.");        cv->Release();        par->Release();    }    CPLDebug( "OGR_IDB", "ExecuteSQL(%s)", oQuery.QueryText().Data() );    if( !oQuery.Exec() )    {        CPLError( CE_Failure, CPLE_AppDefined, "Error update Feature.");        return eErr;    }    return OGRERR_NONE;}#endifOGRErr OGRIDBTableLayer::SetFeature( OGRFeature *poFeature ){    OGRErr eErr(OGRERR_FAILURE);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?