Beispiel #1
0
 def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback):
     """
     Interprets a parameter as an OGR compatible source and layer name
     """
     input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
     ogr_data_path = None
     ogr_layer_name = None
     if input_layer is None:
         # parameter is not a vector layer - try to convert to a source compatible with OGR
         # and extract selection if required
         ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                   QgsVectorFileWriter.supportedFormatExtensions(),
                                                                   feedback=feedback)
         ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
     elif input_layer.dataProvider().name() == 'ogr':
         # parameter is a vector layer, with OGR data provider
         # so extract selection if required
         ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                   QgsVectorFileWriter.supportedFormatExtensions(),
                                                                   feedback=feedback)
         ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
     else:
         # vector layer, but not OGR - get OGR compatible path
         # TODO - handle "selected features only" mode!!
         ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
         ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
     return ogr_data_path, ogr_layer_name
Beispiel #2
0
def getFileFilter(param):
    """
    Returns a suitable file filter pattern for the specified parameter definition
    :param param:
    :return:
    """
    if param.type() == 'multilayer':
        if param.layerType() == QgsProcessingParameterDefinition.TypeRaster:
            exts = dataobjects.getSupportedOutputRasterLayerExtensions()
        elif param.layerType() == QgsProcessingParameterDefinition.TypeFile:
            return tr('All files (*.*)', 'QgsProcessingParameterMultipleLayers')
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterMultipleLayers').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts)
    elif param.type() in ('raster', 'rasterOut'):
        exts = dataobjects.getSupportedOutputRasterLayerExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterRasterOutput').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts)
    elif param.type() == 'table':
        exts = ['csv', 'dbf']
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts)
    elif param.type() == 'sink':
        exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts)
    return ''
Beispiel #3
0
    def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback, executing):
        """
        Interprets a parameter as an OGR compatible source and layer name
        :param executing:
        """
        if not executing and parameter_name in parameters and isinstance(parameters[parameter_name], QgsProcessingFeatureSourceDefinition):
            # if not executing, then we throw away all 'selected features only' settings
            # since these have no meaning for command line gdal use, and we don't want to force
            # an export of selected features only to a temporary file just to show the command!
            parameters = {parameter_name: parameters[parameter_name].source}

        input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
        ogr_data_path = None
        ogr_layer_name = None
        if input_layer is None or input_layer.dataProvider().name() == 'memory':
            if executing:
                # parameter is not a vector layer - try to convert to a source compatible with OGR
                # and extract selection if required
                ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                          QgsVectorFileWriter.supportedFormatExtensions(),
                                                                          feedback=feedback)
                ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
            else:
                #not executing - don't waste time converting incompatible sources, just return dummy strings
                #for the command preview (since the source isn't compatible with OGR, it has no meaning anyway and can't
                #be run directly in the command line)
                ogr_data_path = 'path_to_data_file'
                ogr_layer_name = 'layer_name'
        elif input_layer.dataProvider().name() == 'ogr':
            if executing and isinstance(parameters[parameter_name], QgsProcessingFeatureSourceDefinition) and parameters[parameter_name].selectedFeaturesOnly:
                # parameter is a vector layer, with OGR data provider
                # so extract selection if required
                ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                          QgsVectorFileWriter.supportedFormatExtensions(),
                                                                          feedback=feedback)
                parts = QgsProviderRegistry.instance().decodeUri('ogr', ogr_data_path)
                ogr_data_path = parts['path']
                if 'layerName' in parts and parts['layerName']:
                    ogr_layer_name = parts['layerName']
                else:
                    ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
            else:
                #either not using the selection, or
                #not executing - don't worry about 'selected features only' handling. It has no meaning
                #for the command line preview since it has no meaning outside of a QGIS session!
                ogr_data_path = GdalUtils.ogrConnectionStringAndFormatFromLayer(input_layer)[0]
                ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
        else:
            # vector layer, but not OGR - get OGR compatible path
            # TODO - handle "selected features only" mode!!
            ogr_data_path = GdalUtils.ogrConnectionStringFromLayer(input_layer)
            ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
        return ogr_data_path, ogr_layer_name
Beispiel #4
0
def getFileFilter(param):
    """
    Returns a suitable file filter pattern for the specified parameter definition
    :param param:
    :return:
    """
    if param.type() == 'layer':
        vectors = QgsProviderRegistry.instance().fileVectorFilters().split(';;')
        vectors.pop(0)
        rasters = QgsProviderRegistry.instance().fileRasterFilters().split(';;')
        rasters.pop(0)
        filters = set(vectors + rasters)
        filters = sorted(filters)
        return tr('All files (*.*)') + ';;' + ";;".join(filters)
    elif param.type() == 'multilayer':
        if param.layerType() == QgsProcessing.TypeRaster:
            exts = QgsRasterFileWriter.supportedFormatExtensions()
        elif param.layerType() == QgsProcessing.TypeFile:
            return tr('All files (*.*)', 'QgsProcessingParameterMultipleLayers')
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterMultipleLayers').format(exts[i].upper(), exts[i].lower())
        return tr('All files (*.*)') + ';;' + ';;'.join(exts)
    elif param.type() == 'raster':
        return QgsProviderRegistry.instance().fileRasterFilters()
    elif param.type() == 'rasterDestination':
        if param.provider() is not None:
            exts = param.provider().supportedOutputRasterLayerExtensions()
        else:
            exts = QgsRasterFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'ParameterRaster').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts) + ';;' + tr('All files (*.*)')
    elif param.type() in ('sink', 'vectorDestination'):
        if param.provider() is not None:
            exts = param.provider().supportedOutputVectorLayerExtensions()
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
        return ';;'.join(exts) + ';;' + tr('All files (*.*)')
    elif param.type() == 'source':
        return QgsProviderRegistry.instance().fileVectorFilters()
    elif param.type() == 'vector':
        return QgsProviderRegistry.instance().fileVectorFilters()
    elif param.type() == 'fileDestination':
        return param.fileFilter() + ';;' + tr('All files (*.*)')

    if param.defaultFileExtension():
        return tr('Default extension') + ' (*.' + param.defaultFileExtension() + ')'
    else:
        return ''
Beispiel #5
0
    def testSupportedFormatExtensions(self):
        formats = QgsVectorFileWriter.supportedFormatExtensions()
        self.assertTrue('gpkg' in formats)
        self.assertFalse('exe' in formats)
        self.assertEqual(formats[0], 'gpkg')
        self.assertEqual(formats[1], 'shp')
        self.assertTrue('ods' in formats)

        # alphabetical sorting
        formats2 = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.VectorFormatOptions())
        self.assertTrue(formats2[0] < formats2[1])
        self.assertCountEqual(formats, formats2)
        self.assertNotEqual(formats2[0], 'gpkg')

        formats = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.SkipNonSpatialFormats)
        self.assertFalse('ods' in formats)
