mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-05 00:05:32 -04:00
115 lines
4.5 KiB
Python
115 lines
4.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
rasterize_over.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'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
import os
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.core import (QgsRasterFileWriter,
|
|
QgsProcessingParameterDefinition,
|
|
QgsProcessingParameterFeatureSource,
|
|
QgsProcessingParameterField,
|
|
QgsProcessingParameterRasterLayer,
|
|
QgsProcessingParameterNumber,
|
|
QgsProcessingParameterString,
|
|
QgsProcessingParameterEnum,
|
|
QgsProcessingParameterExtent,
|
|
QgsProcessingParameterBoolean,
|
|
QgsProcessingParameterRasterDestination)
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
|
class rasterize_over(GdalAlgorithm):
|
|
|
|
INPUT = 'INPUT'
|
|
FIELD = 'FIELD'
|
|
INPUT_RASTER = 'INPUT_RASTER'
|
|
ADD = 'ADD'
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def initAlgorithm(self, config=None):
|
|
self.units = [self.tr("Pixels"),
|
|
self.tr("Georeferenced units")]
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
self.tr('Input vector layer')))
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT_RASTER, self.tr('Input raster layer')))
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
|
self.tr('Field to use for a burn-in value'),
|
|
None,
|
|
self.INPUT,
|
|
QgsProcessingParameterField.Numeric,
|
|
optional=False))
|
|
add_param = QgsProcessingParameterBoolean(self.ADD,
|
|
self.tr('Adds the new value to the existing raster'),
|
|
defaultValue=False)
|
|
add_param.setFlags(add_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
self.addParameter(add_param)
|
|
|
|
def name(self):
|
|
return 'rasterize_over'
|
|
|
|
def displayName(self):
|
|
return self.tr('Rasterize (write over existing raster)')
|
|
|
|
def group(self):
|
|
return self.tr('Vector conversion')
|
|
|
|
def groupId(self):
|
|
return 'vectorconversion'
|
|
|
|
def icon(self):
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'rasterize.png'))
|
|
|
|
def commandName(self):
|
|
return 'gdal_rasterize'
|
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
|
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
|
|
|
|
arguments = ['-l']
|
|
arguments.append(layerName)
|
|
|
|
fieldName = self.parameterAsString(parameters, self.FIELD, context)
|
|
if fieldName:
|
|
arguments.append('-a')
|
|
arguments.append(fieldName)
|
|
|
|
if self.parameterAsBool(parameters, self.ADD, context):
|
|
arguments.append('-add')
|
|
|
|
arguments.append(ogrLayer)
|
|
arguments.append(inLayer.source())
|
|
|
|
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
|