2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
GrassAlgorithm . 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 . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"""
2013-09-14 15:52:28 +02:00
2012-10-05 23:28:47 +02:00
__author__ = ' Victor Olaya '
__date__ = ' August 2012 '
__copyright__ = ' (C) 2012, Victor Olaya '
2013-10-01 20:52:22 +03:00
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2013-10-01 20:52:22 +03:00
2012-10-05 23:28:47 +02:00
__revision__ = ' $Format: % H$ '
2012-09-15 18:25:25 +03:00
import os
2013-09-12 13:19:00 +02:00
import time
import uuid
import importlib
2012-09-15 18:25:25 +03:00
from qgis . core import *
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
2013-10-01 20:52:22 +03:00
2013-09-14 15:52:28 +02:00
from processing import interface
2013-08-12 20:44:27 +02:00
from processing . core . GeoAlgorithm import GeoAlgorithm
2013-10-01 20:52:22 +03:00
from processing . core . ProcessingConfig import ProcessingConfig
from processing . core . ProcessingLog import ProcessingLog
from processing . core . WrongHelpFileException import WrongHelpFileException
from processing . core . GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from processing . parameters . ParameterFactory import ParameterFactory
2013-08-12 20:44:27 +02:00
from processing . parameters . ParameterTable import ParameterTable
from processing . parameters . ParameterMultipleInput import ParameterMultipleInput
from processing . parameters . ParameterRaster import ParameterRaster
from processing . parameters . ParameterVector import ParameterVector
from processing . parameters . ParameterBoolean import ParameterBoolean
from processing . parameters . ParameterExtent import ParameterExtent
from processing . parameters . ParameterNumber import ParameterNumber
from processing . parameters . ParameterString import ParameterString
2013-10-01 20:52:22 +03:00
from processing . parameters . ParameterSelection import ParameterSelection
from processing . outputs . OutputRaster import OutputRaster
from processing . outputs . OutputHTML import OutputHTML
from processing . outputs . OutputVector import OutputVector
from processing . outputs . OutputFactory import OutputFactory
from processing . outputs . OutputFile import OutputFile
from processing . grass . GrassUtils import GrassUtils
from processing . tools import dataobjects , system
2012-09-15 18:25:25 +03:00
class GrassAlgorithm ( GeoAlgorithm ) :
2012-12-10 00:12:07 +01:00
2013-10-01 20:52:22 +03:00
GRASS_OUTPUT_TYPE_PARAMETER = ' GRASS_OUTPUT_TYPE_PARAMETER '
GRASS_MIN_AREA_PARAMETER = ' GRASS_MIN_AREA_PARAMETER '
GRASS_SNAP_TOLERANCE_PARAMETER = ' GRASS_SNAP_TOLERANCE_PARAMETER '
GRASS_REGION_EXTENT_PARAMETER = ' GRASS_REGION_PARAMETER '
GRASS_REGION_CELLSIZE_PARAMETER = ' GRASS_REGION_CELLSIZE_PARAMETER '
GRASS_REGION_ALIGN_TO_RESOLUTION = ' -a_r.region '
2013-05-15 20:42:04 +02:00
2013-10-01 20:52:22 +03:00
OUTPUT_TYPES = [ ' auto ' , ' point ' , ' line ' , ' area ' ]
2012-09-15 18:25:25 +03:00
def __init__ ( self , descriptionfile ) :
GeoAlgorithm . __init__ ( self )
self . descriptionFile = descriptionfile
self . defineCharacteristicsFromFile ( )
self . numExportedLayers = 0
2013-10-01 20:52:22 +03:00
# GRASS console output, needed to do postprocessing in case GRASS
# dumps results to the console
2012-12-04 00:15:03 +01:00
self . consoleOutput = [ ]
2012-09-15 18:25:25 +03:00
def getCopy ( self ) :
newone = GrassAlgorithm ( self . descriptionFile )
newone . provider = self . provider
return newone
def getIcon ( self ) :
2013-10-01 20:52:22 +03:00
return QIcon ( os . path . dirname ( __file__ ) + ' /../images/grass.png ' )
2012-09-15 18:25:25 +03:00
def helpFile ( self ) :
2013-10-01 20:52:22 +03:00
return ' http://grass.osgeo.org/grass64/manuals/ ' + self . grassName \
+ ' .html '
2012-09-15 18:25:25 +03:00
def getParameterDescriptions ( self ) :
descs = { }
try :
helpfile = self . helpFile ( )
except WrongHelpFileException :
return descs
if helpfile :
try :
infile = open ( helpfile )
lines = infile . readlines ( )
for i in range ( len ( lines ) ) :
2013-10-01 20:52:22 +03:00
if lines [ i ] . startswith ( ' <DT><b> ' ) :
for param in self . parameters :
searchLine = ' <b> ' + param . name + ' </b> '
2012-09-15 18:25:25 +03:00
if searchLine in lines [ i ] :
2013-10-01 20:52:22 +03:00
i + = 1
descs [ param . name ] = ( lines [ i ] ) [ 4 : - 6 ]
2012-09-15 18:25:25 +03:00
break
infile . close ( )
except Exception :
pass
return descs
def defineCharacteristicsFromFile ( self ) :
lines = open ( self . descriptionFile )
2013-10-01 20:52:22 +03:00
line = lines . readline ( ) . strip ( ' \n ' ) . strip ( )
2012-09-15 18:25:25 +03:00
self . grassName = line
2013-10-01 20:52:22 +03:00
line = lines . readline ( ) . strip ( ' \n ' ) . strip ( )
2012-09-15 18:25:25 +03:00
self . name = line
2013-10-01 20:52:22 +03:00
line = lines . readline ( ) . strip ( ' \n ' ) . strip ( )
2012-09-15 18:25:25 +03:00
self . group = line
2012-12-12 00:47:29 +01:00
hasRasterOutput = False
2013-05-02 23:53:03 +02:00
hasVectorInput = False
vectorOutputs = 0
2013-10-01 20:52:22 +03:00
while line != ' ' :
2012-09-15 18:25:25 +03:00
try :
2013-10-01 20:52:22 +03:00
line = line . strip ( ' \n ' ) . strip ( )
if line . startswith ( ' Parameter ' ) :
parameter = ParameterFactory . getFromString ( line )
2013-01-24 20:12:17 +01:00
self . addParameter ( parameter )
if isinstance ( parameter , ParameterVector ) :
2013-05-02 23:53:03 +02:00
hasVectorInput = True
2013-10-01 20:52:22 +03:00
if isinstance ( parameter , ParameterMultipleInput ) \
and parameter . datatype < 3 :
2013-05-02 23:53:03 +02:00
hasVectorInput = True
2013-10-01 20:52:22 +03:00
elif line . startswith ( ' *Parameter ' ) :
2012-09-15 18:25:25 +03:00
param = ParameterFactory . getFromString ( line [ 1 : ] )
param . isAdvanced = True
self . addParameter ( param )
else :
2013-01-12 23:36:00 +01:00
output = OutputFactory . getFromString ( line )
2013-10-01 20:52:22 +03:00
self . addOutput ( output )
2012-12-12 00:47:29 +01:00
if isinstance ( output , OutputRaster ) :
2013-01-12 23:36:00 +01:00
hasRasterOutput = True
2013-05-02 23:53:03 +02:00
elif isinstance ( output , OutputVector ) :
vectorOutputs + = 1
2013-10-01 20:52:22 +03:00
line = lines . readline ( ) . strip ( ' \n ' ) . strip ( )
except Exception , e :
ProcessingLog . addToLog ( ProcessingLog . LOG_ERROR ,
' Could not open GRASS algorithm: '
+ self . descriptionFile + ' \n ' + line )
2012-09-15 18:25:25 +03:00
raise e
lines . close ( )
2013-10-01 20:52:22 +03:00
self . addParameter ( ParameterExtent ( self . GRASS_REGION_EXTENT_PARAMETER ,
' GRASS region extent ' ) )
2012-12-12 00:47:29 +01:00
if hasRasterOutput :
2013-10-01 20:52:22 +03:00
self . addParameter ( ParameterNumber (
self . GRASS_REGION_CELLSIZE_PARAMETER ,
' GRASS region cellsize (leave 0 for default) ' ,
0 , None , 0.0 ) )
2013-05-02 23:53:03 +02:00
if hasVectorInput :
2013-10-01 20:52:22 +03:00
param = ParameterNumber ( self . GRASS_SNAP_TOLERANCE_PARAMETER ,
' v.in.ogr snap tolerance (-1 = no snap) ' ,
- 1 , None , - 1.0 )
2013-01-24 20:12:17 +01:00
param . isAdvanced = True
self . addParameter ( param )
2013-10-01 20:52:22 +03:00
param = ParameterNumber ( self . GRASS_MIN_AREA_PARAMETER ,
' v.in.ogr min area ' , 0 , None , 0.0001 )
2013-01-24 20:12:17 +01:00
param . isAdvanced = True
self . addParameter ( param )
2013-05-15 20:42:04 +02:00
if vectorOutputs == 1 :
2013-10-01 20:52:22 +03:00
param = ParameterSelection ( self . GRASS_OUTPUT_TYPE_PARAMETER ,
' v.out.ogr output type ' ,
self . OUTPUT_TYPES )
2013-05-02 23:53:03 +02:00
param . isAdvanced = True
self . addParameter ( param )
2013-05-15 20:42:04 +02:00
2012-10-17 22:03:40 +02: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 :
2013-09-12 13:19:00 +02:00
layer = dataobjects . getObjectFromUri ( param . value )
2013-10-01 20:52:22 +03:00
cellsize = max ( cellsize , ( layer . extent ( ) . xMaximum ( )
- layer . extent ( ) . xMinimum ( ) )
/ layer . width ( ) )
2012-10-17 22:03:40 +02:00
elif isinstance ( param , ParameterMultipleInput ) :
2013-10-01 20:52:22 +03:00
layers = param . value . split ( ' ; ' )
2012-10-17 22:03:40 +02:00
for layername in layers :
2013-09-12 13:19:00 +02:00
layer = dataobjects . getObjectFromUri ( layername )
2012-10-17 22:03:40 +02:00
if isinstance ( layer , QgsRasterLayer ) :
2013-10-01 20:52:22 +03:00
cellsize = max ( cellsize ,
( layer . extent ( ) . xMaximum ( )
- layer . extent ( ) . xMinimum ( ) )
/ layer . width ( ) )
2012-10-17 22:03:40 +02:00
if cellsize == 0 :
cellsize = 1
2012-12-10 00:12:07 +01:00
return cellsize
2012-09-15 18:25:25 +03:00
def processAlgorithm ( self , progress ) :
2013-10-01 20:52:22 +03:00
if system . isWindows ( ) :
2012-09-15 18:25:25 +03:00
path = GrassUtils . grassPath ( )
2013-10-01 20:52:22 +03:00
if path == ' ' :
raise GeoAlgorithmExecutionException ( ' GRASS folder is not \
configured . \nPlease configure it before running GRASS \
algorithms . ' )
2012-09-15 18:25:25 +03:00
commands = [ ]
self . exportedLayers = { }
2012-12-04 00:15:03 +01:00
outputCommands = [ ]
2012-09-15 18:25:25 +03:00
2013-10-01 20:52:22 +03:00
# If GRASS session has been created outside of this algorithm then
# get the list of layers loaded in GRASS otherwise start a new
# session
2012-10-27 10:59:23 +02:00
existingSession = GrassUtils . sessionRunning
if existingSession :
self . exportedLayers = GrassUtils . getSessionLayers ( )
else :
GrassUtils . startGrassSession ( )
2012-12-10 00:12:07 +01:00
2013-10-01 20:52:22 +03:00
# 1: Export layer to grass mapset
2012-12-10 00:12:07 +01:00
2012-09-15 18:25:25 +03:00
for param in self . parameters :
if isinstance ( param , ParameterRaster ) :
2013-10-01 20:52:22 +03:00
if param . value is None :
2012-09-15 18:25:25 +03:00
continue
value = param . value
2013-10-01 20:52:22 +03:00
# Check if the layer hasn't already been exported in, for
# example, previous GRASS calls in this session
2012-10-27 10:59:23 +02:00
if value in self . exportedLayers . keys ( ) :
continue
else :
2012-12-20 00:16:05 +01:00
self . setSessionProjectionFromLayer ( value , commands )
2012-10-27 10:59:23 +02:00
commands . append ( self . exportRasterLayer ( value ) )
2012-09-15 18:25:25 +03:00
if isinstance ( param , ParameterVector ) :
2013-10-01 20:52:22 +03:00
if param . value is None :
2012-09-15 18:25:25 +03:00
continue
value = param . value
2012-10-27 10:59:23 +02:00
if value in self . exportedLayers . keys ( ) :
continue
else :
2012-12-20 00:16:05 +01:00
self . setSessionProjectionFromLayer ( value , commands )
2012-10-27 10:59:23 +02:00
commands . append ( self . exportVectorLayer ( value ) )
2012-09-15 18:25:25 +03:00
if isinstance ( param , ParameterTable ) :
pass
if isinstance ( param , ParameterMultipleInput ) :
2013-10-01 20:52:22 +03:00
if param . value is None :
2012-09-15 18:25:25 +03:00
continue
2013-10-01 20:52:22 +03:00
layers = param . value . split ( ' ; ' )
if layers is None or len ( layers ) == 0 :
2012-09-15 18:25:25 +03:00
continue
if param . datatype == ParameterMultipleInput . TYPE_RASTER :
for layer in layers :
2012-10-27 10:59:23 +02:00
if layer in self . exportedLayers . keys ( ) :
continue
else :
2012-12-20 00:16:05 +01:00
self . setSessionProjectionFromLayer ( layer , commands )
2012-10-27 10:59:23 +02:00
commands . append ( self . exportRasterLayer ( layer ) )
2012-09-15 18:25:25 +03:00
elif param . datatype == ParameterMultipleInput . TYPE_VECTOR_ANY :
for layer in layers :
2012-10-27 10:59:23 +02:00
if layer in self . exportedLayers . keys ( ) :
continue
else :
2012-12-20 00:16:05 +01:00
self . setSessionProjectionFromLayer ( layer , commands )
2012-10-27 10:59:23 +02:00
commands . append ( self . exportVectorLayer ( layer ) )
2012-09-15 18:25:25 +03:00
2012-12-20 00:16:05 +01:00
self . setSessionProjectionFromProject ( commands )
2013-01-12 23:36:00 +01:00
2013-10-01 20:52:22 +03:00
region = \
str ( self . getParameterValue ( self . GRASS_REGION_EXTENT_PARAMETER ) )
regionCoords = region . split ( ' , ' )
command = ' g.region '
command + = ' n= ' + str ( regionCoords [ 3 ] )
command + = ' s= ' + str ( regionCoords [ 2 ] )
command + = ' e= ' + str ( regionCoords [ 1 ] )
command + = ' w= ' + str ( regionCoords [ 0 ] )
2012-12-12 00:47:29 +01:00
cellsize = self . getParameterValue ( self . GRASS_REGION_CELLSIZE_PARAMETER )
if cellsize :
2013-10-01 20:52:22 +03:00
command + = ' res= ' + str ( cellsize )
2012-12-04 00:15:03 +01:00
else :
2013-10-01 20:52:22 +03:00
command + = ' res= ' + str ( self . getDefaultCellsize ( ) )
alignToResolution = \
self . getParameterValue ( self . GRASS_REGION_ALIGN_TO_RESOLUTION )
2013-05-02 23:53:03 +02:00
if alignToResolution :
2013-10-01 20:52:22 +03:00
command + = ' -a '
2012-12-04 00:15:03 +01:00
commands . append ( command )
2012-12-10 00:12:07 +01:00
2013-10-01 20:52:22 +03:00
# 2: Set parameters and outputs
2012-09-15 18:25:25 +03:00
command = self . grassName
for param in self . parameters :
2013-10-01 20:52:22 +03:00
if param . value is None or param . value == ' ' :
2012-09-15 18:25:25 +03:00
continue
2013-10-01 20:52:22 +03:00
if param . name == self . GRASS_REGION_CELLSIZE_PARAMETER \
or param . name == self . GRASS_REGION_EXTENT_PARAMETER \
or param . name == self . GRASS_MIN_AREA_PARAMETER or param . name \
== self . GRASS_SNAP_TOLERANCE_PARAMETER or param . name \
== self . GRASS_OUTPUT_TYPE_PARAMETER or param . name \
== self . GRASS_REGION_ALIGN_TO_RESOLUTION :
2012-09-15 18:25:25 +03:00
continue
if isinstance ( param , ( ParameterRaster , ParameterVector ) ) :
value = param . value
if value in self . exportedLayers . keys ( ) :
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name + ' = ' \
+ self . exportedLayers [ value ]
2012-09-15 18:25:25 +03:00
else :
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name + ' = ' + value
2012-09-15 18:25:25 +03:00
elif isinstance ( param , ParameterMultipleInput ) :
s = param . value
for layer in self . exportedLayers . keys ( ) :
s = s . replace ( layer , self . exportedLayers [ layer ] )
2013-10-01 20:52:22 +03:00
s = s . replace ( ' ; ' , ' , ' )
command + = ' ' + param . name + ' = ' + s
2012-09-15 18:25:25 +03:00
elif isinstance ( param , ParameterBoolean ) :
if param . value :
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name
2012-09-15 18:25:25 +03:00
elif isinstance ( param , ParameterSelection ) :
idx = int ( param . value )
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name + ' = ' + str ( param . options [ idx ] )
2013-02-07 23:28:04 +01:00
elif isinstance ( param , ParameterString ) :
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name + ' = " ' + str ( param . value ) + ' " '
2012-09-15 18:25:25 +03:00
else :
2013-10-01 20:52:22 +03:00
command + = ' ' + param . name + ' = ' + str ( param . value )
2012-09-15 18:25:25 +03:00
2013-10-01 20:52:22 +03:00
uniqueSufix = str ( uuid . uuid4 ( ) ) . replace ( ' - ' , ' ' )
2012-09-15 18:25:25 +03:00
for out in self . outputs :
if isinstance ( out , OutputFile ) :
2013-04-11 10:04:33 +02:00
if out . name == ' outputtext ' :
2013-10-01 20:52:22 +03:00
# The 'outputtext' file is generated by piping output
# from GRASS, is not an actual grass command
command + = ' > ' + out . value
2013-04-11 10:04:33 +02:00
else :
2013-10-01 20:52:22 +03:00
command + = ' ' + out . name + ' = " ' + out . value + ' " '
2013-04-15 07:16:20 +02:00
elif not isinstance ( out , OutputHTML ) :
2013-10-01 20:52:22 +03:00
# Html files are not generated by GRASS, only by us to
# decorate GRASS output, so we skip them. An output name
# to make sure it is unique if the session uses this
# algorithm several times.
2012-12-10 00:12:07 +01:00
uniqueOutputName = out . name + uniqueSufix
2013-10-01 20:52:22 +03:00
command + = ' ' + out . name + ' = ' + uniqueOutputName
2012-12-10 00:12:07 +01:00
2013-10-01 20:52:22 +03:00
# Add output file to exported layers, to indicate that
# they are present in GRASS
self . exportedLayers [ out . value ] = uniqueOutputName
2012-09-15 18:25:25 +03:00
2013-10-01 20:52:22 +03:00
command + = ' --overwrite '
2012-09-15 18:25:25 +03:00
commands . append ( command )
2013-10-01 20:52:22 +03:00
# 3: Export resulting layers to a format that qgis can read
2012-09-15 18:25:25 +03:00
for out in self . outputs :
if isinstance ( out , OutputRaster ) :
filename = out . value
2013-10-01 20:52:22 +03:00
# Raster layer output: adjust region to layer before
# exporting
commands . append ( ' g.region rast= ' + out . name + uniqueSufix )
outputCommands . append ( ' g.region rast= ' + out . name
+ uniqueSufix )
command = ' r.out.gdal -c createopt= " TFW=YES,COMPRESS=LZW " '
command + = ' input= '
2012-11-15 00:53:46 +01:00
command + = out . name + uniqueSufix
2013-10-01 20:52:22 +03:00
command + = ' output= " ' + filename + ' " '
2012-12-10 00:12:07 +01:00
commands . append ( command )
2012-12-04 00:15:03 +01:00
outputCommands . append ( command )
2012-12-10 00:12:07 +01:00
2012-09-15 18:25:25 +03:00
if isinstance ( out , OutputVector ) :
2012-11-02 16:03:29 +01:00
filename = out . value
2013-10-01 20:52:22 +03:00
command = ' v.out.ogr -c -e input= ' + out . name + uniqueSufix
command + = ' dsn= " ' + os . path . dirname ( out . value ) + ' " '
command + = ' format=ESRI_Shapefile '
command + = ' olayer= ' + os . path . basename ( out . value ) [ : - 4 ]
typeidx = \
self . getParameterValue ( self . GRASS_OUTPUT_TYPE_PARAMETER )
outtype = ( ' auto ' if typeidx
is None else self . OUTPUT_TYPES [ typeidx ] )
command + = ' type= ' + outtype
2012-09-15 18:25:25 +03:00
commands . append ( command )
2012-12-07 21:11:56 +01:00
outputCommands . append ( command )
2012-12-10 00:12:07 +01:00
2013-10-01 20:52:22 +03:00
# 4: Run GRASS
2012-09-15 18:25:25 +03:00
loglines = [ ]
2013-10-01 20:52:22 +03:00
loglines . append ( ' GRASS execution commands ' )
2012-09-15 18:25:25 +03:00
for line in commands :
progress . setCommand ( line )
loglines . append ( line )
2013-08-12 20:44:27 +02:00
if ProcessingConfig . getSetting ( GrassUtils . GRASS_LOG_COMMANDS ) :
ProcessingLog . addToLog ( ProcessingLog . LOG_INFO , loglines )
2013-10-01 20:52:22 +03:00
self . consoleOutput = GrassUtils . executeGrass ( commands , progress ,
outputCommands )
self . postProcessResults ( )
# If the session has been created outside of this algorithm, add
# the new GRASS layers to it otherwise finish the session
2012-10-27 10:59:23 +02:00
if existingSession :
GrassUtils . addSessionLayers ( self . exportedLayers )
2012-12-10 00:12:07 +01:00
else :
2012-10-27 10:59:23 +02:00
GrassUtils . endGrassSession ( )
2012-09-15 18:25:25 +03:00
2013-01-12 23:36:00 +01:00
def postProcessResults ( self ) :
2013-10-01 20:52:22 +03:00
name = self . commandLineName ( ) . replace ( ' . ' , ' _ ' ) [ len ( ' grass: ' ) : ]
2013-01-12 23:36:00 +01:00
try :
2013-08-12 20:44:27 +02:00
module = importlib . import_module ( ' processing.grass.ext. ' + name )
2013-01-12 23:36:00 +01:00
except ImportError :
2012-12-04 00:15:03 +01:00
return
2013-01-12 23:36:00 +01:00
if hasattr ( module , ' postProcessResults ' ) :
2013-10-01 20:52:22 +03:00
func = getattr ( module , ' postProcessResults ' )
2012-12-10 00:12:07 +01:00
func ( self )
2012-12-04 00:15:03 +01:00
2012-09-15 18:25:25 +03:00
def exportVectorLayer ( self , orgFilename ) :
2013-10-01 20:52:22 +03:00
# TODO: improve this. We are now exporting if it is not a shapefile,
# but the functionality of v.in.ogr could be used for this.
# We also export if there is a selection
if not os . path . exists ( orgFilename ) or not orgFilename . endswith ( ' shp ' ) :
2013-09-12 13:19:00 +02:00
layer = dataobjects . getObjectFromUri ( orgFilename , False )
2012-09-15 18:25:25 +03:00
if layer :
2013-09-12 13:19:00 +02:00
filename = dataobjects . exportVectorLayer ( layer )
2012-09-15 18:25:25 +03:00
else :
2013-09-12 13:19:00 +02:00
layer = dataobjects . getObjectFromUri ( orgFilename , False )
2012-09-15 18:25:25 +03:00
if layer :
2013-10-01 20:52:22 +03:00
useSelection = \
ProcessingConfig . getSetting ( ProcessingConfig . USE_SELECTED )
2012-09-15 18:25:25 +03:00
if useSelection and layer . selectedFeatureCount ( ) != 0 :
2013-09-12 13:19:00 +02:00
filename = dataobjects . exportVectorLayer ( layer )
2012-09-15 18:25:25 +03:00
else :
filename = orgFilename
else :
filename = orgFilename
destFilename = self . getTempFilename ( )
2013-10-01 20:52:22 +03:00
self . exportedLayers [ orgFilename ] = destFilename
command = ' v.in.ogr '
min_area = self . getParameterValue ( self . GRASS_MIN_AREA_PARAMETER )
command + = ' min_area= ' + str ( min_area )
snap = self . getParameterValue ( self . GRASS_SNAP_TOLERANCE_PARAMETER )
command + = ' snap= ' + str ( snap )
command + = ' dsn= " ' + os . path . dirname ( filename ) + ' " '
command + = ' layer= ' + os . path . basename ( filename ) [ : - 4 ]
command + = ' output= ' + destFilename
command + = ' --overwrite -o '
2012-09-15 18:25:25 +03:00
return command
2012-12-20 00:16:05 +01:00
def setSessionProjectionFromProject ( self , commands ) :
2013-09-17 12:33:57 +02:00
if not GrassUtils . projectionSet :
2013-09-12 13:19:00 +02:00
proj4 = interface . iface . mapCanvas ( ) . mapRenderer ( ) . destinationCrs ( ) . toProj4 ( )
2013-10-01 20:52:22 +03:00
command = ' g.proj '
command + = ' -c '
command + = ' proj4= " ' + proj4 + ' " '
2012-12-20 00:16:05 +01:00
commands . append ( command )
GrassUtils . projectionSet = True
def setSessionProjectionFromLayer ( self , layer , commands ) :
2012-12-04 00:15:03 +01:00
if not GrassUtils . projectionSet :
2013-09-12 13:19:00 +02:00
qGisLayer = dataobjects . getObjectFromUri ( layer )
2012-12-04 00:15:03 +01:00
if qGisLayer :
proj4 = str ( qGisLayer . crs ( ) . toProj4 ( ) )
2013-10-01 20:52:22 +03:00
command = ' g.proj '
command + = ' -c '
command + = ' proj4= " ' + proj4 + ' " '
2012-12-04 00:15:03 +01:00
commands . append ( command )
GrassUtils . projectionSet = True
2012-09-15 18:25:25 +03:00
def exportRasterLayer ( self , layer ) :
destFilename = self . getTempFilename ( )
2013-10-01 20:52:22 +03:00
self . exportedLayers [ layer ] = destFilename
command = ' r.external '
command + = ' input= " ' + layer + ' " '
command + = ' band=1 '
command + = ' output= ' + destFilename
2013-10-05 21:58:08 +01:00
command + = ' --overwrite -o -r '
2012-09-15 18:25:25 +03:00
return command
def getTempFilename ( self ) :
2013-10-01 20:52:22 +03:00
filename = ' tmp ' + str ( time . time ( ) ) . replace ( ' . ' , ' ' ) \
+ str ( system . getNumExportedLayers ( ) )
2012-09-15 18:25:25 +03:00
return filename
def commandLineName ( self ) :
2013-10-01 20:52:22 +03:00
return ' grass: ' + self . name [ : self . name . find ( ' ' ) ]
2013-04-15 07:16:20 +02:00
2013-04-12 17:18:51 +02:00
def checkBeforeOpeningParametersDialog ( self ) :
msg = GrassUtils . checkGrassIsInstalled ( )
2013-04-15 07:16:20 +02:00
if msg is not None :
2013-10-01 20:52:22 +03:00
html = ' <p>This algorithm requires GRASS to be run. \
Unfortunately , it seems that GRASS is not installed in \
your system , or it is not correctly configured to be used \
from QGIS < / p > '
html + = ' <p><a href= " http://docs.qgis.org/2.0/html/en/docs/user_manual/processing/3rdParty.html " >Click here</a> to know more about how to install and configure GRASS to be used with QGIS</p> '
2013-04-12 17:18:51 +02:00
return html
2012-12-13 22:28:35 +01:00
def checkParameterValuesBeforeExecuting ( self ) :
2013-10-01 20:52:22 +03:00
name = self . commandLineName ( ) . replace ( ' . ' , ' _ ' ) [ len ( ' grass: ' ) : ]
2013-01-12 23:36:00 +01:00
try :
2013-08-12 20:44:27 +02:00
module = importlib . import_module ( ' processing.grass.ext. ' + name )
2013-01-12 23:36:00 +01:00
except ImportError :
2012-12-13 22:28:35 +01:00
return
2013-01-12 23:36:00 +01:00
if hasattr ( module , ' checkParameterValuesBeforeExecuting ' ) :
2013-10-01 20:52:22 +03:00
func = getattr ( module , ' checkParameterValuesBeforeExecuting ' )
2012-12-13 22:28:35 +01:00
return func ( self )
2012-10-27 10:59:23 +02:00
2013-07-18 17:16:16 +02:00
def getPostProcessingErrorMessage ( self , wrongLayers ) :
html = GeoAlgorithm . getPostProcessingErrorMessage ( self , wrongLayers )
msg = GrassUtils . checkGrassIsInstalled ( True )
2013-10-01 20:52:22 +03:00
html + = ' <p>This algorithm requires GRASS to be run. A test \
to check if GRASS is correctly installed and configured in \
your system has been performed , with the following \
result : < / p > < ul > < i > '
2013-07-18 17:16:16 +02:00
if msg is None :
2013-10-01 20:52:22 +03:00
html + = ' GRASS seems to be correctly installed and \
configured < / i > < / li > < / ul > '
2013-07-18 17:16:16 +02:00
else :
2013-10-01 20:52:22 +03:00
html + = msg + ' </i></li></ul> '
2013-08-12 20:44:27 +02:00
html + = ' <p><a href= " http://docs.qgis.org/2.0/html/en/docs/user_manual/processing/3rdParty.html " >Click here</a> to know more about how to install and configure GRASS to be used with QGIS</p> '
2013-07-18 17:16:16 +02:00
return html