Beispiel #6
0
    def loadVectorLayerFromParameter(self, name, parameters, context, feedback, external=False):
        """
        Creates a dedicated command to load a vector into
        the temporary GRASS DB.
        :param name: name of the parameter
        :param parameters: Parameters of the algorithm.
        :param context: Processing context
        :param external: use v.external (v.in.ogr if False).
        """
        layer = self.parameterAsVectorLayer(parameters, name, context)

        is_ogr_disk_based_layer = layer is not None and layer.dataProvider().name() == 'ogr'
        if is_ogr_disk_based_layer:
            # we only support direct reading of disk based ogr layers -- not ogr postgres layers, etc
            source_parts = QgsProviderRegistry.instance().decodeUri('ogr', layer.source())
            if not source_parts.get('path'):
                is_ogr_disk_based_layer = False
            elif source_parts.get('layerId'):
                # no support for directly reading layers by id in grass
                is_ogr_disk_based_layer = False

        if not is_ogr_disk_based_layer:
            # parameter is not a vector layer or not an OGR layer - try to convert to a source compatible with
            # grass OGR inputs and extract selection if required
            path = self.parameterAsCompatibleSourceLayerPath(parameters, name, context,
                                                             QgsVectorFileWriter.supportedFormatExtensions(),
                                                             feedback=feedback)
            ogr_layer = QgsVectorLayer(path, '', 'ogr')
            self.loadVectorLayer(name, ogr_layer, external=external, feedback=feedback)
        else:
            # already an ogr disk based layer source
            self.loadVectorLayer(name, layer, external=external, feedback=feedback)
Beispiel #7
0
 def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback, executing):
     """
     Interprets a parameter as an OGR compatible source and layer name
     :param executing:
     """
     input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context)
     ogr_data_path = None
     ogr_layer_name = None
     if input_layer is None:
         if executing:
             # parameter is not a vector layer - try to convert to a source compatible with OGR
             # and extract selection if required
             ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                       QgsVectorFileWriter.supportedFormatExtensions(),
                                                                       feedback=feedback)
             ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
         else:
             #not executing - don't waste time converting incompatible sources, just return dummy strings
             #for the command preview (since the source isn't compatible with OGR, it has no meaning anyway and can't
             #be run directly in the command line)
             ogr_data_path = 'path_to_data_file'
             ogr_layer_name = 'layer_name'
     elif input_layer.dataProvider().name() == 'ogr':
         if executing:
             # parameter is a vector layer, with OGR data provider
             # so extract selection if required
             ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
                                                                       QgsVectorFileWriter.supportedFormatExtensions(),
                                                                       feedback=feedback)
             ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
         else:
             #not executing - don't worry about 'selected features only' handling. It has no meaning
             #for the command line preview since it has no meaning outside of a QGIS session!
             ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
             ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
     else:
         # vector layer, but not OGR - get OGR compatible path
         # TODO - handle "selected features only" mode!!
         ogr_data_path = GdalUtils.ogrConnectionString(input_layer.dataProvider().dataSourceUri(), context)[1:-1]
         ogr_layer_name = GdalUtils.ogrLayerName(input_layer.dataProvider().dataSourceUri())
     return ogr_data_path, ogr_layer_name
    def testSupportedFormatExtensions(self):
        formats = QgsVectorFileWriter.supportedFormatExtensions()
        self.assertTrue('gpkg' in formats)
        self.assertFalse('exe' in formats)
        self.assertEqual(formats[0], 'gpkg')
        self.assertEqual(formats[1], 'shp')
        self.assertTrue('ods' in formats)
        self.assertTrue('xtf' in formats)
        self.assertTrue('ili' in formats)

        for i in range(2, len(formats) - 1):
            self.assertLess(formats[i].lower(), formats[i + 1].lower())

        # alphabetical sorting
        formats2 = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.VectorFormatOptions())
        self.assertTrue(formats2[0] < formats2[1])
        self.assertCountEqual(formats, formats2)
        self.assertNotEqual(formats2[0], 'gpkg')
        for i in range(0, len(formats2) - 1):
            self.assertLess(formats2[i].lower(), formats2[i + 1].lower())

        formats = QgsVectorFileWriter.supportedFormatExtensions(QgsVectorFileWriter.SkipNonSpatialFormats)
        self.assertFalse('ods' in formats)
Beispiel #9
0
 def getFileFilter(self, datatype):
     """
     Returns a suitable file filter pattern for the specified parameter definition
     :param param:
     :return:
     """
     if datatype == QgsProcessing.TypeRaster:
         return QgsProviderRegistry.instance().fileRasterFilters()
     elif datatype == QgsProcessing.TypeFile:
         return self.tr('All files (*.*)')
     else:
         exts = QgsVectorFileWriter.supportedFormatExtensions()
         for i in range(len(exts)):
             exts[i] = self.tr('{0} files (*.{1})').format(exts[i].upper(), exts[i].lower())
         return self.tr('All files (*.*)') + ';;' + ';;'.join(exts)
Beispiel #10
0
    def processAlgorithm(self, parameters, context, feedback):
        commands = self.getConsoleCommands(parameters)
        layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
        supported = QgsVectorFileWriter.supportedFormatExtensions()
        for i, c in enumerate(commands):
            for layer in layers:
                if layer.source() in c:
                    exported = dataobjects.exportVectorLayer(layer, supported)
                    exportedFileName = os.path.splitext(os.path.split(exported)[1])[0]
                    c = c.replace(layer.source(), exported)
                    if os.path.isfile(layer.source()):
                        fileName = os.path.splitext(os.path.split(layer.source())[1])[0]
                        c = re.sub('[\s]{}[\s]'.format(fileName), ' ' + exportedFileName + ' ', c)
                        c = re.sub('[\s]{}'.format(fileName), ' ' + exportedFileName, c)
                        c = re.sub('["\']{}["\']'.format(fileName), "'" + exportedFileName + "'", c)

            commands[i] = c
        GdalUtils.runGdal(commands, feedback)
