注:表达式语句不能使用像$area这类QGIS的图层计算、几何计算有关的语句,只支持常规的SQL语句
取最大、最小值
需传入数值型字段的索引,根据字段名取索引建议用int idxfield = pVectorlayer->fields().lookupField(fieldname)
/**
* \see maximumValue()
* \see minimumAndMaximumValue()
* \see uniqueValues()
*/
QVariant minimumValue( int index ) const FINAL;
QVariant maximumValue( int index ) const FINAL;
void minimumAndMaximumValue( int index, QVariant &minimum SIP_OUT, QVariant &maximum SIP_OUT ) const;
取唯一值
可传入任意类型字段的索引,limit参数代表查询结果不超过limit个
QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const FINAL;
聚类统计
可传入数值型字段名称或可转换为数值的SQL表达式
QVariant aggregate( QgsAggregateCalculator::Aggregate aggregate,
const QString &fieldOrExpression,
const QgsAggregateCalculator::AggregateParameters ¶meters = QgsAggregateCalculator::AggregateParameters(),
QgsExpressionContext *context = nullptr,
bool *ok = nullptr,
QgsFeatureIds *fids = nullptr,
QgsFeedback *feedback = nullptr,
QString *error SIP_PYARGREMOVE = nullptr ) const;
QgsAggregateCalculator::Aggregate
是枚举类型,包含了常用的统计类型:
enum Aggregate
{
Count, //!< Count
CountDistinct, //!< Number of distinct values
CountMissing, //!< Number of missing (null) values
Min, //!< Min of values
Max, //!< Max of values
Sum, //!< Sum of values
Mean, //!< Mean of values (numeric fields only)
Median, //!< Median of values (numeric fields only)
StDev, //!< Standard deviation of values (numeric fields only)
StDevSample, //!< Sample standard deviation of values (numeric fields only)
Range, //!< Range of values (max - min) (numeric and datetime fields only)
Minority, //!< Minority of values
Majority, //!< Majority of values
FirstQuartile, //!< First quartile (numeric fields only)
ThirdQuartile, //!< Third quartile (numeric fields only)
InterQuartileRange, //!< Inter quartile range (IQR) (numeric fields only)
StringMinimumLength, //!< Minimum length of string (string fields only)
StringMaximumLength, //!< Maximum length of string (string fields only)
StringConcatenate, //!< Concatenate values with a joining string (string fields only). Specify the delimiter using setDelimiter().
GeometryCollect, //!< Create a multipart geometry from aggregated geometries
ArrayAggregate, //!< Create an array of values
StringConcatenateUnique //!< Concatenate unique values with a joining string (string fields only). Specify the delimiter using setDelimiter().
};
当QgsFeatureIds *fids
使用默认参数时,是对全图层的检查。在应用时,有需要根据过滤条件的需求,可采用两种方式:一种是遍历图层,得到符合条件的QgsFeatureIds,作为参数插入;一种是对图层设置过滤条件bool setSubsetString( const QString &subset )
。