Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Move dataobjects.getSupportedOutputVectorLayerExtensions
to QgsVectorFileWriter
  • Loading branch information
nyalldawson committed Apr 3, 2017
1 parent 3e9c527 commit 26495db
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 27 deletions.
1 change: 1 addition & 0 deletions python/core/qgsvectorfilewriter.sip
Expand Up @@ -357,6 +357,7 @@ class QgsVectorFileWriter

/** Returns map with format filter string as key and OGR format key as value*/
static QMap< QString, QString> supportedFiltersAndFormats();
static QStringList supportedFormatExtensions();

/** Returns driver list that can be used for dialogs. It contains all OGR drivers
* + some additional internal QGIS driver names to distinguish between more
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -30,7 +30,8 @@

from qgis.PyQt.QtCore import QUrl

from qgis.core import QgsApplication
from qgis.core import (QgsApplication,
QgsVectorFileWriter)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.algs.gdal.GdalAlgorithmDialog import GdalAlgorithmDialog
Expand Down Expand Up @@ -58,7 +59,7 @@ def getCustomParametersDialog(self):
def processAlgorithm(self, feedback):
commands = self.getConsoleCommands()
layers = dataobjects.getVectorLayers()
supported = dataobjects.getSupportedOutputVectorLayerExtensions()
supported = QgsVectorFileWriter.supportedFormatExtensions()
for i, c in enumerate(commands):
for layer in layers:
if layer.source() in c:
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/core/AlgorithmProvider.py
Expand Up @@ -26,7 +26,8 @@
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProcessingProvider
from qgis.core import (QgsProcessingProvider,
QgsVectorFileWriter)
from processing.core.ProcessingConfig import Setting, ProcessingConfig
from processing.tools import dataobjects

Expand Down Expand Up @@ -91,7 +92,7 @@ def getSupportedOutputRasterLayerExtensions(self):
return ['tif']

def getSupportedOutputVectorLayerExtensions(self):
return dataobjects.getSupportedOutputVectorLayerExtensions()
return QgsVectorFileWriter.supportedFormatExtensions()

def getSupportedOutputTableExtensions(self):
return ['csv']
Expand Down
7 changes: 5 additions & 2 deletions python/plugins/processing/core/ProcessingConfig.py
Expand Up @@ -30,7 +30,10 @@
import os

from qgis.PyQt.QtCore import QCoreApplication, QObject, pyqtSignal
from qgis.core import NULL, QgsApplication, QgsSettings
from qgis.core import (NULL,
QgsApplication,
QgsSettings,
QgsVectorFileWriter)
from processing.tools.system import defaultOutputFolder
import processing.tools.dataobjects

Expand Down Expand Up @@ -165,7 +168,7 @@ def initialize():
valuetype=Setting.SELECTION,
options=invalidFeaturesOptions))

extensions = processing.tools.dataobjects.getSupportedOutputVectorLayerExtensions()
extensions = QgsVectorFileWriter.supportedFormatExtensions()
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
Expand Down
10 changes: 8 additions & 2 deletions python/plugins/processing/core/outputs.py
Expand Up @@ -38,7 +38,13 @@
from processing.tools.vector import VectorWriter, TableWriter
from processing.tools import dataobjects

from qgis.core import QgsExpressionContext, QgsExpressionContextUtils, QgsExpression, QgsExpressionContextScope, QgsProject, QgsSettings
from qgis.core import (QgsExpressionContext,
QgsExpressionContextUtils,
QgsExpression,
QgsExpressionContextScope,
QgsProject,
QgsSettings,
QgsVectorFileWriter)


def _expressionContext(alg):
Expand Down Expand Up @@ -316,7 +322,7 @@ def hasGeometry(self):
return dataobjects.canUseVectorLayer(self.base_layer, [-1])

def getSupportedOutputVectorLayerExtensions(self):
exts = dataobjects.getSupportedOutputVectorLayerExtensions()
exts = QgsVectorFileWriter.supportedFormatExtensions()
if not self.hasGeometry():
exts = ['dbf'] + [ext for ext in exts if ext in VectorWriter.nogeometry_extensions]
return exts
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -40,7 +40,8 @@
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsRasterLayer, QgsVectorLayer, QgsMapLayer, QgsCoordinateReferenceSystem,
QgsExpressionContext, QgsExpressionContextUtils, QgsExpression, QgsExpressionContextScope,
QgsProject)
QgsProject,
QgsVectorFileWriter)

from processing.tools.vector import resolveFieldIndex, features
from processing.tools import dataobjects
Expand Down Expand Up @@ -751,7 +752,7 @@ def getFileFilter(self):
elif self.datatype == dataobjects.TYPE_FILE:
return self.tr('All files (*.*)', 'ParameterMultipleInput')
else:
exts = dataobjects.getSupportedOutputVectorLayerExtensions()
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterMultipleInput').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
Expand Down Expand Up @@ -1523,7 +1524,7 @@ def getSafeExportedLayer(self):
return self.exported

def getFileFilter(self):
exts = dataobjects.getSupportedOutputVectorLayerExtensions()
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = self.tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts)
Expand Down
14 changes: 0 additions & 14 deletions python/plugins/processing/tools/dataobjects.py
Expand Up @@ -65,20 +65,6 @@ def resetLoadedLayers():
_loadedLayers = {}


def getSupportedOutputVectorLayerExtensions():
formats = QgsVectorFileWriter.supportedFiltersAndFormats()
exts = []
for extension in list(formats.keys()):
extension = str(extension)
extension = extension[extension.find('*.') + 2:]
extension = extension[:extension.find(' ')]
if extension.lower() != 'shp':
exts.append(extension)
exts.sort()
exts.insert(0, 'shp') # shp is the default, should be the first
return exts


def getSupportedOutputRasterLayerExtensions():
allexts = []
for exts in list(GdalUtils.getSupportedRasters().values()):
Expand Down
29 changes: 29 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2604,6 +2604,35 @@ QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats()
return resultMap;
}

QStringList QgsVectorFileWriter::supportedFormatExtensions()
{
QgsStringMap formats = supportedFiltersAndFormats();
QStringList extensions;

QRegularExpression rx( "\\*\\.([a-zA-Z0-9]*)" );

QgsStringMap::const_iterator formatIt = formats.constBegin();
for ( ; formatIt != formats.constEnd(); ++formatIt )
{
QString ext = formatIt.key();
QRegularExpressionMatch match = rx.match( ext );
if ( !match.hasMatch() )
continue;

QString matched = match.captured( 1 );
if ( matched.compare( QStringLiteral( "shp" ), Qt::CaseInsensitive ) == 0 )
continue;

extensions << matched;
}

std::sort( extensions.begin(), extensions.end() );

// Make https://twitter.com/shapefiIe a happy little fellow
extensions.insert( 0, QStringLiteral( "shp" ) );
return extensions;
}

QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
{
QMap<QString, QString> resultMap;
Expand Down
12 changes: 11 additions & 1 deletion src/core/qgsvectorfilewriter.h
Expand Up @@ -432,9 +432,19 @@ class CORE_EXPORT QgsVectorFileWriter
//! QgsVectorFileWriter cannot be copied.
QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete;

//! Returns map with format filter string as key and OGR format key as value
/**
* Returns a map with format filter string as key and OGR format key as value.
* \see supportedOutputVectorLayerExtensions()
*/
static QMap< QString, QString> supportedFiltersAndFormats();

/**
* Returns a list of file extensions for supported formats.
* \since QGIS 3.0
* \see supportedFiltersAndFormats()
*/
static QStringList supportedFormatExtensions();

/** 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
Expand Down
8 changes: 7 additions & 1 deletion tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -65,7 +65,7 @@ def convert(self, idx, value):
return 'unexpected_idx'


class TestQgsVectorLayer(unittest.TestCase):
class TestQgsVectorFileWriter(unittest.TestCase):

mMemoryLayer = None

Expand Down Expand Up @@ -656,6 +656,12 @@ def testOverwriteLayer(self):

gdal.Unlink(filename)

def testSupportedFormatExtensions(self):
formats = QgsVectorFileWriter.supportedFormatExtensions()
self.assertTrue('gpkg' in formats)
self.assertFalse('exe' in formats)
self.assertEqual(formats[0], 'shp')


if __name__ == '__main__':
unittest.main()

0 comments on commit 26495db

Please sign in to comment.