Skip to content

Commit 55c960b

Browse files
author
jef
committed
improve handling of empty extents (fixes #2997)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15809 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 04360f5 commit 55c960b

File tree

5 files changed

+93
-61
lines changed

5 files changed

+93
-61
lines changed

src/app/legend/qgslegend.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,11 @@ void QgsLegend::legendLayerZoom()
18051805
}
18061806
}
18071807

1808+
if( extent.isEmpty() )
1809+
{
1810+
return;
1811+
}
1812+
18081813
// Increase bounding box with 5%, so that layer is a bit inside the borders
18091814
extent.scale( 1.05 );
18101815

src/app/qgsvectorlayerproperties.cpp

+40-33
Original file line numberDiff line numberDiff line change
@@ -907,44 +907,51 @@ QString QgsVectorLayerProperties::metadata()
907907
// - for all smaller numbers let the OS decide which format to use (it will
908908
// generally use non-scientific unless the number gets much less than 1).
909909

910-
QString xMin, yMin, xMax, yMax;
911-
double changeoverValue = 99999; // The 'largest' 5 digit number
912-
if ( qAbs( myExtent.xMinimum() ) > changeoverValue )
910+
if ( !myExtent.isEmpty() )
913911
{
914-
xMin = QString( "%1" ).arg( myExtent.xMinimum(), 0, 'f', 2 );
915-
}
916-
else
917-
{
918-
xMin = QString( "%1" ).arg( myExtent.xMinimum() );
919-
}
920-
if ( qAbs( myExtent.yMinimum() ) > changeoverValue )
921-
{
922-
yMin = QString( "%1" ).arg( myExtent.yMinimum(), 0, 'f', 2 );
923-
}
924-
else
925-
{
926-
yMin = QString( "%1" ).arg( myExtent.yMinimum() );
927-
}
928-
if ( qAbs( myExtent.xMaximum() ) > changeoverValue )
929-
{
930-
xMax = QString( "%1" ).arg( myExtent.xMaximum(), 0, 'f', 2 );
931-
}
932-
else
933-
{
934-
xMax = QString( "%1" ).arg( myExtent.xMaximum() );
935-
}
936-
if ( qAbs( myExtent.yMaximum() ) > changeoverValue )
937-
{
938-
yMax = QString( "%1" ).arg( myExtent.yMaximum(), 0, 'f', 2 );
912+
QString xMin, yMin, xMax, yMax;
913+
double changeoverValue = 99999; // The 'largest' 5 digit number
914+
if ( qAbs( myExtent.xMinimum() ) > changeoverValue )
915+
{
916+
xMin = QString( "%1" ).arg( myExtent.xMinimum(), 0, 'f', 2 );
917+
}
918+
else
919+
{
920+
xMin = QString( "%1" ).arg( myExtent.xMinimum() );
921+
}
922+
if ( qAbs( myExtent.yMinimum() ) > changeoverValue )
923+
{
924+
yMin = QString( "%1" ).arg( myExtent.yMinimum(), 0, 'f', 2 );
925+
}
926+
else
927+
{
928+
yMin = QString( "%1" ).arg( myExtent.yMinimum() );
929+
}
930+
if ( qAbs( myExtent.xMaximum() ) > changeoverValue )
931+
{
932+
xMax = QString( "%1" ).arg( myExtent.xMaximum(), 0, 'f', 2 );
933+
}
934+
else
935+
{
936+
xMax = QString( "%1" ).arg( myExtent.xMaximum() );
937+
}
938+
if ( qAbs( myExtent.yMaximum() ) > changeoverValue )
939+
{
940+
yMax = QString( "%1" ).arg( myExtent.yMaximum(), 0, 'f', 2 );
941+
}
942+
else
943+
{
944+
yMax = QString( "%1" ).arg( myExtent.yMaximum() );
945+
}
946+
947+
myMetadata += tr( "In layer spatial reference system units : " )
948+
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
949+
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
939950
}
940951
else
941952
{
942-
yMax = QString( "%1" ).arg( myExtent.yMaximum() );
953+
myMetadata += tr( "In layer spatial reference system units : " ) + tr( "Empty" );
943954
}
944-
945-
myMetadata += tr( "In layer spatial reference system units : " )
946-
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
947-
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
948955
myMetadata += "</td></tr>";
949956

950957
//extents in project cs

