mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-15 00:02:52 -04:00
Add flags to QgsVectorFileWriter methods which return lists of drivers
Initially only flag available is whether to sort drivers by recommended order. The recommended order puts GPKG first and SHP second, then leaves the rest alphabetical. This fixes a few instances in the QGIS gui where these recommended formats are not listed first.
This commit is contained in:
parent
eb6f64ee4d
commit
dc341d2047
@ -145,6 +145,14 @@ Some formats require a compulsory encoding, typically UTF-8. If no compulsory en
|
||||
SymbolLayerSymbology
|
||||
};
|
||||
|
||||
|
||||
enum VectorFormatOption
|
||||
{
|
||||
SortRecommended,
|
||||
};
|
||||
typedef QFlags<QgsVectorFileWriter::VectorFormatOption> VectorFormatOptions;
|
||||
|
||||
|
||||
class FieldValueConverter
|
||||
{
|
||||
%Docstring
|
||||
@ -460,27 +468,41 @@ Create a new vector file writer
|
||||
|
||||
|
||||
|
||||
static QMap< QString, QString> supportedFiltersAndFormats();
|
||||
static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
|
||||
%Docstring
|
||||
Returns a map with format filter string as key and OGR format key as value.
|
||||
Returns a list or pairs, with format filter string as first element and OGR format key as second element.
|
||||
|
||||
The ``options`` argument can be used to control the sorting and filtering of
|
||||
returned formats.
|
||||
|
||||
.. seealso:: supportedOutputVectorLayerExtensions()
|
||||
:rtype: QMap< str, QString>
|
||||
:rtype: list of QPair< str, QString >
|
||||
%End
|
||||
|
||||
static QStringList supportedFormatExtensions();
|
||||
static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );
|
||||
%Docstring
|
||||
Returns a list of file extensions for supported formats.
|
||||
|
||||
The ``options`` argument can be used to control the sorting and filtering of
|
||||
returned formats.
|
||||
|
||||
.. versionadded:: 3.0
|
||||
.. seealso:: supportedFiltersAndFormats()
|
||||
:rtype: list of str
|
||||
%End
|
||||
|
||||
static QMap< QString, QString> ogrDriverList();
|
||||
static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended );
|
||||
%Docstring
|
||||
Returns driver list that can be used for dialogs. It contains all OGR drivers
|
||||
+ some additional internal QGIS driver names to distinguish between more
|
||||
supported formats of the same OGR driver
|
||||
:rtype: QMap< str, QString>
|
||||
plus some additional internal QGIS driver names to distinguish between more
|
||||
supported formats of the same OGR driver.
|
||||
|
||||
The returned list consists of pairs of driver long name (e.g. user-friendly
|
||||
display name for the format) to internal driver short name.
|
||||
|
||||
The ``options`` argument can be used to control the sorting and filtering of
|
||||
returned drivers.
|
||||
:rtype: list of QPair< str, QString >
|
||||
%End
|
||||
|
||||
static QString driverForExtension( const QString &extension );
|
||||
@ -492,9 +514,12 @@ Create a new vector file writer
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
static QString fileFilterString();
|
||||
static QString fileFilterString( VectorFormatOptions options = SortRecommended );
|
||||
%Docstring
|
||||
Returns filter string that can be used for dialogs
|
||||
Returns filter string that can be used for dialogs.
|
||||
|
||||
The ``options`` argument can be used to control the sorting and filtering of
|
||||
returned drivers.
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
@ -639,6 +664,8 @@ Close opened shapefile for writing
|
||||
|
||||
QFlags<QgsVectorFileWriter::EditionCapability> operator|(QgsVectorFileWriter::EditionCapability f1, QFlags<QgsVectorFileWriter::EditionCapability> f2);
|
||||
|
||||
QFlags<QgsVectorFileWriter::VectorFormatOption> operator|(QgsVectorFileWriter::VectorFormatOption f1, QFlags<QgsVectorFileWriter::VectorFormatOption> f2);
|
||||
|
||||
|
||||
|
||||
/************************************************************************
|
||||
|
@ -174,9 +174,9 @@ class GdalUtils(object):
|
||||
return 'ESRI Shapefile'
|
||||
|
||||
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
|
||||
for k, v in list(formats.items()):
|
||||
if ext in k:
|
||||
return v
|
||||
for format in formats:
|
||||
if ext in format[0]:
|
||||
return format[1]
|
||||
return 'ESRI Shapefile'
|
||||
|
||||
@staticmethod
|
||||
|
@ -2681,13 +2681,16 @@ void QgsVectorFileWriter::setSymbologyScale( double d )
|
||||
mRenderContext.setRendererScale( mSymbologyScale );
|
||||
}
|
||||
|
||||
QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats()
|
||||
QList< QPair< QString, QString > > QgsVectorFileWriter::supportedFiltersAndFormats( const VectorFormatOptions options )
|
||||
{
|
||||
QMap<QString, QString> resultMap;
|
||||
QList< QPair< QString, QString > > resultMap;
|
||||
|
||||
QgsApplication::registerOgrDrivers();
|
||||
int const drvCount = OGRGetDriverCount();
|
||||
|
||||
QPair< QString, QString > shapeFormat;
|
||||
QPair< QString, QString > gpkgFormat;
|
||||
|
||||
for ( int i = 0; i < drvCount; ++i )
|
||||
{
|
||||
OGRSFDriverH drv = OGRGetDriver( i );
|
||||
@ -2700,51 +2703,68 @@ QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats()
|
||||
if ( filterString.isEmpty() )
|
||||
continue;
|
||||
|
||||
resultMap.insert( filterString, drvName );
|
||||
if ( options & SortRecommended )
|
||||
{
|
||||
if ( drvName == QStringLiteral( "ESRI Shapefile" ) )
|
||||
{
|
||||
shapeFormat = qMakePair( filterString, drvName );
|
||||
continue;
|
||||
}
|
||||
else if ( drvName == QStringLiteral( "GPKG" ) )
|
||||
{
|
||||
gpkgFormat = qMakePair( filterString, drvName );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
resultMap << qMakePair( filterString, drvName );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::sort( resultMap.begin(), resultMap.end(), []( const QPair< QString, QString > &a, const QPair< QString, QString > &b ) -> bool
|
||||
{
|
||||
return a.second < b.second;
|
||||
} );
|
||||
|
||||
if ( options & SortRecommended )
|
||||
{
|
||||
if ( !shapeFormat.first.isEmpty() )
|
||||
{
|
||||
resultMap.insert( 0, shapeFormat );
|
||||
}
|
||||
if ( !gpkgFormat.first.isEmpty() )
|
||||
{
|
||||
resultMap.insert( 0, gpkgFormat );
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
QStringList QgsVectorFileWriter::supportedFormatExtensions()
|
||||
QStringList QgsVectorFileWriter::supportedFormatExtensions( const VectorFormatOptions options )
|
||||
{
|
||||
QgsStringMap formats = supportedFiltersAndFormats();
|
||||
const auto formats = supportedFiltersAndFormats( options );
|
||||
QStringList extensions;
|
||||
|
||||
QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]*)" ) );
|
||||
|
||||
QgsStringMap::const_iterator formatIt = formats.constBegin();
|
||||
auto formatIt = formats.constBegin();
|
||||
for ( ; formatIt != formats.constEnd(); ++formatIt )
|
||||
{
|
||||
QString ext = formatIt.key();
|
||||
QString ext = formatIt->first;
|
||||
QRegularExpressionMatch match = rx.match( ext );
|
||||
if ( !match.hasMatch() )
|
||||
continue;
|
||||
|
||||
QString matched = match.captured( 1 );
|
||||
|
||||
// special handling for the two main contenders for glory
|
||||
if ( matched.compare( QStringLiteral( "gpkg" ), Qt::CaseInsensitive ) == 0 )
|
||||
continue;
|
||||
if ( matched.compare( QStringLiteral( "shp" ), Qt::CaseInsensitive ) == 0 )
|
||||
continue;
|
||||
|
||||
extensions << matched;
|
||||
}
|
||||
|
||||
std::sort( extensions.begin(), extensions.end() );
|
||||
|
||||
// Make https://twitter.com/shapefiIe a sad little fellow
|
||||
extensions.insert( 0, QStringLiteral( "gpkg" ) );
|
||||
extensions.insert( 1, QStringLiteral( "shp" ) );
|
||||
return extensions;
|
||||
}
|
||||
|
||||
QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
|
||||
QList< QPair< QString, QString> > QgsVectorFileWriter::ogrDriverList( const VectorFormatOptions options )
|
||||
{
|
||||
QMap<QString, QString> resultMap;
|
||||
QList< QPair< QString, QString> > resultMap;
|
||||
|
||||
QgsApplication::registerOgrDrivers();
|
||||
int const drvCount = OGRGetDriverCount();
|
||||
@ -2756,6 +2776,10 @@ QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
|
||||
if ( drv )
|
||||
{
|
||||
QString drvName = OGR_Dr_GetName( drv );
|
||||
if ( drvName == QLatin1String( "ESRI Shapefile" ) )
|
||||
{
|
||||
writableDrivers << QStringLiteral( "DBF file" );
|
||||
}
|
||||
if ( OGR_Dr_TestCapability( drv, "CreateDataSource" ) != 0 )
|
||||
{
|
||||
// Add separate format for Mapinfo MIF (MITAB is OGR default)
|
||||
@ -2786,24 +2810,35 @@ QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
|
||||
}
|
||||
CPLFree( options[0] );
|
||||
}
|
||||
else if ( drvName == QLatin1String( "ESRI Shapefile" ) )
|
||||
{
|
||||
writableDrivers << QStringLiteral( "DBF file" );
|
||||
}
|
||||
writableDrivers << drvName;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::sort( writableDrivers.begin(), writableDrivers.end() );
|
||||
if ( options & SortRecommended )
|
||||
{
|
||||
// recommended order sorting, so we shift certain formats to the top
|
||||
if ( writableDrivers.contains( QStringLiteral( "ESRI Shapefile" ) ) )
|
||||
{
|
||||
writableDrivers.removeAll( QStringLiteral( "ESRI Shapefile" ) );
|
||||
writableDrivers.insert( 0, QStringLiteral( "ESRI Shapefile" ) );
|
||||
}
|
||||
if ( writableDrivers.contains( QStringLiteral( "GPKG" ) ) )
|
||||
{
|
||||
// Make https://twitter.com/shapefiIe a sad little fellow
|
||||
writableDrivers.removeAll( QStringLiteral( "GPKG" ) );
|
||||
writableDrivers.insert( 0, QStringLiteral( "GPKG" ) );
|
||||
}
|
||||
}
|
||||
|
||||
Q_FOREACH ( const QString &drvName, writableDrivers )
|
||||
for ( const QString &drvName : qgis::as_const( writableDrivers ) )
|
||||
{
|
||||
MetaData metadata;
|
||||
if ( driverMetadata( drvName, metadata ) && !metadata.trLongName.isEmpty() )
|
||||
{
|
||||
resultMap.insert( metadata.trLongName, drvName );
|
||||
resultMap << qMakePair( metadata.trLongName, drvName );
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
@ -2841,17 +2876,16 @@ QString QgsVectorFileWriter::driverForExtension( const QString &extension )
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString QgsVectorFileWriter::fileFilterString()
|
||||
QString QgsVectorFileWriter::fileFilterString( const VectorFormatOptions options )
|
||||
{
|
||||
QString filterString;
|
||||
QMap< QString, QString> driverFormatMap = supportedFiltersAndFormats();
|
||||
QMap< QString, QString>::const_iterator it = driverFormatMap.constBegin();
|
||||
for ( ; it != driverFormatMap.constEnd(); ++it )
|
||||
const auto driverFormatMap = supportedFiltersAndFormats( options );
|
||||
for ( auto it = driverFormatMap.constBegin(); it != driverFormatMap.constEnd(); ++it )
|
||||
{
|
||||
if ( !filterString.isEmpty() )
|
||||
filterString += QLatin1String( ";;" );
|
||||
|
||||
filterString += it.key();
|
||||
filterString += it->first;
|
||||
}
|
||||
return filterString;
|
||||
}
|
||||
|
@ -186,6 +186,17 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
|
||||
SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Options for sorting and filtering vector formats.
|
||||
* \since QGIS 3.0
|
||||
*/
|
||||
enum VectorFormatOption
|
||||
{
|
||||
SortRecommended = 1 << 1, //!< Use recommended sort order, with extremely commonly used formats listed first
|
||||
};
|
||||
Q_DECLARE_FLAGS( VectorFormatOptions, VectorFormatOption )
|
||||
|
||||
/**
|
||||
* \ingroup core
|
||||
* Interface to convert raw field values to their user-friendly value.
|
||||
@ -496,24 +507,38 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
|
||||
QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete;
|
||||
|
||||
/**
|
||||
* Returns a map with format filter string as key and OGR format key as value.
|
||||
* Returns a list or pairs, with format filter string as first element and OGR format key as second element.
|
||||
*
|
||||
* The \a options argument can be used to control the sorting and filtering of
|
||||
* returned formats.
|
||||
*
|
||||
* \see supportedOutputVectorLayerExtensions()
|
||||
*/
|
||||
static QMap< QString, QString> supportedFiltersAndFormats();
|
||||
static QList< QPair< QString, QString > > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
|
||||
|
||||
/**
|
||||
* Returns a list of file extensions for supported formats.
|
||||
*
|
||||
* The \a options argument can be used to control the sorting and filtering of
|
||||
* returned formats.
|
||||
*
|
||||
* \since QGIS 3.0
|
||||
* \see supportedFiltersAndFormats()
|
||||
*/
|
||||
static QStringList supportedFormatExtensions();
|
||||
static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );
|
||||
|
||||
/**
|
||||
* Returns driver list that can be used for dialogs. It contains all OGR drivers
|
||||
* + some additional internal QGIS driver names to distinguish between more
|
||||
* supported formats of the same OGR driver
|
||||
* plus some additional internal QGIS driver names to distinguish between more
|
||||
* supported formats of the same OGR driver.
|
||||
*
|
||||
* The returned list consists of pairs of driver long name (e.g. user-friendly
|
||||
* display name for the format) to internal driver short name.
|
||||
*
|
||||
* The \a options argument can be used to control the sorting and filtering of
|
||||
* returned drivers.
|
||||
*/
|
||||
static QMap< QString, QString> ogrDriverList();
|
||||
static QList< QPair< QString, QString > > ogrDriverList( VectorFormatOptions options = SortRecommended );
|
||||
|
||||
/**
|
||||
* Returns the OGR driver name for a specified file \a extension. E.g. the
|
||||
@ -523,8 +548,13 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
|
||||
*/
|
||||
static QString driverForExtension( const QString &extension );
|
||||
|
||||
//! Returns filter string that can be used for dialogs
|
||||
static QString fileFilterString();
|
||||
/**
|
||||
* Returns filter string that can be used for dialogs.
|
||||
*
|
||||
* The \a options argument can be used to control the sorting and filtering of
|
||||
* returned drivers.
|
||||
*/
|
||||
static QString fileFilterString( VectorFormatOptions options = SortRecommended );
|
||||
|
||||
//! Creates a filter for an OGR driver key
|
||||
static QString filterForDriver( const QString &driverName );
|
||||
@ -701,6 +731,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
|
||||
};
|
||||
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::EditionCapabilities )
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::VectorFormatOptions )
|
||||
|
||||
// clazy:excludeall=qstring-allocations
|
||||
|
||||
|
@ -87,11 +87,11 @@ void QgsVectorLayerSaveAsDialog::setup()
|
||||
QgsSettings settings;
|
||||
restoreGeometry( settings.value( QStringLiteral( "Windows/VectorLayerSaveAs/geometry" ) ).toByteArray() );
|
||||
|
||||
QMap<QString, QString> map = QgsVectorFileWriter::ogrDriverList();
|
||||
const QList< QPair< QString, QString > > map = QgsVectorFileWriter::ogrDriverList();
|
||||
mFormatComboBox->blockSignals( true );
|
||||
for ( QMap< QString, QString>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
|
||||
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
|
||||
{
|
||||
mFormatComboBox->addItem( it.key(), it.value() );
|
||||
mFormatComboBox->addItem( it->first, it->second );
|
||||
}
|
||||
|
||||
QString format = settings.value( QStringLiteral( "UI/lastVectorFormat" ), "GPKG" ).toString();
|
||||
|
@ -54,10 +54,10 @@ QgsGeometryCheckerSetupTab::QgsGeometryCheckerSetupTab( QgisInterface *iface, QD
|
||||
mAbortButton = new QPushButton( tr( "Abort" ) );
|
||||
mRunButton->setEnabled( false );
|
||||
|
||||
QMap<QString, QString> filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
|
||||
for ( const QString &filter : filterFormatMap.keys() )
|
||||
const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
|
||||
for ( const auto &filter : filterFormatMap )
|
||||
{
|
||||
QString driverName = filterFormatMap.value( filter );
|
||||
QString driverName = filter.second;
|
||||
ui.comboBoxOutputFormat->addItem( driverName );
|
||||
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
|
||||
{
|
||||
@ -216,13 +216,13 @@ void QgsGeometryCheckerSetupTab::validateInput()
|
||||
void QgsGeometryCheckerSetupTab::selectOutputDirectory()
|
||||
{
|
||||
QString filterString = QgsVectorFileWriter::filterForDriver( QStringLiteral( "GPKG" ) );
|
||||
QMap<QString, QString> filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
|
||||
for ( const QString &filter : filterFormatMap.keys() )
|
||||
const auto filterFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats();
|
||||
for ( const auto &filter : filterFormatMap )
|
||||
{
|
||||
QString driverName = filterFormatMap.value( filter );
|
||||
QString driverName = filter.second;
|
||||
if ( driverName != QLatin1String( "ESRI Shapefile" ) ) // Default entry, first in list (see above)
|
||||
{
|
||||
filterString += ";;" + filter;
|
||||
filterString += ";;" + filter.first;
|
||||
}
|
||||
}
|
||||
QString initialdir = ui.lineEditOutputDirectory->text();
|
||||
|
@ -723,6 +723,28 @@ class TestQgsVectorFileWriter(unittest.TestCase):
|
||||
|
||||
gdal.Unlink(filename)
|
||||
|
||||
def testSupportedFiltersAndFormat(self):
|
||||
# test with formats in recommended order
|
||||
formats = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.SortRecommended)
|
||||
self.assertEqual(formats[0], ('GeoPackage [OGR] (*.gpkg *.GPKG)', 'GPKG'))
|
||||
self.assertEqual(formats[1], ('ESRI Shapefile [OGR] (*.shp *.SHP)', 'ESRI Shapefile'))
|
||||
# alphabetical sorting
|
||||
formats2 = QgsVectorFileWriter.supportedFiltersAndFormats(QgsVectorFileWriter.VectorFormatOptions())
|
||||
self.assertTrue(formats2[0][0] < formats2[1][0])
|
||||
self.assertCountEqual(formats, formats2)
|
||||
self.assertNotEqual(formats2[0], ('GeoPackage', 'GPKG'))
|
||||
|
||||
def testOgrDriverList(self):
|
||||
# test with drivers in recommended order
|
||||
drivers = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.SortRecommended)
|
||||
self.assertEqual(drivers[0], ('GeoPackage', 'GPKG'))
|
||||
self.assertEqual(drivers[1], ('ESRI Shapefile', 'ESRI Shapefile'))
|
||||
# alphabetical sorting
|
||||
drivers2 = QgsVectorFileWriter.ogrDriverList(QgsVectorFileWriter.VectorFormatOptions())
|
||||
self.assertTrue(drivers2[0][0] < drivers2[1][0])
|
||||
self.assertCountEqual(drivers, drivers2)
|
||||
self.assertNotEqual(drivers2[0], ('GeoPackage', 'GPKG'))
|
||||
|
||||
def testSupportedFormatExtensions(self):
|
||||
formats = QgsVectorFileWriter.supportedFormatExtensions()
|
||||
self.assertTrue('gpkg' in formats)
|
||||
@ -730,6 +752,22 @@ class TestQgsVectorFileWriter(unittest.TestCase):
|
||||
self.assertEqual(formats[0], 'gpkg')
|
||||
self.assertEqual(formats[1], 'shp')
|
||||
|
||||
# alphabetical sorting
|
||||
formats2 = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.VectorFormatOptions())
|
||||
self.assertTrue(formats2[0] < formats2[1])
|
||||
self.assertCountEqual(formats, formats2)
|
||||
self.assertNotEqual(formats2[0], 'gpkg')
|
||||
|
||||
def testFileFilterString(self):
|
||||
formats = QgsVectorFileWriter.fileFilterString()
|
||||
self.assertTrue('gpkg' in formats)
|
||||
self.assertTrue('shp' in formats)
|
||||
self.assertTrue(formats.index('gpkg') < formats.index('shp'))
|
||||
|
||||
# alphabetical sorting
|
||||
formats2 = QgsVectorFileWriter.fileFilterString(QgsVectorFileWriter.VectorFormatOptions())
|
||||
self.assertNotEqual(formats.index('gpkg'), formats2.index('gpkg'))
|
||||
|
||||
def testDriverForExtension(self):
|
||||
self.assertEqual(QgsVectorFileWriter.driverForExtension('shp'), 'ESRI Shapefile')
|
||||
self.assertEqual(QgsVectorFileWriter.driverForExtension('SHP'), 'ESRI Shapefile')
|
||||
|
Loading…
x
Reference in New Issue
Block a user