Beispiel #11
0
 def loadVectorLayerFromParameter(self, name, parameters, context, feedback, external=False):
     """
     Creates a dedicated command to load a vector into
     the temporary GRASS DB.
     :param name: name of the parameter
     :param parameters: Parameters of the algorithm.
     :param context: Processing context
     :param external: use v.external (v.in.ogr if False).
     """
     layer = self.parameterAsVectorLayer(parameters, name, context)
     if layer is None or layer.dataProvider().name() != 'ogr':
         # parameter is not a vector layer or not an OGR layer - try to convert to a source compatible with
         # grass OGR inputs and extract selection if required
         path = self.parameterAsCompatibleSourceLayerPath(parameters, name, context,
                                                          QgsVectorFileWriter.supportedFormatExtensions(),
                                                          feedback=feedback)
         ogr_layer = QgsVectorLayer(path, '', 'ogr')
         self.loadVectorLayer(name, ogr_layer, external)
     else:
         # already an ogr layer source
         self.loadVectorLayer(name, layer, external)
    def addDirectory(self):
        settings = QgsSettings()
        path = str(settings.value('/Processing/LastInputPath'))

        ret = QFileDialog.getExistingDirectory(self, self.tr('Select File(s)'), path)
        if ret:
            exts = []

            if self.datatype == QgsProcessing.TypeVector:
                exts = QgsVectorFileWriter.supportedFormatExtensions()
            elif self.datatype == QgsProcessing.TypeRaster:
                for t in QgsProviderRegistry.instance().fileRasterFilters().split(';;')[1:]:
                    for e in t[t.index('(') + 1:-1].split(' '):
                        if e != "*.*" and e.startswith("*."):
                            exts.append(e[2:])

            files = []
            for pp in Path(ret).rglob("*"):
                if not pp.is_file():
                    continue

                if exts and pp.suffix[1:] not in exts:
                    continue

                p = pp.as_posix()

                files.append(p)

            settings.setValue('/Processing/LastInputPath', ret)

            for filename in files:
                item = QStandardItem(filename)
                item.setData(filename, Qt.UserRole)
                item.setCheckState(Qt.Checked)
                item.setCheckable(True)
                item.setDropEnabled(False)
                self.model.appendRow(item)
Beispiel #13
0
    def initialize():
        icon = QgsApplication.getThemeIcon("/processingAlgorithm.svg")
        ProcessingConfig.settingIcons['General'] = icon
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.KEEP_DIALOG_OPEN,
                ProcessingConfig.tr(
                    'Keep dialog open after running an algorithm'), True))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.PREFER_FILENAME_AS_LAYER_NAME,
                ProcessingConfig.tr('Prefer output filename for layer names'),
                True))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,
                ProcessingConfig.tr(
                    'Show tooltip when there are disabled providers'), True))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.OUTPUT_FOLDER,
                    ProcessingConfig.tr('Output folder'),
                    defaultOutputFolder(),
                    valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'), ProcessingConfig.SHOW_CRS_DEF,
                ProcessingConfig.tr(
                    'Show layer CRS definition in selection boxes'), True))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.WARN_UNMATCHING_CRS,
                ProcessingConfig.tr(
                    "Warn before executing if parameter CRS's do not match"),
                True))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.SHOW_ALGORITHMS_KNOWN_ISSUES,
                    ProcessingConfig.tr("Show algorithms with known issues"),
                    False))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.RASTER_STYLE,
                    ProcessingConfig.tr('Style for raster layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_POINT_STYLE,
                    ProcessingConfig.tr('Style for point layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_LINE_STYLE,
                    ProcessingConfig.tr('Style for line layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_POLYGON_STYLE,
                    ProcessingConfig.tr('Style for polygon layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.PRE_EXECUTION_SCRIPT,
                    ProcessingConfig.tr('Pre-execution script'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.POST_EXECUTION_SCRIPT,
                    ProcessingConfig.tr('Post-execution script'),
                    '',
                    valuetype=Setting.FILE))

        invalidFeaturesOptions = [
            ProcessingConfig.tr('Do not filter (better performance)'),
            ProcessingConfig.tr(
                'Skip (ignore) features with invalid geometries'),
            ProcessingConfig.tr(
                'Stop algorithm execution when a geometry is invalid')
        ]
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.FILTER_INVALID_GEOMETRIES,
                    ProcessingConfig.tr('Invalid features filtering'),
                    invalidFeaturesOptions[2],
                    valuetype=Setting.SELECTION,
                    options=invalidFeaturesOptions))

        threads = QgsApplication.maxThreads(
        )  # if user specified limit for rendering, lets keep that as default here, otherwise max
        threads = cpu_count(
        ) if threads == -1 else threads  # if unset, maxThreads() returns -1
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.MAX_THREADS,
                    ProcessingConfig.tr('Max Threads'),
                    threads,
                    valuetype=Setting.INT))

        extensions = QgsVectorFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
                ProcessingConfig.tr('Default output vector layer extension'),
                QgsVectorFileWriter.supportedFormatExtensions()[0],
                valuetype=Setting.SELECTION,
                options=extensions))

        extensions = QgsRasterFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
                ProcessingConfig.tr('Default output raster layer extension'),
                'tif',
                valuetype=Setting.SELECTION,
                options=extensions))

        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.TEMP_PATH,
                    ProcessingConfig.tr('Temporary output folder path'),
                    QgsProcessingUtils.tempFolder(),
                    valuetype=Setting.FOLDER))
