Skip to content
Permalink
Browse files

Remove NAME parameter, use OUTPUT_FILE instead

For output to directory, OUTPUT_DIRECTORY destination parameter is used.
For output to MBTiles file, OUTPUT_FILE destimation parameter is used.
  • Loading branch information
wonder-sk committed Apr 25, 2019
1 parent ee63cff commit 285d4e0da052fa1df67e4dce11fa8fc0e91444ca
Showing with 23 additions and 15 deletions.
  1. +23 −15 python/plugins/processing/algs/qgis/TilesXYZ.py
@@ -133,7 +133,7 @@ def writeTile(self, tile, image):
return path

def close(self):
return self.folder
pass


class MBTilesWriter:
@@ -212,18 +212,17 @@ def close(self):
bounds = ','.join(map(str, self.extent))
ds.ExecuteSQL("UPDATE metadata SET value='{}' WHERE name='bounds'".format(bounds))
ds = None
return self.filename


class TilesXYZ(QgisAlgorithm):
NAME = 'NAME'
EXTENT = 'EXTENT'
ZOOM_MIN = 'ZOOM_MIN'
ZOOM_MAX = 'ZOOM_MAX'
TILE_FORMAT = 'TILE_FORMAT'
DPI = 'DPI'
OUTPUT_FORMAT = 'OUTPUT_FORMAT'
OUTPUT_DIRECTORY = 'OUTPUT_DIRECTORY'
OUTPUT_FILE = 'OUTPUT_FILE'

def group(self):
return self.tr('Raster tools')
@@ -232,9 +231,6 @@ def groupId(self):
return 'rastertools'

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterString(self.NAME,
self.tr('Name'),
defaultValue='Tiles'))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT, self.tr('Extent')))
self.addParameter(QgsProcessingParameterNumber(self.ZOOM_MIN,
self.tr('Minimum zoom'),
@@ -262,7 +258,12 @@ def initAlgorithm(self, config=None):
self.outputs,
defaultValue=0))
self.addParameter(QgsProcessingParameterFolderDestination(self.OUTPUT_DIRECTORY,
self.tr('Output directory')))
self.tr('Output directory'),
optional=True))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT_FILE,
self.tr('Output file (for MBTiles)'),
self.tr('MBTiles files (*.mbtiles)'),
optional=True))

def name(self):
return 'tilesxyz'
@@ -279,14 +280,16 @@ def prepareAlgorithm(self, parameters, context, feedback):
def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(1)

name = self.parameterAsString(parameters, self.NAME, context)
output_dir = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context)
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context)
max_zoom = self.parameterAsInt(parameters, self.ZOOM_MAX, context)
dpi = self.parameterAsInt(parameters, self.DPI, context)
tile_format = self.formats[self.parameterAsEnum(parameters, self.TILE_FORMAT, context)]
output_format = self.outputs[self.parameterAsEnum(parameters, self.OUTPUT_FORMAT, context)]
if output_format == 'Directory':
output_dir = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context)

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Apr 25, 2019

Contributor

This should throw an exception if the directory or file is not set, but required by the selected format

else: # MBTiles
output_file = self.parameterAsString(parameters, self.OUTPUT_FILE, context)
tile_width = 256
tile_height = 256

@@ -323,10 +326,9 @@ def processAlgorithm(self, parameters, context, feedback):
'height': tile_height
}
if output_format == 'Directory':
writer = DirectoryWriter(os.path.join(output_dir, name), tile_params)
writer = DirectoryWriter(output_dir, tile_params)
else:
filename = os.path.join(output_dir, '%s.mbtiles' % name)
writer = MBTilesWriter(filename, tile_params, wgs_extent, min_zoom, max_zoom)
writer = MBTilesWriter(output_file, tile_params, wgs_extent, min_zoom, max_zoom)

for zoom in range(min_zoom, max_zoom + 1):
feedback.pushConsoleInfo('Generating tiles for zoom level: %s' % zoom)
@@ -358,7 +360,7 @@ def processAlgorithm(self, parameters, context, feedback):
painter.end()

# For analysing metatiles (labels, etc.)
# metatile_dir = os.path.join(output_dir, name, str(zoom))
# metatile_dir = os.path.join(output_dir, str(zoom))
# os.makedirs(metatile_dir, exist_ok=True)
# image.save(os.path.join(metatile_dir, 'metatile_%s.png' % i))

@@ -369,8 +371,14 @@ def processAlgorithm(self, parameters, context, feedback):
progress += 1
feedback.setProgress(100 * (progress / metatiles_count))

output = writer.close()
return {'OUTPUT': output}
writer.close()

results = {}
if output_format == 'Directory':
results['OUTPUT_DIRECTORY'] = output_dir
else: # MBTiles
results['OUTPUT_FILE'] = output_file
return results

def checkParameterValues(self, parameters, context):
min_zoom = self.parameterAsInt(parameters, self.ZOOM_MIN, context)

0 comments on commit 285d4e0

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