Skip to content
Permalink
Browse files

Refactoring of rasterize algorithm

  • Loading branch information
marioba committed Aug 8, 2017
1 parent baede35 commit f792661c1c4020f2fe82e1fa696b864b31de37ee
@@ -36,7 +36,9 @@
QgsProcessingParameterNumber,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterDestination
QgsProcessingParameterRasterDestination,
QgsMessageLog,
QgsRasterFileWriter
)

import qgis
@@ -98,9 +100,8 @@ def initAlgorithm(self, config=None):
self.addParameter(map_theme_param)

self.addParameter(
# TODO Why is this restricted to raster layers only? I think
# QgsProcessingParameterMapLayer makes more sense here so that
# users can choose to render single vector layers if desired.
# TODO use QgsProcessingParameterMapLayer when
# the LayerWidgetWrapper class will be implemented
QgsProcessingParameterRasterLayer(
self.LAYER,
description=self.tr(
@@ -185,23 +186,13 @@ def processAlgorithm(self, parameters, context, feedback):
self.OUTPUT,
context)

print('map_theme {}'.format(map_theme))
print('layer {}'.format(layer))
print('extent {}'.format(extent))
print('tile_size {}'.format(tile_size))
print('mupp {}'.format(mupp))
print('output_layer {}'.format(output_layer))

# This probably affects the whole system but it's a lot nicer
osgeo.gdal.UseExceptions()

tile_set = TileSet(map_theme, layer, extent, tile_size, mupp,
output_layer,
qgis.utils.iface.mapCanvas().mapSettings())
# TODO Can you add feedback as a parameter to render and add
# appropriate hooks within render to check for feedback.isCanceled()
# and abort the render early?
tile_set.render()
tile_set.render(feedback)

return {self.OUTPUT: output_layer}

@@ -274,14 +265,18 @@ def __init__(self, map_theme, layer, extent, tile_size, mupp, output,
else:
self.settings.setLayers(map_settings.layers())

def render(self):
def render(self, feedback):
for x in range(self.x_tile_count):
for y in range(self.y_tile_count):
if feedback.isCanceled():
return
cur_tile = x * self.y_tile_count + y
num_tiles = self.x_tile_count * self.y_tile_count
self.renderTile(x, y)
self.renderTile(x, y, feedback)

feedback.setProgress(int((cur_tile / num_tiles) * 100))

def renderTile(self, x, y):
def renderTile(self, x, y, feedback):
"""
Render one tile
@@ -311,6 +306,8 @@ def renderTile(self, x, y):
self.tile_size, self.tile_size,
src_ds.ReadRaster(0, 0, self.tile_size,
self.tile_size))
except Exception as e:
feedback.reportError(str(e))
finally:
del src_ds
tmpfile.close()
@@ -327,9 +324,5 @@ def getDriverForFile(self, filename):
if extension == '':
extension = '.tif'

# TODO It should be removed and
# QgsRasterFileWriter::driverForExtension used instead.
for i in range(osgeo.gdal.GetDriverCount()):
driver = osgeo.gdal.GetDriver(i)
if driver.GetMetadataItem('DMD_EXTENSION') == extension[1:]:
return driver
driver_name = QgsRasterFileWriter.driverForExtension(extension[1:])
return osgeo.gdal.GetDriverByName(driver_name)
@@ -756,7 +756,7 @@ def value(self):

class LayerWidgetWrapper(WidgetWrapper):
def __init__(self):
raise NotImplementedError('Layer widget is not implemented yet')
raise NotImplementedError('Layer widget wrapper is not implemented yet')


class VectorWidgetWrapper(WidgetWrapper):
@@ -18,52 +18,32 @@
"""


from qgis.core import (QgsSettings,
QgsProcessingParameterNumber,
QgsProcessingParameterFile,
QgsProcessingParameterField,
QgsProcessingParameterExpression,
QgsProcessingOutputString,
QgsProcessingParameterString,
QgsMapThemeCollection,
QgsProject
)
from qgis.core import QgsProject

from qgis.PyQt.QtWidgets import QComboBox
from qgis.PyQt.QtCore import pyqtSignal

from processing.gui.wrappers import (
BasicWidgetWrapper
)
from processing.tools.postgis import GeoDB


class MapThemeWrapper(BasicWidgetWrapper):
"""
WidgetWrapper for ParameterString that create and manage a combobox widget
with existing postgis connections.
WidgetWrapper for ParameterString that createe a combobox widget
with the existing map themes.
"""

def createWidget(self):
self._combo = QComboBox()
self._combo.addItem('', '')
for item in self.items():
self._combo.addItem(item, item)
self._combo.currentIndexChanged.connect(lambda:
self.widgetValueHasChanged.emit(self))
return self._combo

def items(self):
items = QgsProject.instance().mapThemeCollection().mapThemes()
#if self.dialogType == DIALOG_MODELER:
# strings = self.dialog.getAvailableValuesOfType(
# [QgsProcessingParameterString, QgsProcessingParameterNumber,
# QgsProcessingParameterFile,
# QgsProcessingParameterField,
# QgsProcessingParameterExpression], QgsProcessingOutputString)
# items = items + [(self.dialog.resolveValueDescription(s),
# s) for s in strings]
#
return items
return QgsProject.instance().mapThemeCollection().mapThemes()

def setValue(self, value):
self.setComboValue(value, self._combo)

0 comments on commit f792661

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