2013-09-14 18:24:39 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
rasterize.py
|
|
|
|
---------------------
|
|
|
|
Date : September 2013
|
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy 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__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'September 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-14 18:24:39 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-14 18:24:39 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
2016-03-06 17:59:57 +00:00
|
|
|
from processing.core.parameters import ParameterExtent
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.parameters import ParameterNumber
|
2014-10-21 22:21:52 +01:00
|
|
|
from processing.core.parameters import ParameterBoolean
|
2015-02-05 13:52:19 +00:00
|
|
|
from processing.core.parameters import ParameterString
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.outputs import OutputRaster
|
2016-01-22 15:45:09 +02:00
|
|
|
|
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-09-14 18:24:39 +03:00
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
from processing.tools.vector import ogrConnectionString, ogrLayerName
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
class rasterize(GdalAlgorithm):
|
2013-09-14 18:24:39 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
DIMENSIONS = 'DIMENSIONS'
|
|
|
|
WIDTH = 'WIDTH'
|
|
|
|
HEIGHT = 'HEIGHT'
|
2015-10-22 10:07:43 +01:00
|
|
|
EXTRA = 'EXTRA'
|
2014-10-19 23:24:06 +01:00
|
|
|
RTYPE = 'RTYPE'
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2015-01-31 21:28:01 +00:00
|
|
|
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']
|
2015-02-05 13:52:19 +00:00
|
|
|
NO_DATA = 'NO_DATA'
|
|
|
|
TILED = 'TILED'
|
|
|
|
COMPRESS = 'COMPRESS'
|
|
|
|
JPEGCOMPRESSION = 'JPEGCOMPRESSION'
|
|
|
|
PREDICTOR = 'PREDICTOR'
|
|
|
|
ZLEVEL = 'ZLEVEL'
|
|
|
|
BIGTIFF = 'BIGTIFF'
|
|
|
|
BIGTIFFTYPE = ['', 'YES', 'NO', 'IF_NEEDED', 'IF_SAFER']
|
|
|
|
COMPRESSTYPE = ['NONE', 'JPEG', 'LZW', 'PACKBITS', 'DEFLATE']
|
|
|
|
TFW = 'TFW'
|
2016-03-06 17:59:57 +00:00
|
|
|
RAST_EXT = 'RAST_EXT'
|
2016-03-08 09:05:48 +00:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'rasterize.png'))
|
|
|
|
|
2013-10-06 13:25:01 +02:00
|
|
|
def commandLineName(self):
|
|
|
|
return "gdalogr:rasterize"
|
2013-09-14 18:24:39 +03:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Rasterize (vector to raster)')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Conversion')
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer')))
|
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Attribute field'), self.INPUT))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterSelection(self.DIMENSIONS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Set output raster size (ignored if above option is checked)'),
|
|
|
|
['Output size in pixels', 'Output resolution in map units per pixel'], 1))
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterNumber(self.WIDTH,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Horizontal'), 0.0, 99999999.999999, 100.0))
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterNumber(self.HEIGHT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Vertical'), 0.0, 99999999.999999, 100.0))
|
2016-03-06 17:59:57 +00:00
|
|
|
self.addParameter(ParameterExtent(self.RAST_EXT, self.tr('Raster extent')))
|
2015-06-30 13:25:32 +02:00
|
|
|
|
|
|
|
params = []
|
|
|
|
params.append(ParameterSelection(self.RTYPE, self.tr('Raster type'),
|
2015-08-22 14:29:41 +02:00
|
|
|
self.TYPE, 5))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterString(self.NO_DATA,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr("Nodata value"),
|
2016-04-25 22:09:42 +01:00
|
|
|
'', optional=True))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterSelection(self.COMPRESS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('GeoTIFF options. Compression type:'), self.COMPRESSTYPE, 4))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterNumber(self.JPEGCOMPRESSION,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Set the JPEG compression level'),
|
|
|
|
1, 100, 75))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterNumber(self.ZLEVEL,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Set the DEFLATE compression level'),
|
|
|
|
1, 9, 6))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterNumber(self.PREDICTOR,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Set the predictor for LZW or DEFLATE compression'),
|
|
|
|
1, 3, 1))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterBoolean(self.TILED,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Create tiled output (only used for the GTiff format)'), False))
|
2015-06-30 13:25:32 +02:00
|
|
|
params.append(ParameterSelection(self.BIGTIFF,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Control whether the created file is a BigTIFF or a classic TIFF'), self.BIGTIFFTYPE, 0))
|
2015-02-05 13:52:19 +00:00
|
|
|
self.addParameter(ParameterBoolean(self.TFW,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Force the generation of an associated ESRI world file (.tfw)'), False))
|
2015-10-22 10:07:43 +01:00
|
|
|
params.append(ParameterString(self.EXTRA,
|
2015-10-30 23:30:16 +01:00
|
|
|
self.tr('Additional creation parameters'), '', optional=True))
|
2015-06-30 13:25:32 +02:00
|
|
|
|
|
|
|
for param in params:
|
|
|
|
param.isAdvanced = True
|
|
|
|
self.addParameter(param)
|
|
|
|
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addOutput(OutputRaster(self.OUTPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Rasterized')))
|
2013-09-14 18:24:39 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
def getConsoleCommands(self):
|
2015-02-05 13:52:19 +00:00
|
|
|
inLayer = self.getParameterValue(self.INPUT)
|
2016-01-22 15:45:09 +02:00
|
|
|
ogrLayer = ogrConnectionString(inLayer)[1:-1]
|
2016-04-26 14:18:41 +02:00
|
|
|
noData = self.getParameterValue(self.NO_DATA)
|
|
|
|
if noData is not None:
|
|
|
|
noData = unicode(noData)
|
2015-08-16 20:57:24 +02:00
|
|
|
jpegcompression = unicode(self.getParameterValue(self.JPEGCOMPRESSION))
|
|
|
|
predictor = unicode(self.getParameterValue(self.PREDICTOR))
|
|
|
|
zlevel = unicode(self.getParameterValue(self.ZLEVEL))
|
|
|
|
tiled = unicode(self.getParameterValue(self.TILED))
|
2015-02-05 13:52:19 +00:00
|
|
|
compress = self.COMPRESSTYPE[self.getParameterValue(self.COMPRESS)]
|
|
|
|
bigtiff = self.BIGTIFFTYPE[self.getParameterValue(self.BIGTIFF)]
|
2015-08-16 20:57:24 +02:00
|
|
|
tfw = unicode(self.getParameterValue(self.TFW))
|
2015-02-05 13:52:19 +00:00
|
|
|
out = self.getOutputValue(self.OUTPUT)
|
2016-04-26 14:18:41 +02:00
|
|
|
extra = self.getParameterValue(self.EXTRA)
|
|
|
|
if extra is not None:
|
|
|
|
extra = unicode(extra)
|
2016-03-06 17:59:57 +00:00
|
|
|
rastext = unicode(self.getParameterValue(self.RAST_EXT))
|
2015-02-07 13:38:35 +01:00
|
|
|
|
2013-09-14 18:24:39 +03:00
|
|
|
arguments = []
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-a')
|
2015-08-16 20:57:24 +02:00
|
|
|
arguments.append(unicode(self.getParameterValue(self.FIELD)))
|
2014-10-21 22:21:52 +01:00
|
|
|
|
2015-04-10 18:03:55 +01:00
|
|
|
arguments.append('-ot')
|
|
|
|
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
|
2015-01-17 12:17:15 +02:00
|
|
|
dimType = self.getParameterValue(self.DIMENSIONS)
|
2016-02-19 18:03:02 +00:00
|
|
|
arguments.append('-of')
|
2016-02-19 21:19:49 +02:00
|
|
|
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
|
2016-03-06 17:59:57 +00:00
|
|
|
|
|
|
|
regionCoords = rastext.split(',')
|
|
|
|
try:
|
|
|
|
rastext = []
|
|
|
|
rastext.append('-te')
|
|
|
|
rastext.append(regionCoords[0])
|
|
|
|
rastext.append(regionCoords[2])
|
|
|
|
rastext.append(regionCoords[1])
|
|
|
|
rastext.append(regionCoords[3])
|
|
|
|
except IndexError:
|
|
|
|
rastext = []
|
|
|
|
if rastext:
|
|
|
|
arguments.extend(rastext)
|
|
|
|
|
2015-04-10 18:03:55 +01:00
|
|
|
if dimType == 0:
|
2015-08-22 14:29:41 +02:00
|
|
|
# size in pixels
|
|
|
|
arguments.append('-ts')
|
|
|
|
arguments.append(unicode(self.getParameterValue(self.WIDTH)))
|
|
|
|
arguments.append(unicode(self.getParameterValue(self.HEIGHT)))
|
2015-04-10 18:03:55 +01:00
|
|
|
else:
|
2015-08-22 14:29:41 +02:00
|
|
|
# resolution in map units per pixel
|
|
|
|
arguments.append('-tr')
|
|
|
|
arguments.append(unicode(self.getParameterValue(self.WIDTH)))
|
|
|
|
arguments.append(unicode(self.getParameterValue(self.HEIGHT)))
|
2015-04-10 18:03:55 +01:00
|
|
|
|
2016-04-26 14:18:41 +02:00
|
|
|
if noData and len(noData) > 0:
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append('-a_nodata')
|
|
|
|
arguments.append(noData)
|
2015-04-10 18:03:55 +01:00
|
|
|
|
|
|
|
if (GdalUtils.getFormatShortNameFromFilename(out) == "GTiff"):
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co COMPRESS=" + compress)
|
2015-02-05 13:52:19 +00:00
|
|
|
if compress == 'JPEG':
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co JPEG_QUALITY=" + jpegcompression)
|
2015-02-05 13:52:19 +00:00
|
|
|
elif (compress == 'LZW') or (compress == 'DEFLATE'):
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co PREDICTOR=" + predictor)
|
2015-02-05 13:52:19 +00:00
|
|
|
if compress == 'DEFLATE':
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co ZLEVEL=" + zlevel)
|
2015-02-05 13:52:19 +00:00
|
|
|
if tiled == "True":
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co TILED=YES")
|
2015-02-05 13:52:19 +00:00
|
|
|
if tfw == "True":
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co TFW=YES")
|
2015-02-05 13:52:19 +00:00
|
|
|
if len(bigtiff) > 0:
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append("-co BIGTIFF=" + bigtiff)
|
2016-04-26 14:18:41 +02:00
|
|
|
if extra and len(extra) > 0:
|
2015-10-22 10:07:43 +01:00
|
|
|
arguments.append(extra)
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-l')
|
2015-10-22 10:07:43 +01:00
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
arguments.append(ogrLayerName(inLayer))
|
2015-02-05 13:52:19 +00:00
|
|
|
arguments.append(ogrLayer)
|
2013-09-14 18:24:39 +03:00
|
|
|
|
|
|
|
arguments.append(unicode(self.getOutputValue(self.OUTPUT)))
|
2015-05-07 13:14:01 +02:00
|
|
|
return ['gdal_rasterize', GdalUtils.escapeAndJoin(arguments)]
|