Skip to content
Permalink
Browse files

[processing] expose Aspect from Raster terrain analysis plugin in too…

…lbox
  • Loading branch information
alexbruy committed Oct 11, 2016
1 parent 60cc853 commit e76a4678babf533a811c566e5ad42839f265e2a9
@@ -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)
@@ -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))
@@ -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:
@@ -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'
@@ -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)
@@ -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()))

0 comments on commit e76a467

Please sign in to comment.
You can’t perform that action at this time.