Skip to content
Permalink
Browse files
Added "$rownum" token to syntax of expressions. In field calculator i…
…t can be used for counting rows. The counter starts from 1. Outside field calculator it returns always zero.

Developed for Faunalia (http://www.faunalia.it) with funding from Regione Toscana - Sistema Informativo per la Gestione del Territorio e dell' Ambiente [RT-SIGTA].
For the project: "Sviluppo di prodotti software GIS open-source basati sui prodotti QuantumGIS e Postgis (CIG 037728516E)"


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13941 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 20, 2010
1 parent 9da341a commit 6bdb0c1
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
@@ -57,7 +57,9 @@ class QgsSearchTreeNode
opLike, // LIKE

// string handling
opCONCAT
opCONCAT,

opROWNUM
};

//! constructors
@@ -126,6 +128,11 @@ class QgsSearchTreeNode
//! @note added in 1.5
static QString quotedColumnRef( QString name );

//! Set current row number within this tree.
//! This value is stored only in the nodes being $rownum operator - in mNumber
//! @note added in 1.6
void setCurrentRowNumber( int rownum );

protected:


@@ -154,6 +154,7 @@ void QgsFieldCalculator::accept()
mVectorLayer->blockSignals( true );

bool useGeometry = calcString.contains( "$area" ) || calcString.contains( "$length" );
int rownum = 1;

mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), QgsRectangle(), useGeometry, false );
while ( mVectorLayer->nextFeature( feature ) )
@@ -166,6 +167,8 @@ void QgsFieldCalculator::accept()
}
}

searchTree->setCurrentRowNumber( rownum );

QgsSearchTreeValue value;
if ( useGeometry )
{
@@ -188,6 +191,8 @@ void QgsFieldCalculator::accept()
{
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.string(), false );
}

rownum++;
}

// stop blocking layerModified signals and make sure that one layerModified signal is emitted
@@ -387,6 +392,11 @@ void QgsFieldCalculator::on_mAreaButton_clicked()
mExpressionTextEdit->insertPlainText( "$area" );
}

void QgsFieldCalculator::on_mRowNumButton_clicked()
{
mExpressionTextEdit->insertPlainText( "$rownum" );
}

void QgsFieldCalculator::on_mSamplePushButton_clicked()
{
getFieldValues( 25 );
@@ -53,6 +53,7 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
void on_mToStringButton_clicked();
void on_mLengthButton_clicked();
void on_mAreaButton_clicked();
void on_mRowNumButton_clicked();
void on_mSamplePushButton_clicked();
void on_mAllPushButton_clicked();
void on_mOutputFieldNameLineEdit_textChanged( const QString& text );
@@ -102,6 +102,8 @@ string "'"{str_char}*"'"
{string} { return STRING; }
"$rownum" { return ROWNUM; }
"$area" { return AREA; }
"$length" { return LENGTH; }
@@ -64,6 +64,7 @@ void addToTmpNodes(QgsSearchTreeNode* node);
%token <op> FUNCTION
%token CONCAT
%token IS
%token ROWNUM
%token AREA
%token LENGTH
%token NULLVALUE
@@ -140,6 +141,7 @@ scalar_exp:
| '+' scalar_exp %prec UMINUS { $$ = $2; }
| '-' scalar_exp %prec UMINUS { $$ = $2; if ($$->type() == QgsSearchTreeNode::tNumber) $$->setNumber(- $$->number()); }
| scalar_exp CONCAT scalar_exp { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opCONCAT, $1, $3); joinTmpNodes($$, $1, $3); }
| ROWNUM { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opROWNUM, 0, 0); addToTmpNodes($$); }
| AREA { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opAREA, 0, 0); addToTmpNodes($$); }
| LENGTH { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opLENGTH, 0, 0); addToTmpNodes($$); }
| NUMBER { $$ = new QgsSearchTreeNode($1); addToTmpNodes($$); }
@@ -125,6 +125,8 @@ QgsSearchTreeNode::~QgsSearchTreeNode()

void QgsSearchTreeNode::init()
{
mCalc = NULL;

if ( mType == tOperator && ( mOp == opLENGTH || mOp == opAREA ) )
{
//initialize QgsDistanceArea
@@ -134,9 +136,10 @@ void QgsSearchTreeNode::init()
QString ellipsoid = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString();
mCalc->setEllipsoid( ellipsoid );
}
else
else if ( mType == tOperator && mOp == opROWNUM )
{
mCalc = NULL;
// initialize row number to a sane value
mNumber = 0;
}
}

@@ -535,6 +538,12 @@ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst( const QgsFieldMap& fields, c
return QgsSearchTreeValue( mCalc->measure( geom ) );
}

if ( mOp == opROWNUM )
{
// the row number has to be previously set by the caller using setCurrentRowNumber
return QgsSearchTreeValue( mNumber );
}

//string operations with one argument
if ( !mRight && !value1.isNumeric() )
{
@@ -635,6 +644,25 @@ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst( const QgsFieldMap& fields, c
}


void QgsSearchTreeNode::setCurrentRowNumber( int rownum )
{
if ( mType == tOperator )
{
if ( mOp == opROWNUM )
mNumber = rownum;
else
{
// propagate the new row number to children
if ( mLeft )
mLeft->setCurrentRowNumber( rownum );
if ( mRight )
mRight->setCurrentRowNumber( rownum );
}
}
}



int QgsSearchTreeValue::compare( QgsSearchTreeValue& value1, QgsSearchTreeValue& value2, Qt::CaseSensitivity cs )
{
if ( value1.isNumeric() || value2.isNumeric() )
@@ -95,7 +95,9 @@ class CORE_EXPORT QgsSearchTreeNode
opLike, // LIKE

// string handling
opCONCAT
opCONCAT,

opROWNUM
};

//! constructors
@@ -164,6 +166,11 @@ class CORE_EXPORT QgsSearchTreeNode
//! @note added in 1.5
static QString quotedColumnRef( QString name );

//! Set current row number within this tree.
//! This value is stored only in the nodes being $rownum operator - in mNumber
//! @note added in 1.6
void setCurrentRowNumber( int rownum );

protected:


@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>615</width>
<width>624</width>
<height>686</height>
</rect>
</property>
@@ -269,6 +269,13 @@
</property>
</widget>
</item>
<item row="2" column="5">
<widget class="QPushButton" name="mRowNumButton">
<property name="text">
<string>rownum</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -328,6 +335,7 @@
<tabstop>mToStringButton</tabstop>
<tabstop>mLengthButton</tabstop>
<tabstop>mAreaButton</tabstop>
<tabstop>mRowNumButton</tabstop>
<tabstop>mExpressionTextEdit</tabstop>
<tabstop>mButtonBox</tabstop>
</tabstops>

0 comments on commit 6bdb0c1

Please sign in to comment.