Skip to content
Permalink
Browse files

Fix delimitedtext CSVs in virtual layers

  • Loading branch information
suricactus committed Jun 12, 2020
1 parent 59ce5c8 commit e67b75d1955e2a07818fc60cc6db431843e6a2c6
@@ -61,6 +61,15 @@ QRegExp QgsDelimitedTextProvider::sCrdDmsRegexp( "^\\s*(?:([-+nsew])\\s*)?(\\d{1
QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const ProviderOptions &options )
: QgsVectorDataProvider( uri, options )
{
// uri should be in the form of "file:///path/to/file.csv?query=params", if not, enforce it in that format
// first read the already encoded url to get the query string
QUrl url = QUrl::fromEncoded( uri.toLatin1() );
// temporarily store the query string
const QString tmpUrlQuery = url.query();
// make sure that the url is actually prefixed with "file://". However, this breaks the query part ("?" char gets encoded), so discard the query string
url = QUrl::fromLocalFile( url.path() );
// finally restore the query part
url.setQuery( tmpUrlQuery );

// Add supported types to enable creating expression fields in field calculator
setNativeTypes( QList< NativeType >()
@@ -72,7 +81,6 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const Pr

QgsDebugMsgLevel( "Delimited text file uri is " + uri, 2 );

const QUrl url = QUrl::fromEncoded( uri.toLatin1() );
mFile = qgis::make_unique< QgsDelimitedTextFile >();
mFile->setFromUrl( url );

@@ -317,7 +317,9 @@ bool QgsVirtualLayerProvider::createIt()
provider.replace( QLatin1String( "'" ), QLatin1String( "''" ) );
QString source = mLayers.at( i ).source;
source.replace( QLatin1String( "'" ), QLatin1String( "''" ) );
QString encoding = mLayers.at( i ).encoding;
QString encoding = mLayers.at( i ).encoding.isEmpty()
? QStringLiteral( "System" )
: mLayers.at( i ).encoding;
QString createStr = QStringLiteral( "DROP TABLE IF EXISTS \"%1\"; CREATE VIRTUAL TABLE \"%1\" USING QgsVLayer('%2','%4',%3)" )
.arg( vname,
provider,
@@ -555,7 +557,10 @@ QgsRectangle QgsVirtualLayerProvider::extent() const
void QgsVirtualLayerProvider::updateStatistics() const
{
bool hasGeometry = mDefinition.geometryWkbType() != QgsWkbTypes::NoGeometry;
QString subset = mSubset.isEmpty() ? QString() : " WHERE " + mSubset;
QString subset = mSubset.isEmpty() ? QString() : QStringLiteral( " WHERE %1" ).arg( mSubset );

Q_ASSERT( ! mTableName.isNull() );

QString sql = QStringLiteral( "SELECT Count(*)%1 FROM %2%3" )
.arg( hasGeometry ? QStringLiteral( ",Min(MbrMinX(%1)),Min(MbrMinY(%1)),Max(MbrMaxX(%1)),Max(MbrMaxY(%1))" ).arg( quotedColumn( mDefinition.geometryField() ) ) : QString(),
mTableName,

0 comments on commit e67b75d

Please sign in to comment.
You can’t perform that action at this time.