Beispiel #14
0
    def processCommand(self, parameters, context, feedback, delOutputs=False):
        """
        Prepare the GRASS algorithm command
        :param parameters:
        :param context:
        :param delOutputs: do not add outputs to commands.
        """
        noOutputs = [o for o in self.parameterDefinitions() if o not in self.destinationParameterDefinitions()]
        command = '{} '.format(self.grass7Name)
        command += '{}'.join(self.hardcodedStrings)

        # Add algorithm command
        for param in noOutputs:
            paramName = param.name()
            value = None

            # Exclude default GRASS parameters
            if paramName in [self.GRASS_REGION_CELLSIZE_PARAMETER,
                             self.GRASS_REGION_EXTENT_PARAMETER,
                             self.GRASS_MIN_AREA_PARAMETER,
                             self.GRASS_SNAP_TOLERANCE_PARAMETER,
                             self.GRASS_OUTPUT_TYPE_PARAMETER,
                             self.GRASS_REGION_ALIGN_TO_RESOLUTION,
                             self.GRASS_RASTER_FORMAT_OPT,
                             self.GRASS_RASTER_FORMAT_META,
                             self.GRASS_VECTOR_DSCO,
                             self.GRASS_VECTOR_LCO,
                             self.GRASS_VECTOR_EXPORT_NOCAT]:
                continue

            # Raster and vector layers
            if isinstance(param, (QgsProcessingParameterRasterLayer,
                                  QgsProcessingParameterVectorLayer,
                                  QgsProcessingParameterFeatureSource)):
                if paramName in self.exportedLayers:
                    value = self.exportedLayers[paramName]
                else:
                    value = self.parameterAsCompatibleSourceLayerPath(
                        parameters, paramName, context,
                        QgsVectorFileWriter.supportedFormatExtensions()
                    )
            # MultipleLayers
            elif isinstance(param, QgsProcessingParameterMultipleLayers):
                layers = self.parameterAsLayerList(parameters, paramName, context)
                values = []
                for idx in range(len(layers)):
                    layerName = '{}_{}'.format(paramName, idx)
                    values.append(self.exportedLayers[layerName])
                value = ','.join(values)
            # For booleans, we just add the parameter name
            elif isinstance(param, QgsProcessingParameterBoolean):
                if self.parameterAsBool(parameters, paramName, context):
                    command += ' {}'.format(paramName)
            # For Extents, remove if the value is null
            elif isinstance(param, QgsProcessingParameterExtent):
                if self.parameterAsExtent(parameters, paramName, context):
                    value = self.parameterAsString(parameters, paramName, context)
            # For enumeration, we need to grab the string value
            elif isinstance(param, QgsProcessingParameterEnum):
                # Handle multiple values
                if param.allowMultiple():
                    indexes = self.parameterAsEnums(parameters, paramName, context)
                else:
                    indexes = [self.parameterAsEnum(parameters, paramName, context)]
                if indexes:
                    value = '"{}"'.format(','.join([param.options()[i] for i in indexes]))
            # For strings, we just translate as string
            elif isinstance(param, QgsProcessingParameterString):
                data = self.parameterAsString(parameters, paramName, context)
                # if string is empty, we don't add it
                if len(data) > 0:
                    value = '"{}"'.format(
                        self.parameterAsString(parameters, paramName, context)
                    )
            # For fields, we just translate as string
            elif isinstance(param, QgsProcessingParameterField):
                value = ','.join(
                    self.parameterAsFields(parameters, paramName, context)
                )
            elif isinstance(param, QgsProcessingParameterFile):
                if self.parameterAsString(parameters, paramName, context):
                    value = '"{}"'.format(
                        self.parameterAsString(parameters, paramName, context)
                    )
            elif isinstance(param, QgsProcessingParameterPoint):
                if self.parameterAsString(parameters, paramName, context):
                    # parameter specified, evaluate as point
                    # TODO - handle CRS transform
                    point = self.parameterAsPoint(parameters, paramName, context)
                    value = '{},{}'.format(point.x(), point.y())
            # For numbers, we translate as a string
            elif isinstance(param, (QgsProcessingParameterNumber,
                                    QgsProcessingParameterPoint)):
                value = self.parameterAsString(parameters, paramName, context)
            # For everything else, we assume that it is a string
            else:
                value = '"{}"'.format(
                    self.parameterAsString(parameters, paramName, context)
                )
            if value:
                command += ' {}={}'.format(paramName.replace('~', ''), value)

        # Handle outputs
        if not delOutputs:
            for out in self.destinationParameterDefinitions():
                # We exclude hidden parameters
                if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
                    continue
                outName = out.name()
                # For File destination
                if isinstance(out, QgsProcessingParameterFileDestination):
                    if outName in parameters and parameters[outName] is not None:
                        # for HTML reports, we need to redirect stdout
                        if out.defaultFileExtension().lower() == 'html':
                            command += ' {}=- > "{}"'.format(
                                outName,
                                self.parameterAsFileOutput(parameters, outName, context))
                        else:
                            command += ' {}="{}"'.format(
                                outName,
                                self.parameterAsFileOutput(parameters, outName, context))
                # For folders destination
                elif isinstance(out, QgsProcessingParameterFolderDestination):
                    # We need to add a unique temporary basename
                    uniqueBasename = outName + self.uniqueSuffix
                    command += ' {}={}'.format(outName, uniqueBasename)
                else:
                    if outName in parameters and parameters[outName] is not None:
                        # We add an output name to make sure it is unique if the session
                        # uses this algorithm several times.
                        uniqueOutputName = outName + self.uniqueSuffix
                        command += ' {}={}'.format(outName, uniqueOutputName)

                        # Add output file to exported layers, to indicate that
                        # they are present in GRASS
                        self.exportedLayers[outName] = uniqueOutputName

        command += ' --overwrite'
        self.commands.append(command)
        QgsMessageLog.logMessage(self.tr('processCommands end. Commands: {}').format(self.commands), 'Grass7', Qgis.Info)
Beispiel #15
0
    def initialize():
        icon = QgsApplication.getThemeIcon("/processingAlgorithm.svg")
        ProcessingConfig.settingIcons['General'] = icon
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.KEEP_DIALOG_OPEN,
                ProcessingConfig.tr(
                    'Keep dialog open after running an algorithm'), True))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.USE_FILENAME_AS_LAYER_NAME,
                    ProcessingConfig.tr('Use filename as layer name'), False))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,
                ProcessingConfig.tr(
                    'Show tooltip when there are disabled providers'), True))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.OUTPUT_FOLDER,
                    ProcessingConfig.tr('Output folder'),
                    defaultOutputFolder(),
                    valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'), ProcessingConfig.SHOW_CRS_DEF,
                ProcessingConfig.tr(
                    'Show layer CRS definition in selection boxes'), True))
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.WARN_UNMATCHING_CRS,
                ProcessingConfig.tr(
                    "Warn before executing if parameter CRS's do not match"),
                True))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.RASTER_STYLE,
                    ProcessingConfig.tr('Style for raster layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_POINT_STYLE,
                    ProcessingConfig.tr('Style for point layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_LINE_STYLE,
                    ProcessingConfig.tr('Style for line layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.VECTOR_POLYGON_STYLE,
                    ProcessingConfig.tr('Style for polygon layers'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.PRE_EXECUTION_SCRIPT,
                    ProcessingConfig.tr('Pre-execution script'),
                    '',
                    valuetype=Setting.FILE))
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.POST_EXECUTION_SCRIPT,
                    ProcessingConfig.tr('Post-execution script'),
                    '',
                    valuetype=Setting.FILE))

        invalidFeaturesOptions = [
            ProcessingConfig.tr('Do not filter (better performance)'),
            ProcessingConfig.tr('Ignore features with invalid geometries'),
            ProcessingConfig.tr(
                'Stop algorithm execution when a geometry is invalid')
        ]
        ProcessingConfig.addSetting(
            Setting(ProcessingConfig.tr('General'),
                    ProcessingConfig.FILTER_INVALID_GEOMETRIES,
                    ProcessingConfig.tr('Invalid features filtering'),
                    invalidFeaturesOptions[2],
                    valuetype=Setting.SELECTION,
                    options=invalidFeaturesOptions))

        extensions = QgsVectorFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
                ProcessingConfig.tr('Default output vector layer extension'),
                extensions[0],
                valuetype=Setting.SELECTION,
                options=extensions))

        extensions = QgsRasterFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(
            Setting(
                ProcessingConfig.tr('General'),
                ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
                ProcessingConfig.tr('Default output raster layer extension'),
                extensions[0],
                valuetype=Setting.SELECTION,
                options=extensions))
