Skip to content

Commit

Permalink
improve handling of empty extents (fixes #2997)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15809 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Apr 22, 2011
1 parent 04360f5 commit 55c960b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 61 deletions.
5 changes: 5 additions & 0 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -1805,6 +1805,11 @@ void QgsLegend::legendLayerZoom()
}
}

if( extent.isEmpty() )
{
return;
}

// Increase bounding box with 5%, so that layer is a bit inside the borders
extent.scale( 1.05 );

Expand Down
73 changes: 40 additions & 33 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -907,44 +907,51 @@ QString QgsVectorLayerProperties::metadata()
// - for all smaller numbers let the OS decide which format to use (it will
// generally use non-scientific unless the number gets much less than 1).

QString xMin, yMin, xMax, yMax;
double changeoverValue = 99999; // The 'largest' 5 digit number
if ( qAbs( myExtent.xMinimum() ) > changeoverValue )
if ( !myExtent.isEmpty() )
{
xMin = QString( "%1" ).arg( myExtent.xMinimum(), 0, 'f', 2 );
}
else
{
xMin = QString( "%1" ).arg( myExtent.xMinimum() );
}
if ( qAbs( myExtent.yMinimum() ) > changeoverValue )
{
yMin = QString( "%1" ).arg( myExtent.yMinimum(), 0, 'f', 2 );
}
else
{
yMin = QString( "%1" ).arg( myExtent.yMinimum() );
}
if ( qAbs( myExtent.xMaximum() ) > changeoverValue )
{
xMax = QString( "%1" ).arg( myExtent.xMaximum(), 0, 'f', 2 );
}
else
{
xMax = QString( "%1" ).arg( myExtent.xMaximum() );
}
if ( qAbs( myExtent.yMaximum() ) > changeoverValue )
{
yMax = QString( "%1" ).arg( myExtent.yMaximum(), 0, 'f', 2 );
QString xMin, yMin, xMax, yMax;
double changeoverValue = 99999; // The 'largest' 5 digit number
if ( qAbs( myExtent.xMinimum() ) > changeoverValue )
{
xMin = QString( "%1" ).arg( myExtent.xMinimum(), 0, 'f', 2 );
}
else
{
xMin = QString( "%1" ).arg( myExtent.xMinimum() );
}
if ( qAbs( myExtent.yMinimum() ) > changeoverValue )
{
yMin = QString( "%1" ).arg( myExtent.yMinimum(), 0, 'f', 2 );
}
else
{
yMin = QString( "%1" ).arg( myExtent.yMinimum() );
}
if ( qAbs( myExtent.xMaximum() ) > changeoverValue )
{
xMax = QString( "%1" ).arg( myExtent.xMaximum(), 0, 'f', 2 );
}
else
{
xMax = QString( "%1" ).arg( myExtent.xMaximum() );
}
if ( qAbs( myExtent.yMaximum() ) > changeoverValue )
{
yMax = QString( "%1" ).arg( myExtent.yMaximum(), 0, 'f', 2 );
}
else
{
yMax = QString( "%1" ).arg( myExtent.yMaximum() );
}

myMetadata += tr( "In layer spatial reference system units : " )
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
}
else
{
yMax = QString( "%1" ).arg( myExtent.yMaximum() );
myMetadata += tr( "In layer spatial reference system units : " ) + tr( "Empty" );
}

myMetadata += tr( "In layer spatial reference system units : " )
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
myMetadata += "</td></tr>";

