Skip to content

Commit

Permalink
add log messages to OGR provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Nov 26, 2011
1 parent 414729c commit 7c40bf3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgsogrdataitems.h"

#include "qgslogger.h"
#include "qgsmessagelog.h"

#include <QFileInfo>
#include <QTextStream>
Expand Down Expand Up @@ -93,7 +94,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) );
return false;
}
OSRDestroySpatialReference( hSRS );
Expand All @@ -109,7 +110,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) );
return false;
}

Expand Down
89 changes: 43 additions & 46 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -17,6 +17,7 @@ email : sherman at mrcc.com

#include "qgsogrprovider.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"

#define CPL_SUPRESS_CPLUSPLUS
#include <gdal.h> // to collect version information
Expand Down Expand Up @@ -58,7 +59,8 @@ class QgsCPLErrorHandler
{
static void CPL_STDCALL showError( CPLErr errClass, int errNo, const char *msg )
{
QgsLogger::warning( QString( "OGR[%1] error %2: %3" ).arg( errClass ).arg( errNo ).arg( msg ) );
if ( errNo != OGRERR_NONE )
QgsMessageLog::logMessage( QObject::tr( "OGR[%1] error %2: %3" ).arg( errClass ).arg( errNo ).arg( msg ), QObject::tr( "OGR" ) );
}

public:
Expand Down Expand Up @@ -189,13 +191,14 @@ QgsVectorLayerImport::ImportError QgsOgrProvider::createEmptyLayer(


QgsOgrProvider::QgsOgrProvider( QString const & uri )
: QgsVectorDataProvider( uri ),
ogrDataSource( 0 ),
extent_( 0 ),
ogrLayer( 0 ),
ogrOrigLayer( 0 ),
ogrDriver( 0 ),
featuresCounted( -1 )
: QgsVectorDataProvider( uri )
, ogrDataSource( 0 )
, extent_( 0 )
, ogrLayer( 0 )
, ogrOrigLayer( 0 )
, ogrDriver( 0 )
, valid( false )
, featuresCounted( -1 )
{
QgsCPLErrorHandler handler;

Expand Down Expand Up @@ -272,14 +275,13 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
//TODO is in read-only mode, otherwise edit ops will fail
//TODO: capabilities() should now reflect this; need to test.
}

if ( ogrDataSource )
{

QgsDebugMsg( "Data source is valid" );
QgsDebugMsg( "OGR Driver was " + QString( OGR_Dr_GetName( ogrDriver ) ) );

valid = true;

ogrDriverName = OGR_Dr_GetName( ogrDriver );

// We get the layer which was requested by the uri. The layername
Expand All @@ -294,20 +296,14 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
}

ogrLayer = ogrOrigLayer;
if (ogrLayer != NULL )
if ( ogrLayer )
{
setSubsetString( mSubsetString );
}
else
{
valid = false;
valid = setSubsetString( mSubsetString );
}
}
else
{
QgsLogger::critical( "Data source is invalid" );
QgsLogger::critical( QString::fromUtf8( CPLGetLastErrorMsg() ) );
valid = false;
QgsMessageLog::logMessage( tr( "Data source is invalid (%1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ), tr( "OGR" ) );
}

// FIXME: sync with app/qgsnewvectorlayerdialog.cpp
Expand Down Expand Up @@ -426,10 +422,11 @@ QString QgsOgrProvider::subsetString()
QStringList QgsOgrProvider::subLayers() const
{
QStringList theList = QStringList();
if ( ! valid )
if ( !valid )
{
return theList;
}

for ( unsigned int i = 0; i < layerCount() ; i++ )
{
QString theLayerName = FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( OGR_DS_GetLayer( ogrDataSource, i ) ) ) );
Expand Down Expand Up @@ -630,7 +627,7 @@ bool QgsOgrProvider::nextFeature( QgsFeature& feature )

if ( !valid )
{
QgsLogger::warning( "Read attempt on an invalid shapefile data source" );
QgsMessageLog::logMessage( tr( "Read attempt on an invalid shapefile data source" ), tr( "OGR" ) );
return false;
}

Expand Down Expand Up @@ -994,15 +991,15 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
break;

default:
QgsLogger::warning( "QgsOgrProvider::addFeature, no type found" );
QgsMessageLog::logMessage( tr( "type %1 for attribute %2 not found" ).arg( type ).arg( targetAttributeId ), tr( "OGR" ) );
break;
}
}
}