Beispiel #16
0
def getFileFilter(param):
    """
    Returns a suitable file filter pattern for the specified parameter definition
    :param param:
    :return:
    """
    if param.type() == 'layer':
        vectors = QgsProviderRegistry.instance().fileVectorFilters().split(
            ';;')
        vectors.pop(0)
        rasters = QgsProviderRegistry.instance().fileRasterFilters().split(
            ';;')
        rasters.pop(0)
        filters = set(vectors + rasters)
        filters = sorted(filters)
        return tr('All files (*.*)') + ';;' + ";;".join(filters)
    elif param.type() == 'multilayer':
        if param.layerType() == QgsProcessing.TypeRaster:
            exts = QgsRasterFileWriter.supportedFormatExtensions()
        elif param.layerType() == QgsProcessing.TypeFile:
            return tr('All files (*.*)',
                      'QgsProcessingParameterMultipleLayers')
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})',
                         'QgsProcessingParameterMultipleLayers').format(
                             exts[i].upper(), exts[i].lower())
        return tr('All files (*.*)') + ';;' + ';;'.join(exts)
    elif param.type() == 'raster':
        return QgsProviderRegistry.instance().fileRasterFilters()
    elif param.type() == 'rasterDestination':
        if param.provider() is not None:
            exts = param.provider().supportedOutputRasterLayerExtensions()
        else:
            exts = QgsRasterFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})',
                         'ParameterRaster').format(exts[i].upper(),
                                                   exts[i].lower())
        return ';;'.join(exts) + ';;' + tr('All files (*.*)')
    elif param.type() in ('sink', 'vectorDestination'):
        if param.provider() is not None:
            exts = param.provider().supportedOutputVectorLayerExtensions()
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
        for i in range(len(exts)):
            exts[i] = tr('{0} files (*.{1})',
                         'ParameterVector').format(exts[i].upper(),
                                                   exts[i].lower())
        return ';;'.join(exts) + ';;' + tr('All files (*.*)')
    elif param.type() == 'source':
        return QgsProviderRegistry.instance().fileVectorFilters()
    elif param.type() == 'vector':
        return QgsProviderRegistry.instance().fileVectorFilters()
    elif param.type() == 'fileDestination':
        return param.fileFilter() + ';;' + tr('All files (*.*)')

    if param.defaultFileExtension():
        return tr(
            'Default extension') + ' (*.' + param.defaultFileExtension() + ')'
    else:
        return ''
Beispiel #17
0
 def supportedOutputVectorLayerExtensions(self):
     # We use the same extensions than QGIS because:
     # - QGIS is using OGR like GRASS
     # - There are very chances than OGR version used in GRASS is
     # different from QGIS OGR version.
     return QgsVectorFileWriter.supportedFormatExtensions()
Beispiel #18
0
 def getSupportedOutputVectorLayerExtensions(self):
     exts = QgsVectorFileWriter.supportedFormatExtensions()
     if not self.hasGeometry():
         exts = ['dbf'] + [ext for ext in exts if ext in NOGEOMETRY_EXTENSIONS]
     return exts
 def testSupportedFormatExtensions(self):
     formats = QgsVectorFileWriter.supportedFormatExtensions()
     self.assertTrue('gpkg' in formats)
     self.assertFalse('exe' in formats)
     self.assertEqual(formats[0], 'shp')
Beispiel #20
0
 def getSupportedOutputVectorLayerExtensions(self):
     exts = QgsVectorFileWriter.supportedFormatExtensions()
     if not self.hasGeometry():
         exts = ['dbf'] + [ext for ext in exts if ext in NOGEOMETRY_EXTENSIONS]
     return exts
Beispiel #21
0
 def testSupportedFormatExtensions(self):
     formats = QgsVectorFileWriter.supportedFormatExtensions()
     self.assertTrue('gpkg' in formats)
     self.assertFalse('exe' in formats)
     self.assertEqual(formats[0], 'shp')
