Skip to content
Permalink
Browse files
[processing] Move dataobjects.getSupportedOutputVectorLayerExtensions
to QgsVectorFileWriter
  • Loading branch information
nyalldawson committed Apr 3, 2017
1 parent 3e9c527 commit 26495dbd819c44e77381c0c185f73ba0bfcc0ec5
@@ -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
@@ -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
@@ -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:
@@ -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

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

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

def getSupportedOutputTableExtensions(self):
return ['csv']
@@ -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

@@ -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,
@@ -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):
@@ -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
@@ -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
@@ -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)
@@ -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)
@@ -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()):
@@ -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;
@@ -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
@@ -65,7 +65,7 @@ def convert(self, idx, value):
return 'unexpected_idx'


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

mMemoryLayer = None

@@ -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.