Skip to content
Permalink
Browse files
Add expression functions for bounding box (bounds), bounding box widt…
…h & height (bounds_width/bounds_height), and min/max x/y coordinates (xmin/xmax/ymin/ymax)
  • Loading branch information
nyalldawson committed May 18, 2014
1 parent c305c2f commit 2ce741678f71e55ca2954a648fdf2c182ebb4ae9
@@ -0,0 +1,12 @@
<h3>bounds function</h3>
Returns a geometry which represents the bounding box of an input geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>bounds(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> bounds($geometry) &rarr; returns bounding box of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>bounds_height function</h3>
Returns the height of the bounding box of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>bounds_height(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> bounds_height($geometry) &rarr; returns height of bounding box of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>bounds_width function</h3>
Returns the width of the bounding box of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>bounds_width(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> bounds_width($geometry) &rarr; returns width of bounding box of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>xmax function</h3>
Returns the maximum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>xmax(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> xmax($geometry) &rarr; returns maximum x coordinate of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>xmin function</h3>
Returns the minimum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>xmin(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> xmin($geometry) &rarr; returns minimum x coordinate of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>ymax function</h3>
Returns the maximum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>ymax(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> ymax($geometry) &rarr; returns maximum y coordinate of $geometry</pre>

@@ -0,0 +1,12 @@
<h3>ymin function</h3>
Returns the minimum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

<h4>Syntax</h4>
<pre>ymin(geom)</pre>

<h4>Arguments</h4>
geom &rarr; a geometry

<h4>Example</h4>
<pre> ymin($geometry) &rarr; returns minimum y coordinate of $geometry</pre>

@@ -1087,6 +1087,56 @@ static QVariant fcnGeomPerimeter( const QVariantList& , const QgsFeature* f, Qgs
return QVariant( calc->measurePerimeter( f->geometry() ) );
}

static QVariant fcnBounds( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
QgsGeometry* geomBounds = QgsGeometry::fromRect( geom.boundingBox() );
if ( geomBounds )
{
return QVariant::fromValue( *geomBounds );
}
else
{
return QVariant();
}
}

static QVariant fcnBoundsWidth( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().width() );
}

static QVariant fcnBoundsHeight( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().height() );
}

static QVariant fcnXMin( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().xMinimum() );
}

static QVariant fcnXMax( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().xMaximum() );
}

static QVariant fcnYMin( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().yMinimum() );
}

static QVariant fcnYMax( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
return QVariant::fromValue( geom.boundingBox().yMaximum() );
}

static QVariant fcnBbox( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QgsGeometry fGeom = getGeometry( values.at( 0 ), parent );
@@ -1574,14 +1624,18 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "color_hsva", 4, fncColorHsva, "Color" )
<< new StaticFunction( "color_cmyk", 4, fcnColorCmyk, "Color" )
<< new StaticFunction( "color_cmyka", 5, fncColorCmyka, "Color" )
<< new StaticFunction( "xat", 1, fcnXat, "Geometry", "", true )
<< new StaticFunction( "yat", 1, fcnYat, "Geometry", "", true )
<< new StaticFunction( "$geometry", 0, fcnGeometry, "Geometry", "" , true )
<< new StaticFunction( "$area", 0, fcnGeomArea, "Geometry", "", true )
<< new StaticFunction( "$length", 0, fcnGeomLength, "Geometry", "", true )
<< new StaticFunction( "$perimeter", 0, fcnGeomPerimeter, "Geometry", "", true )
<< new StaticFunction( "$x", 0, fcnX, "Geometry", "", true )
<< new StaticFunction( "$y", 0, fcnY, "Geometry", "" , true )
<< new StaticFunction( "$geometry", 0, fcnGeometry, "Geometry", "" , true )
<< new StaticFunction( "xat", 1, fcnXat, "Geometry", "", true )
<< new StaticFunction( "yat", 1, fcnYat, "Geometry", "", true )
<< new StaticFunction( "xmin", 1, fcnXMin, "Geometry", "", true )
<< new StaticFunction( "xmax", 1, fcnXMax, "Geometry", "", true )
<< new StaticFunction( "ymin", 1, fcnYMin, "Geometry", "", true )
<< new StaticFunction( "ymax", 1, fcnYMax, "Geometry", "", true )
<< new StaticFunction( "geomFromWKT", 1, fcnGeomFromWKT, "Geometry" )
<< new StaticFunction( "geomFromGML", 1, fcnGeomFromGML, "Geometry" )
<< new StaticFunction( "bbox", 2, fcnBbox, "Geometry" )
@@ -1594,6 +1648,9 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "within", 2, fcnWithin, "Geometry" )
<< new StaticFunction( "buffer", -1, fcnBuffer, "Geometry" )
<< new StaticFunction( "centroid", 1, fcnCentroid, "Geometry" )
<< new StaticFunction( "bounds", 1, fcnBounds, "Geometry", "", true )
<< new StaticFunction( "bounds_width", 1, fcnBoundsWidth, "Geometry", "", true )
<< new StaticFunction( "bounds_height", 1, fcnBoundsHeight, "Geometry", "", true )
<< new StaticFunction( "convexHull", 1, fcnConvexHull, "Geometry" )
<< new StaticFunction( "difference", 2, fcnDifference, "Geometry" )
<< new StaticFunction( "distance", 2, fcnDistance, "Geometry" )
@@ -627,7 +627,7 @@ class TestQgsExpression: public QObject
{
QgsPolyline polyline, polygon_ring;
polyline << QgsPoint( 0, 0 ) << QgsPoint( 10, 0 );
polygon_ring << QgsPoint( 1, 1 ) << QgsPoint( 6, 1 ) << QgsPoint( 6, 6 ) << QgsPoint( 1, 6 ) << QgsPoint( 1, 1 );
polygon_ring << QgsPoint( 2, 1 ) << QgsPoint( 10, 1 ) << QgsPoint( 10, 6 ) << QgsPoint( 2, 6 ) << QgsPoint( 2, 1 );
QgsPolygon polygon;
polygon << polygon_ring;
QgsFeature fPolygon, fPolyline;
@@ -636,15 +636,39 @@ class TestQgsExpression: public QObject

QgsExpression exp1( "$area" );
QVariant vArea = exp1.evaluate( &fPolygon );
QCOMPARE( vArea.toDouble(), 25. );
QCOMPARE( vArea.toDouble(), 40. );

QgsExpression exp2( "$length" );
QVariant vLength = exp2.evaluate( &fPolyline );
QCOMPARE( vLength.toDouble(), 10. );

QgsExpression exp3( "$perimeter" );
QVariant vPerimeter = exp3.evaluate( &fPolygon );
QCOMPARE( vPerimeter.toDouble(), 20. );
QCOMPARE( vPerimeter.toDouble(), 26. );

QgsExpression exp4( "bounds_width($geometry)" );
QVariant vBoundsWidth = exp4.evaluate( &fPolygon );
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );

