179 lines
7.4 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
nviz.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf 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__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
import os
import time
from PyQt4.QtGui import QIcon
from qgis.core import QgsRasterLayer
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterRaster
from GrassUtils import GrassUtils
from processing.tools.system import getNumExportedLayers
from processing.tools import dataobjects
2012-09-15 18:25:25 +03:00
pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
2012-09-15 18:25:25 +03:00
class nviz(GeoAlgorithm):
ELEVATION = 'ELEVATION'
VECTOR = 'VECTOR'
COLOR = 'COLOR'
GRASS_REGION_EXTENT_PARAMETER = 'GRASS_REGION_PARAMETER'
GRASS_REGION_CELLSIZE_PARAMETER = 'GRASS_REGION_CELLSIZE_PARAMETER'
2012-09-15 18:25:25 +03:00
def __init__(self):
GeoAlgorithm.__init__(self)
self.showInModeler = False
2012-09-15 18:25:25 +03:00
def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'grass.png'))
2012-09-15 18:25:25 +03:00
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('nviz')
self.group, self.i18n_group = self.trAlgorithm('Visualization(NVIZ)')
self.addParameter(ParameterMultipleInput(nviz.ELEVATION,
self.tr('Raster file(s) for elevation'),
ParameterMultipleInput.TYPE_RASTER, True))
self.addParameter(ParameterMultipleInput(nviz.VECTOR,
self.tr('Vector lines/areas overlay file(s)'),
ParameterMultipleInput.TYPE_VECTOR_ANY, True))
self.addParameter(ParameterMultipleInput(nviz.COLOR,
self.tr('Raster file(s) for color'),
ParameterMultipleInput.TYPE_RASTER, True))
self.addParameter(ParameterExtent(nviz.GRASS_REGION_EXTENT_PARAMETER,
self.tr('GRASS region extent')))
self.addParameter(ParameterNumber(self.GRASS_REGION_CELLSIZE_PARAMETER,
self.tr('GRASS region cellsize (leave 0 for default)'),
0, None, 0.0))
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
commands = []
vector = self.getParameterValue(self.VECTOR)
elevation = self.getParameterValue(self.ELEVATION)
color = self.getParameterValue(self.COLOR)
region = \
unicode(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(',')
command = 'g.region '
command += 'n=' + unicode(regionCoords[3])
command += ' s=' + unicode(regionCoords[2])
command += ' e=' + unicode(regionCoords[1])
command += ' w=' + unicode(regionCoords[0])
2012-12-16 21:37:13 +01:00
cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER)
if cellsize:
command += ' res=' + unicode(cellsize)
2012-12-16 21:37:13 +01:00
else:
command += ' res=' + unicode(self.getDefaultCellsize())
2012-12-16 21:37:13 +01:00
commands.append(command)
command = 'nviz'
2012-09-15 18:25:25 +03:00
if vector:
layers = vector.split(';')
2012-09-15 18:25:25 +03:00
for layer in layers:
(cmd, newfilename) = self.exportVectorLayer(layer)
2012-12-16 21:37:13 +01:00
commands.append(cmd)
2012-09-15 18:25:25 +03:00
vector = vector.replace(layer, newfilename)
command += ' vector=' + vector.replace(';', ',')
2013-10-20 20:41:25 +02:00
if color:
layers = color.split(';')
for layer in layers:
(cmd, newfilename) = self.exportRasterLayer(layer)
commands.append(cmd)
color = color.replace(layer, newfilename)
command += ' color=' + color.replace(';', ',')
2012-09-15 18:25:25 +03:00
if elevation:
layers = elevation.split(';')
2012-09-15 18:25:25 +03:00
for layer in layers:
(cmd, newfilename) = self.exportRasterLayer(layer)
2012-12-16 21:37:13 +01:00
commands.append(cmd)
2012-09-15 18:25:25 +03:00
elevation = elevation.replace(layer, newfilename)
command += ' elevation=' + elevation.replace(';', ',')
2012-09-15 18:25:25 +03:00
if elevation is None and vector is None:
command += ' -q'
2012-09-15 18:25:25 +03:00
commands.append(command)
GrassUtils.createTempMapset()
2012-09-15 18:25:25 +03:00
GrassUtils.executeGrass(commands, progress)
def getTempFilename(self):
filename = 'tmp' + unicode(time.time()).replace('.', '') \
+ unicode(getNumExportedLayers())
2012-09-15 18:25:25 +03:00
return filename
def exportVectorLayer(self, layer):
2012-09-15 18:25:25 +03:00
destFilename = self.getTempFilename()
command = 'v.in.ogr'
command += ' min_area=-1'
command += ' dsn="' + os.path.dirname(layer) + '"'
command += ' layer=' + os.path.basename(layer)[:-4]
command += ' output=' + destFilename
command += ' --overwrite -o'
return (command, destFilename)
2012-09-15 18:25:25 +03:00
def exportRasterLayer(self, layer):
destFilename = self.getTempFilename()
command = 'r.in.gdal'
command += ' input="' + layer + '"'
command += ' band=1'
command += ' out=' + destFilename
command += ' --overwrite -o'
return (command, destFilename)
2012-12-16 21:37:13 +01:00
def getDefaultCellsize(self):
cellsize = 0
for param in self.parameters:
if param.value:
if isinstance(param, ParameterRaster):
if isinstance(param.value, QgsRasterLayer):
layer = param.value
else:
layer = dataobjects.getObjectFromUri(param.value)
cellsize = max(cellsize, (layer.extent().xMaximum()
2016-01-14 10:59:38 +11:00
- layer.extent().xMinimum())
/ layer.width())
2012-12-16 21:37:13 +01:00
elif isinstance(param, ParameterMultipleInput):
layers = param.value.split(';')
2012-12-16 21:37:13 +01:00
for layername in layers:
layer = dataobjects.getObjectFromUri(layername)
2012-12-16 21:37:13 +01:00
if isinstance(layer, QgsRasterLayer):
cellsize = max(cellsize, (
layer.extent().xMaximum()
- layer.extent().xMinimum())
/ layer.width())
2012-09-15 18:25:25 +03:00
2012-12-16 21:37:13 +01:00
if cellsize == 0:
cellsize = 1
return cellsize