Skip to content

Commit 8a34dd9

Browse files
author
wonder
committedJul 20, 2010
Added "$rownum" token to syntax of expressions. In field calculator it 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@13941 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a2a985b commit 8a34dd9

8 files changed

+70
-5
lines changed
 

‎python/core/qgssearchtreenode.sip

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class QgsSearchTreeNode
5757
opLike, // LIKE
5858

5959
// string handling
60-
opCONCAT
60+
opCONCAT,
61+
62+
opROWNUM
6163
};
6264

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

131+
//! Set current row number within this tree.
132+
//! This value is stored only in the nodes being $rownum operator - in mNumber
133+
//! @note added in 1.6
134+
void setCurrentRowNumber( int rownum );
135+
129136
protected:
130137

131138

‎src/app/qgsfieldcalculator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ void QgsFieldCalculator::accept()
154154
mVectorLayer->blockSignals( true );
155155

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

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

170+
searchTree->setCurrentRowNumber( rownum );
171+
169172
QgsSearchTreeValue value;
170173
if ( useGeometry )
171174
{
@@ -188,6 +191,8 @@ void QgsFieldCalculator::accept()
188191
{
189192
mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.string(), false );
190193
}
194+
195+
rownum++;
191196
}
192197

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

395+
void QgsFieldCalculator::on_mRowNumButton_clicked()
396+
{
397+
mExpressionTextEdit->insertPlainText( "$rownum" );
398+
}
399+
390400
void QgsFieldCalculator::on_mSamplePushButton_clicked()
391401
{
392402
getFieldValues( 25 );

‎src/app/qgsfieldcalculator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
5353
void on_mToStringButton_clicked();
5454
void on_mLengthButton_clicked();
5555
void on_mAreaButton_clicked();
56+
void on_mRowNumButton_clicked();
5657
void on_mSamplePushButton_clicked();
5758
void on_mAllPushButton_clicked();
5859
void on_mOutputFieldNameLineEdit_textChanged( const QString& text );

‎src/core/qgssearchstringlexer.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ string "'"{str_char}*"'"
102102
103103
{string} { return STRING; }
104104
105+
"$rownum" { return ROWNUM; }
106+
105107
"$area" { return AREA; }
106108
"$length" { return LENGTH; }
107109

‎src/core/qgssearchstringparser.yy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void addToTmpNodes(QgsSearchTreeNode* node);
6464
%token <op> FUNCTION
6565
%token CONCAT
6666
%token IS
67+
%token ROWNUM
6768
%token AREA
6869
%token LENGTH
6970
%token NULLVALUE
@@ -140,6 +141,7 @@ scalar_exp:
140141
| '+' scalar_exp %prec UMINUS { $$ = $2; }
141142
| '-' scalar_exp %prec UMINUS { $$ = $2; if ($$->type() == QgsSearchTreeNode::tNumber) $$->setNumber(- $$->number()); }
142143
| scalar_exp CONCAT scalar_exp { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opCONCAT, $1, $3); joinTmpNodes($$, $1, $3); }
144+
| ROWNUM { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opROWNUM, 0, 0); addToTmpNodes($$); }
143145
| AREA { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opAREA, 0, 0); addToTmpNodes($$); }
144146
| LENGTH { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opLENGTH, 0, 0); addToTmpNodes($$); }
145147
| NUMBER { $$ = new QgsSearchTreeNode($1); addToTmpNodes($$); }

‎src/core/qgssearchtreenode.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ QgsSearchTreeNode::~QgsSearchTreeNode()
125125

126126
void QgsSearchTreeNode::init()
127127
{
128+
mCalc = NULL;
129+
128130
if ( mType == tOperator && ( mOp == opLENGTH || mOp == opAREA ) )
129131
{
130132
//initialize QgsDistanceArea
@@ -134,9 +136,10 @@ void QgsSearchTreeNode::init()
134136
QString ellipsoid = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString();
135137
mCalc->setEllipsoid( ellipsoid );
136138
}
137-
else
139+
else if ( mType == tOperator && mOp == opROWNUM )
138140
{
139-
mCalc = NULL;
141+
// initialize row number to a sane value
142+
mNumber = 0;
140143
}
141144
}
142145

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

541+
if ( mOp == opROWNUM )
542+
{
543+
// the row number has to be previously set by the caller using setCurrentRowNumber
544+
return QgsSearchTreeValue( mNumber );
545+
}
546+
538547
//string operations with one argument
539548
if ( !mRight && !value1.isNumeric() )
540549
{
@@ -635,6 +644,25 @@ QgsSearchTreeValue QgsSearchTreeNode::valueAgainst( const QgsFieldMap& fields, c
635644
}
636645

637646

647+
void QgsSearchTreeNode::setCurrentRowNumber( int rownum )
648+
{
649+
if ( mType == tOperator )
650+
{
651+
if ( mOp == opROWNUM )
652+
mNumber = rownum;
653+
else
654+
{
655+
// propagate the new row number to children
656+
if ( mLeft )
657+
mLeft->setCurrentRowNumber( rownum );
658+
if ( mRight )
659+
mRight->setCurrentRowNumber( rownum );
660+
}
661+
}
662+
}
663+
664+
665+
638666
int QgsSearchTreeValue::compare( QgsSearchTreeValue& value1, QgsSearchTreeValue& value2, Qt::CaseSensitivity cs )
639667
{
640668
if ( value1.isNumeric() || value2.isNumeric() )

‎src/core/qgssearchtreenode.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class CORE_EXPORT QgsSearchTreeNode
9595
opLike, // LIKE
9696

9797
// string handling
98-
opCONCAT
98+
opCONCAT,
99+
100+
opROWNUM
99101
};
100102

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

169+
//! Set current row number within this tree.
170+
//! This value is stored only in the nodes being $rownum operator - in mNumber
171+
//! @note added in 1.6
172+
void setCurrentRowNumber( int rownum );
173+
167174
protected:
168175

169176

‎src/ui/qgsfieldcalculatorbase.ui

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>615</width>
9+
<width>624</width>
1010
<height>686</height>
1111
</rect>
1212
</property>
@@ -269,6 +269,13 @@
269269
</property>
270270
</widget>
271271
</item>
272+
<item row="2" column="5">
273+
<widget class="QPushButton" name="mRowNumButton">
274+
<property name="text">
275+
<string>rownum</string>
276+
</property>
277+
</widget>
278+
</item>
272279
</layout>
273280
</widget>
274281
</item>
@@ -328,6 +335,7 @@
328335
<tabstop>mToStringButton</tabstop>
329336
<tabstop>mLengthButton</tabstop>
330337
<tabstop>mAreaButton</tabstop>
338+
<tabstop>mRowNumButton</tabstop>
331339
<tabstop>mExpressionTextEdit</tabstop>
332340
<tabstop>mButtonBox</tabstop>
333341
</tabstops>

0 commit comments

Comments
 (0)
Please sign in to comment.