@@ -329,14 +329,13 @@ static QgsExpression::Interval getInterval( const QVariant& value, QgsExpression
329
329
330
330
return QgsExpression::Interval::invalidInterVal ();
331
331
}
332
-
333
- static QgsGeometry* getGeometry ( const QVariant& value, QgsExpression* parent)
332
+ static QgsGeometry getGeometry ( const QVariant& value, QgsExpression* parent)
334
333
{
335
- if ( value.canConvert <QgsGeometry* >() )
336
- return value.value <QgsGeometry* >();
334
+ if ( value.canConvert <QgsGeometry>() )
335
+ return value.value <QgsGeometry>();
337
336
338
337
parent->setEvalErrorString ( " Cannot convert to QgsGeometry" );
339
- return 0 ;
338
+ return QgsGeometry () ;
340
339
}
341
340
342
341
@@ -770,7 +769,7 @@ static QVariant fcnGeometry( const QVariantList& , QgsFeature* f, QgsExpression*
770
769
{
771
770
QgsGeometry* geom = f->geometry ();
772
771
if ( geom )
773
- return QVariant::fromValue ( geom );
772
+ return QVariant::fromValue ( * geom );
774
773
else
775
774
return QVariant ();
776
775
}
@@ -779,7 +778,7 @@ static QVariant fcnGeomFromWKT( const QVariantList& values, QgsFeature*, QgsExpr
779
778
QString wkt = getStringValue ( values.at ( 0 ), parent );
780
779
QgsGeometry* geom = QgsGeometry::fromWkt ( wkt );
781
780
if ( geom )
782
- return QVariant::fromValue ( geom );
781
+ return QVariant::fromValue ( * geom );
783
782
else
784
783
return QVariant ();
785
784
}
@@ -802,7 +801,7 @@ static QVariant fcnGeomFromGML2( const QVariantList& values, QgsFeature*, QgsExp
802
801
geom = QgsGeometry::fromGML2 ( doc.documentElement () );
803
802
804
803
if ( geom )
805
- return QVariant::fromValue ( geom );
804
+ return QVariant::fromValue ( * geom );
806
805
else
807
806
return QVariant ();
808
807
}
@@ -828,68 +827,132 @@ static QVariant fcnGeomPerimeter( const QVariantList& , QgsFeature* f, QgsExpres
828
827
829
828
static QVariant fcnBbox ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
830
829
{
831
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
832
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
833
- if ( fGeom && sGeom )
834
- return fGeom ->intersects ( sGeom ->boundingBox () ) ? TVL_True : TVL_False;
835
- return QVariant ();
830
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
831
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
832
+ return fGeom .intersects ( sGeom .boundingBox () ) ? TVL_True : TVL_False;
836
833
}
837
834
static QVariant fcnDisjoint ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
838
835
{
839
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
840
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
841
- if ( fGeom && sGeom )
842
- return fGeom ->disjoint ( sGeom ) ? TVL_True : TVL_False;
843
- return QVariant ();
836
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
837
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
838
+ return fGeom .disjoint ( &sGeom ) ? TVL_True : TVL_False;
844
839
}
845
840
static QVariant fcnIntersects ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
846
841
{
847
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
848
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
849
- if ( fGeom && sGeom )
850
- return fGeom ->intersects ( sGeom ) ? TVL_True : TVL_False;
851
- return QVariant ();
842
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
843
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
844
+ return fGeom .intersects ( &sGeom ) ? TVL_True : TVL_False;
852
845
}
853
846
static QVariant fcnTouches ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
854
847
{
855
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
856
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
857
- if ( fGeom && sGeom )
858
- return fGeom ->touches ( sGeom ) ? TVL_True : TVL_False;
859
- return QVariant ();
848
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
849
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
850
+ return fGeom .touches ( &sGeom ) ? TVL_True : TVL_False;
860
851
}
861
852
static QVariant fcnCrosses ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
862
853
{
863
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
864
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
865
- if ( fGeom && sGeom )
866
- return fGeom ->crosses ( sGeom ) ? TVL_True : TVL_False;
867
- return QVariant ();
854
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
855
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
856
+ return fGeom .crosses ( &sGeom ) ? TVL_True : TVL_False;
868
857
}
869
858
static QVariant fcnContains ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
870
859
{
871
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
872
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
873
- if ( fGeom && sGeom )
874
- return fGeom ->contains ( sGeom ) ? TVL_True : TVL_False;
875
- return QVariant ();
860
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
861
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
862
+ return fGeom .contains ( &sGeom ) ? TVL_True : TVL_False;
876
863
}
877
864
static QVariant fcnOverlaps ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
878
865
{
879
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
880
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
881
- if ( fGeom && sGeom )
882
- return fGeom ->overlaps ( sGeom ) ? TVL_True : TVL_False;
883
- return QVariant ();
866
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
867
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
868
+ return fGeom .overlaps ( &sGeom ) ? TVL_True : TVL_False;
884
869
}
885
870
static QVariant fcnWithin ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
886
871
{
887
- QgsGeometry* fGeom = getGeometry ( values.at ( 0 ), parent );
888
- QgsGeometry* sGeom = getGeometry ( values.at ( 1 ), parent );
889
- if ( fGeom && sGeom )
890
- return fGeom ->within ( sGeom ) ? TVL_True : TVL_False;
872
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
873
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
874
+ return fGeom .within ( &sGeom ) ? TVL_True : TVL_False;
875
+ }
876
+ static QVariant fcnBuffer ( const QVariantList& values, QgsFeature*, QgsExpression* parent )
877
+ {
878
+ if ( values.length () < 2 || values.length () > 3 )
879
+ return QVariant ();
880
+
881
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
882
+ double dist = getDoubleValue ( values.at ( 1 ), parent );
883
+ int seg = 8 ;
884
+ if ( values.length () == 3 )
885
+ seg = getIntValue ( values.at ( 2 ), parent );
886
+
887
+ QgsGeometry* geom = fGeom .buffer ( dist, seg );
888
+ if ( geom )
889
+ return QVariant::fromValue ( *geom );
890
+ return QVariant ();
891
+ }
892
+ static QVariant fcnCentroid ( const QVariantList& values, QgsFeature*, QgsExpression* parent )
893
+ {
894
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
895
+ QgsGeometry* geom = fGeom .centroid ();
896
+ if ( geom )
897
+ return QVariant::fromValue ( *geom );
898
+ return QVariant ();
899
+ }
900
+ static QVariant fcnConvexHull ( const QVariantList& values, QgsFeature*, QgsExpression* parent )
901
+ {
902
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
903
+ QgsGeometry* geom = fGeom .convexHull ();
904
+ if ( geom )
905
+ return QVariant::fromValue ( *geom );
906
+ return QVariant ();
907
+ }
908
+ static QVariant fcnDifference ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
909
+ {
910
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
911
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
912
+ QgsGeometry* geom = fGeom .difference ( &sGeom );
913
+ if ( geom )
914
+ return QVariant::fromValue ( *geom );
915
+ return QVariant ();
916
+ }
917
+ static QVariant fcnDistance ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
918
+ {
919
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
920
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
921
+ return QVariant ( fGeom .distance ( sGeom ) );
922
+ }
923
+ static QVariant fcnIntersection ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
924
+ {
925
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
926
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
927
+ QgsGeometry* geom = fGeom .intersection ( &sGeom );
928
+ if ( geom )
929
+ return QVariant::fromValue ( *geom );
891
930
return QVariant ();
892
931
}
932
+ static QVariant fcnSymDifference ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
933
+ {
934
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
935
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
936
+ QgsGeometry* geom = fGeom .symDifference ( &sGeom );
937
+ if ( geom )
938
+ return QVariant::fromValue ( *geom );
939
+ return QVariant ();
940
+ }
941
+ static QVariant fcnCombine ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
942
+ {
943
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
944
+ QgsGeometry sGeom = getGeometry ( values.at ( 1 ), parent );
945
+ QgsGeometry* geom = fGeom .combine ( &sGeom );
946
+ if ( geom )
947
+ return QVariant::fromValue ( *geom );
948
+ return QVariant ();
949
+ }
950
+ static QVariant fcnGeomToWKT ( const QVariantList& values, QgsFeature* , QgsExpression* parent )
951
+ {
952
+ QgsGeometry fGeom = getGeometry ( values.at ( 0 ), parent );
953
+ QString wkt = fGeom .exportToWkt ();
954
+ return QVariant ( wkt );
955
+ }
893
956
894
957
static QVariant fcnRound ( const QVariantList& values , QgsFeature *f, QgsExpression* parent )
895
958
{
@@ -1057,6 +1120,16 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
1057
1120
<< new StaticFunction ( " contains" , 2 , fcnContains, QObject::tr ( " Geometry" ) )
1058
1121
<< new StaticFunction ( " overlaps" , 2 , fcnOverlaps, QObject::tr ( " Geometry" ) )
1059
1122
<< new StaticFunction ( " within" , 2 , fcnWithin, QObject::tr ( " Geometry" ) )
1123
+ << new StaticFunction ( " buffer" , -1 , fcnBuffer, QObject::tr ( " Geometry" ) )
1124
+ << new StaticFunction ( " centroid" , 1 , fcnCentroid, QObject::tr ( " Geometry" ) )
1125
+ << new StaticFunction ( " convexHull" , 1 , fcnConvexHull, QObject::tr ( " Geometry" ) )
1126
+ << new StaticFunction ( " difference" , 2 , fcnDifference, QObject::tr ( " Geometry" ) )
1127
+ << new StaticFunction ( " distance" , 2 , fcnDistance, QObject::tr ( " Geometry" ) )
1128
+ << new StaticFunction ( " intersection" , 2 , fcnIntersection, QObject::tr ( " Geometry" ) )
1129
+ << new StaticFunction ( " symDifference" , 2 , fcnSymDifference, QObject::tr ( " Geometry" ) )
1130
+ << new StaticFunction ( " combine" , 2 , fcnCombine, QObject::tr ( " Geometry" ) )
1131
+ << new StaticFunction ( " union" , 2 , fcnCombine, QObject::tr ( " Geometry" ) )
1132
+ << new StaticFunction ( " geomToWKT" , 1 , fcnGeomToWKT, QObject::tr ( " Geometry" ) )
1060
1133
<< new StaticFunction ( " $rownum" , 0 , fcnRowNumber, QObject::tr ( " Record" ) )
1061
1134
<< new StaticFunction ( " $id" , 0 , fcnFeatureId, QObject::tr ( " Record" ) )
1062
1135
<< new StaticFunction ( " $scale" , 0 , fcnScale, QObject::tr ( " Record" ) )
0 commit comments