ogridbtablelayer.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,052 行 · 第 1/3 页
CPP
1,052 行
/****************************************************************************** * $Id: ogridbtablelayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRIDBTableLayer class, access to an existing table * (based on ODBC and PG drivers). * Author: Oleg Semykin, oleg.semykin@gmail.com * ****************************************************************************** * Copyright (c) 2006, Oleg Semykin * * 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_idb.h"CPL_CVSID("$Id: ogridbtablelayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//* OGRIDBTableLayer() *//************************************************************************/OGRIDBTableLayer::OGRIDBTableLayer( OGRIDBDataSource *poDSIn ){ poDS = poDSIn; pszQuery = NULL; bUpdateAccess = TRUE; bHaveSpatialExtents = FALSE; iNextShapeId = 0; poFeatureDefn = NULL;}/************************************************************************//* ~OGRIDBTableLayer() *//************************************************************************/OGRIDBTableLayer::~OGRIDBTableLayer(){ CPLFree( pszQuery ); ClearQuery();}/************************************************************************//* Initialize() *//************************************************************************/CPLErr OGRIDBTableLayer::Initialize( const char *pszTableName, const char *pszGeomCol, int bUpdate ){ bUpdateAccess = bUpdate; ITConnection *poConn = poDS->GetConnection(); if ( pszFIDColumn ) { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; }/* -------------------------------------------------------------------- *//* Do we have a simple primary key? *//* -------------------------------------------------------------------- */ if ( pszFIDColumn == NULL ) { ITCursor oGetKey( *poConn ); CPLString osSql = " select sc.colname" " from syscolumns sc, sysindexes si, systables st" " where st.tabid = si.tabid" " and st.tabid = sc.tabid" " and si.idxtype = 'U'" " and sc.colno = si.part1" " and si.part2 = 0" // only one-column keys " and st.tabname='"; osSql += pszTableName; osSql += "'"; if( oGetKey.Prepare( osSql.c_str() ) && oGetKey.Open(ITCursor::ReadOnly) ) { ITValue * poVal = oGetKey.Fetch(); if ( poVal && poVal->IsNull() == false ) { pszFIDColumn = CPLStrdup(poVal->Printable()); poVal->Release(); } if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug("OGR_IDB", "Table %s has multiple primary key fields," " ignoring them all.", pszTableName ); } } }/* -------------------------------------------------------------------- *//* Have we been provided a geometry column? *//* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol );/* -------------------------------------------------------------------- *//* Get the column definitions for this table. *//* -------------------------------------------------------------------- */ ITCursor oGetCol( *poConn ); CPLErr eErr; CPLString sql; sql.Printf( "select * from %s where 1=0", pszTableName ); if( ! oGetCol.Prepare( sql.c_str() ) || ! oGetCol.Open(ITCursor::ReadOnly) ) return CE_Failure; eErr = BuildFeatureDefn( pszTableName, &oGetCol ); if( eErr != CE_None ) return eErr; if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszTableName ); return CE_Failure; }/* -------------------------------------------------------------------- *//* Do we have XMIN, YMIN, XMAX, YMAX extent fields? *//* -------------------------------------------------------------------- */ if( poFeatureDefn->GetFieldIndex( "XMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "XMAX" ) != -1 && poFeatureDefn->GetFieldIndex( "YMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "YMAX" ) != -1 ) { bHaveSpatialExtents = TRUE; CPLDebug( "OGR_IDB", "Table %s has geometry extent fields.", pszTableName ); }/* -------------------------------------------------------------------- *//* If we got a geometry column, does it exist? Is it binary? *//* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.RowType()->ColumnId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } bGeomColumnWKB = TRUE; /*else { if( ITCursor::GetTypeMapping( oGetCol.GetColType( iColumn )) == SQL_C_BINARY ) bGeomColumnWKB = TRUE; }*/ } return CE_None;}/************************************************************************//* ClearQuery() *//************************************************************************/void OGRIDBTableLayer::ClearQuery(){ if( poCurr != NULL ) { poCurr->Close(); delete poCurr; poCurr = NULL; }}/************************************************************************//* GetQuery() *//************************************************************************/ITCursor *OGRIDBTableLayer::GetQuery(){ if( poCurr == NULL ) ResetQuery(); return poCurr;}/************************************************************************//* ResetQuery() *//************************************************************************/OGRErr OGRIDBTableLayer::ResetQuery(){ ClearQuery(); iNextShapeId = 0; poCurr = new ITCursor( *poDS->GetConnection() ); // Create list of fields CPLString osFields; if ( pszGeomColumn ) { if ( ! osFields.empty() ) osFields += ","; osFields += "st_asbinary("; osFields += pszGeomColumn; osFields += ") as "; osFields += pszGeomColumn; } for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if ( ! osFields.empty() ) osFields += ","; osFields += poFeatureDefn->GetFieldDefn(i)->GetNameRef(); } CPLString sql; sql += "SELECT "; sql += osFields; sql += " FROM "; sql += poFeatureDefn->GetName(); /* Append attribute query if we have it */ if( pszQuery != NULL ) { sql += " WHERE "; sql += pszQuery; } /* If we have a spatial filter, and per record extents, query on it */ if( m_poFilterGeom != NULL && bHaveSpatialExtents ) { if( pszQuery == NULL ) sql += " WHERE"; else sql += " AND"; sql.Printf( "%s XMAX > %.8f AND XMIN < %.8f" " AND YMAX > %.8f AND YMIN < %.8f", sql.c_str(), m_sFilterEnvelope.MinX, m_sFilterEnvelope.MaxX, m_sFilterEnvelope.MinY, m_sFilterEnvelope.MaxY ); } CPLDebug( "OGR_IDB", "Exec(%s)", sql.c_str() ); if( poCurr->Prepare( sql.c_str() ) && poCurr->Open(ITCursor::ReadOnly) ) { return OGRERR_NONE; } else { delete poCurr; poCurr = NULL; return OGRERR_FAILURE; }}/************************************************************************//* ResetReading() *//************************************************************************/void OGRIDBTableLayer::ResetReading(){ ClearQuery(); OGRIDBLayer::ResetReading();}/************************************************************************//* GetFeature() *//************************************************************************/OGRFeature *OGRIDBTableLayer::GetFeature( long nFeatureId ){ if( pszFIDColumn == NULL ) return OGRIDBLayer::GetFeature( nFeatureId ); ClearQuery(); iNextShapeId = nFeatureId; poCurr = new ITCursor( *poDS->GetConnection() ); // Create list of fields CPLString osFields; if ( poFeatureDefn->GetFieldIndex( pszFIDColumn ) == -1 ) osFields += pszFIDColumn; if ( pszGeomColumn ) { if ( ! osFields.empty() ) osFields += ","; osFields += "st_asbinary("; osFields += pszGeomColumn; osFields += ") as "; osFields += pszGeomColumn; } for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if ( ! osFields.empty() ) osFields += ",";
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?