Skip to content
Permalink
Browse files

Port build virtual vector alg to new API

  • Loading branch information
nyalldawson committed Aug 18, 2017
1 parent f2d2777 commit 48202165fc5581b6616a246d696c91f0306a2a3a
@@ -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):
@@ -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'
@@ -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):
@@ -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:
@@ -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
@@ -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
@@ -192,7 +192,7 @@ def getAlgs(self):
# SpatialJoin(),
# GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# FieldsMapper(), SelectByAttributeSum()
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(),
@@ -212,6 +212,7 @@ def getAlgs(self):
ConvexHull(),
CreateAttributeIndex(),
CreateConstantRaster(),
Datasources2Vrt(),
Delaunay(),
DeleteColumn(),
DeleteDuplicateGeometries(),

0 comments on commit 4820216

Please sign in to comment.
You can’t perform that action at this time.