Skip to content

Commit

Permalink
Limit the mssql geography spatial filter to valid coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
szekerest authored and nyalldawson committed Jun 15, 2019
1 parent 38659fb commit 4869499
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/providers/mssql/qgsmssqlfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ QgsMssqlFeatureIterator::~QgsMssqlFeatureIterator()
close();
}

double QgsMssqlFeatureIterator::ValidLat( const double latitude )
{
if (latitude < -90.0)
return -90.0;
if (latitude > 90.0)
return 90.0;
return latitude;
}

double QgsMssqlFeatureIterator::ValidLon( const double longitude )
{
if (longitude < -15069.0)
return -15069.0;
if (longitude > 15069.0)
return 15069.0;
return longitude;
}

void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
{
mFallbackStatement.clear();
Expand Down Expand Up @@ -135,11 +153,23 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )

foo.setRealNumberPrecision( 8 );
foo.setRealNumberNotation( QTextStream::FixedNotation );
foo << qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMaximum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMaximum() ) << ' ' << qgsDoubleToString( mFilterRect.yMaximum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMaximum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() );

if (mSource->mGeometryColType == QLatin1String("geometry"))
{
foo << qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMaximum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMaximum() ) << ' ' << qgsDoubleToString( mFilterRect.yMaximum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMaximum() ) << ", "
<< qgsDoubleToString( mFilterRect.xMinimum() ) << ' ' << qgsDoubleToString( mFilterRect.yMinimum() );
}
else
{
foo << qgsDoubleToString( ValidLon( mFilterRect.xMinimum() ) ) << ' ' << qgsDoubleToString( ValidLat( mFilterRect.yMinimum() ) ) << ", "
<< qgsDoubleToString( ValidLon( mFilterRect.xMaximum() ) ) << ' ' << qgsDoubleToString( ValidLat( mFilterRect.yMinimum() ) ) << ", "
<< qgsDoubleToString( ValidLon( mFilterRect.xMaximum() ) ) << ' ' << qgsDoubleToString( ValidLat( mFilterRect.yMaximum() ) ) << ", "
<< qgsDoubleToString( ValidLon( mFilterRect.xMinimum() ) ) << ' ' << qgsDoubleToString( ValidLat( mFilterRect.yMaximum() ) ) << ", "
<< qgsDoubleToString( ValidLon( mFilterRect.xMinimum() ) ) << ' ' << qgsDoubleToString( ValidLat( mFilterRect.yMinimum() ) );
}

mStatement += QStringLiteral( " WHERE " );
if ( !mDisableInvalidGeometryHandling )
Expand Down Expand Up @@ -379,7 +409,7 @@ bool QgsMssqlFeatureIterator::fetchFeature( QgsFeature &feature )
std::unique_ptr<QgsAbstractGeometry> geom = mParser.ParseSqlGeometry(reinterpret_cast< unsigned char * >(ar.data()), ar.size());
if ( geom != nullptr )
{
feature.setGeometry( QgsGeometry( geom.release() ) );
feature.setGeometry( QgsGeometry( std::move( geom ) ) );
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/providers/mssql/qgsmssqlfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class QgsMssqlFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsM

bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;

double ValidLat( const double latitude );
double ValidLon( const double longitude );

// The current database
QSqlDatabase mDatabase;

Expand Down

0 comments on commit 4869499

Please sign in to comment.