Skip to content

Commit

Permalink
[processing] added new options (alpha, quality, tile_size) to xyz alg
Browse files Browse the repository at this point in the history
  • Loading branch information
varmar05 authored and wonder-sk committed Jun 7, 2019
1 parent 861c511 commit 3a9b32d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
43 changes: 37 additions & 6 deletions python/plugins/processing/algs/qgis/TilesXYZ.py
Expand Up @@ -129,6 +129,8 @@ class TilesXYZAlgorithmBase(QgisAlgorithm):
ZOOM_MAX = 'ZOOM_MAX'
DPI = 'DPI'
TILE_FORMAT = 'TILE_FORMAT'
ALPHA = 'ALPHA'
QUALITY = 'QUALITY'

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterExtent(self.EXTENT, self.tr('Extent')))
Expand All @@ -152,6 +154,16 @@ def initAlgorithm(self, config=None):
self.tr('Tile format'),
self.formats,
defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.ALPHA,
self.tr('Background transparency (applies to PNG only)'),
minValue=0,
maxValue=255,
defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.QUALITY,
self.tr('Quality (applies to JPG only)'),
minValue=1,
maxValue=100,
defaultValue=75))

def prepareAlgorithm(self, parameters, context, feedback):
project = context.project()
Expand All @@ -167,8 +179,14 @@ def generate(self, writer, parameters, context, feedback):
self.max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context)
dpi = self.parameterAsInt(parameters, self.DPI, context)
self.tile_format = self.formats[self.parameterAsEnum(parameters, self.TILE_FORMAT, context)]
tile_width = 256
tile_height = 256
alpha = self.parameterAsInt(parameters, self.ALPHA, context)
quality = self.parameterAsInt(parameters, self.QUALITY, context)
try:
tile_width = self.parameterAsInt(parameters, self.TILE_WIDTH, context)
tile_height = self.parameterAsInt(parameters, self.TILE_HEIGHT, context)
except AttributeError:
tile_width = 256
tile_height = 256

wgs_crs = QgsCoordinateReferenceSystem('EPSG:4326')
dest_crs = QgsCoordinateReferenceSystem('EPSG:3857')
Expand All @@ -183,7 +201,9 @@ def generate(self, writer, parameters, context, feedback):
settings.setLayers(self.layers)
settings.setOutputDpi(dpi)
if self.tile_format == 'PNG':
settings.setBackgroundColor(QColor(Qt.transparent))
color = QColor()
color.setAlpha(alpha)
settings.setBackgroundColor(color)

self.wgs_extent = src_to_wgs.transformBoundingBox(extent)
self.wgs_extent = [self.wgs_extent.xMinimum(), self.wgs_extent.yMinimum(), self.wgs_extent.xMaximum(),
Expand All @@ -201,7 +221,7 @@ def generate(self, writer, parameters, context, feedback):

tile_params = {
'format': self.tile_format,
'quality': 75,
'quality': quality,
'width': tile_width,
'height': tile_height,
'min_zoom': self.min_zoom,
Expand Down Expand Up @@ -282,11 +302,10 @@ def set_parameters(self, tile_params):
self.min_zoom = tile_params.get('min_zoom')
self.max_zoom = tile_params.get('max_zoom')
tile_format = tile_params['format']
options = []
if tile_format == 'JPG':
tile_format = 'JPEG'
options = ['QUALITY=%s' % tile_params.get('quality', 75)]
else:
options = ['ZLEVEL=8']
driver = gdal.GetDriverByName('MBTiles')
ds = driver.Create(self.filename, 1, 1, 1, options=['TILE_FORMAT=%s' % tile_format] + options)
ds = None
Expand Down Expand Up @@ -458,9 +477,21 @@ class TilesXYZAlgorithmDirectory(TilesXYZAlgorithmBase):
TMS_CONVENTION = 'TMS_CONVENTION'
OUTPUT_DIRECTORY = 'OUTPUT_DIRECTORY'
OUTPUT_HTML = 'OUTPUT_HTML'
TILE_WIDTH = 'TILE_WIDTH'
TILE_HEIGHT = 'TILE_HEIGHT'

def initAlgorithm(self, config=None):
super(TilesXYZAlgorithmDirectory, self).initAlgorithm()
self.addParameter(QgsProcessingParameterNumber(self.TILE_WIDTH,
self.tr('Tile width'),
minValue=1,
maxValue=4096,
defaultValue=256))
self.addParameter(QgsProcessingParameterNumber(self.TILE_HEIGHT,
self.tr('Tile height'),
minValue=1,
maxValue=4096,
defaultValue=256))
self.addParameter(QgsProcessingParameterBoolean(self.TMS_CONVENTION,
self.tr('Use inverted tile Y axis (TMS convention)'),
defaultValue=False,
Expand Down
Expand Up @@ -2134,7 +2134,10 @@ tests:
ZOOM_MIN: 1
ZOOM_MAX: 3
TILE_FORMAT: 0 # png
ALPHA: 0 # fully transparent
TMS_CONVENTION: false
TILE_WIDTH: 256
TILE_HEIGHT: 256

results:
OUTPUT_DIRECTORY:
Expand Down

0 comments on commit 3a9b32d

Please sign in to comment.