Skip to content

Commit

Permalink
[processing] restore gdaltindex algorithm and expose additional param…
Browse files Browse the repository at this point in the history
…eters
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent fce3e18 commit 232fd47
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -39,6 +39,7 @@
from .ColorRelief import ColorRelief
from .contour import contour
from .fillnodata import fillnodata
from .gdaltindex import gdaltindex
from .hillshade import hillshade
from .information import information
from .nearblack import nearblack
Expand All @@ -65,7 +66,6 @@
# from .GridAverage import GridAverage
# from .GridNearest import GridNearest
# from .GridDataMetrics import GridDataMetrics
# from .gdaltindex import gdaltindex
# from .gdalcalc import gdalcalc
# from .rasterize_over import rasterize_over
# from .retile import retile
Expand Down Expand Up @@ -140,6 +140,7 @@ def loadAlgorithms(self):
ColorRelief(),
contour(),
fillnodata(),
gdaltindex(),
hillshade(),
information(),
nearblack(),
Expand All @@ -165,7 +166,6 @@ def loadAlgorithms(self):
# GridAverage(),
# GridNearest(),
# GridDataMetrics(),
# gdaltindex(),
# gdalcalc(),
# rasterize_over(),
# retile(),
Expand Down
124 changes: 97 additions & 27 deletions python/plugins/processing/algs/gdal/gdaltindex.py
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from builtins import str

__author__ = 'Pedro Venancio'
__date__ = 'February 2015'
Expand All @@ -27,62 +26,133 @@
__revision__ = '$Format:%H$'

import os
from collections import OrderedDict

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsMapLayer,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterCrs,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterDefinition,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterVectorDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.outputs import OutputVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterString
from processing.tools import dataobjects
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class gdaltindex(GdalAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
FIELD_NAME = 'FIELD_NAME'
LAYERS = 'LAYERS'
PATH_FIELD_NAME = 'PATH_FIELD_NAME'
ABSOLUTE_PATH = 'ABSOLUTE_PATH'
PROJ_DIFFERENCE = 'PROJ_DIFFERENCE'
TARGET_CRS = 'TARGET_CRS'
CRS_FIELD_NAME = 'CRS_FIELD_NAME'
CRS_FORMAT = 'CRS_FORMAT'
OUTPUT = 'OUTPUT'

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterMultipleInput(self.INPUT,
self.tr('Input layers'), dataobjects.TYPE_RASTER))
self.addParameter(ParameterString(self.FIELD_NAME,
self.tr('Tile index field'),
'location', optional=True))
self.addParameter(ParameterBoolean(self.PROJ_DIFFERENCE,
self.tr('Skip files with different projection reference'), False))
self.addOutput(OutputVector(gdaltindex.OUTPUT, self.tr('Tile index')))
self.FORMATS = OrderedDict([(self.tr('Auto'), 'AUTO'),
(self.tr('Well-known text (WKT)'), 'WKT'),
(self.tr('EPSG'), 'EPSG'),
(self.tr('Proj.4'), 'PROJ')])

self.addParameter(QgsProcessingParameterMultipleLayers(self.LAYERS,
self.tr('Input files'),
QgsProcessing.TypeRaster))
self.addParameter(QgsProcessingParameterString(
self.PATH_FIELD_NAME, self.tr('Field name to hold the file path to the indexed rasters'),
defaultValue='location'))
self.addParameter(QgsProcessingParameterBoolean(
self.ABSOLUTE_PATH, self.tr('Store absolute path to the indexed rasters'),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(
self.PROJ_DIFFERENCE, self.tr('Skip files with different projection reference'),
defaultValue=False))

target_crs_param = QgsProcessingParameterCrs(
self.TARGET_CRS, self.tr('Transform geometries to the given CRS'), optional=True)
target_crs_param.setFlags(target_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(target_crs_param)

crs_field_param = QgsProcessingParameterString(
self.CRS_FIELD_NAME, self.tr('The name of the field to store the SRS of each tile'),
optional=True)
crs_field_param.setFlags(crs_field_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(crs_field_param)

keys = list(self.FORMATS.keys())
crs_format_param = QgsProcessingParameterEnum(self.CRS_FORMAT,
self.tr('The format in which the CRS of each tile must be written'),
keys,
allowMultiple=False,
defaultValue=0)
crs_format_param.setFlags(crs_format_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(crs_format_param)

self.addParameter(QgsProcessingParameterVectorDestination(
self.OUTPUT, self.tr('Tile index'), QgsProcessing.TypeVectorPolygon))

def name(self):
return 'tileindex'

def displayName(self):
return self.tr('Tile Index')

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'tiles.png'))

def group(self):
return self.tr('Raster miscellaneous')

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'tiles.png'))

def getConsoleCommands(self, parameters, context, feedback):
fieldName = str(self.getParameterValue(self.FIELD_NAME))
input_layers = self.parameterAsLayerList(parameters, self.LAYERS, context)
crs_field = self.parameterAsString(parameters, self.CRS_FIELD_NAME, context)
crs_format = self.parameterAsEnum(parameters, self.CRS_FORMAT, context)
target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
output, format = GdalUtils.ogrConnectionStringAndFormat(outFile, context)

layers = []
for layer in input_layers:
if layer.type() != QgsMapLayer.RasterLayer:
raise QgsProcessingException(
self.tr('All layers must be raster layers!'))
layers.append(layer.source())

arguments = []
if len(fieldName) > 0:
arguments.append('-tileindex')
arguments.append(fieldName)
if self.getParameterValue(gdaltindex.PROJ_DIFFERENCE):
arguments.append('-tileindex')
arguments.append(self.parameterAsString(parameters, self.PATH_FIELD_NAME, context))

if self.parameterAsBool(parameters, self.ABSOLUTE_PATH, context):
arguments.append('-write_absolute_path')

if self.parameterAsBool(parameters, self.PROJ_DIFFERENCE, context):
arguments.append('-skip_different_projection')
arguments.append(str(self.getOutputValue(gdaltindex.OUTPUT)))
arguments.extend(str(self.getParameterValue(gdaltindex.INPUT)).split(';'))

if crs_field:
arguments.append('-src_srs_name {}'.format(crs_field))

if crs_format:
arguments.append('-src_srs_format {}'.format(list(self.FORMATS.values())[crs_format]))

if target_crs:
arguments.append('-t_srs {}'.format(target_crs.authid()))

if format:
arguments.append('-f {}'.format(format))

arguments.append(output)
arguments.extend(' '.join(layers))

return ['gdaltindex', GdalUtils.escapeAndJoin(arguments)]

0 comments on commit 232fd47

Please sign in to comment.