Beispiel #22
0
    def processCommand(self, parameters, context, feedback, delOutputs=False):
        """
        Prepare the GRASS algorithm command
        :param parameters:
        :param context:
        :param delOutputs: do not add outputs to commands.
        """
        noOutputs = [
            o for o in self.parameterDefinitions()
            if o not in self.destinationParameterDefinitions()
        ]
        command = '{} '.format(self.grass7Name)
        command += '{}'.join(self.hardcodedStrings)

        # Add algorithm command
        for param in noOutputs:
            paramName = param.name()
            value = None

            # Exclude default GRASS parameters
            if paramName in [
                    self.GRASS_REGION_CELLSIZE_PARAMETER,
                    self.GRASS_REGION_EXTENT_PARAMETER,
                    self.GRASS_MIN_AREA_PARAMETER,
                    self.GRASS_SNAP_TOLERANCE_PARAMETER,
                    self.GRASS_OUTPUT_TYPE_PARAMETER,
                    self.GRASS_REGION_ALIGN_TO_RESOLUTION,
                    self.GRASS_RASTER_FORMAT_OPT,
                    self.GRASS_RASTER_FORMAT_META, self.GRASS_VECTOR_DSCO,
                    self.GRASS_VECTOR_LCO
            ]:
                continue

            # Raster and vector layers
            if isinstance(param, (QgsProcessingParameterRasterLayer,
                                  QgsProcessingParameterVectorLayer,
                                  QgsProcessingParameterFeatureSource)):
                if paramName in self.exportedLayers:
                    value = self.exportedLayers[paramName]
                else:
                    value = self.parameterAsCompatibleSourceLayerPath(
                        parameters, paramName, context,
                        QgsVectorFileWriter.supportedFormatExtensions())
            # MultipleLayers
            elif isinstance(param, QgsProcessingParameterMultipleLayers):
                layers = self.parameterAsLayerList(parameters, paramName,
                                                   context)
                values = []
                for idx in range(len(layers)):
                    layerName = '{}_{}'.format(paramName, idx)
                    values.append(self.exportedLayers[layerName])
                value = ','.join(values)
            # For booleans, we just add the parameter name
            elif isinstance(param, QgsProcessingParameterBoolean):
                if self.parameterAsBool(parameters, paramName, context):
                    command += ' {}'.format(paramName)
            # For Extents, remove if the value is null
            elif isinstance(param, QgsProcessingParameterExtent):
                if self.parameterAsExtent(parameters, paramName, context):
                    value = self.parameterAsString(parameters, paramName,
                                                   context)
            # For enumeration, we need to grab the string value
            elif isinstance(param, QgsProcessingParameterEnum):
                # Handle multiple values
                if param.allowMultiple():
                    indexes = self.parameterAsEnums(parameters, paramName,
                                                    context)
                else:
                    indexes = [
                        self.parameterAsEnum(parameters, paramName, context)
                    ]
                if indexes:
                    value = '"{}"'.format(','.join(
                        [param.options()[i] for i in indexes]))
            # For strings, we just translate as string
            elif isinstance(param, QgsProcessingParameterString):
                data = self.parameterAsString(parameters, paramName, context)
                # if string is empty, we don't add it
                if len(data) > 0:
                    value = '"{}"'.format(
                        self.parameterAsString(parameters, paramName, context))
            # For fields, we just translate as string
            elif isinstance(param, QgsProcessingParameterField):
                value = ','.join(
                    self.parameterAsFields(parameters, paramName, context))
            elif isinstance(param, QgsProcessingParameterFile):
                if self.parameterAsString(parameters, paramName, context):
                    value = '"{}"'.format(
                        self.parameterAsString(parameters, paramName, context))
            elif isinstance(param, QgsProcessingParameterPoint):
                if self.parameterAsString(parameters, paramName, context):
                    # parameter specified, evaluate as point
                    # TODO - handle CRS transform
                    point = self.parameterAsPoint(parameters, paramName,
                                                  context)
                    value = '{},{}'.format(point.x(), point.y())
            # For numbers, we translate as a string
            elif isinstance(
                    param,
                (QgsProcessingParameterNumber, QgsProcessingParameterPoint)):
                value = self.parameterAsString(parameters, paramName, context)
            # For everything else, we assume that it is a string
            else:
                value = '"{}"'.format(
                    self.parameterAsString(parameters, paramName, context))
            if value:
                command += ' {}={}'.format(paramName.replace('~', ''), value)

        # Handle outputs
        if not delOutputs:
            for out in self.destinationParameterDefinitions():
                # We exclude hidden parameters
                if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
                    continue
                outName = out.name()
                # For File destination
                if isinstance(out, QgsProcessingParameterFileDestination):
                    if outName in parameters and parameters[
                            outName] is not None:
                        # for HTML reports, we need to redirect stdout
                        if out.defaultFileExtension().lower() == 'html':
                            command += ' > "{}"'.format(
                                self.parameterAsFileOutput(
                                    parameters, outName, context))
                        else:
                            command += ' {}="{}"'.format(
                                outName,
                                self.parameterAsFileOutput(
                                    parameters, outName, context))
                # For folders destination
                elif isinstance(out, QgsProcessingParameterFolderDestination):
                    # We need to add a unique temporary basename
                    uniqueBasename = outName + self.uniqueSuffix
                    command += ' {}={}'.format(outName, uniqueBasename)
                else:
                    if outName in parameters and parameters[
                            outName] is not None:
                        # We add an output name to make sure it is unique if the session
                        # uses this algorithm several times.
                        uniqueOutputName = outName + self.uniqueSuffix
                        command += ' {}={}'.format(outName, uniqueOutputName)

                        # Add output file to exported layers, to indicate that
                        # they are present in GRASS
                        self.exportedLayers[outName] = uniqueOutputName

        command += ' --overwrite'
        self.commands.append(command)
        QgsMessageLog.logMessage(
            self.tr('processCommands end. Commands: {}').format(self.commands),
            'Grass7', Qgis.Info)
Beispiel #23
0
    def processAlgorithm(self, parameters, context, feedback):
        layers = self.parameterAsLayerList(parameters, self.INPUT_DATASOURCES,
                                           context)
        query = self.parameterAsString(parameters, self.INPUT_QUERY, context)
        uid_field = self.parameterAsString(parameters, self.INPUT_UID_FIELD,
                                           context)
        geometry_field = self.parameterAsString(parameters,
                                                self.INPUT_GEOMETRY_FIELD,
                                                context)
        geometry_type = self.parameterAsEnum(parameters,
                                             self.INPUT_GEOMETRY_TYPE, context)
        geometry_crs = self.parameterAsCrs(parameters, self.INPUT_GEOMETRY_CRS,
                                           context)

        df = QgsVirtualLayerDefinition()
        for layerIdx, layer in enumerate(layers):

            # Issue https://github.com/qgis/QGIS/issues/24041
            # When using this algorithm from the graphic modeler, it may try to
            # access (thanks the QgsVirtualLayerProvider) to memory layer that
            # belongs to temporary QgsMapLayerStore, not project.
            # So, we write them to disk is this is the case.
            if context.project() and not context.project().mapLayer(
                    layer.id()):
                basename = "memorylayer." + QgsVectorFileWriter.supportedFormatExtensions(
                )[0]
                tmp_path = QgsProcessingUtils.generateTempFilename(basename)
                QgsVectorFileWriter.writeAsVectorFormat(
                    layer, tmp_path,
                    layer.dataProvider().encoding())
                df.addSource('input{}'.format(layerIdx + 1), tmp_path, "ogr")
            else:
                df.addSource('input{}'.format(layerIdx + 1), layer.id())

        if query == '':
            raise QgsProcessingException(
                self.
                tr('Empty SQL. Please enter valid SQL expression and try again.'
                   ))
        else:
            localContext = self.createExpressionContext(parameters, context)
            expandedQuery = QgsExpression.replaceExpressionText(
                query, localContext)
            df.setQuery(expandedQuery)

        if uid_field:
            df.setUid(uid_field)

        if geometry_type == 1:  # no geometry
            df.setGeometryWkbType(QgsWkbTypes.NoGeometry)
        else:
            if geometry_field:
                df.setGeometryField(geometry_field)
            if geometry_type > 1:
                df.setGeometryWkbType(geometry_type - 1)
            if geometry_crs.isValid():
                df.setGeometrySrid(geometry_crs.postgisSrid())

        vLayer = QgsVectorLayer(df.toString(), "temp_vlayer", "virtual")
        if not vLayer.isValid():
            raise QgsProcessingException(
                vLayer.dataProvider().error().message())

        if vLayer.wkbType() == QgsWkbTypes.Unknown:
            raise QgsProcessingException(self.tr("Cannot find geometry field"))

        (sink, dest_id) = self.parameterAsSink(
            parameters, self.OUTPUT, context, vLayer.fields(),
            vLayer.wkbType() if geometry_type != 1 else 1, vLayer.crs())
        if sink is None:
            raise QgsProcessingException(
                self.invalidSinkError(parameters, self.OUTPUT))

        features = vLayer.getFeatures()
        total = 100.0 / vLayer.featureCount() if vLayer.featureCount() else 0
        for current, inFeat in enumerate(features):
            if feedback.isCanceled():
                break

            sink.addFeature(inFeat, QgsFeatureSink.FastInsert)
            feedback.setProgress(int(current * total))
        return {self.OUTPUT: dest_id}
