Skip to content

Commit

Permalink
Return an invalid iterator if a transform exception occurs
Browse files Browse the repository at this point in the history
when projecting filter rects from destination crs
  • Loading branch information
nyalldawson committed Jun 8, 2017
1 parent 7d847bf commit e437812
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 25 deletions.
1 change: 1 addition & 0 deletions python/core/qgsfeatureiterator.sip
Expand Up @@ -120,6 +120,7 @@ end of iterating: free the resources / lock
the requested filterRect() transformed to the source's coordinate reference system.
Iterators should call this method and use the returned rectangle for filtering
features to ensure that any QgsFeatureRequest.destinationCrs() set on the request is respected.
Will throw a QgsCsException if the rect cannot be transformed from the destination CRS.
.. versionadded:: 3.0
:rtype: QgsRectangle
%End
Expand Down
12 changes: 11 additions & 1 deletion src/core/providers/memory/qgsmemoryfeatureiterator.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsspatialindex.h"
#include "qgsmessagelog.h"
#include "qgsproject.h"
#include "qgscsexception.h"

///@cond PRIVATE

Expand All @@ -31,7 +32,16 @@ QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource *sour
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

if ( !mSource->mSubsetString.isEmpty() )
{
Expand Down
23 changes: 21 additions & 2 deletions src/core/qgscachedfeatureiterator.cpp
Expand Up @@ -15,6 +15,7 @@

#include "qgscachedfeatureiterator.h"
#include "qgsvectorlayercache.h"
#include "qgscsexception.h"

QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache, const QgsFeatureRequest &featureRequest )
: QgsAbstractFeatureIterator( featureRequest )
Expand All @@ -24,7 +25,16 @@ QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache
{
mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}
if ( !mFilterRect.isNull() )
{
// update request to be the unprojected filter rect
Expand Down Expand Up @@ -101,7 +111,16 @@ QgsCachedFeatureWriterIterator::QgsCachedFeatureWriterIterator( QgsVectorLayerCa
{
mTransform = QgsCoordinateTransform( mVectorLayerCache->sourceCrs(), mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}
if ( !mFilterRect.isNull() )
{
// update request to be the unprojected filter rect
Expand Down
10 changes: 1 addition & 9 deletions src/core/qgsfeatureiterator.cpp
Expand Up @@ -124,15 +124,7 @@ QgsRectangle QgsAbstractFeatureIterator::filterRectToSourceCrs( const QgsCoordin
if ( mRequest.filterRect().isNull() )
return QgsRectangle();

try
{
return transform.transformBoundingBox( mRequest.filterRect(), QgsCoordinateTransform::ReverseTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
return mRequest.filterRect();
}
return transform.transformBoundingBox( mRequest.filterRect(), QgsCoordinateTransform::ReverseTransform );
}

void QgsAbstractFeatureIterator::ref()
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsfeatureiterator.h
Expand Up @@ -131,6 +131,7 @@ class CORE_EXPORT QgsAbstractFeatureIterator
* the requested filterRect() transformed to the source's coordinate reference system.
* Iterators should call this method and use the returned rectangle for filtering
* features to ensure that any QgsFeatureRequest::destinationCrs() set on the request is respected.
* Will throw a QgsCsException if the rect cannot be transformed from the destination CRS.
* \since QGIS 3.0
*/
QgsRectangle filterRectToSourceCrs( const QgsCoordinateTransform &transform ) const;
Expand Down
12 changes: 11 additions & 1 deletion src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsdistancearea.h"
#include "qgsproject.h"
#include "qgsmessagelog.h"
#include "qgscsexception.h"

QgsVectorLayerFeatureSource::QgsVectorLayerFeatureSource( const QgsVectorLayer *layer )
{
Expand Down Expand Up @@ -113,7 +114,16 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayerFeat
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}
if ( !mFilterRect.isNull() )
{
// update request to be the unprojected filter rect
Expand Down
13 changes: 11 additions & 2 deletions src/providers/arcgisrest/qgsafsfeatureiterator.cpp
Expand Up @@ -17,7 +17,7 @@
#include "qgsafsprovider.h"
#include "qgsmessagelog.h"
#include "geometry/qgsgeometry.h"

#include "qgscsexception.h"

QgsAfsFeatureSource::QgsAfsFeatureSource( const QgsAfsProvider *provider )
// FIXME: ugly const_cast...
Expand Down Expand Up @@ -45,7 +45,16 @@ QgsAfsFeatureIterator::QgsAfsFeatureIterator( QgsAfsFeatureSource *source, bool
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}
}

QgsAfsFeatureIterator::~QgsAfsFeatureIterator()
Expand Down
12 changes: 11 additions & 1 deletion src/providers/db2/qgsdb2featureiterator.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgssettings.h"
#include "qgslogger.h"
#include "qgsgeometry.h"
#include "qgscsexception.h"

#include <QObject>
#include <QTextStream>
Expand All @@ -37,7 +38,16 @@ QgsDb2FeatureIterator::QgsDb2FeatureIterator( QgsDb2FeatureSource *source, bool
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

BuildStatement( request );

Expand Down
11 changes: 10 additions & 1 deletion src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
Expand Up @@ -43,7 +43,16 @@ QgsDelimitedTextFeatureIterator::QgsDelimitedTextFeatureIterator( QgsDelimitedTe
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

if ( !mFilterRect.isNull() && hasGeometry )
{
Expand Down
12 changes: 11 additions & 1 deletion src/providers/gpx/qgsgpxfeatureiterator.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgscsexception.h"

#include <limits>
#include <cstring>
Expand All @@ -32,7 +33,16 @@ QgsGPXFeatureIterator::QgsGPXFeatureIterator( QgsGPXFeatureSource *source, bool
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

rewind();
}
Expand Down
12 changes: 11 additions & 1 deletion src/providers/mssql/qgsmssqlfeatureiterator.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsmssqlprovider.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgscsexception.h"

#include <QObject>
#include <QTextStream>
Expand All @@ -37,7 +38,16 @@ QgsMssqlFeatureIterator::QgsMssqlFeatureIterator( QgsMssqlFeatureSource *source,
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

BuildStatement( request );

Expand Down
11 changes: 10 additions & 1 deletion src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -80,7 +80,16 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

mFetchGeometry = ( !mFilterRect.isNull() ) || !( mRequest.flags() & QgsFeatureRequest::NoGeometry );
QgsAttributeList attrs = ( mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes ) ? mRequest.subsetOfAttributes() : mSource->mFields.allAttributesList();
Expand Down
12 changes: 11 additions & 1 deletion src/providers/oracle/qgsoraclefeatureiterator.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsmessagelog.h"
#include "qgsgeometry.h"
#include "qgssettings.h"
#include "qgscsexception.h"

#include <QObject>

Expand All @@ -42,7 +43,16 @@ QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource *sour
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

QVariantList args;
mQry = QSqlQuery( *mConnection );
Expand Down
12 changes: 11 additions & 1 deletion src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgssettings.h"
#include "qgscsexception.h"

#include <QElapsedTimer>
#include <QObject>
Expand Down Expand Up @@ -57,7 +58,16 @@ QgsPostgresFeatureIterator::QgsPostgresFeatureIterator( QgsPostgresFeatureSource
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

mCursorName = mConn->uniqueCursorName();
QString whereClause;
Expand Down
11 changes: 10 additions & 1 deletion src/providers/spatialite/qgsspatialitefeatureiterator.cpp
Expand Up @@ -47,7 +47,16 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

//beware - limitAtProvider needs to be set to false if the request cannot be completely handled
//by the provider (e.g., utilising QGIS expression filters)
Expand Down
12 changes: 11 additions & 1 deletion src/providers/virtual/qgsvirtuallayerfeatureiterator.cpp
Expand Up @@ -19,6 +19,7 @@ email : hugo dot mercier at oslandia dot com
#include "qgsmessagelog.h"
#include "qgsgeometry.h"
#include "qgsvirtuallayerblob.h"
#include "qgscsexception.h"

#include <stdexcept>

Expand Down Expand Up @@ -46,7 +47,16 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

try
{
Expand Down
12 changes: 11 additions & 1 deletion src/providers/wfs/qgswfsfeatureiterator.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgswfsutils.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgscsexception.h"

#include <QDir>
#include <QProgressDialog>
Expand Down Expand Up @@ -793,7 +794,16 @@ QgsWFSFeatureIterator::QgsWFSFeatureIterator( QgsWFSFeatureSource *source,
{
mTransform = QgsCoordinateTransform( mSource->mCrs, mRequest.destinationCrs() );
}
mFilterRect = filterRectToSourceCrs( mTransform );
try
{
mFilterRect = filterRectToSourceCrs( mTransform );
}
catch ( QgsCsException & )
{
// can't reproject mFilterRect
mClosed = true;
return;
}

// Configurable for the purpose of unit tests
QString threshold( getenv( "QGIS_WFS_ITERATOR_TRANSFER_THRESHOLD" ) );
Expand Down

0 comments on commit e437812

Please sign in to comment.