[GRASS-SVN] r70739 - grass-addons/grass7/raster/r.lfp
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Mar 10 21:06:28 PST 2017
Author: hcho
Date: 2017-03-10 21:06:28 -0800 (Fri, 10 Mar 2017)
New Revision: 70739
Modified:
grass-addons/grass7/raster/r.lfp/r.lfp.py
Log:
r.lfp: thin output map; write metadata
Modified: grass-addons/grass7/raster/r.lfp/r.lfp.py
===================================================================
--- grass-addons/grass7/raster/r.lfp/r.lfp.py 2017-03-11 04:07:29 UTC (rev 70738)
+++ grass-addons/grass7/raster/r.lfp/r.lfp.py 2017-03-11 05:06:28 UTC (rev 70739)
@@ -58,6 +58,7 @@
def calculate_lfp(input, output, coords):
prefix = "r_lfp_%d_" % os.getpid()
+ # create the outlet vector map
outlet = prefix + "outlet"
p = grass.feed_command("v.in.ascii", overwrite=True,
input="-", output=outlet, separator=",")
@@ -65,37 +66,42 @@
p.stdin.close()
p.wait()
if p.returncode != 0:
- grass.fatal(_("Cannot create outlet vector map"))
+ grass.fatal(_("Cannot create the outlet vector map"))
+ # convert the outlet vector map to raster
try:
grass.run_command("v.to.rast", overwrite=True,
input=outlet, output=outlet, use="cat", type="point")
except CalledModuleError:
- grass.fatal(_("Cannot convert outlet vector to raster"))
+ grass.fatal(_("Cannot convert the outlet vector to raster"))
+ # calculate the downstream flow length
flds = prefix + "flds"
try:
grass.run_command("r.stream.distance", overwrite=True, flags="om",
- stream_rast=outlet, direction=input, method="downstream",
- distance=flds)
+ stream_rast=outlet, direction=input,
+ method="downstream", distance=flds)
except CalledModuleError:
- grass.fatal(_("Cannot create flow length downstream raster map"))
+ grass.fatal(_("Cannot calculate the downstream flow length"))
+ # calculate the upstream flow length
flus = prefix + "flus"
try:
grass.run_command("r.stream.distance", overwrite=True, flags="o",
- stream_rast=outlet, direction=input, method="upstream",
- distance=flus)
+ stream_rast=outlet, direction=input,
+ method="upstream", distance=flus)
except CalledModuleError:
- grass.fatal(_("Cannot create flow length upstream raster map"))
+ grass.fatal(_("Cannot calculate the upstream flow length"))
+ # calculate the sum of downstream and upstream flow lengths
fldsus = prefix + "fldsus"
try:
grass.run_command("r.mapcalc", overwrite=True,
expression="%s=%s+%s" % (fldsus, flds, flus))
except CalledModuleError:
- grass.fatal(_("Cannot create flds+flus raster map"))
+ grass.fatal(_("Cannot calculate the sum of downstream and upstream flow lengths"))
+ # find the longest flow length
p = grass.pipe_command("r.info", flags="r", map=fldsus)
max = ""
for line in p.stdout:
@@ -105,19 +111,39 @@
break
p.wait()
if p.returncode != 0 or max == "":
- grass.fatal(_("Cannot find max flds+flus cell value"))
+ grass.fatal(_("Cannot find the longest flow length"))
min = float(max) - 0.0005
+ # extract the longest flow path
+ lfp = prefix + "lfp"
try:
- grass.run_command("r.mapcalc",
- expression="%s=if(%s>=%f, 1, null())" % (output, fldsus, min))
+ grass.run_command("r.mapcalc", overwrite=True,
+ expression="%s=if(%s>=%f, 1, null())" %
+ (lfp, fldsus, min))
except CalledModuleError:
- grass.fatal(_("Cannot create longest flow path raster map"))
+ grass.fatal(_("Cannot create the longest flow path raster map"))
- grass.run_command("g.remove", flags="f",
- type="raster,vector", pattern="%s*" % prefix)
+ # thin the longest flow path raster map
+ try:
+ grass.run_command("r.thin", input=lfp, output=output)
+ except CalledModuleError:
+ grass.fatal(_("Cannot thin the longest flow path raster map"))
+ # remove intermediate outputs
+ grass.run_command("g.remove", flags="f", type="raster,vector",
+ pattern="%s*" % prefix)
+
+ # write history
+ tmphist = grass.tempfile()
+ f = open(tmphist, "w+")
+ f.write(os.environ["CMDLINE"])
+ f.close()
+
+ grass.run_command("r.support", map=output, title="Longest flow path",
+ loadhistory=tmphist, description="generated by r.lfp")
+ grass.try_remove(tmphist)
+
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())
More information about the grass-commit
mailing list