if ( OGR_L_CreateFeature( ogrLayer, feature ) != OGRERR_NONE )
{
QgsLogger::warning( "Writing of the feature failed" );
QgsMessageLog::logMessage( tr( "Writing of the feature %1 failed" ).arg( f.id() ), tr( "OGR" ) );
returnValue = false;
}
else
Expand Down Expand Up @@ -1059,7 +1056,7 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
type = OFTString;
break;
default:
QgsLogger::warning( QString( "QgsOgrProvider::addAttributes, type %1 not found" ).arg( iter->typeName() ) );
QgsMessageLog::logMessage( tr( "type %1 for field %2 not found" ).arg( iter->typeName() ).arg( iter->name() ), tr( "OGR" ) );
returnvalue = false;
continue;
}
Expand All @@ -1070,7 +1067,7 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )

if ( OGR_L_CreateField( ogrLayer, fielddefn, true ) != OGRERR_NONE )
{
QgsLogger::warning( "QgsOgrProvider.cpp: writing of field failed" );
QgsMessageLog::logMessage( tr( "writing of field %1 failed" ).arg( iter->name() ), tr( "OGR" ) );
returnvalue = false;
}
OGR_Fld_Destroy( fielddefn );
Expand All @@ -1090,7 +1087,7 @@ bool QgsOgrProvider::deleteAttributes( const QgsAttributeIds &attributes )
{
if ( OGR_L_DeleteField( ogrLayer, attr ) != OGRERR_NONE )
{
QgsDebugMsg( "Failed to delete attribute " + QString::number( attr ) );
QgsMessageLog::logMessage( tr( "Failed to delete attribute %1" ).arg( attr ), tr( "OGR" ) );
res = false;
}
}
Expand Down Expand Up @@ -1119,15 +1116,15 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr

if ( FID_TO_NUMBER( fid ) > std::numeric_limits<long>::max() )
{
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, feature id too large for OGR" );
QgsMessageLog::logMessage( tr( "Feature id %1 too large for OGR" ).arg( fid ), tr( "OGR" ) );
continue;
}

OGRFeatureH of = OGR_L_GetFeature( ogrLayer, static_cast<long>( FID_TO_NUMBER( fid ) ) );

if ( !of )
{
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, Cannot read feature, cannot change attributes" );
QgsMessageLog::logMessage( tr( "Feature %1 for attribute update not found." ).arg( fid ), tr( "OGR" ) );
continue;
}

Expand All @@ -1140,7 +1137,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
OGRFieldDefnH fd = OGR_F_GetFieldDefnRef( of, f );
if ( !fd )
{
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, Field " + QString::number( f ) + " doesn't exist" );
QgsMessageLog::logMessage( tr( "Field %1 of feature %2 doesn't exist." ).arg( f ).arg( fid ), tr( "OGR" ) );
continue;
}

Expand All @@ -1165,7 +1162,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
OGR_F_SetFieldString( of, f, mEncoding->fromUnicode( it2->toString() ).constData() );
break;
default:
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, Unknown field type, cannot change attribute" );
QgsMessageLog::logMessage( tr( "Type %1 of attribute %2 of feature %3 unknown." ).arg( type ).arg( fid ).arg( f ), tr( "OGR" ) );
break;
}
}
Expand All @@ -1174,7 +1171,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
OGRErr res;
if (( res = OGR_L_SetFeature( ogrLayer, of ) ) != OGRERR_NONE )
{
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, setting the feature failed: " + QString::number( res ) );
QgsMessageLog::logMessage( tr( "Update of Feature %1 failed: %2" ).arg( fid ).arg( res ), tr( "OGR" ) );
}
}

Expand All @@ -1194,14 +1191,14 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
{
if ( FID_TO_NUMBER( it.key() ) > std::numeric_limits<long>::max() )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, fid too large for OGR" );
QgsMessageLog::logMessage( tr( "Feature id %1 too large for OGR" ).arg( it.key() ), tr( "OGR" ) );
continue;
}

theOGRFeature = OGR_L_GetFeature( ogrLayer, static_cast<long>( FID_TO_NUMBER( it.key() ) ) );
if ( !theOGRFeature )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, cannot find feature" );
QgsMessageLog::logMessage( tr( "Feature %1 not found for geometry update." ).arg( it.key() ), tr( "OGR" ) );
continue;
}

Expand All @@ -1211,22 +1208,22 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
&theNewGeometry,
it->wkbSize() ) != OGRERR_NONE )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while creating new OGRGeometry" );
QgsMessageLog::logMessage( tr( "Creation of new geometry for feature %1 failed." ).arg( it.key() ), tr( "OGR" ) );
OGR_G_DestroyGeometry( theNewGeometry );
theNewGeometry = 0;
continue;
}

