[QGIS Commit] r15809 - in trunk/qgis/src: app app/legend core providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Apr 22 10:02:32 EDT 2011


Author: jef
Date: 2011-04-22 07:02:32 -0700 (Fri, 22 Apr 2011)
New Revision: 15809

Modified:
   trunk/qgis/src/app/legend/qgslegend.cpp
   trunk/qgis/src/app/qgsvectorlayerproperties.cpp
   trunk/qgis/src/core/qgscoordinatetransform.cpp
   trunk/qgis/src/core/qgsrectangle.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
Log:
improve handling of empty extents (fixes #2997)

Modified: trunk/qgis/src/app/legend/qgslegend.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgslegend.cpp	2011-04-22 14:00:10 UTC (rev 15808)
+++ trunk/qgis/src/app/legend/qgslegend.cpp	2011-04-22 14:02:32 UTC (rev 15809)
@@ -1805,6 +1805,11 @@
     }
   }
 
+  if( extent.isEmpty() )
+  {
+    return;
+  }
+
   // Increase bounding box with 5%, so that layer is a bit inside the borders
   extent.scale( 1.05 );
 

Modified: trunk/qgis/src/app/qgsvectorlayerproperties.cpp
===================================================================
--- trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2011-04-22 14:00:10 UTC (rev 15808)
+++ trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2011-04-22 14:02:32 UTC (rev 15809)
@@ -907,44 +907,51 @@
   // - 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 );
+    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
   {
-    xMin = QString( "%1" ).arg( myExtent.xMinimum() );
+    myMetadata += tr( "In layer spatial reference system units : " ) + tr( "Empty" );
   }
-  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 );
   myMetadata += "</td></tr>";
 
   //extents in project cs

Modified: trunk/qgis/src/core/qgscoordinatetransform.cpp
===================================================================
--- trunk/qgis/src/core/qgscoordinatetransform.cpp	2011-04-22 14:00:10 UTC (rev 15808)
+++ trunk/qgis/src/core/qgscoordinatetransform.cpp	2011-04-22 14:02:32 UTC (rev 15809)
@@ -350,7 +350,7 @@
   // 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;

Modified: trunk/qgis/src/core/qgsrectangle.cpp
===================================================================
--- trunk/qgis/src/core/qgsrectangle.cpp	2011-04-22 14:00:10 UTC (rev 15808)
+++ trunk/qgis/src/core/qgsrectangle.cpp	2011-04-22 14:02:32 UTC (rev 15809)
@@ -229,17 +229,18 @@
 // 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 );
 
-  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
+  QgsDebugMsgLevel( QString( "Extents : %1" ).arg( rep ), 4 );
+
   return rep;
 }
 

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2011-04-22 14:00:10 UTC (rev 15808)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2011-04-22 14:02:32 UTC (rev 15809)
@@ -438,12 +438,19 @@
     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 @@
       {
         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 );
+            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 = 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();
+              // 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 @@
   if ( value.isNull() )
     return "NULL";
 
-  // FIXME: use PQescapeStringConn
   value.replace( "'", "''" );
+  value.replace( "\\\"", "\\\\\"" );
   return value.prepend( "'" ).append( "'" );
 }
 
@@ -3482,7 +3501,7 @@
   return sqlWhereClause;
 }
 
-PGconn * QgsPostgresProvider::pgConnection()
+PGconn *QgsPostgresProvider::pgConnection()
 {
   connectRW();
   return connectionRW->pgConnection();



More information about the QGIS-commit mailing list