Skip to content
Permalink
Browse files

Only build list of supported raster/vector filters and formats once

It's expensive to calculate, so only ever do it once...

Refs #38987
  • Loading branch information
nyalldawson committed Sep 25, 2020
1 parent 279c499 commit 776768eb9f4ee2a8d13909b4ced5c175a0d05b3d
@@ -35,6 +35,7 @@
#include "qgsgeometryengine.h"
#include "qgsproviderregistry.h"
#include "qgsexpressioncontextutils.h"
#include "qgsreadwritelocker.h"

#include <QFile>
#include <QFileInfo>
@@ -43,6 +44,7 @@
#include <QTextStream>
#include <QSet>
#include <QMetaType>
#include <QMutex>

#include <cassert>
#include <cstdlib> // size_t
@@ -3231,7 +3233,17 @@ void QgsVectorFileWriter::setSymbologyScale( double d )

QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
{
QList< FilterFormatDetails > results;
static QReadWriteLock sFilterLock;
static QMap< VectorFormatOptions, QList< QgsVectorFileWriter::FilterFormatDetails > > sFilters;

QgsReadWriteLocker locker( sFilterLock, QgsReadWriteLocker::Read );

const auto it = sFilters.constFind( options );
if ( it != sFilters.constEnd() )
return it.value();

locker.changeMode( QgsReadWriteLocker::Write );
QList< QgsVectorFileWriter::FilterFormatDetails > results;

QgsApplication::registerOgrDrivers();
int const drvCount = OGRGetDriverCount();
@@ -3303,6 +3315,7 @@ QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supported
return a.filterString.toLower().localeAwareCompare( b.filterString.toLower() ) < 0;
} );

sFilters.insert( options, results );
return results;
}

@@ -24,6 +24,7 @@
#include "qgsrasterprojector.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasternuller.h"
#include "qgsreadwritelocker.h"

#include <QCoreApplication>
#include <QProgressDialog>
@@ -34,6 +35,7 @@

#include <gdal.h>
#include <cpl_string.h>
#include <mutex>

QgsRasterDataProvider *QgsRasterFileWriter::createOneBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
@@ -1133,11 +1135,21 @@ QString QgsRasterFileWriter::filterForDriver( const QString &driverName )

QList< QgsRasterFileWriter::FilterFormatDetails > QgsRasterFileWriter::supportedFiltersAndFormats( RasterFormatOptions options )
{
QList< FilterFormatDetails > results;
static QReadWriteLock sFilterLock;
static QMap< RasterFormatOptions, QList< QgsRasterFileWriter::FilterFormatDetails > > sFilters;

QgsReadWriteLocker locker( sFilterLock, QgsReadWriteLocker::Read );

const auto it = sFilters.constFind( options );
if ( it != sFilters.constEnd() )
return it.value();

GDALAllRegister();
int const drvCount = GDALGetDriverCount();

locker.changeMode( QgsReadWriteLocker::Write );
QList< QgsRasterFileWriter::FilterFormatDetails > results;

FilterFormatDetails tifFormat;

for ( int i = 0; i < drvCount; ++i )
@@ -1183,6 +1195,8 @@ QList< QgsRasterFileWriter::FilterFormatDetails > QgsRasterFileWriter::supported
}
}

sFilters.insert( options, results );

return results;
}

@@ -870,13 +870,19 @@ def testFileFilterString(self):
self.assertTrue('ods' in formats)
parts = formats.split(';;')
for i in range(2, len(parts) - 1):
if 'GeoJSON - Newline Delimited' in parts[i] or 'GeoJSON - Newline Delimited' in parts[i + 1]:
# Python's < operator doesn't do locale aware sorting, so skip this problematic one
continue
self.assertLess(parts[i].lower(), parts[i + 1].lower())

# alphabetical sorting
formats2 = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.VectorFormatOptions())
self.assertNotEqual(formats.index('gpkg'), formats2.index('gpkg'))
parts = formats2.split(';;')
for i in range(len(parts) - 1):
if 'GeoJSON - Newline Delimited' in parts[i] or 'GeoJSON - Newline Delimited' in parts[i + 1]:
# Python's < operator doesn't do locale aware sorting, so skip this problematic one
continue
self.assertLess(parts[i].lower(), parts[i + 1].lower())

# hide non spatial

0 comments on commit 776768e

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