Skip to content

Commit

Permalink
[processing] expose Aspect from Raster terrain analysis plugin in too…
Browse files Browse the repository at this point in the history
…lbox
  • Loading branch information
alexbruy committed Oct 11, 2016
1 parent 60cc853 commit e76a467
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
64 changes: 64 additions & 0 deletions python/plugins/processing/algs/qgis/Aspect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
Aspect.py
---------------------
Date : October 2016
Copyright : (C) 2016 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. *
* *
***************************************************************************
"""
from builtins import str

__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.analysis import QgsAspectFilter

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools import raster


class Aspect(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
Z_FACTOR = 'Z_FACTOR'
OUTPUT_LAYER = 'OUTPUT_LAYER'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Aspect')
self.group, self.i18n_group = self.trAlgorithm('Raster terrain analysis')

self.addParameter(ParameterRaster(self.INPUT_LAYER,
self.tr('Elevation layer')))
self.addParameter(ParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), 1.0, 999999.99, 1.0))
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
self.tr('Aspect')))

def processAlgorithm(self, progress):
inputFile = self.getParameterValue(self.INPUT_LAYER)
zFactor = self.getParameterValue(self.Z_FACTOR)
outputFile = self.getOutputValue(self.OUTPUT_LAYER)

outputFormat = raster.formatShortNameFromFileName(outputFile)

aspect = QgsAspectFilter(inputFile, outputFile, outputFormat)
aspect.setZFactor(zFactor)
aspect.processRaster(None)
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
from .Translate import Translate
from .SingleSidedBuffer import SingleSidedBuffer
from .PointsAlongGeometry import PointsAlongGeometry
from .Aspect import Aspect

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
Expand Down Expand Up @@ -212,9 +213,9 @@ def __init__(self):
RectanglesOvalsDiamondsVariable(),
RectanglesOvalsDiamondsFixed(), MergeLines(),
BoundingBox(), Boundary(), PointOnSurface(),
OffsetLine(), PolygonCentroids(),
Translate(), SingleSidedBuffer(),
PointsAlongGeometry()
OffsetLine(), PolygonCentroids(), Translate(),
SingleSidedBuffer(), PointsAlongGeometry(),
Aspect(),
]

if hasMatplotlib:
Expand Down
50 changes: 43 additions & 7 deletions python/plugins/processing/tools/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
* *
***************************************************************************
"""
from builtins import str
from builtins import range
from builtins import object

__author__ = 'Victor Olaya and Alexander Bruy'
__date__ = 'February 2013'
Expand All @@ -28,16 +25,55 @@

__revision__ = '$Format:%H$'

from builtins import str
from builtins import range
from builtins import object

import os
import struct

import numpy
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly

from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


RASTER_EXTENSION_MAP = None


def initGdalData():
global RASTER_EXTENSION_MAP

if RASTER_EXTENSION_MAP is not None:
return

if gdal.GetDriverCount() == 0:
gdal.AllRegister()

RASTER_EXTENSION_MAP = dict()
for i in range(gdal.GetDriverCount()):
driver = gdal.GetDriver(i)
if driver is None:
continue
md = driver.GetMetadata()
if gdal.DCAP_CREATE in md and md[gdal.DCAP_CREATE].lower() == 'yes':
ext = md[gdal.DMD_EXTENSION] if gdal.DMD_EXTENSION in md else None
if ext is not None and ext != '':
RASTER_EXTENSION_MAP[driver.ShortName] = ext


def formatShortNameFromFileName(fileName):
initGdalData()
ext = os.path.splitext(fileName)[1][1:]
for k, v in RASTER_EXTENSION_MAP.items():
if ext == v:
return k
return 'GTiff'


def scanraster(layer, progress):
filename = str(layer.source())
dataset = gdal.Open(filename, GA_ReadOnly)
dataset = gdal.Open(filename, gdal.GA_ReadOnly)
band = dataset.GetRasterBand(1)
nodata = band.GetNoDataValue()
bandtype = gdal.GetDataTypeName(band.DataType)
Expand Down Expand Up @@ -114,8 +150,8 @@ def getValue(self, x, y, band=0):
return self.NODATA

def close(self):
format = 'GTiff'
driver = gdal.GetDriverByName(format)
fmt = 'GTiff'
driver = gdal.GetDriverByName(fmt)
dst_ds = driver.Create(self.fileName, self.nx, self.ny, 1,
gdal.GDT_Float32)
dst_ds.SetProjection(str(self.crs.toWkt()))
Expand Down

0 comments on commit e76a467

Please sign in to comment.