1

I am writing a Python script which uses a vector polygon outline to construct a raster map of a hypothetical 'mound'. Finally, the script will output the volume of this mound using r.volume.

The problem is that the r.volume calculation is resulting in an error. I can't get much info on the error but when I run the volume command in terminal I get no output other than:

:~> Floating point exception: 8

If I run the volume command using python I get:

grass.run_command("r.volume", input=rounded, clump='clumped')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/GRASS-7.0.app/Contents/MacOS/etc/python/grass/script/core.py", line 395, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "/Applications/GRASS-7.0.app/Contents/MacOS/etc/python/grass/script/core.py", line 313, in handle_errors
    returncode=returncode)
CalledModuleError: Module run None ['r.volume', 'input=ditchpoly_area_comp_int_round', 'clump=clumped'] ended with error
Process ended with non-zero return code -8. See errors in the (error) output.

Below is the annotated python script that I used. If you want to reproduce the situation then I suggest drawing your own 'mound' shape and then inputting the name of this layer as the first python object and running the script.

import grass.script as grass
import os
import numpy as np

# name of your mound polygon shape layer
geooutline = "ditchpoly_area"

############ mound particulars ############

# distance from edge of polygon to foot of mound
erosion = float(-1.5)

# horizontal distance from mound base to mound peak
baseoffset = float(-3.00)

# height of mound
depth = float(3.30)

# rounding value for mound raster
rastrounding = float(0.1)

# names of mound objects
erooutline = geooutline + "_ero"
erooutlinerast = geooutline + "_ero_rast"
baseoutline = geooutline + "_base"
baseoutlinerast = geooutline + "_base_rast"
surface = geooutline + "_surface"
ditchbase = geooutline + "_ditchbase"
comp = geooutline + "_comp"
comp_int = geooutline + "_comp_int"
rounded = geooutline + "_comp_int_round"
ditchonly = geooutline + "_ditchonly"

############ grass commands ############ 

# set region
grass.run_command("g.region", flags="a", vectory=geooutline, nsres=1, ewres=1)

# create buffer outlines
grass.run_command("v.buffer", overwrite=True, input=geooutline, output=erooutline, distance=erosion)
grass.run_command("v.buffer", overwrite=True, input=erooutline, output=baseoutline, distance=baseoffset)

# prepare surface raster
grass.run_command("v.to.rast", overwrite=True, input=erooutline, output=erooutlinerast, use='cat')
formula = '"%s" = if(isnull("%s"), 0,null())' % (surface, erooutlinerast)
grass.raster.mapcalc(formula, overwrite=True)

# prepare ditch base
grass.run_command("v.to.rast", overwrite=True, input=baseoutline, output=baseoutlinerast, use='cat')
formula = '{0}" = if({1}" == 1,"{2}",null())' .format(ditchbase, baseoutlinerast, depth)
formula = formula.replace('"', '')
grass.raster.mapcalc(formula, overwrite=True)

# patch composite raster
compformula = surface + ',' + ditchbase
grass.run_command("r.patch", overwrite=True, input=compformula, output=comp)

# fill nulls interpolate ditch profile
grass.run_command("r.fillnulls", overwrite=True, input=comp, output=comp_int, method='rst')

# round values
formula = '"{0}" = float(round("{1}","{2}"))' .format(rounded, comp_int, rastrounding)
formula = formula.replace('"', '')
grass.raster.mapcalc(formula, overwrite=True)

# extract ditch
formula = '"{0}" = if("{1}" == 0,null(),"{2}")' .format(ditchonly, comp_int, rounded)
formula = formula.replace('"', '')
grass.raster.mapcalc(formula, overwrite=True)

# calculate volume
grass.run_command("r.volume", input=rounded, clump=erooutlinerast)

############  remove old files ############ 

#clear old
grass.run_command("g.remove", flags="f", type='all', pattern=geooutline + "_*")
  • If you test it try with a few different shapes and different offset and depth values - it's very temperamental – Beeman Oct 20 '16 at 12:54
  • Just to add - I recently ran the script once resulting in a successful calculation, then when I ran it again (nothing changed) I got the error described above. Possible bug? – Beeman Oct 24 '16 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.