2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
polygonize.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-08-31 13:15:29 +03:00
|
|
|
from qgis.core import (QgsProcessing,
|
2018-05-06 10:29:01 +10:00
|
|
|
QgsProcessingException,
|
2019-06-24 19:30:00 +03:00
|
|
|
QgsProcessingParameterDefinition,
|
2017-08-31 13:15:29 +03:00
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterBand,
|
|
|
|
QgsProcessingParameterString,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterVectorDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import isWindows
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class polygonize(GdalAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
2017-08-31 13:15:29 +03:00
|
|
|
BAND = 'BAND'
|
2013-10-01 20:52:22 +03:00
|
|
|
FIELD = 'FIELD'
|
2017-08-31 13:15:29 +03:00
|
|
|
EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
|
2019-06-24 19:30:00 +03:00
|
|
|
EXTRA = 'EXTRA'
|
2017-08-31 13:15:29 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-27 17:20:57 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-08-31 13:15:29 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
|
2017-09-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterBand(self.BAND,
|
|
|
|
self.tr('Band number'),
|
2018-12-30 09:18:23 +02:00
|
|
|
1,
|
2017-09-06 16:59:48 +03:00
|
|
|
parentLayerParameterName=self.INPUT))
|
|
|
|
self.addParameter(QgsProcessingParameterString(self.FIELD,
|
|
|
|
self.tr('Name of the field to create'),
|
|
|
|
defaultValue='DN'))
|
|
|
|
self.addParameter(QgsProcessingParameterBoolean(self.EIGHT_CONNECTEDNESS,
|
|
|
|
self.tr('Use 8-connectedness'),
|
|
|
|
defaultValue=False))
|
|
|
|
|
2019-06-24 19:30:00 +03:00
|
|
|
extra_param = QgsProcessingParameterString(self.EXTRA,
|
|
|
|
self.tr('Additional command-line parameters'),
|
|
|
|
defaultValue=None,
|
|
|
|
optional=True)
|
|
|
|
extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(extra_param)
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
|
|
|
|
self.tr('Vectorized'),
|
|
|
|
QgsProcessing.TypeVectorPolygon))
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'polygonize'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Polygonize (raster to vector)')
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster conversion')
|
|
|
|
|
2017-12-20 12:07:32 +01:00
|
|
|
def groupId(self):
|
2017-12-14 12:05:40 +02:00
|
|
|
return 'rasterconversion'
|
|
|
|
|
2017-08-31 13:15:29 +03:00
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'polygonize.png'))
|
2017-01-13 09:16:19 +02:00
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
def commandName(self):
|
|
|
|
return 'gdal_polygonize'
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments = []
|
2017-08-31 13:15:29 +03:00
|
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
2018-05-06 10:29:01 +10:00
|
|
|
if inLayer is None:
|
|
|
|
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
|
|
|
|
2017-08-31 13:15:29 +03:00
|
|
|
arguments.append(inLayer.source())
|
|
|
|
|
|
|
|
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2019-02-05 12:48:43 +10:00
|
|
|
self.setOutputValue(self.OUTPUT, outFile)
|
2017-09-06 16:59:48 +03:00
|
|
|
output, outFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
|
2013-04-10 12:15:55 +04:00
|
|
|
arguments.append(output)
|
2017-08-31 13:15:29 +03:00
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.EIGHT_CONNECTEDNESS, context):
|
2017-08-31 13:15:29 +03:00
|
|
|
arguments.append('-8')
|
|
|
|
|
|
|
|
arguments.append('-b')
|
|
|
|
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
if outFormat:
|
|
|
|
arguments.append('-f {}'.format(outFormat))
|
2017-08-31 13:15:29 +03:00
|
|
|
|
2019-06-24 19:30:00 +03:00
|
|
|
if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
|
|
|
|
extra = self.parameterAsString(parameters, self.EXTRA, context)
|
|
|
|
arguments.append(extra)
|
|
|
|
|
2019-02-05 20:13:41 +10:00
|
|
|
layerName = GdalUtils.ogrOutputLayerName(output)
|
2019-01-01 19:26:14 +02:00
|
|
|
if layerName:
|
|
|
|
arguments.append(layerName)
|
2017-08-31 13:15:29 +03:00
|
|
|
arguments.append(self.parameterAsString(parameters, self.FIELD, context))
|
2013-04-10 12:15:55 +04:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2019-02-10 21:06:56 +01:00
|
|
|
commands = ["python3", "-m", self.commandName()]
|
|
|
|
else:
|
|
|
|
commands = [self.commandName() + '.py']
|
|
|
|
|
|
|
|
commands.append(GdalUtils.escapeAndJoin(arguments))
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|