delimited text provider: fix indentation, some warnings and use

intersection only when requested in select().

git-svn-id: http://svn.osgeo.org/qgis/trunk@14825 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2010-12-02 08:32:12 +00:00
parent 2517e13a88
commit eb5f4912a8
3 changed files with 206 additions and 203 deletions

View File

@ -104,23 +104,23 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
.arg( txtDelimiter->text() )
.arg( delimiterType );
if( geomTypeXY->isChecked())
{
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
{
uri += QString( "&xField=%1&yField=%2" )
.arg( cmbXField->currentText() )
.arg( cmbYField->currentText() );
}
}
else
{
if( ! cmbWktField->currentText().isEmpty() )
{
uri += QString( "&wktField=%1" )
.arg( cmbWktField->currentText() );
}
}
if ( geomTypeXY->isChecked() )
{
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
{
uri += QString( "&xField=%1&yField=%2" )
.arg( cmbXField->currentText() )
.arg( cmbYField->currentText() );
}
}
else
{
if ( ! cmbWktField->currentText().isEmpty() )
{
uri += QString( "&wktField=%1" )
.arg( cmbWktField->currentText() );
}
}
int skipLines = rowCounter->value();
if ( skipLines > 0 )
@ -222,9 +222,9 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
disconnect( cmbXField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect( cmbYField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect( cmbWktField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect(geomTypeXY, SIGNAL(toggled(bool)), cmbXField, SLOT(setEnabled(bool)));
disconnect(geomTypeXY, SIGNAL(toggled(bool)), cmbYField, SLOT(setEnabled(bool)));
disconnect(geomTypeXY, SIGNAL(toggled(bool)), cmbWktField, SLOT(setDisabled(bool)));
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbXField, SLOT( setEnabled( bool ) ) );
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbYField, SLOT( setEnabled( bool ) ) );
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbWktField, SLOT( setDisabled( bool ) ) );
QString columnX = cmbXField->currentText();
QString columnY = cmbYField->currentText();
@ -241,7 +241,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
cmbYField->setEnabled( false );
cmbWktField->setEnabled( false );
if( ! haveValidFileAndDelimiters()) return;
if ( ! haveValidFileAndDelimiters() ) return;
QFile file( txtFilePath->text() );
if ( !file.open( QIODevice::ReadOnly ) )
@ -283,28 +283,28 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
cmbXField->addItem( field );
cmbYField->addItem( field );
cmbWktField->addItem( field );
haveFields = true;
cmbWktField->addItem( field );
haveFields = true;
}
int indexWkt = -1;
if( ! columnWkt.isEmpty() )
if ( ! columnWkt.isEmpty() )
{
indexWkt = cmbWktField->findText( columnWkt );
indexWkt = cmbWktField->findText( columnWkt );
}
if( indexWkt < 0 )
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText("wkt", Qt::MatchContains );
indexWkt = cmbWktField->findText( "wkt", Qt::MatchContains );
}
if( indexWkt < 0 )
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText("geometry", Qt::MatchContains );
indexWkt = cmbWktField->findText( "geometry", Qt::MatchContains );
}
if( indexWkt < 0 )
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText("shape", Qt::MatchContains );
indexWkt = cmbWktField->findText( "shape", Qt::MatchContains );
}
cmbWktField->setCurrentIndex( indexWkt);
cmbWktField->setCurrentIndex( indexWkt );
int indexX = -1;
if ( !columnX.isEmpty() )
@ -343,26 +343,26 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
cmbYField->setCurrentIndex( indexY );
bool isXY = (geomTypeXY->isChecked() && indexX >= 0 && indexY >= 0) || indexWkt < 0;
bool isXY = ( geomTypeXY->isChecked() && indexX >= 0 && indexY >= 0 ) || indexWkt < 0;
geomTypeXY->setChecked( isXY );
geomTypeXY->setChecked( isXY );
geomTypeWKT->setChecked( ! isXY );
if( haveFields )
if ( haveFields )
{
geomTypeXY->setEnabled(true);
geomTypeWKT->setEnabled(true);
cmbXField->setEnabled( isXY );
cmbYField->setEnabled( isXY );
cmbWktField->setEnabled( ! isXY );
geomTypeXY->setEnabled( true );
geomTypeWKT->setEnabled( true );
cmbXField->setEnabled( isXY );
cmbYField->setEnabled( isXY );
cmbWktField->setEnabled( ! isXY );
connect( cmbXField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbYField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbWktField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect(geomTypeXY, SIGNAL(toggled(bool)), cmbXField, SLOT(setEnabled(bool)));
connect(geomTypeXY, SIGNAL(toggled(bool)), cmbYField, SLOT(setEnabled(bool)));
connect(geomTypeXY, SIGNAL(toggled(bool)), cmbWktField, SLOT(setDisabled(bool)));
connect( cmbXField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbYField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbWktField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbXField, SLOT( setEnabled( bool ) ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbYField, SLOT( setEnabled( bool ) ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbWktField, SLOT( setDisabled( bool ) ) );
}
// clear the sample text box
@ -414,28 +414,28 @@ void QgsDelimitedTextPluginGui::getOpenFileName()
void QgsDelimitedTextPluginGui::updateFieldsAndEnable()
{
updateFieldLists();
enableAccept();
updateFieldLists();
enableAccept();
}
void QgsDelimitedTextPluginGui::enableAccept()
{
// If the geometry type field is enabled then there must be
// a valid file, and it must be
// If the geometry type field is enabled then there must be
// a valid file, and it must be
bool enabled = haveValidFileAndDelimiters();
if ( enabled )
{
if( geomTypeXY->isChecked() )
{
enabled = !( cmbXField->currentText().isEmpty() || cmbYField->currentText().isEmpty() || cmbXField->currentText() == cmbYField->currentText() );
}
else
{
enabled = !cmbWktField->currentText().isEmpty();
}
if ( geomTypeXY->isChecked() )
{
enabled = !( cmbXField->currentText().isEmpty() || cmbYField->currentText().isEmpty() || cmbXField->currentText() == cmbYField->currentText() );
}
else
{
enabled = !cmbWktField->currentText().isEmpty();
}
}
pbnOK->setEnabled( enabled );

View File

@ -132,13 +132,16 @@ QStringList QgsDelimitedTextProvider::splitLine( QString line )
}
QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
: QgsVectorDataProvider( uri ),
mHasWktField( false ), mFieldCount(0),
mXFieldIndex( -1 ), mYFieldIndex( -1 ),
mWktFieldIndex( -1 ), mWktHasZM( false),
mWktZMRegexp("\\s+(?:z|m|zm)(?=\\s*\\()",Qt::CaseInsensitive),
mWktCrdRegexp("(\\-?\\d+(?:\\.\\d*)?\\s+\\-?\\d+(?:\\.\\d*)?)\\s[\\s\\d\\.\\-]+"),
mShowInvalidLines( true ), mWkbType( QGis::WKBNoGeometry )
: QgsVectorDataProvider( uri )
, mHasWktField( false )
, mFieldCount( 0 )
, mXFieldIndex( -1 ), mYFieldIndex( -1 )
, mWktFieldIndex( -1 )
, mWktHasZM( false )
, mWktZMRegexp( "\\s+(?:z|m|zm)(?=\\s*\\()", Qt::CaseInsensitive )
, mWktCrdRegexp( "(\\-?\\d+(?:\\.\\d*)?\\s+\\-?\\d+(?:\\.\\d*)?)\\s[\\s\\d\\.\\-]+" )
, mShowInvalidLines( true )
, mWkbType( QGis::WKBNoGeometry )
{
// Get the file name and mDelimiter out of the uri
mFileName = uri.left( uri.indexOf( "?" ) );
@ -169,7 +172,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
yField = QUrl::fromPercentEncoding( yField.toUtf8() );
mHasWktField = wktField != "";
skipLines = QUrl::fromPercentEncoding( skipLines.toUtf8() );
mSkipLines = skipLines.toInt();
@ -247,13 +250,13 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
// fields vector
QStringList fieldList = splitLine( line );
mFieldCount = fieldList.count();
mFieldCount = fieldList.count();
// We don't know anything about a text based field other
// than its name. All fields are assumed to be text
int fieldPos = 0;
for( int column = 0; column < mFieldCount; column++ )
{
for ( int column = 0; column < mFieldCount; column++ )
{
QString field = fieldList[column];
if ( field.length() > 0 )
{
@ -275,20 +278,20 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
mYFieldIndex = column;
}
// WKT geometry field won't be displayed in attribute tables
if( column == mWktFieldIndex ) continue;
// WKT geometry field won't be displayed in attribute tables
if ( column == mWktFieldIndex ) continue;
QgsDebugMsg( "Adding field: " + ( field ) );
// assume that the field could be integer or double
// for now, let's set field type as text
attributeColumns.append(column);
attributeColumns.append( column );
attributeFields[fieldPos] = QgsField( field, QVariant::String, "Text" );
couldBeInt.insert( fieldPos, true );
couldBeDouble.insert( fieldPos, true );
fieldPos++;
}
}
if( mWktFieldIndex >= 0 ) { mXFieldIndex = -1; mYFieldIndex=-1; }
if ( mWktFieldIndex >= 0 ) { mXFieldIndex = -1; mYFieldIndex = -1; }
QgsDebugMsg( "Field count for the delimited text file is " + QString::number( attributeFields.size() ) );
hasFields = true;
}
@ -304,83 +307,83 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
continue;
}
if( mHasWktField && mWktFieldIndex >= 0 )
{
// Get the wkt - confirm it is valid, get the type, and
// if compatible with the rest of file, add to the extents
if ( mHasWktField && mWktFieldIndex >= 0 )
{
// Get the wkt - confirm it is valid, get the type, and
// if compatible with the rest of file, add to the extents
QString sWkt = parts[mWktFieldIndex];
QgsGeometry *geom = 0;
try
{
if( ! mWktHasZM && sWkt.indexOf(mWktZMRegexp) >= 0 ) mWktHasZM = true;
if( mWktHasZM )
{
sWkt.remove(mWktZMRegexp).replace(mWktCrdRegexp,"\\1");
}
geom = QgsGeometry::fromWkt(sWkt);
}
catch(...)
{
geom = 0;
}
QString sWkt = parts[mWktFieldIndex];
QgsGeometry *geom = 0;
try
{
if ( ! mWktHasZM && sWkt.indexOf( mWktZMRegexp ) >= 0 ) mWktHasZM = true;
if ( mWktHasZM )
{
sWkt.remove( mWktZMRegexp ).replace( mWktCrdRegexp, "\\1" );
}
geom = QgsGeometry::fromWkt( sWkt );
}
catch ( ... )
{
geom = 0;
}
if( geom )
{
QGis::WkbType type = geom->wkbType();
if( type != QGis::WKBNoGeometry )
{
if( mNumberFeatures == 0 )
{
mNumberFeatures++;
mWkbType = type;
mExtent = geom->boundingBox();
}
else if( type == mWkbType )
{
mNumberFeatures++;
QgsRectangle bbox( geom->boundingBox());
mExtent.combineExtentWith( &bbox );
}
}
delete geom;
}
}
if ( geom )
{
QGis::WkbType type = geom->wkbType();
if ( type != QGis::WKBNoGeometry )
{
if ( mNumberFeatures == 0 )
{
mNumberFeatures++;
mWkbType = type;
mExtent = geom->boundingBox();
}
else if ( type == mWkbType )
{
mNumberFeatures++;
QgsRectangle bbox( geom->boundingBox() );
mExtent.combineExtentWith( &bbox );
}
}
delete geom;
}
}
else if( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
{
else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
{
// Get the x and y values, first checking to make sure they
// aren't null.
// Get the x and y values, first checking to make sure they
// aren't null.
QString sX = parts[mXFieldIndex];
QString sY = parts[mYFieldIndex];
QString sX = parts[mXFieldIndex];
QString sY = parts[mYFieldIndex];
bool xOk = true;
bool yOk = true;
double x = sX.toDouble( &xOk );
double y = sY.toDouble( &yOk );
bool xOk = true;
bool yOk = true;
double x = sX.toDouble( &xOk );
double y = sY.toDouble( &yOk );
if ( xOk && yOk )
{
if ( mNumberFeatures > 0 )
{
mExtent.combineExtentWith( x, y );
}
else
{
// Extent for the first point is just the first point
mExtent.set( x, y, x, y );
mWkbType = QGis::WKBPoint;
}
mNumberFeatures++;
}
}
if ( xOk && yOk )
{
if ( mNumberFeatures > 0 )
{
mExtent.combineExtentWith( x, y );
}
else
{
// Extent for the first point is just the first point
mExtent.set( x, y, x, y );
mWkbType = QGis::WKBPoint;
}
mNumberFeatures++;
}
}
for( int i = 0; i < attributeFields.size(); i++ )
{
QString &value = parts[attributeColumns[i]];
if( value.isEmpty()) continue;
for ( int i = 0; i < attributeFields.size(); i++ )
{
QString &value = parts[attributeColumns[i]];
if ( value.isEmpty() ) continue;
// try to convert attribute values to integer and double
if ( couldBeInt[i] )
{
@ -433,8 +436,6 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
feature.setValid( false );
while ( ! mStream->atEnd() )
{
double x = 0.0;
double y = 0.0;
QString line = readLine( mStream ); // Default local 8 bit encoding
if ( line.isEmpty() )
continue;
@ -442,55 +443,55 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
// lex the tokens from the current data line
QStringList tokens = splitLine( line );
QgsGeometry *geom = 0;
QgsGeometry *geom = 0;
if( mHasWktField && mWktFieldIndex >= 0 )
{
try
{
QString &sWkt = tokens[mWktFieldIndex];
if( mWktHasZM )
{
sWkt.remove(mWktZMRegexp).replace(mWktCrdRegexp,"\\1");
}
if ( mHasWktField && mWktFieldIndex >= 0 )
{
try
{
QString &sWkt = tokens[mWktFieldIndex];
if ( mWktHasZM )
{
sWkt.remove( mWktZMRegexp ).replace( mWktCrdRegexp, "\\1" );
}
geom = QgsGeometry::fromWkt(sWkt);
}
catch(...)
{
geom = 0;
}
geom = QgsGeometry::fromWkt( sWkt );
}
catch ( ... )
{
geom = 0;
}
if( geom && geom->wkbType() != mWkbType )
{
delete geom;
geom = 0;
}
mFid++;
if( ! boundsCheck(geom))
{
delete geom;
geom = 0;
}
}
else if( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
{
bool xOk, yOk;
double x = tokens[mXFieldIndex].toDouble(&xOk);
double y = tokens[mYFieldIndex].toDouble(&yOk);
if( xOk && yOk )
{
mFid++;
if( boundsCheck(x,y) )
{
geom = QgsGeometry::fromPoint(QgsPoint(x,y));
}
}
}
if ( geom && geom->wkbType() != mWkbType )
{
delete geom;
geom = 0;
}
mFid++;
if ( ! boundsCheck( geom ) )
{
delete geom;
geom = 0;
}
}
else if ( ! mHasWktField && mXFieldIndex >= 0 && mYFieldIndex >= 0 )
{
bool xOk, yOk;
double x = tokens[mXFieldIndex].toDouble( &xOk );
double y = tokens[mYFieldIndex].toDouble( &yOk );
if ( xOk && yOk )
{
mFid++;
if ( boundsCheck( x, y ) )
{
geom = QgsGeometry::fromPoint( QgsPoint( x, y ) );
}
}
}
// If no valid geometry skip to the next line
// If no valid geometry skip to the next line
if( ! geom ) continue;
if ( ! geom ) continue;
// At this point the current feature values are valid
@ -498,13 +499,13 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
feature.setFeatureId( mFid );
feature.setGeometry( geom );
feature.setGeometry( geom );
for ( QgsAttributeList::const_iterator i = mAttributesToFetch.begin();
i != mAttributesToFetch.end();
++i )
{
QString &value = tokens[attributeColumns[*i]];
QString &value = tokens[attributeColumns[*i]];
QVariant val;
switch ( attributeFields[*i].type() )
{
@ -568,6 +569,7 @@ void QgsDelimitedTextProvider::select( QgsAttributeList fetchAttributes,
mSelectionRectangle = rect;
mAttributesToFetch = fetchAttributes;
mFetchGeom = fetchGeometry;
mUseIntersect = useIntersect;
if ( rect.isEmpty() )
{
mSelectionRectangle = mExtent;
@ -644,7 +646,7 @@ bool QgsDelimitedTextProvider::boundsCheck( double x, double y )
if ( mSelectionRectangle.isEmpty() || !mFetchGeom )
return true;
return mSelectionRectangle.contains( QgsPoint(x,y) );
return mSelectionRectangle.contains( QgsPoint( x, y ) );
}
/**
* Check to see if the geometry is within the selection rectangle
@ -655,8 +657,8 @@ bool QgsDelimitedTextProvider::boundsCheck( QgsGeometry *geom )
if ( mSelectionRectangle.isEmpty() || !mFetchGeom )
return true;
return geom->boundingBox().intersects( mSelectionRectangle )
&& geom->intersects(mSelectionRectangle);;
return geom->boundingBox().intersects( mSelectionRectangle ) &&
( !mUseIntersect || geom->intersects( mSelectionRectangle ) );
}
int QgsDelimitedTextProvider::capabilities() const

View File

@ -188,20 +188,20 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
QRegExp mDelimiterRegexp;
QString mDelimiterType;
bool mHasWktField;
int mFieldCount; // Note: this includes field count for wkt field
bool mHasWktField;
int mFieldCount; // Note: this includes field count for wkt field
int mXFieldIndex;
int mYFieldIndex;
int mWktFieldIndex;
int mWktFieldIndex;
// Handling of WKT types with .. Z, .. M, and .. ZM geometries (ie
// Z values and/or measures). mWktZMRegexp is used to test for and
// remove the Z or M fields, and mWktCrdRegexp is used to remove the
// extra coordinate values.
// Handling of WKT types with .. Z, .. M, and .. ZM geometries (ie
// Z values and/or measures). mWktZMRegexp is used to test for and
// remove the Z or M fields, and mWktCrdRegexp is used to remove the
// extra coordinate values.
bool mWktHasZM;
QRegExp mWktZMRegexp;
QRegExp mWktCrdRegexp;
bool mWktHasZM;
QRegExp mWktZMRegexp;
QRegExp mWktCrdRegexp;
//! Layer extent
QgsRectangle mExtent;
@ -216,6 +216,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
QTextStream *mStream;
bool mValid;
bool mUseIntersect;
int mGeomType;