Skip to content

Commit

Permalink
Port build virtual vector alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 18, 2017
1 parent f2d2777 commit 4820216
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
75 changes: 45 additions & 30 deletions python/plugins/processing/algs/qgis/Datasources2Vrt.py
Expand Up @@ -31,21 +31,20 @@

from osgeo import ogr
from qgis.core import (QgsProcessingFeedback,
QgsApplication)
from processing.tools import dataobjects
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterBoolean,
QgsProcessing,
QgsProcessingParameterVectorDestination,
QgsProcessingOutputString,
QgsProcessingException)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterBoolean
from processing.core.outputs import OutputFile
from processing.core.outputs import OutputString


class Datasources2Vrt(QgisAlgorithm):
DATASOURCES = 'DATASOURCES'
INPUT = 'INPUT'
UNIONED = 'UNIONED'

VRT_FILE = 'VRT_FILE'
OUTPUT = 'OUTPUT'
VRT_STRING = 'VRT_STRING'

def group(self):
Expand All @@ -55,17 +54,32 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterMultipleInput(self.DATASOURCES,
self.tr('Input datasources'),
dataobjects.TYPE_TABLE))
self.addParameter(ParameterBoolean(self.UNIONED,
self.tr('Create "unioned" VRT'),
default=False))

self.addOutput(OutputFile(self.VRT_FILE,
self.tr('Virtual vector'), ext='vrt'))
self.addOutput(OutputString(self.VRT_STRING,
self.tr('Virtual string')))
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
self.tr('Input datasources'),
QgsProcessing.TypeTable))
self.addParameter(QgsProcessingParameterBoolean(self.UNIONED,
self.tr('Create "unioned" VRT'),
defaultValue=False))

class ParameterVectorVrtDestination(QgsProcessingParameterVectorDestination):

def __init__(self, name, description):
super().__init__(name, description)

def clone(self):
copy = ParameterVectorVrtDestination(self.name(), self.description())
return copy

def type(self):
return 'vrt_vector_destination'

def defaultFileExtension(self):
return 'vrt'

self.addParameter(ParameterVectorVrtDestination(self.OUTPUT,
self.tr('Virtual vector')))
self.addOutput(QgsProcessingOutputString(self.VRT_STRING,
self.tr('Virtual string')))

def name(self):
return 'buildvirtualvector'
Expand All @@ -74,20 +88,17 @@ def displayName(self):
return self.tr('Build virtual vector')

def processAlgorithm(self, parameters, context, feedback):
input_layers = self.getParameterValue(self.DATASOURCES)
unioned = self.getParameterValue(self.UNIONED)
vrtPath = self.getOutputValue(self.VRT_FILE)

layers = input_layers.split(';')
input_layers = self.parameterAsLayerList(parameters, self.INPUT, context)
unioned = self.parameterAsBool(parameters, self.UNIONED, context)
vrtPath = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

vrtString = self.mergeDataSources2Vrt(layers,
vrtString = self.mergeDataSources2Vrt(input_layers,
vrtPath,
union=unioned,
relative=False,
schema=False,
feedback=feedback)

self.setOutputValue(self.VRT_STRING, vrtString)
return {self.OUTPUT: vrtPath, self.VRT_STRING: vrtString}

def mergeDataSources2Vrt(self, dataSources, outFile, union=False, relative=False,
schema=False, feedback=None):
Expand All @@ -107,12 +118,16 @@ def mergeDataSources2Vrt(self, dataSources, outFile, union=False, relative=False
vrt += '<OGRVRTUnionLayer name="UnionedLayer">'

total = 100.0 / len(dataSources) if dataSources else 1
for current, inFile in enumerate(dataSources):
for current, layer in enumerate(dataSources):
if feedback.isCanceled():
break

feedback.setProgress(int(current * total))

inFile = layer.source()
srcDS = ogr.Open(inFile, 0)
if srcDS is None:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Invalid datasource: {}'.format(inFile)))

if schema:
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -52,6 +52,7 @@
from .ConvexHull import ConvexHull
from .CreateAttributeIndex import CreateAttributeIndex
from .CreateConstantRaster import CreateConstantRaster
from .Datasources2Vrt import Datasources2Vrt
from .Delaunay import Delaunay
from .DeleteColumn import DeleteColumn
from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
Expand Down Expand Up @@ -164,7 +165,6 @@
# from .FieldsCalculator import FieldsCalculator
# from .FieldPyculator import FieldsPyculator
# from .SelectByAttributeSum import SelectByAttributeSum
# from .Datasources2Vrt import Datasources2Vrt
# from .DefineProjection import DefineProjection
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
Expand Down Expand Up @@ -192,7 +192,7 @@ def getAlgs(self):
# SpatialJoin(),
# GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# FieldsMapper(), SelectByAttributeSum()
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(),
Expand All @@ -212,6 +212,7 @@ def getAlgs(self):
ConvexHull(),
CreateAttributeIndex(),
CreateConstantRaster(),
Datasources2Vrt(),
Delaunay(),
DeleteColumn(),
DeleteDuplicateGeometries(),
Expand Down

0 comments on commit 4820216

Please sign in to comment.