2015-05-22 20:17:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2019-09-20 12:52:56 +03:00
|
|
|
Datasources2Vrt.py
|
2015-05-22 20:17:27 +02:00
|
|
|
---------------------
|
|
|
|
Date : May 2015
|
|
|
|
Copyright : (C) 2015 by Luigi Pirelli
|
|
|
|
Email : luipir 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__ = 'Luigi Pirelli'
|
|
|
|
__date__ = 'May 2015'
|
|
|
|
__copyright__ = '(C) 2015, Luigi Pirelli'
|
|
|
|
|
2019-09-24 09:27:29 +03:00
|
|
|
import html
|
|
|
|
|
2019-09-20 12:52:56 +03:00
|
|
|
from qgis.core import (QgsProcessing,
|
|
|
|
QgsProcessingException,
|
2017-08-19 01:26:55 +10:00
|
|
|
QgsProcessingParameterMultipleLayers,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterVectorDestination,
|
2019-09-23 11:52:04 +03:00
|
|
|
QgsProcessingOutputString
|
2019-09-20 12:52:56 +03:00
|
|
|
)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2019-09-20 12:52:56 +03:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2015-05-22 20:17:27 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class Datasources2Vrt(QgisAlgorithm):
|
2017-08-19 01:26:55 +10:00
|
|
|
INPUT = 'INPUT'
|
2016-02-19 12:48:12 +02:00
|
|
|
UNIONED = 'UNIONED'
|
2017-08-19 01:26:55 +10:00
|
|
|
OUTPUT = 'OUTPUT'
|
2016-02-19 12:48:12 +02:00
|
|
|
VRT_STRING = 'VRT_STRING'
|
2015-05-22 20:17:27 +02:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector general')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectorgeneral'
|
|
|
|
|
2019-09-20 12:52:56 +03:00
|
|
|
def name(self):
|
|
|
|
return 'buildvirtualvector'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Build virtual vector')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-08-19 01:26:55 +10:00
|
|
|
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
|
|
|
|
self.tr('Input datasources'),
|
2017-08-19 02:46:22 +10:00
|
|
|
QgsProcessing.TypeVector))
|
2017-08-19 01:26:55 +10:00
|
|
|
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')))
|
2015-05-22 20:17:27 +02:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-08-19 01:26:55 +10:00
|
|
|
input_layers = self.parameterAsLayerList(parameters, self.INPUT, context)
|
2019-04-16 08:30:00 +02:00
|
|
|
unioned = self.parameterAsBoolean(parameters, self.UNIONED, context)
|
2017-08-19 01:26:55 +10:00
|
|
|
vrtPath = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2016-02-19 12:48:12 +02:00
|
|
|
|
|
|
|
vrt = '<OGRVRTDataSource>'
|
2019-09-20 12:52:56 +03:00
|
|
|
if unioned:
|
2016-02-19 12:48:12 +02:00
|
|
|
vrt += '<OGRVRTUnionLayer name="UnionedLayer">'
|
2015-07-26 03:48:27 +02:00
|
|
|
|
2019-09-20 12:52:56 +03:00
|
|
|
total = 100.0 / len(input_layers) if input_layers else 0
|
|
|
|
for current, layer in enumerate(input_layers):
|
2017-08-19 01:26:55 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2019-09-23 11:52:04 +03:00
|
|
|
basePath = GdalUtils.ogrConnectionStringFromLayer(layer)
|
|
|
|
layerName = GdalUtils.ogrLayerName(layer.source())
|
2019-09-20 12:52:56 +03:00
|
|
|
|
2019-09-24 09:27:29 +03:00
|
|
|
vrt += '<OGRVRTLayer name="{}">'.format(html.escape(layerName, True))
|
|
|
|
vrt += '<SrcDataSource>{}</SrcDataSource>'.format(html.escape(basePath, True))
|
|
|
|
vrt += '<SrcLayer>{}</SrcLayer>'.format(html.escape(layerName, True))
|
2019-09-20 12:52:56 +03:00
|
|
|
vrt += '</OGRVRTLayer>'
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2015-07-26 03:48:27 +02:00
|
|
|
|
2019-09-20 12:52:56 +03:00
|
|
|
if unioned:
|
2016-02-19 12:48:12 +02:00
|
|
|
vrt += '</OGRVRTUnionLayer>'
|
|
|
|
vrt += '</OGRVRTDataSource>'
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2019-09-20 12:52:56 +03:00
|
|
|
with open(vrtPath, 'w', encoding='utf-8') as f:
|
|
|
|
f.write(vrt)
|
|
|
|
|
|
|
|
return {self.OUTPUT: vrtPath, self.VRT_STRING: vrt}
|