Skip to content

Commit

Permalink
Fix delimitedtext CSVs in virtual layers
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Jun 12, 2020
1 parent 59ce5c8 commit e67b75d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -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 >()
Expand All @@ -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 );

Expand Down
9 changes: 7 additions & 2 deletions src/providers/virtual/qgsvirtuallayerprovider.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e67b75d

Please sign in to comment.