[QGIS Commit] r13588 - in trunk/qgis: python/core src/analysis/vector src/core src/plugins/labeling

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat May 29 07:03:31 EDT 2010


Author: jef
Date: 2010-05-29 07:03:30 -0400 (Sat, 29 May 2010)
New Revision: 13588

Modified:
   trunk/qgis/python/core/qgsgeometry.sip
   trunk/qgis/src/analysis/vector/qgszonalstatistics.cpp
   trunk/qgis/src/core/qgsgeometry.cpp
   trunk/qgis/src/core/qgsgeometry.h
   trunk/qgis/src/plugins/labeling/pallabeling.cpp
Log:
add length() and area() methods to QgsGeometry
and use it in labeling and zonal statistics


Modified: trunk/qgis/python/core/qgsgeometry.sip
===================================================================
--- trunk/qgis/python/core/qgsgeometry.sip	2010-05-29 10:32:56 UTC (rev 13587)
+++ trunk/qgis/python/core/qgsgeometry.sip	2010-05-29 11:03:30 UTC (rev 13588)
@@ -111,6 +111,16 @@
     // TODO: unsupported class... would be possible to use PyGEOS?
     //void fromGeos(geos::Geometry* geos);
 
+    /** get area using GEOS
+      @note added in 1.5
+     */
+    double area();
+
+    /** get length using GEOS
+      @note added in 1.5
+     */
+    double length();
+
     double distance(QgsGeometry& geom);
 
     /**

Modified: trunk/qgis/src/analysis/vector/qgszonalstatistics.cpp
===================================================================
--- trunk/qgis/src/analysis/vector/qgszonalstatistics.cpp	2010-05-29 10:32:56 UTC (rev 13587)
+++ trunk/qgis/src/analysis/vector/qgszonalstatistics.cpp	2010-05-29 11:03:30 UTC (rev 13588)
@@ -23,13 +23,19 @@
 #include "cpl_string.h"
 #include <QProgressDialog>
 
-QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand ) :
-    mRasterFilePath( rasterFile ), mRasterBand( rasterBand ), mPolygonLayer( polygonLayer ), mAttributePrefix( attributePrefix ), mInputNodataValue( -1 )
+QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
+    : mRasterFilePath( rasterFile )
+    , mRasterBand( rasterBand )
+    , mPolygonLayer( polygonLayer )
+    , mAttributePrefix( attributePrefix )
+    , mInputNodataValue( -1 )
 {
 
 }
 
-QgsZonalStatistics::QgsZonalStatistics(): mRasterBand( 0 ), mPolygonLayer( 0 )
+QgsZonalStatistics::QgsZonalStatistics()
+    : mRasterBand( 0 )
+    , mPolygonLayer( 0 )
 {
 
 }
@@ -162,13 +168,13 @@
       continue;
     }
 
-    statisticsFromMiddlePointTest_improved( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
+    statisticsFromMiddlePointTest_improved( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
                                             rasterBBox, sum, count );
 
     if ( count <= 1 )
     {
       //the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
-      statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
+      statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
                                          rasterBBox, sum, count );
     }
 
@@ -227,7 +233,7 @@
   return 0;
 }
 
-void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry* poly, int pixelOffsetX, \
+void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry* poly, int pixelOffsetX,
     int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
 {
   double cellCenterX, cellCenterY;
@@ -260,7 +266,7 @@
   CPLFree( scanLine );
 }
 
-void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeometry* poly, int pixelOffsetX, \
+void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeometry* poly, int pixelOffsetX,
     int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
 {
   sum = 0;
@@ -268,12 +274,10 @@
   double currentY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2;
   float* pixelData = ( float * ) CPLMalloc( sizeof( float ) );
   QgsGeometry* pixelRectGeometry = 0;
-  QgsGeometry* intersectGeometry = 0;
 
   double hCellSizeX = cellSizeX / 2.0;
   double hCellSizeY = cellSizeY / 2.0;
   double pixelArea = cellSizeX * cellSizeY;
-  double intersectionArea = 0;
   double weight = 0;
 
   for ( int row = 0; row < nCellsY; ++row )
@@ -286,10 +290,11 @@
       if ( pixelRectGeometry )
       {
         //intersection
-        intersectGeometry = pixelRectGeometry->intersection( poly );
+        QgsGeometry *intersectGeometry = pixelRectGeometry->intersection( poly );
         if ( intersectGeometry )
         {
-          if ( GEOSArea( intersectGeometry->asGeos(), &intersectionArea ) )
+          double intersectionArea = intersectGeometry->area();
+          if ( intersectionArea >= 0.0 )
           {
             weight = intersectionArea / pixelArea;
             count += weight;
@@ -305,7 +310,7 @@
   CPLFree( pixelData );
 }
 
-void QgsZonalStatistics::statisticsFromMiddlePointTest_improved( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, \
+void QgsZonalStatistics::statisticsFromMiddlePointTest_improved( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
     double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
 {
   double cellCenterX, cellCenterY;

Modified: trunk/qgis/src/core/qgsgeometry.cpp
===================================================================
--- trunk/qgis/src/core/qgsgeometry.cpp	2010-05-29 10:32:56 UTC (rev 13587)
+++ trunk/qgis/src/core/qgsgeometry.cpp	2010-05-29 11:03:30 UTC (rev 13588)
@@ -5859,6 +5859,43 @@
   return polygons;
 }
 
+double QgsGeometry::area()
+{
+  if ( !mGeos )
+  {
+    exportWkbToGeos();
+  }
+
+  double area;
+
+  try
+  {
+    if ( GEOSArea( mGeos, &area ) == 0 )
+      return -1.0;
+  }
+  CATCH_GEOS( -1.0 )
+
+  return area;
+}
+
+double QgsGeometry::length()
+{
+  if ( !mGeos )
+  {
+    exportWkbToGeos();
+  }
+
+  double length;
+
+  try
+  {
+    if ( GEOSLength( mGeos, &length ) == 0 )
+      return -1.0;
+  }
+  CATCH_GEOS( -1.0 )
+
+  return length;
+}
 double QgsGeometry::distance( QgsGeometry& geom )
 {
   if ( !mGeos )

Modified: trunk/qgis/src/core/qgsgeometry.h
===================================================================
--- trunk/qgis/src/core/qgsgeometry.h	2010-05-29 10:32:56 UTC (rev 13587)
+++ trunk/qgis/src/core/qgsgeometry.h	2010-05-29 11:03:30 UTC (rev 13588)
@@ -146,6 +146,16 @@
      */
     bool isGeosEmpty();
 