Beispiel #24
0
    def getOgrCompatibleSource(self, parameter_name, parameters, context,
                               feedback, executing):
        """
        Interprets a parameter as an OGR compatible source and layer name
        :param executing:
        """
        if not executing and parameter_name in parameters and isinstance(
                parameters[parameter_name],
                QgsProcessingFeatureSourceDefinition):
            # if not executing, then we throw away all 'selected features only' settings
            # since these have no meaning for command line gdal use, and we don't want to force
            # an export of selected features only to a temporary file just to show the command!
            parameters = {parameter_name: parameters[parameter_name].source}

        input_layer = self.parameterAsVectorLayer(parameters, parameter_name,
                                                  context)
        ogr_data_path = None
        ogr_layer_name = None
        if input_layer is None or input_layer.dataProvider().name(
        ) == 'memory':
            if executing:
                # parameter is not a vector layer - try to convert to a source compatible with OGR
                # and extract selection if required
                ogr_data_path = self.parameterAsCompatibleSourceLayerPath(
                    parameters,
                    parameter_name,
                    context,
                    QgsVectorFileWriter.supportedFormatExtensions(),
                    feedback=feedback)
                ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
            else:
                #not executing - don't waste time converting incompatible sources, just return dummy strings
                #for the command preview (since the source isn't compatible with OGR, it has no meaning anyway and can't
                #be run directly in the command line)
                ogr_data_path = 'path_to_data_file'
                ogr_layer_name = 'layer_name'
        elif input_layer.dataProvider().name() == 'ogr':
            if executing and isinstance(
                    parameters[parameter_name],
                    QgsProcessingFeatureSourceDefinition
            ) and parameters[parameter_name].selectedFeaturesOnly:
                # parameter is a vector layer, with OGR data provider
                # so extract selection if required
                ogr_data_path = self.parameterAsCompatibleSourceLayerPath(
                    parameters,
                    parameter_name,
                    context,
                    QgsVectorFileWriter.supportedFormatExtensions(),
                    feedback=feedback)
                parts = QgsProviderRegistry.instance().decodeUri(
                    'ogr', ogr_data_path)
                ogr_data_path = parts['path']
                if 'layerName' in parts and parts['layerName']:
                    ogr_layer_name = parts['layerName']
                else:
                    ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
            else:
                #either not using the selection, or
                #not executing - don't worry about 'selected features only' handling. It has no meaning
                #for the command line preview since it has no meaning outside of a QGIS session!
                ogr_data_path = GdalUtils.ogrConnectionStringAndFormatFromLayer(
                    input_layer)[0]
                ogr_layer_name = GdalUtils.ogrLayerName(
                    input_layer.dataProvider().dataSourceUri())
        else:
            # vector layer, but not OGR - get OGR compatible path
            # TODO - handle "selected features only" mode!!
            ogr_data_path = GdalUtils.ogrConnectionStringFromLayer(input_layer)
            ogr_layer_name = GdalUtils.ogrLayerName(
                input_layer.dataProvider().dataSourceUri())
        return ogr_data_path, ogr_layer_name
    def generateCommand(self, parameters, context, feedback):
        layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
        if layer is None:
            raise QgsProcessingException(
                self.invalidRasterError(parameters, self.INPUT))

        samples = self.parameterAsSource(parameters, self.SAMPLES, context)
        if samples is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.SAMPLES))

        rule = self.rules[self.parameterAsEnum(parameters, self.RULE,
                                               context)][1]
        if rule in ('mode', 'proportion',
                    'count') and (self.CLASSES not in parameters
                                  or parameters[self.CLASSES] is None):
            raise QgsProcessingException(
                self.
                tr('Please specify classes to extract or choose another extraction rule.'
                   ))

        if rule == 'percentile' and (self.PERCENTILE not in parameters
                                     or parameters[self.PERCENTILE] is None):
            raise QgsProcessingException(
                self.
                tr('Please specify percentile or choose another extraction rule.'
                   ))

        sampleLayer, sampleLayerName = self.parameterAsCompatibleSourceLayerPathAndLayerName(
            parameters, self.SAMPLES, context,
            QgsVectorFileWriter.supportedFormatExtensions(), 'gpkg', feedback)

        output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

        arguments = []
        arguments.append(self.commandName())
        arguments.append('-i')
        arguments.append(layer.source())
        arguments.append('-s')
        arguments.append(sampleLayer)
        if sampleLayerName:
            arguments.append('-ln')
            arguments.append(sampleLayerName)
        arguments.append('-buf')
        arguments.append('{}'.format(
            self.parameterAsInt(parameters, self.BUFFER, context)))
        arguments.append('-r')
        arguments.append(rule)

        if rule in ('mode', 'proportion', 'count'):
            classes = self.parameterAsString(parameters, self.CLASSES, context)
            arguments.extend(pktoolsUtils.parseCompositeOption('-c', classes))

        if rule == 'percentile':
            arguments.append('-perc')
            arguments.append('{}'.format(
                self.parameterAsDouble(parameters, self.PERCENTILE, context)))

        if self.THRESHOLD in parameters and parameters[
                self.THRESHOLD] is not None:
            arguments.append('-t')
            arguments.append('{}'.format(
                self.parameterAsDouble(parameters, self.THRESHOLD, context)))

        if self.ARGUMENTS in parameters and parameters[
                self.ARGUMENTS] is not None:
            args = self.parameterAsString(parameters, self.ARGUMENTS,
                                          context).split(' ')
            if args:
                arguments.extend(args)

        arguments.append('-f')
        arguments.append(
            QgsVectorFileWriter.driverForExtension(
                os.path.splitext(output)[1]))
        arguments.append('-o')
        arguments.append(output)

        return arguments
