Skip to content

Commit e76a467

Browse files
committed
[processing] expose Aspect from Raster terrain analysis plugin in toolbox
1 parent 60cc853 commit e76a467

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Aspect.py
6+
---------------------
7+
Date : October 2016
8+
Copyright : (C) 2016 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+
from builtins import str
20+
21+
__author__ = 'Alexander Bruy'
22+
__date__ = 'October 2016'
23+
__copyright__ = '(C) 2016, Alexander Bruy'
24+
25+
# This will get replaced with a git SHA1 when you do a git archive
26+
27+
__revision__ = '$Format:%H$'
28+
29+
from qgis.analysis import QgsAspectFilter
30+
31+
from processing.core.GeoAlgorithm import GeoAlgorithm
32+
from processing.core.parameters import ParameterRaster
33+
from processing.core.parameters import ParameterNumber
34+
from processing.core.outputs import OutputRaster
35+
from processing.tools import raster
36+
37+
38+
class Aspect(GeoAlgorithm):
39+
40+
INPUT_LAYER = 'INPUT_LAYER'
41+
Z_FACTOR = 'Z_FACTOR'
42+
OUTPUT_LAYER = 'OUTPUT_LAYER'
43+
44+
def defineCharacteristics(self):
45+
self.name, self.i18n_name = self.trAlgorithm('Aspect')
46+
self.group, self.i18n_group = self.trAlgorithm('Raster terrain analysis')
47+
48+
self.addParameter(ParameterRaster(self.INPUT_LAYER,
49+
self.tr('Elevation layer')))
50+
self.addParameter(ParameterNumber(self.Z_FACTOR,
51+
self.tr('Z factor'), 1.0, 999999.99, 1.0))
52+
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
53+
self.tr('Aspect')))
54+
55+
def processAlgorithm(self, progress):
56+
inputFile = self.getParameterValue(self.INPUT_LAYER)
57+
zFactor = self.getParameterValue(self.Z_FACTOR)
58+
outputFile = self.getOutputValue(self.OUTPUT_LAYER)
59+
60+
outputFormat = raster.formatShortNameFromFileName(outputFile)
61+
62+
aspect = QgsAspectFilter(inputFile, outputFile, outputFormat)
63+
aspect.setZFactor(zFactor)
64+
aspect.processRaster(None)

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
from .Translate import Translate
159159
from .SingleSidedBuffer import SingleSidedBuffer
160160
from .PointsAlongGeometry import PointsAlongGeometry
161+
from .Aspect import Aspect
161162

162163
pluginPath = os.path.normpath(os.path.join(
163164
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -212,9 +213,9 @@ def __init__(self):
212213
RectanglesOvalsDiamondsVariable(),
213214
RectanglesOvalsDiamondsFixed(), MergeLines(),
214215
BoundingBox(), Boundary(), PointOnSurface(),
215-
OffsetLine(), PolygonCentroids(),
216-
Translate(), SingleSidedBuffer(),
217-
PointsAlongGeometry()
216+
OffsetLine(), PolygonCentroids(), Translate(),
217+
SingleSidedBuffer(), PointsAlongGeometry(),
218+
Aspect(),
218219
]
219220

220221
if hasMatplotlib:

python/plugins/processing/tools/raster.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from builtins import str
20-
from builtins import range
21-
from builtins import object
2219

2320
__author__ = 'Victor Olaya and Alexander Bruy'
2421
__date__ = 'February 2013'
@@ -28,16 +25,55 @@
2825

2926
__revision__ = '$Format:%H$'
3027

28+
from builtins import str
29+
from builtins import range
30+
from builtins import object
31+
32+
import os
3133
import struct
34+
3235
import numpy
3336
from osgeo import gdal
34-
from osgeo.gdalconst import GA_ReadOnly
37+
3538
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3639

3740

41+
RASTER_EXTENSION_MAP = None
42+
43+
44+
def initGdalData():
45+
global RASTER_EXTENSION_MAP
46+
47+
if RASTER_EXTENSION_MAP is not None:
48+
return
49+
50+
if gdal.GetDriverCount() == 0:
51+
gdal.AllRegister()
52+
53+
RASTER_EXTENSION_MAP = dict()
54+
for i in range(gdal.GetDriverCount()):
55+
driver = gdal.GetDriver(i)
56+
if driver is None:
57+
continue
58+
md = driver.GetMetadata()
59+
if gdal.DCAP_CREATE in md and md[gdal.DCAP_CREATE].lower() == 'yes':
60+
ext = md[gdal.DMD_EXTENSION] if gdal.DMD_EXTENSION in md else None
61+
if ext is not None and ext != '':
62+
RASTER_EXTENSION_MAP[driver.ShortName] = ext
63+
64+
65+
def formatShortNameFromFileName(fileName):
66+
initGdalData()
67+
ext = os.path.splitext(fileName)[1][1:]
68+
for k, v in RASTER_EXTENSION_MAP.items():
69+
if ext == v:
70+
return k
71+
return 'GTiff'
72+
73+
3874
def scanraster(layer, progress):
3975
filename = str(layer.source())
40-
dataset = gdal.Open(filename, GA_ReadOnly)
76+
dataset = gdal.Open(filename, gdal.GA_ReadOnly)
4177
band = dataset.GetRasterBand(1)
4278
nodata = band.GetNoDataValue()
4379
bandtype = gdal.GetDataTypeName(band.DataType)
@@ -114,8 +150,8 @@ def getValue(self, x, y, band=0):
114150
return self.NODATA
115151

116152
def close(self):
117-
format = 'GTiff'
118-
driver = gdal.GetDriverByName(format)
153+
fmt = 'GTiff'
154+
driver = gdal.GetDriverByName(fmt)
119155
dst_ds = driver.Create(self.fileName, self.nx, self.ny, 1,
120156
gdal.GDT_Float32)
121157
dst_ds.SetProjection(str(self.crs.toWkt()))

0 commit comments

Comments
 (0)