QgsExpression exp5( "bounds_height($geometry)" );
QVariant vBoundsHeight = exp5.evaluate( &fPolygon );
QCOMPARE( vBoundsHeight.toDouble(), 5.0 );

QgsExpression exp6( "xmin($geometry)" );
QVariant vXMin = exp6.evaluate( &fPolygon );
QCOMPARE( vXMin.toDouble(), 2.0 );

QgsExpression exp7( "xmax($geometry)" );
QVariant vXMax = exp7.evaluate( &fPolygon );
QCOMPARE( vXMax.toDouble(), 10.0 );

QgsExpression exp8( "ymin($geometry)" );
QVariant vYMin = exp8.evaluate( &fPolygon );
QCOMPARE( vYMin.toDouble(), 1.0 );

QgsExpression exp9( "ymax($geometry)" );
QVariant vYMax = exp9.evaluate( &fPolygon );
QCOMPARE( vYMax.toDouble(), 6.0 );
}

void eval_geometry_constructor_data()
@@ -803,6 +827,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "convexHull simple" ) << "convexHull( $geometry )" << ( void* ) geom << false << true << ( void* ) geom->convexHull();
geom = QgsGeometry::fromPolygon( polygon );
QTest::newRow( "convexHull multi" ) << "convexHull( geomFromWKT('GEOMETRYCOLLECTION(POINT(0 1), POINT(0 0), POINT(1 0), POINT(1 1))') )" << ( void* ) geom << false << false << ( void* ) QgsGeometry::fromWkt( "POLYGON ((0 0,0 1,1 1,1 0,0 0))" );
geom = QgsGeometry::fromPolygon( polygon );
QTest::newRow( "bounds" ) << "bounds( $geometry )" << ( void* ) geom << false << true << ( void* ) QgsGeometry::fromRect( geom->boundingBox() );
}

void eval_geometry_method()

0 comments on commit 2ce7416

Please sign in to comment.