Skip to content

Commit e7ae5d2

Browse files
committedSep 14, 2013
[processing] add GDAL rasterize algorithm (fix #5854)
1 parent fd50ac0 commit e7ae5d2

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed
 

‎python/plugins/processing/gdal/GdalOgrAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from processing.gdal.ClipByExtent import ClipByExtent
4747
from processing.gdal.ClipByMask import ClipByMask
4848
from processing.gdal.contour import contour
49+
from processing.gdal.rasterize import rasterize
4950

5051
from processing.gdal.ogr2ogr import Ogr2Ogr
5152
from processing.gdal.ogrinfo import OgrInfo
@@ -95,7 +96,7 @@ def createAlgsList(self):
9596
self.preloadedAlgs = [nearblack(), information(), warp(), translate(),
9697
rgb2pct(), pct2rgb(), merge(), polygonize(),
9798
gdaladdo(), ClipByExtent(), ClipByMask(),
98-
contour(),
99+
contour(), rasterize(),
99100
OgrInfo(), Ogr2Ogr(), OgrSql()]
100101

101102
#And then we add those that are created as python scripts
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
rasterize.py
6+
---------------------
7+
Date : September 2013
8+
Copyright : (C) 2013 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'September 2013'
22+
__copyright__ = '(C) 2013, Alexander Bruy'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
import os
27+
from PyQt4 import QtGui, QtCore
28+
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.tools.system import *
31+
32+
from processing.parameters.ParameterVector import ParameterVector
33+
from processing.parameters.ParameterTableField import ParameterTableField
34+
from processing.parameters.ParameterSelection import ParameterSelection
35+
from processing.parameters.ParameterNumber import ParameterNumber
36+
37+
from processing.outputs.OutputRaster import OutputRaster
38+
39+
from processing.gdal.GdalUtils import GdalUtils
40+
41+
class rasterize(GeoAlgorithm):
42+
43+
INPUT = "INPUT"
44+
FIELD = "FIELD"
45+
DIMENSIONS = "DIMENSIONS"
46+
WIDTH = "WIDTH"
47+
HEIGHT = "HEIGHT"
48+
OUTPUT = "OUTPUT"
49+
50+
51+
52+
def getIcon(self):
53+
filepath = os.path.dirname(__file__) + "/icons/rasterize.png"
54+
return QtGui.QIcon(filepath)
55+
56+
def defineCharacteristics(self):
57+
self.name = "Rasterize"
58+
self.group = "[GDAL] Conversion"
59+
self.addParameter(ParameterVector(self.INPUT, "Input layer"))
60+
self.addParameter(ParameterTableField(self.FIELD, "Attribute field", self.INPUT))
61+
self.addParameter(ParameterSelection(self.DIMENSIONS, "Set output raster size", ["Output size in pixels", "Output resolution in map units per pixel"], 0))
62+
self.addParameter(ParameterNumber(self.WIDTH, "Horizontal", 0.0, 99999999.999999, 3000.0))
63+
self.addParameter(ParameterNumber(self.HEIGHT, "Vertical", 0.0, 99999999.999999, 3000.0))
64+
65+
self.addOutput(OutputRaster(self.OUTPUT, "Output layer"))
66+
67+
def processAlgorithm(self, progress):
68+
arguments = []
69+
arguments.append("-a")
70+
arguments.append(str(self.getParameterValue(self.FIELD)))
71+
72+
dimType = self.getParameterValue(self.DIMENSIONS)
73+
if dimType == 0:
74+
# size in pixels
75+
arguments.append("-ts")
76+
else:
77+
# resolution in map units per pixel
78+
arguments.append("-tr")
79+
arguments.append(str(self.getParameterValue(self.WIDTH)))
80+
arguments.append(str(self.getParameterValue(self.HEIGHT)))
81+
82+
arguments.append("-l")
83+
arguments.append(os.path.basename(os.path.splitext(unicode(self.getParameterValue(self.INPUT)))[0]))
84+
arguments.append(unicode(self.getParameterValue(self.INPUT)))
85+
86+
arguments.append(unicode(self.getOutputValue(self.OUTPUT)))
87+
88+
GdalUtils.runGdal(["gdal_rasterize", GdalUtils.escapeAndJoin(arguments)], progress)

0 commit comments

Comments
 (0)