if ( !theNewGeometry )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, new geometry is NULL" );
QgsMessageLog::logMessage( tr( "Newly created geometry for feature % is null." ).arg( it.key() ), tr( "OGR" ) );
continue;
}

//set the new geometry
if (( res = OGR_F_SetGeometryDirectly( theOGRFeature, theNewGeometry ) ) != OGRERR_NONE )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while replacing geometry: " + QString::number( res ) );
QgsMessageLog::logMessage( tr( "Geometry update for feature %1 failed: %2" ).arg( it.key() ).arg( res ), tr( "OGR" ) );
OGR_G_DestroyGeometry( theNewGeometry );
theNewGeometry = 0;
continue;
Expand All @@ -1235,7 +1232,7 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )

if (( res = OGR_L_SetFeature( ogrLayer, theOGRFeature ) ) != OGRERR_NONE )
{
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while setting feature: " + QString::number( res ) );
QgsMessageLog::logMessage( tr( "Update of feature %1 failed: %2" ).arg( it.key() ).arg( res ), tr( "OGR" ) );
OGR_G_DestroyGeometry( theNewGeometry );
theNewGeometry = 0;
continue;
Expand Down Expand Up @@ -1317,7 +1314,7 @@ bool QgsOgrProvider::deleteFeature( QgsFeatureId id )
{
if ( FID_TO_NUMBER( id ) > std::numeric_limits<long>::max() )
{
QgsDebugMsg( "id too large for OGR" );
QgsMessageLog::logMessage( tr( "id %1 too large for OGR" ).arg( id ), tr( "OGR" ) );
return false;
}

Expand Down Expand Up @@ -1425,7 +1422,7 @@ int QgsOgrProvider::capabilities() const

if ( mAttributeFields.size() == 0 )
{
QgsDebugMsg( "OGR doesn't handle shapefile without attributes well, ie. missing DBFs" );
QgsMessageLog::logMessage( tr( "Shapefiles without attribute are considered read-only." ), tr( "OGR" ) );
ability &= ~( AddFeatures | DeleteFeatures | ChangeAttributeValues | AddAttributes | DeleteAttributes );
}

Expand Down Expand Up @@ -1519,7 +1516,7 @@ QString createFilters( QString type )

if ( !driver )
{
QgsLogger::warning( "unable to get driver " + QString::number( i ) );
QgsMessageLog::logMessage( QObject::tr( "Unable to get driver %1" ).arg( i ), QObject::tr( "OGR" ) );
continue;
}

Expand Down Expand Up @@ -1705,7 +1702,7 @@ QString createFilters( QString type )
{
// NOP, we don't know anything about the current driver
// with regards to a proper file filter string
QgsDebugMsg( "fileVectorFilters, unknown driver: " + driverName );
QgsDebugMsg( QString( "Unknown driver %1 for file filters." ).arg( driverName ) );
}

} // each loaded GDAL driver
Expand Down Expand Up @@ -1874,7 +1871,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,
QString name = fldIt->first.left( 10 );
if ( fieldNames.contains( name ) )
{
QgsDebugMsg( QString( "duplicate field (10 significant characters): %1" ).arg( name ) );
QgsMessageLog::logMessage( QObject::tr( "Duplicate field (10 significant characters): %1" ).arg( name ), QObject::tr( "OGR" ) );
return false;
}
fieldNames << name;
Expand Down Expand Up @@ -1938,7 +1935,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,
break;
default:
{
QgsDebugMsg( QString( "Unknown vector type of: %1" ).arg(( int )( vectortype ) ) );
QgsMessageLog::logMessage( QObject::tr( "Unknown vector type of %1" ).arg(( int )( vectortype ) ), QObject::tr( "OGR" ) );
return false;
break;
}
Expand Down Expand Up @@ -2008,7 +2005,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,

if ( OGR_L_CreateField( layer, field, true ) != OGRERR_NONE )
{
QgsLogger::warning( "creation of field failed" );
QgsMessageLog::logMessage( QObject::tr( "creation of field %1 failed" ).arg( it->first ), QObject::tr( "OGR" ) );
}
}

Expand All @@ -2026,7 +2023,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
QgsMessageLog::logMessage( QObject::tr( "Couldn't create file %1.qpj" ).arg( layerName ), QObject::tr( "OGR" ) );
}
}

Expand All @@ -2042,7 +2039,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,

QgsCoordinateReferenceSystem QgsOgrProvider::crs()
{
QgsDebugMsg( "entering." );
QgsDebugMsg( "Entering." );

QgsCoordinateReferenceSystem srs;

Expand Down

0 comments on commit 7c40bf3

Please sign in to comment.