Skip to content
Permalink
Browse files
improve handling of empty extents (fixes #2997)
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 55c960b1a82dfcfd48ee182a64bf491c6e90dbe5
Showing with 93 additions and 61 deletions.
  1. +5 −0 src/app/legend/qgslegend.cpp
  2. +40 −33 src/app/qgsvectorlayerproperties.cpp
  3. +1 −1 src/core/qgscoordinatetransform.cpp
  4. +11 −10 src/core/qgsrectangle.cpp
  5. +36 −17 src/providers/postgres/qgspostgresprovider.cpp
@@ -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 );

@@ -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
@@ -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;
@@ -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;
}

@@ -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,
@@ -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
@@ -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( "'" );
}

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

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

0 comments on commit 55c960b

Please sign in to comment.