Beispiel #26
0
    def initialize():
        icon = QgsApplication.getThemeIcon("/processingAlgorithm.svg")
        ProcessingConfig.settingIcons['General'] = icon
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_DEBUG_IN_DIALOG,
            ProcessingConfig.tr('Show extra info in Log panel'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.KEEP_DIALOG_OPEN,
            ProcessingConfig.tr('Keep dialog open after running an algorithm'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.USE_FILENAME_AS_LAYER_NAME,
            ProcessingConfig.tr('Use filename as layer name'), False))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_RECENT_ALGORITHMS,
            ProcessingConfig.tr('Show recently executed algorithms'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,
            ProcessingConfig.tr('Show tooltip when there are disabled providers'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.OUTPUT_FOLDER,
            ProcessingConfig.tr('Output folder'), defaultOutputFolder(),
            valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_CRS_DEF,
            ProcessingConfig.tr('Show layer CRS definition in selection boxes'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.WARN_UNMATCHING_CRS,
            ProcessingConfig.tr("Warn before executing if layer CRS's do not match"), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.WARN_UNMATCHING_EXTENT_CRS,
            ProcessingConfig.tr("Warn before executing if extent CRS might not match layers CRS"), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.RASTER_STYLE,
            ProcessingConfig.tr('Style for raster layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_POINT_STYLE,
            ProcessingConfig.tr('Style for point layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_LINE_STYLE,
            ProcessingConfig.tr('Style for line layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_POLYGON_STYLE,
            ProcessingConfig.tr('Style for polygon layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.PRE_EXECUTION_SCRIPT,
            ProcessingConfig.tr('Pre-execution script'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.POST_EXECUTION_SCRIPT,
            ProcessingConfig.tr('Post-execution script'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.RECENT_ALGORITHMS,
            ProcessingConfig.tr('Recent algorithms'), '', hidden=True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.MODELS_SCRIPTS_REPO,
            ProcessingConfig.tr('Scripts and models repository'),
            'https://raw.githubusercontent.com/qgis/QGIS-Processing/master'))

        invalidFeaturesOptions = [ProcessingConfig.tr('Do not filter (better performance)'),
                                  ProcessingConfig.tr('Ignore features with invalid geometries'),
                                  ProcessingConfig.tr('Stop algorithm execution when a geometry is invalid')]
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.FILTER_INVALID_GEOMETRIES,
            ProcessingConfig.tr('Invalid features filtering'),
            invalidFeaturesOptions[2],
            valuetype=Setting.SELECTION,
            options=invalidFeaturesOptions))

        extensions = QgsVectorFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
            ProcessingConfig.tr('Default output vector layer extension'),
            extensions[0],
            valuetype=Setting.SELECTION,
            options=extensions))

        extensions = QgsRasterFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
            ProcessingConfig.tr('Default output raster layer extension'),
            extensions[0],
            valuetype=Setting.SELECTION,
            options=extensions))
 def supportedOutputVectorLayerExtensions(self):
     # We use the same extensions than QGIS because:
     # - QGIS is using OGR like GRASS
     # - There are very chances than OGR version used in GRASS is
     # different from QGIS OGR version.
     return QgsVectorFileWriter.supportedFormatExtensions()
Beispiel #28
0
    def initialize():
        icon = QgsApplication.getThemeIcon("/processingAlgorithm.svg")
        ProcessingConfig.settingIcons['General'] = icon
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_DEBUG_IN_DIALOG,
            ProcessingConfig.tr('Show extra info in Log panel'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.KEEP_DIALOG_OPEN,
            ProcessingConfig.tr('Keep dialog open after running an algorithm'), False))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.USE_FILENAME_AS_LAYER_NAME,
            ProcessingConfig.tr('Use filename as layer name'), False))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_RECENT_ALGORITHMS,
            ProcessingConfig.tr('Show recently executed algorithms'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,
            ProcessingConfig.tr('Show tooltip when there are disabled providers'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.OUTPUT_FOLDER,
            ProcessingConfig.tr('Output folder'), defaultOutputFolder(),
            valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.SHOW_CRS_DEF,
            ProcessingConfig.tr('Show layer CRS definition in selection boxes'), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.WARN_UNMATCHING_CRS,
            ProcessingConfig.tr("Warn before executing if layer CRS's do not match"), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.WARN_UNMATCHING_EXTENT_CRS,
            ProcessingConfig.tr("Warn before executing if extent CRS might not match layers CRS"), True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.RASTER_STYLE,
            ProcessingConfig.tr('Style for raster layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_POINT_STYLE,
            ProcessingConfig.tr('Style for point layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_LINE_STYLE,
            ProcessingConfig.tr('Style for line layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.VECTOR_POLYGON_STYLE,
            ProcessingConfig.tr('Style for polygon layers'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.PRE_EXECUTION_SCRIPT,
            ProcessingConfig.tr('Pre-execution script'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.POST_EXECUTION_SCRIPT,
            ProcessingConfig.tr('Post-execution script'), '',
            valuetype=Setting.FILE))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.RECENT_ALGORITHMS,
            ProcessingConfig.tr('Recent algorithms'), '', hidden=True))
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.MODELS_SCRIPTS_REPO,
            ProcessingConfig.tr('Scripts and models repository'),
            'https://raw.githubusercontent.com/qgis/QGIS-Processing/master'))

        invalidFeaturesOptions = [ProcessingConfig.tr('Do not filter (better performance)'),
                                  ProcessingConfig.tr('Ignore features with invalid geometries'),
                                  ProcessingConfig.tr('Stop algorithm execution when a geometry is invalid')]
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.FILTER_INVALID_GEOMETRIES,
            ProcessingConfig.tr('Invalid features filtering'),
            invalidFeaturesOptions[2],
            valuetype=Setting.SELECTION,
            options=invalidFeaturesOptions))

        extensions = QgsVectorFileWriter.supportedFormatExtensions()
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.DEFAULT_OUTPUT_VECTOR_LAYER_EXT,
            ProcessingConfig.tr('Default output vector layer extension'),
            extensions[0],
            valuetype=Setting.SELECTION,
            options=extensions))

        extensions = processing.tools.dataobjects.getSupportedOutputRasterLayerExtensions()
        ProcessingConfig.addSetting(Setting(
            ProcessingConfig.tr('General'),
            ProcessingConfig.DEFAULT_OUTPUT_RASTER_LAYER_EXT,
            ProcessingConfig.tr('Default output raster layer extension'),
            extensions[0],
            valuetype=Setting.SELECTION,
            options=extensions))
Beispiel #29
0
 def getSupportedOutputVectorLayerExtensions(self):
     exts = QgsVectorFileWriter.supportedFormatExtensions()
     if not self.hasGeometry():
         exts = ['dbf'] + [ext for ext in exts if ext in VectorWriter.nogeometry_extensions]
     return exts
Beispiel #30
0
 def getSupportedOutputVectorLayerExtensions(self):
     return QgsVectorFileWriter.supportedFormatExtensions()