//extents in project cs
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscoordinatetransform.cpp
Expand Up @@ -350,7 +350,7 @@ QgsRectangle QgsCoordinateTransform::transformBoundingBox( const QgsRectangle re
// This is done by looking at a number of points spread evenly
// across the rectangle

if ( mShortCircuit || !mInitialisedFlag )
if ( mShortCircuit || !mInitialisedFlag || rect.isEmpty() )
return rect;

static const int numP = 8;
Expand Down
21 changes: 11 additions & 10 deletions src/core/qgsrectangle.cpp
Expand Up @@ -229,17 +229,18 @@ QString QgsRectangle::toString( bool automaticPrecision ) const
// Return a string representation of the rectangle with high precision
QString QgsRectangle::toString( int thePrecision ) const
{
QString rep;
if ( isEmpty() )
rep = "Empty";
else
rep = QString( "%1,%2 : %3,%4" )
.arg( xmin, 0, 'f', thePrecision )
.arg( ymin, 0, 'f', thePrecision )
.arg( xmax, 0, 'f', thePrecision )
.arg( ymax, 0, 'f', thePrecision );

QgsDebugMsgLevel( QString( "Extents : %1" ).arg( rep ), 4 );

QString rep = QString::number( xmin, 'f', thePrecision ) +
QString( "," ) +
QString::number( ymin, 'f', thePrecision ) +
QString( " : " ) +
QString::number( xmax, 'f', thePrecision ) +
QString( "," ) +
QString::number( ymax, 'f', thePrecision ) ;
#ifdef QGISDEBUG
// QgsDebugMsg(QString("Extents : %1").arg(rep));
#endif
return rep;
}

Expand Down
53 changes: 36 additions & 17 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -438,12 +438,19 @@ bool QgsPostgresProvider::declareCursor(
if ( !whereClause.isEmpty() )
query += QString( " where %1" ).arg( whereClause );

return connectionRO->openCursor( cursorName, query );
if ( !connectionRO->openCursor( cursorName, query ) )
{
// reloading the fields might help next time around
rewind();
return false;
}
}
catch ( PGFieldNotFound )
{
return false;
}

return true;
}

bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetchGeometry,
Expand Down Expand Up @@ -2937,25 +2944,37 @@ QgsRectangle QgsPostgresProvider::extent()
{
if ( QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toInt() > 0 )
{
sql = QString( "select %1(%2,%3,%4)" )
.arg( connectionRO->majorVersion() < 2 ? "estimated_extent" : "st_estimated_extent" )
.arg( quotedValue( mSchemaName ) )
.arg( quotedValue( mTableName ) )
.arg( quotedValue( geometryColumn ) );
sql = QString( "select reltuples::int from pg_catalog.pg_class where oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
result = connectionRO->PQexec( sql );
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
if ( PQresultStatus( result ) == PGRES_TUPLES_OK &&
PQntuples( result ) == 1 &&
QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toLong() > 0 )
{
ext = PQgetvalue( result, 0, 0 );

// fix for what might be a postgis bug: when the extent crosses the
// dateline extent() returns -180 to 180 (which appears right), but
// estimated_extent() returns eastern bound of data (>-180) and
// 180 degrees.
if ( !ext.startsWith( "-180 " ) && ext.contains( ",180 " ) )
sql = QString( "select %1(%2,%3,%4)" )
.arg( connectionRO->majorVersion() < 2 ? "estimated_extent" : "st_estimated_extent" )
.arg( quotedValue( mSchemaName ) )
.arg( quotedValue( mTableName ) )
.arg( quotedValue( geometryColumn ) );
result = connectionRO->PQexec( sql );
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
{
ext.clear();
ext = PQgetvalue( result, 0, 0 );

// fix for what might be a postgis bug: when the extent crosses the
// dateline extent() returns -180 to 180 (which appears right), but
// estimated_extent() returns eastern bound of data (>-180) and
// 180 degrees.
if ( !ext.startsWith( "-180 " ) && ext.contains( ",180 " ) )
{
ext.clear();
}
}
}
else
{
// no features => ignore estimated extent
ext.clear();
}
}
}
else
Expand Down Expand Up @@ -3322,8 +3341,8 @@ QString QgsPostgresProvider::quotedValue( QString value ) const
if ( value.isNull() )
return "NULL";

// FIXME: use PQescapeStringConn
value.replace( "'", "''" );
value.replace( "\\\"", "\\\\\"" );
return value.prepend( "'" ).append( "'" );
}

Expand Down Expand Up @@ -3482,7 +3501,7 @@ QString QgsPostgresProvider::subsetString()
return sqlWhereClause;
}

PGconn * QgsPostgresProvider::pgConnection()
PGconn *QgsPostgresProvider::pgConnection()
{
connectRW();
return connectionRW->pgConnection();
Expand Down

0 comments on commit 55c960b

Please sign in to comment.