src/core/qgscoordinatetransform.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ QgsRectangle QgsCoordinateTransform::transformBoundingBox( const QgsRectangle re
350350
// This is done by looking at a number of points spread evenly
351351
// across the rectangle
352352

353-
if ( mShortCircuit || !mInitialisedFlag )
353+
if ( mShortCircuit || !mInitialisedFlag || rect.isEmpty() )
354354
return rect;
355355

356356
static const int numP = 8;

src/core/qgsrectangle.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,18 @@ QString QgsRectangle::toString( bool automaticPrecision ) const
229229
// Return a string representation of the rectangle with high precision
230230
QString QgsRectangle::toString( int thePrecision ) const
231231
{
232+
QString rep;
233+
if ( isEmpty() )
234+
rep = "Empty";
235+
else
236+
rep = QString( "%1,%2 : %3,%4" )
237+
.arg( xmin, 0, 'f', thePrecision )
238+
.arg( ymin, 0, 'f', thePrecision )
239+
.arg( xmax, 0, 'f', thePrecision )
240+
.arg( ymax, 0, 'f', thePrecision );
241+
242+
QgsDebugMsgLevel( QString( "Extents : %1" ).arg( rep ), 4 );
232243

233-
QString rep = QString::number( xmin, 'f', thePrecision ) +
234-
QString( "," ) +
235-
QString::number( ymin, 'f', thePrecision ) +
236-
QString( " : " ) +
237-
QString::number( xmax, 'f', thePrecision ) +
238-
QString( "," ) +
239-
QString::number( ymax, 'f', thePrecision ) ;
240-
#ifdef QGISDEBUG
241-
// QgsDebugMsg(QString("Extents : %1").arg(rep));
242-
#endif
243244
return rep;
244245
}
245246

src/providers/postgres/qgspostgresprovider.cpp

+36-17
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,19 @@ bool QgsPostgresProvider::declareCursor(
438438
if ( !whereClause.isEmpty() )
439439
query += QString( " where %1" ).arg( whereClause );
440440

441-
return connectionRO->openCursor( cursorName, query );
441+
if ( !connectionRO->openCursor( cursorName, query ) )
442+
{
443+
// reloading the fields might help next time around
444+
rewind();
445+
return false;
446+
}
442447
}
443448
catch ( PGFieldNotFound )
444449
{
445450
return false;
446451
}
452+
453+
return true;
447454
}
448455

449456
bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetchGeometry,
@@ -2937,25 +2944,37 @@ QgsRectangle QgsPostgresProvider::extent()
29372944
{
29382945
if ( QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toInt() > 0 )
29392946
{
2940-
sql = QString( "select %1(%2,%3,%4)" )
2941-
.arg( connectionRO->majorVersion() < 2 ? "estimated_extent" : "st_estimated_extent" )
2942-
.arg( quotedValue( mSchemaName ) )
2943-
.arg( quotedValue( mTableName ) )
2944-
.arg( quotedValue( geometryColumn ) );
2947+
sql = QString( "select reltuples::int from pg_catalog.pg_class where oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
29452948
result = connectionRO->PQexec( sql );
2946-
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
2949+
if ( PQresultStatus( result ) == PGRES_TUPLES_OK &&
2950+
PQntuples( result ) == 1 &&
2951+
QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toLong() > 0 )
29472952
{
2948-
ext = PQgetvalue( result, 0, 0 );
2949-
2950-
// fix for what might be a postgis bug: when the extent crosses the
2951-
// dateline extent() returns -180 to 180 (which appears right), but
2952-
// estimated_extent() returns eastern bound of data (>-180) and
2953-
// 180 degrees.
2954-
if ( !ext.startsWith( "-180 " ) && ext.contains( ",180 " ) )
2953+
sql = QString( "select %1(%2,%3,%4)" )
2954+
.arg( connectionRO->majorVersion() < 2 ? "estimated_extent" : "st_estimated_extent" )
2955+
.arg( quotedValue( mSchemaName ) )
2956+
.arg( quotedValue( mTableName ) )
2957+
.arg( quotedValue( geometryColumn ) );
2958+
result = connectionRO->PQexec( sql );
2959+
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
29552960
{
2956-
ext.clear();
2961+
ext = PQgetvalue( result, 0, 0 );
2962+
2963+
// fix for what might be a postgis bug: when the extent crosses the
2964+
// dateline extent() returns -180 to 180 (which appears right), but
2965+
// estimated_extent() returns eastern bound of data (>-180) and
2966+
// 180 degrees.
2967+
if ( !ext.startsWith( "-180 " ) && ext.contains( ",180 " ) )
2968+
{
2969+
ext.clear();
2970+
}
29572971
}
29582972
}
2973+
else
2974+
{
2975+
// no features => ignore estimated extent
2976+
ext.clear();
2977+
}
29592978
}
29602979
}
29612980
else
@@ -3322,8 +3341,8 @@ QString QgsPostgresProvider::quotedValue( QString value ) const
33223341
if ( value.isNull() )
33233342
return "NULL";
33243343

3325-
// FIXME: use PQescapeStringConn
33263344
value.replace( "'", "''" );
3345+
value.replace( "\\\"", "\\\\\"" );
33273346
return value.prepend( "'" ).append( "'" );
33283347
}
33293348

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

3485-
PGconn * QgsPostgresProvider::pgConnection()
3504+
PGconn *QgsPostgresProvider::pgConnection()
34863505
{
34873506
connectRW();
34883507
return connectionRW->pgConnection();

0 commit comments

Comments
 (0)