+    /** get area of geometry using GEOS
+      @note added in 1.5
+     */
+    double area();
+
+    /** get length of geometry using GEOS
+      @note added in 1.5
+     */
+    double length();
+
     double distance( QgsGeometry& geom );
 
     /**

Modified: trunk/qgis/src/plugins/labeling/pallabeling.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 10:32:56 UTC (rev 13587)
+++ trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 11:03:30 UTC (rev 13588)
@@ -229,25 +229,19 @@
     return true;
   }
 
-  GEOSGeometry* geosGeom = geom->asGeos();
-  if ( !geosGeom )
-  {
-    return true;
-  }
-
   double mapUnitsPerMM = ct.mapToPixel().mapUnitsPerPixel() * ct.scaleFactor();
   if ( featureType == QGis::Line )
   {
-    double length;
-    if ( GEOSLength( geosGeom, &length ) )
+    double length = geom->length();
+    if ( length >= 0.0 )
     {
       return ( length >= ( minSize * mapUnitsPerMM ) );
     }
   }
   else if ( featureType == QGis::Polygon )
   {
-    double area;
-    if ( GEOSArea( geosGeom, &area ) )
+    double area = geom->area();
+    if ( area >= 0.0 )
     {
       return ( sqrt( area ) >= ( minSize * mapUnitsPerMM ) );
     }



More information about the QGIS-commit mailing list