Skip to content

Commit 6af5b09

Browse files
author
jef
committedMay 29, 2010
add length() and area() methods to QgsGeometry
and use it in labeling and zonal statistics git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13588 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed
 

‎python/core/qgsgeometry.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ class QgsGeometry
111111
// TODO: unsupported class... would be possible to use PyGEOS?
112112
//void fromGeos(geos::Geometry* geos);
113113

114+
/** get area using GEOS
115+
@note added in 1.5
116+
*/
117+
double area();
118+
119+
/** get length using GEOS
120+
@note added in 1.5
121+
*/
122+
double length();
123+
114124
double distance(QgsGeometry& geom);
115125

116126
/**

‎src/analysis/vector/qgszonalstatistics.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
2323
#include "cpl_string.h"
2424
#include <QProgressDialog>
2525

26-
QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand ) :
27-
mRasterFilePath( rasterFile ), mRasterBand( rasterBand ), mPolygonLayer( polygonLayer ), mAttributePrefix( attributePrefix ), mInputNodataValue( -1 )
26+
QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
27+
: mRasterFilePath( rasterFile )
28+
, mRasterBand( rasterBand )
29+
, mPolygonLayer( polygonLayer )
30+
, mAttributePrefix( attributePrefix )
31+
, mInputNodataValue( -1 )
2832
{
2933

3034
}
3135

32-
QgsZonalStatistics::QgsZonalStatistics(): mRasterBand( 0 ), mPolygonLayer( 0 )
36+
QgsZonalStatistics::QgsZonalStatistics()
37+
: mRasterBand( 0 )
38+
, mPolygonLayer( 0 )
3339
{
3440

3541
}
@@ -162,13 +168,13 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
162168
continue;
163169
}
164170

165-
statisticsFromMiddlePointTest_improved( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
171+
statisticsFromMiddlePointTest_improved( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
166172
rasterBBox, sum, count );
167173

168174
if ( count <= 1 )
169175
{
170176
//the cell resolution is probably larger than the polygon area. We switch to precise pixel - polygon intersection in this case
171-
statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY, \
177+
statisticsFromPreciseIntersection( rasterBand, featureGeometry, offsetX, offsetY, nCellsX, nCellsY, cellsizeX, cellsizeY,
172178
rasterBBox, sum, count );
173179
}
174180

@@ -227,7 +233,7 @@ int QgsZonalStatistics::cellInfoForBBox( const QgsRectangle& rasterBBox, const Q
227233
return 0;
228234
}
229235

230-
void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry* poly, int pixelOffsetX, \
236+
void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry* poly, int pixelOffsetX,
231237
int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
232238
{
233239
double cellCenterX, cellCenterY;
@@ -260,20 +266,18 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, QgsGeometry*
260266
CPLFree( scanLine );
261267
}
262268

263-
void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeometry* poly, int pixelOffsetX, \
269+
void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeometry* poly, int pixelOffsetX,
264270
int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
265271
{
266272
sum = 0;
267273
count = 0;
268274
double currentY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2;
269275
float* pixelData = ( float * ) CPLMalloc( sizeof( float ) );
270276
QgsGeometry* pixelRectGeometry = 0;
271-
QgsGeometry* intersectGeometry = 0;
272277

273278
double hCellSizeX = cellSizeX / 2.0;
274279
double hCellSizeY = cellSizeY / 2.0;
275280
double pixelArea = cellSizeX * cellSizeY;
276-
double intersectionArea = 0;
277281
double weight = 0;
278282

279283
for ( int row = 0; row < nCellsY; ++row )
@@ -286,10 +290,11 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
286290
if ( pixelRectGeometry )
287291
{
288292
//intersection
289-
intersectGeometry = pixelRectGeometry->intersection( poly );
293+
QgsGeometry *intersectGeometry = pixelRectGeometry->intersection( poly );
290294
if ( intersectGeometry )
291295
{
292-
if ( GEOSArea( intersectGeometry->asGeos(), &intersectionArea ) )
296+
double intersectionArea = intersectGeometry->area();
297+
if ( intersectionArea >= 0.0 )
293298
{
294299
weight = intersectionArea / pixelArea;
295300
count += weight;
@@ -305,7 +310,7 @@ void QgsZonalStatistics::statisticsFromPreciseIntersection( void* band, QgsGeome
305310
CPLFree( pixelData );
306311
}
307312

308-
void QgsZonalStatistics::statisticsFromMiddlePointTest_improved( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, \
313+
void QgsZonalStatistics::statisticsFromMiddlePointTest_improved( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY,
309314
double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count )
310315
{
311316
double cellCenterX, cellCenterY;

‎src/core/qgsgeometry.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5859,6 +5859,43 @@ QgsMultiPolygon QgsGeometry::asMultiPolygon()
58595859
return polygons;
58605860
}
58615861

5862+
double QgsGeometry::area()
5863+
{
5864+
if ( !mGeos )
5865+
{
5866+
exportWkbToGeos();
5867+
}
5868+
5869+
double area;
5870+
5871+
try
5872+
{
5873+
if ( GEOSArea( mGeos, &area ) == 0 )
5874+
return -1.0;
5875+
}
5876+
CATCH_GEOS( -1.0 )
5877+
5878+
return area;
5879+
}
5880+
5881+
double QgsGeometry::length()
5882+
{
5883+
if ( !mGeos )
5884+
{
5885+
exportWkbToGeos();
5886+
}
5887+
5888+
double length;
5889+
5890+
try
5891+
{
5892+
if ( GEOSLength( mGeos, &length ) == 0 )
5893+
return -1.0;
5894+
}
5895+
CATCH_GEOS( -1.0 )
5896+
5897+
return length;
5898+
}
58625899
double QgsGeometry::distance( QgsGeometry& geom )
58635900
{
58645901
if ( !mGeos )

‎src/core/qgsgeometry.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ class CORE_EXPORT QgsGeometry
146146
*/
147147
bool isGeosEmpty();
148148

149+
/** get area of geometry using GEOS
150+
@note added in 1.5
151+
*/
152+
double area();
153+
154+
/** get length of geometry using GEOS
155+
@note added in 1.5
156+
*/
157+
double length();
158+
149159
double distance( QgsGeometry& geom );
150160

151161
/**

‎src/plugins/labeling/pallabeling.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,19 @@ bool LayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry*
229229
return true;
230230
}
231231

232-
GEOSGeometry* geosGeom = geom->asGeos();
233-
if ( !geosGeom )
234-
{
235-
return true;
236-
}
237-
238232
double mapUnitsPerMM = ct.mapToPixel().mapUnitsPerPixel() * ct.scaleFactor();
239233
if ( featureType == QGis::Line )
240234
{
241-
double length;
242-
if ( GEOSLength( geosGeom, &length ) )
235+
double length = geom->length();
236+
if ( length >= 0.0 )
243237
{
244238
return ( length >= ( minSize * mapUnitsPerMM ) );
245239
}
246240
}
247241
else if ( featureType == QGis::Polygon )
248242
{
249-
double area;
250-
if ( GEOSArea( geosGeom, &area ) )
243+
double area = geom->area();
244+
if ( area >= 0.0 )
251245
{
252246
return ( sqrt( area ) >= ( minSize * mapUnitsPerMM ) );
253247
}

0 commit comments

Comments
 (0)
Please sign in to comment.