0

I want to run the GRASS addon r.skyview from an external Python script. I have managed to run other GRASS commands, such as r.horizon (see code below),

import os
import sys
import subprocess

sys.path.append(os.path.abspath('PATH/AppData/Roaming/GRASS7/addons/'))

grass7bin = r'C:\OSGeo4W\bin\grass78.bat'
startcmd = [grass7bin, '--config', 'path']
try:
    p = subprocess.Popen(startcmd, shell=False,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         universal_newlines=True)
    out, err = p.communicate()
except OSError as error:
    sys.exit("ERROR: Cannot find GRASS GIS start script"
             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
if p.returncode != 0:
    sys.exit("ERROR: Issues running GRASS GIS start script"
             " {cmd}: {error}"
             .format(cmd=' '.join(startcmd), error=err))
gisbase = out.strip(os.linesep)

os.environ['GISBASE'] = gisbase
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

import grass.script.setup as gsetup

gisdb = os.path.join(os.path.expanduser("~"), "Documents\grassdata")
location = "LOCATION"
mapset = "PERMANENT"

rcfile = gsetup.init(gisbase, gisdb, location, mapset)

import grass.script as gscript

gisdbtmp = os.getcwd()

inputFile = 'DEM_Buildings.tif'
inputFile_path  = os.path.join(gisdbtmp, inputFile)

from qgis.core import QgsApplication
from qgis.core import QgsRasterLayer
import processing
from PyQt5.QtCore import QFileInfo
rasterPath="PATH/DEM_Buildings.tif"
fileInfo=QFileInfo(rasterPath)
baseName=fileInfo.baseName()
rLayer = QgsRasterLayer(fileInfo.filePath(), baseName)

outputFile = 'DEM_Buildings_out.tif'
outputFile_path = os.path.join(gisdbtmp,outputFile)
fileInfoi=QFileInfo(outputFile_path)
baseNameOut=fileInfoi.baseName()


# Define extent
extent=rLayer.extent()
xmin=extent.xMinimum()
xmax=extent.xMaximum()
ymin=extent.yMinimum()
ymax=extent.yMaximum()

extStr="%f,%f,%f,%f" % (xmin, xmax, ymin, ymax)

gscript.run_command('g.region',raster=baseName)

import processing
from processing.core.Processing import Processing
Processing.initialize()

from grass.pygrass.modules import Module

from grass.exceptions import CalledModuleError
import grass.script.core as gcore
import grass.script.raster as grast
from grass.pygrass.messages import get_msgr
import csv
import atexit

horizon_step = 360.0 / 90
ndir_inp = 90

gcore.run_command(
            "r.horizon",
            elevation=baseName,
            step=horizon_step,
            output=baseNameOut,
            flags="d",
        )

However, if I try to call r.skyview instead of r.horizon (see code below), it does not work,

gcore.run_command(
            "r.skyview",
            input=baseName,
            output=baseNameOut,
            ndir=ndir_inp,
        )

OSError: Cannot find the executable r.skyview

PS. r.skyview is installed, enter image description here

0 Answers0