Skip to content

Commit

Permalink
Added "$rownum" token to syntax of expressions. In field calculator i…
Browse files Browse the repository at this point in the history
…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.
9 changes: 8 additions & 1 deletion python/core/qgssearchtreenode.sip
Expand Up @@ -57,7 +57,9 @@ class QgsSearchTreeNode
opLike, // LIKE

// string handling
opCONCAT
opCONCAT,

opROWNUM
};

//! constructors
Expand Down Expand Up @@ -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:


Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsfieldcalculator.cpp
Expand Up @@ -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 ) )
Expand All @@ -166,6 +167,8 @@ void QgsFieldCalculator::accept()
}
}

searchTree->setCurrentRowNumber( rownum );

QgsSearchTreeValue value;
if ( useGeometry )
{
Expand All @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsfieldcalculator.h
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgssearchstringlexer.ll
Expand Up @@ -102,6 +102,8 @@ string "'"{str_char}*"'"
{string} { return STRING; }
"$rownum" { return ROWNUM; }
"$area" { return AREA; }
"$length" { return LENGTH; }
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgssearchstringparser.yy
Expand Up @@ -64,6 +64,7 @@ void addToTmpNodes(QgsSearchTreeNode* node);
%token <op> FUNCTION
%token CONCAT
%token IS
%token ROWNUM
%token AREA
%token LENGTH
%token NULLVALUE
Expand Down Expand Up @@ -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($$); }
Expand Down
32 changes: 30 additions & 2 deletions src/core/qgssearchtreenode.cpp
Expand Up @@ -125,6 +125,8 @@ QgsSearchTreeNode::~QgsSearchTreeNode()

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

if ( mType == tOperator && ( mOp == opLENGTH || mOp == opAREA ) )
{
//initialize QgsDistanceArea
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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() )
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgssearchtreenode.h
Expand Up @@ -95,7 +95,9 @@ class CORE_EXPORT QgsSearchTreeNode
opLike, // LIKE

// string handling
opCONCAT
opCONCAT,

opROWNUM
};

//! constructors
Expand Down Expand Up @@ -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:


Expand Down
10 changes: 9 additions & 1 deletion src/ui/qgsfieldcalculatorbase.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>615</width>
<width>624</width>
<height>686</height>
</rect>
</property>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -328,6 +335,7 @@
<tabstop>mToStringButton</tabstop>
<tabstop>mLengthButton</tabstop>
<tabstop>mAreaButton</tabstop>
<tabstop>mRowNumButton</tabstop>
<tabstop>mExpressionTextEdit</tabstop>
<tabstop>mButtonBox</tabstop>
</tabstops>
Expand Down

0 comments on commit 6bdb0c1

Please sign in to comment.