Skip to content

Commit

Permalink
Refactoring code from iterator to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrook committed Apr 30, 2013
1 parent 2a23652 commit 1fb1c9c
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 181 deletions.
174 changes: 3 additions & 171 deletions src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,77 +49,9 @@ bool QgsDelimitedTextFeatureIterator::nextFeature( QgsFeature& feature )
if ( mClosed )
return false;

QStringList tokens;
while ( true )
{
QgsDelimitedTextFile::Status status = P->mFile->nextRecord( tokens );
if ( status == QgsDelimitedTextFile::RecordEOF ) break;
if ( status != QgsDelimitedTextFile::RecordOk ) continue;

int fid = P->mFile->recordLineNumber();
if( mRequest.filterType() == QgsFeatureRequest::FilterFid && fid != mRequest.filterFid()) continue;
if ( P->recordIsEmpty( tokens ) ) continue;

while ( tokens.size() < P->mFieldCount )
tokens.append( QString::null );

QgsGeometry *geom = 0;

if ( P->mWktFieldIndex >= 0 )
{
geom = loadGeometryWkt( tokens );
}
else if ( P->mXFieldIndex >= 0 && P->mYFieldIndex >= 0 )
{
geom = loadGeometryXY( tokens );
}

if ( !geom && P->mWkbType != QGis::WKBNoGeometry )
{
// Already dealt with invalid lines in provider - no need to repeat
// removed code (CC 2013-04-13) ...
// P->mInvalidLines << line;
// In any case it may be a valid line that is excluded because of
// bounds check...
continue;
}

// At this point the current feature values are valid

feature.setValid( true );
feature.setFields( &P->attributeFields ); // allow name-based attribute lookups
feature.setFeatureId( fid );
feature.initAttributes( P->attributeFields.count() );

if ( geom )
feature.setGeometry( geom );

if ( mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes )
{
const QgsAttributeList& attrs = mRequest.subsetOfAttributes();
for ( QgsAttributeList::const_iterator i = attrs.begin(); i != attrs.end(); ++i )
{
int fieldIdx = *i;
fetchAttribute( feature, fieldIdx, tokens );
}
}
else
{
for ( int idx = 0; idx < P->attributeFields.count(); ++idx )
fetchAttribute( feature, idx, tokens );
}

// We have a good line, so return
return true;

} // !mStream->atEnd()

// End of the file. If there are any lines that couldn't be
// loaded, display them now.
// P->handleInvalidLines();

close();
return false;
bool gotFeature = P->nextFeature(feature,P->mFile,mRequest);
if( ! gotFeature ) close();
return gotFeature;
}

bool QgsDelimitedTextFeatureIterator::rewind()
Expand All @@ -142,103 +74,3 @@ bool QgsDelimitedTextFeatureIterator::close()
mClosed = true;
return true;
}


QgsGeometry* QgsDelimitedTextFeatureIterator::loadGeometryWkt( const QStringList& tokens )
{
QgsGeometry* geom = 0;
QString sWkt = tokens[P->mWktFieldIndex];

geom = P->geomFromWkt( sWkt );

if ( geom && geom->type() != P->mGeometryType )
{
delete geom;
geom = 0;
}
if ( geom && !boundsCheck( geom ) )
{
delete geom;
geom = 0;
}
return geom;
}


QgsGeometry* QgsDelimitedTextFeatureIterator::loadGeometryXY( const QStringList& tokens )
{
QString sX = tokens[P->mXFieldIndex];
QString sY = tokens[P->mYFieldIndex];
QgsPoint pt;
bool ok = P->pointFromXY( sX, sY, pt );

if ( ok && boundsCheck( pt ) )
{
return QgsGeometry::fromPoint( pt );
}
return 0;
}

/**
* Check to see if the point is within the selection rectangle
*/
bool QgsDelimitedTextFeatureIterator::boundsCheck( const QgsPoint &pt )
{
// no selection rectangle or geometry => always in the bounds
if ( mRequest.filterType() != QgsFeatureRequest::FilterRect || ( mRequest.flags() & QgsFeatureRequest::NoGeometry ) )
return true;

return mRequest.filterRect().contains( pt );
}

/**
* Check to see if the geometry is within the selection rectangle
*/
bool QgsDelimitedTextFeatureIterator::boundsCheck( QgsGeometry *geom )
{
// no selection rectangle or geometry => always in the bounds
if ( mRequest.filterType() != QgsFeatureRequest::FilterRect || ( mRequest.flags() & QgsFeatureRequest::NoGeometry ) )
return true;

if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
return geom->intersects( mRequest.filterRect() );
else
return geom->boundingBox().intersects( mRequest.filterRect() );
}


void QgsDelimitedTextFeatureIterator::fetchAttribute( QgsFeature& feature, int fieldIdx, const QStringList& tokens )
{
if( fieldIdx < 0 || fieldIdx >= P->attributeColumns.count()) return;
int column = P->attributeColumns[fieldIdx];
if( column < 0 || column >= tokens.count()) return;
const QString &value = tokens[column];
QVariant val;
switch ( P->attributeFields[fieldIdx].type() )
{
case QVariant::Int:
if ( value.isEmpty() )
val = QVariant( P->attributeFields[fieldIdx].type() );
else
val = QVariant( value );
break;
case QVariant::Double:
if ( value.isEmpty() )
{
val = QVariant( P->attributeFields[fieldIdx].type() );
}
else if ( P->mDecimalPoint.isEmpty() )
{
val = QVariant( value.toDouble() );
}
else
{
val = QVariant( QString( value ).replace( P->mDecimalPoint, "." ).toDouble() );
}
break;
default:
val = QVariant( value );
break;
}
feature.setAttribute( fieldIdx, val );
}
8 changes: 0 additions & 8 deletions src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ class QgsDelimitedTextFeatureIterator : public QgsAbstractFeatureIterator

protected:
QgsDelimitedTextProvider* P;

QgsGeometry* loadGeometryWkt( const QStringList& tokens );
QgsGeometry* loadGeometryXY( const QStringList& tokens );

bool boundsCheck( const QgsPoint &pt );
bool boundsCheck( QgsGeometry *geom );

void fetchAttribute( QgsFeature& feature, int fieldIdx, const QStringList& tokens );
};


Expand Down
Loading

0 comments on commit 1fb1c9c

Please sign in to comment.