Prechádzať zdrojové kódy

scripts/i.spectral: show error message if one of the supported monitor isn't running (#1991)

* scripts/i.spectral: show error message if one of the supported monitor isn't running

By default, d.linegraph is used internally to create a graph image
which requires the launch of one of the supported monitor (cairo, png, ps).

Co-authored-by: Markus Neteler <neteler@osgeo.org>
Tomas Zigo 3 rokov pred
rodič
commit
960133e8f6

+ 12 - 7
scripts/i.spectral/i.spectral.html

@@ -1,11 +1,11 @@
 <h2>DESCRIPTION</h2>
 
-<em>i.spectral</em> displays spectral response at user specified 
+<em>i.spectral</em> displays spectral response at user specified
 locations in images.
 
 <h2>NOTES</h2>
 
-This script needs gnuplot to be installed.
+This script requires the launch of one of the supported monitors (cairo, png, ps) or gnuplot to be installed.
 
 
 <h2>EXAMPLE</h2>
@@ -14,7 +14,7 @@ Analysis of LANDSAT TM7 channels (North Carolina dataset):
 
 <div class="code"><pre>
 g.region raster=lsat7_2002_10 -p
-i.spectral -g input=lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40,lsat7_2002_50,lsat7_2002_70 \
+i.spectral -g raster=lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40,lsat7_2002_50,lsat7_2002_70 \
            coordinates=636069,215440,637958,223393,633277,223605
 </pre></div>
 
@@ -30,10 +30,12 @@ North Carolina sample dataset:
 <div class="code"><pre>
 g.region raster=lsat7_2002_10 -p
 LIST=`g.list type=raster pattern="lsat7_2002_[1-5,7]0" separator=","`
-i.spectral input=$LIST coordinates=637502.25,221744.25
+d.mon start=cairo output=plot.png
+i.spectral raster=$LIST coordinates=637502.25,221744.25
+d.mon stop=cairo
 </pre></div>
 
-This will search all LANDSAT map for 2002 but select only the B, G, R, 
+This will search all LANDSAT map for 2002 but select only the B, G, R,
 NIR, and MIR channels.
 
 
@@ -41,10 +43,13 @@ NIR, and MIR channels.
 
 <em>
 <a href="d.where.html">d.where</a>,
-<a href="r.what.html">r.what</a>
+<a href="r.what.html">r.what</a>,
+<a href="cairodriver.html">Cairo driver</a>,
+<a href="pngdriver.html">PNG driver</a>,
+<a href="psdriver.html">PS driver (Postscript)</a>
 </em>
 
-<h2>AUTHOR</h2>
+<h2>AUTHORS</h2>
 
 Markus Neteler<br>
 Francesco Pirotti

+ 97 - 55
scripts/i.spectral/i.spectral.py

@@ -25,58 +25,58 @@
 # 3 March 2006: Added multiple images and group support by Francesco Pirotti - CIRGEO
 #
 
-#%Module
-#% description: Displays spectral response at user specified locations in group or images.
-#% keyword: imagery
-#% keyword: querying
-#% keyword: raster
-#% keyword: multispectral
-#%End
-#%option G_OPT_I_GROUP
-#% required : no
-#% guisection: Input
-#%end
-#%option G_OPT_I_SUBGROUP
-#% required : no
-#% guisection: Input
-#%end
-#%option G_OPT_R_INPUTS
-#% key: raster
-#% required : no
-#% guisection: Input
-#%end
-#%option G_OPT_M_COORDS
-#% multiple: yes
-#% required: yes
-#% guisection: Input
-#%end
-#%option G_OPT_F_OUTPUT
-#% key: output
-#% description: Name for output image (or text file for -t)
-#% guisection: Output
-#% required : no
-#%end
-#%Option
-#% key: format
-#% type: string
-#% description: Graphics format for output file
-#% options: png,eps,svg
-#% answer: png
-#% multiple: no
-#% guisection: Output
-#%End
-#%flag
-#% key: c
-#% description: Show sampling coordinates instead of numbering in the legend
-#%end
-#% flag
-#% key: g
-#% description: Use gnuplot for display
-#%end
-#% flag
-#% key: t
-#% description: output to text file
-#%end
+# %Module
+# % description: Displays spectral response at user specified locations in group or images.
+# % keyword: imagery
+# % keyword: querying
+# % keyword: raster
+# % keyword: multispectral
+# %End
+# %option G_OPT_I_GROUP
+# % required : no
+# % guisection: Input
+# %end
+# %option G_OPT_I_SUBGROUP
+# % required : no
+# % guisection: Input
+# %end
+# %option G_OPT_R_INPUTS
+# % key: raster
+# % required : no
+# % guisection: Input
+# %end
+# %option G_OPT_M_COORDS
+# % multiple: yes
+# % required: yes
+# % guisection: Input
+# %end
+# %option G_OPT_F_OUTPUT
+# % key: output
+# % description: Name for output image is valid for -g (or text file for -t flag)
+# % guisection: Output
+# % required : no
+# %end
+# %Option
+# % key: format
+# % type: string
+# % description: Graphics format for output file (valid for -g flag)
+# % options: png,eps,svg
+# % answer: png
+# % multiple: no
+# % guisection: Output
+# %End
+# %flag
+# % key: c
+# % description: Show sampling coordinates instead of numbering in the legend (valid for -g flag)
+# %end
+# % flag
+# % key: g
+# % description: Use gnuplot for display
+# %end
+# % flag
+# % key: t
+# % description: output to text file
+# %end
 
 import os
 import atexit
@@ -191,9 +191,51 @@ def draw_linegraph(what):
         colors += gp_colors
     colors = colors[0:len(what)]
 
-    gcore.run_command('d.linegraph', x_file=xfile, y_file=yfiles,
-                      y_color=colors, title=_("Spectral signatures"),
-                      x_title=_("Bands"), y_title=_("DN Value"))
+    supported_monitors = ("cairo", "png", "ps")
+    monitors = gcore.read_command("d.mon", flags="l", quiet=True)
+    found = []
+    for monitor in supported_monitors:
+        if monitor in monitors:
+            found.append(monitor)
+    if not found:
+        gcore.fatal(
+            _(
+                "Supported monitor isn't running. Please launch one of the"
+                " monitors {}.".format(", ".join(supported_monitors))
+            )
+        )
+    selected_monitor = gcore.read_command("d.mon", flags="p", quiet=True).replace(
+        "\n", ""
+    )
+    if selected_monitor not in supported_monitors:
+        gcore.fatal(
+            _(
+                "Supported monitor isn't selected. Please select one of the"
+                " monitors {}.".format(", ".join(supported_monitors))
+            )
+        )
+    with open(gcore.parse_command("d.mon", flags="g", quiet=True)["env"]) as f:
+        for line in f.readlines():
+            if "GRASS_RENDER_FILE=" in line:
+                gcore.info(
+                    _(
+                        "{} monitor is used, output file {}".format(
+                            selected_monitor.capitalize(), line.split("=")[-1]
+                        )
+                    )
+                )
+                break
+
+    gcore.run_command(
+        "d.linegraph",
+        x_file=xfile,
+        y_file=yfiles,
+        y_color=colors,
+        title=_("Spectral signatures"),
+        x_title=_("Bands"),
+        y_title=_("DN Value"),
+    )
+>>>>>>> 79b77ba90 (scripts/i.spectral: show error message if one of the supported monitor isn't running (#1991))
 
 
 def main():