mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-06 00:07:29 -04:00
Add expression functions for bounding box (bounds), bounding box width & height (bounds_width/bounds_height), and min/max x/y coordinates (xmin/xmax/ymin/ymax)
This commit is contained in:
parent
c305c2fdb6
commit
2ce741678f
12
resources/function_help/bounds
Normal file
12
resources/function_help/bounds
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> bounds($geometry) → returns bounding box of $geometry</pre>
|
||||
|
12
resources/function_help/bounds_height
Normal file
12
resources/function_help/bounds_height
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> bounds_height($geometry) → returns height of bounding box of $geometry</pre>
|
||||
|
12
resources/function_help/bounds_width
Normal file
12
resources/function_help/bounds_width
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> bounds_width($geometry) → returns width of bounding box of $geometry</pre>
|
||||
|
12
resources/function_help/xmax
Normal file
12
resources/function_help/xmax
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> xmax($geometry) → returns maximum x coordinate of $geometry</pre>
|
||||
|
12
resources/function_help/xmin
Normal file
12
resources/function_help/xmin
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> xmin($geometry) → returns minimum x coordinate of $geometry</pre>
|
||||
|
12
resources/function_help/ymax
Normal file
12
resources/function_help/ymax
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> ymax($geometry) → returns maximum y coordinate of $geometry</pre>
|
||||
|
12
resources/function_help/ymin
Normal file
12
resources/function_help/ymin
Normal file
@ -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 → a geometry
|
||||
|
||||
<h4>Example</h4>
|
||||
<pre> ymin($geometry) → 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,7 +636,7 @@ 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 );
|
||||
@ -644,7 +644,31 @@ class TestQgsExpression: public QObject
|
||||
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user