[GRASS-SVN] r64481 - in grass/trunk/temporal: . t.rast.to.vect t.rast.to.vect/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 6 04:49:25 PST 2015


Author: huhabla
Date: 2015-02-06 04:49:25 -0800 (Fri, 06 Feb 2015)
New Revision: 64481

Added:
   grass/trunk/temporal/t.rast.to.vect/
   grass/trunk/temporal/t.rast.to.vect/Makefile
   grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.html
   grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.py
   grass/trunk/temporal/t.rast.to.vect/testsuite/
   grass/trunk/temporal/t.rast.to.vect/testsuite/test_to_vect.py
Log:
temporal modules: New module to convert strds into stvds


Added: grass/trunk/temporal/t.rast.to.vect/Makefile
===================================================================
--- grass/trunk/temporal/t.rast.to.vect/Makefile	                        (rev 0)
+++ grass/trunk/temporal/t.rast.to.vect/Makefile	2015-02-06 12:49:25 UTC (rev 64481)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../../
+
+PGM = t.rast.to.vect
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script $(TEST_DST)

Added: grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.html
===================================================================
--- grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.html	                        (rev 0)
+++ grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.html	2015-02-06 12:49:25 UTC (rev 64481)
@@ -0,0 +1,42 @@
+<h2>DESCRIPTION</h2>
+
+<em>t.rast.to.vect</em> is designed to convert a space time raster dataset
+into a space time vector dataset. This module works as a front-end to
+<a href="r.to.vect.html">r.to.vect</a> and therefore supports all parameter
+of this module. Hence, all raster map layers in a space time raster dataset
+are passed to <a href="r.to.vect.html">r.to.vect</a> that converts them into
+vector map layers (using point,line or area as conversion criteria). 
+Please refer to the <a href="r.to.vect.html">r.to.vect</a> documentation 
+for a detailed description of the raster to vector conversion options.
+The new generated vector map
+layers will be registered in the output space time vector dataset, using 
+the same time stamps as their raster map layer origins.
+<p>
+This module supports the parallel processing of <a href="r.to.vect.html">r.to.vect</a>
+module instances. The number of parallel processes 
+can be set with the <em>nprocs</em> option. 
+However, this will only work in conjunction with the <em>-t</em>
+flag, that avoids the creation of attribute tables. 
+The parallel creation of attribute tables is not supported.
+<p>
+The <em>where</em> option allows to select subsets of the input space time raster
+dataset.
+<p>
+The flag <em>-n</em> can be used to force the registration of empty 
+vector map layers. Empty vector maps may occur in case that empty 
+raster map layers should be converted into vector map layers.
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.to.vect.html">r.to.vect</a>,
+<a href="t.vect.db.select.html">t.rast.db.select</a>,
+<a href="t.info.html">t.info</a>
+</em>
+
+
+<h2>AUTHOR</h2>
+
+Sören Gebbert, Geoinformatikbüro Dassau
+
+<p><i>Last changed: $Date: 2014-12-03 20:39:26 +0100 (Mi, 03. Dez 2014) $</i>

Added: grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.py
===================================================================
--- grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.py	                        (rev 0)
+++ grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.py	2015-02-06 12:49:25 UTC (rev 64481)
@@ -0,0 +1,255 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE:       t.rast.to.vect
+# AUTHOR(S):    Soeren Gebbert
+#
+# PURPOSE:      Converts a space time raster dataset into a space time vector dataset.
+#
+# COPYRIGHT:    (C) 2015 by the GRASS Development Team
+#
+#               This program is free software under the GNU General Public
+#               License (version 2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Converts a space time raster dataset into a space time vector dataset
+#% keyword: temporal
+#% keyword: conversion
+#% keyword: raster
+#% keyword: vector
+#%end
+
+#%option G_OPT_STRDS_INPUT
+#%end
+
+#%option G_OPT_STVDS_OUTPUT
+#%end
+
+#%option G_OPT_T_WHERE
+#%end
+
+#%option
+#% key: type
+#% type: string
+#% description: Output feature type
+#% required: yes
+#% multiple: no
+#% options: point,line,area
+#%end
+
+#%option
+#% key: basename
+#% type: string
+#% label: Basename of the new generated output maps
+#% description: A numerical suffix separated by an underscore will be attached to create a unique identifier
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: column
+#% type: string
+#% description: Name of attribute column to store value
+#% required: no
+#% multiple: no
+#% answer: value
+#%end
+
+#%option
+#% key: nprocs
+#% type: integer
+#% description: Number of r.to.vect processes to run in parallel, more than 1 process works only in conjunction with flag -t
+#% required: no
+#% multiple: no
+#% answer: 1
+#%end
+
+#%flag
+#% key: n
+#% description: Register empty vector maps
+#%end
+
+#%flag
+#% key: t
+#% description: Do not create attribute tables
+#%end
+
+#%flag
+#% key: s
+#% description: Smooth corners of area features
+#%end
+
+#%flag
+#% key: z
+#% label: Write raster values as z coordinate
+#% description: Table is not created. Currently supported only for points.
+#%end
+
+#%flag
+#% key: b
+#% label: Do not build vector topology
+#% description: Name must be SQL compliant
+#%end
+
+#%flag
+#% key: v
+#% description: Use raster values as categories instead of unique sequence (CELL only)
+#%end
+
+import sys
+import copy
+import grass.script as gscript
+import grass.temporal as tgis
+import grass.pygrass.modules as pymod
+
+
+############################################################################
+
+def main(options, flags):
+
+    # Get the options
+    input = options["input"]
+    output = options["output"]
+    where = options["where"]
+    base = options["basename"]
+    method = options["type"]
+    nprocs = int(options["nprocs"])
+    column = options["column"]
+
+    register_null = flags["n"]
+    t_flag = flags["t"]
+    s_flag = flags["s"]
+    v_flag = flags["v"]
+    b_flag = flags["b"]
+    z_flag = flags["z"]
+    
+    # Make sure the temporal database exists
+    tgis.init()
+    # We need a database interface
+    dbif = tgis.SQLDatabaseInterfaceConnection()
+    dbif.connect()
+
+    overwrite = gscript.overwrite()
+
+    sp = tgis.open_old_stds(input, "strds", dbif)
+    maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif)
+
+    if not maps:
+        dbif.close()
+        gscript.warning(_("Space time raster dataset <%s> is empty") % sp.get_id())
+        return
+
+    # Check the new stvds
+    new_sp = tgis.check_new_stds(output, "stvds", dbif=dbif,
+                                 overwrite=overwrite)
+                                               
+    # Setup the flags
+    flags = ""
+    if t_flag is True:
+        flags += "t"
+    if s_flag is True:
+        flags += "s"
+    if v_flag is True:
+        flags += "v"
+    if b_flag is True:
+        flags += "b"
+    if z_flag is True:
+        flags += "z"
+    
+    # Configure the r.to.vect module
+    to_vector_module = pymod.Module("r.to.vect", input="dummy",
+                                   output="dummy", run_=False,
+                                   finish_=False, flags=flags,
+                                   type=method, overwrite=overwrite,
+                                   quiet=True)
+
+    # The module queue for parallel execution, except if attribute tables should
+    # be created. Then force single process use
+    if t_flag is False:
+        if nprocs > 1:
+            nprocs = 1
+            gscript.warning(_("The number of parellel r.to.vect processes was "\
+                               "reduced to 1 because of the table attribute "\
+                               "creation"))
+    process_queue = pymod.ParallelModuleQueue(int(nprocs))
+
+    count = 0
+    num_maps = len(maps)
+    new_maps = []
+
+    # run r.to.vect all selected maps
+    for map in maps:
+        count += 1
+        map_name = "%s_%i" % (base, count)
+        new_map = tgis.open_new_map_dataset(map_name, None, type="vector",
+                                            temporal_extent=map.get_temporal_extent(),
+                                            overwrite=overwrite, dbif=dbif)
+        new_maps.append(new_map)
+
+        mod = copy.deepcopy(to_vector_module)
+        mod(input=map.get_id(), output=new_map.get_id())
+        sys.stderr.write(mod.get_bash() + "\n")
+        process_queue.put(mod)
+
+        if count%10 == 0:
+            gscript.percent(count, num_maps, 1)
+
+    # Wait for unfinished processes
+    process_queue.wait()
+
+    # Open the new space time vector dataset
+    ttype, stype, title, descr = sp.get_initial_values()
+    new_sp = tgis.open_new_stds(output, "stvds", ttype, title,
+                                descr, stype, dbif, overwrite)
+    # collect empty maps to remove them
+    num_maps = len(new_maps)
+    empty_maps = []
+
+    # Register the maps in the database
+    count = 0
+    for map in new_maps:
+        count += 1
+
+        if count%10 == 0:
+            gscript.percent(count, num_maps, 1)
+
+        # Do not register empty maps
+        map.load()
+        if map.metadata.get_number_of_primitives() == 0:
+            if not register_null:
+                empty_maps.append(map)
+                continue
+
+        # Insert map in temporal database
+        map.insert(dbif)
+        new_sp.register_map(map, dbif)
+
+    # Update the spatio-temporal extent and the metadata table entries
+    new_sp.update_from_registered_maps(dbif)
+    gscript.percent(1, 1, 1)
+
+    # Remove empty maps
+    if len(empty_maps) > 0:
+        names = ""
+        count = 0
+        for map in empty_maps:
+            if count == 0:
+                count += 1
+                names += "%s" % (map.get_name())
+            else:
+                names += ",%s" % (map.get_name())
+
+        gscript.run_command("g.remove", flags='f', type='vector', name=names, 
+                            quiet=True)
+
+    dbif.close()
+
+############################################################################
+
+if __name__ == "__main__":
+    options, flags = gscript.parser()
+    main(options, flags)


Property changes on: grass/trunk/temporal/t.rast.to.vect/t.rast.to.vect.py
___________________________________________________________________
Added: svn:executable
   + *

Added: grass/trunk/temporal/t.rast.to.vect/testsuite/test_to_vect.py
===================================================================
--- grass/trunk/temporal/t.rast.to.vect/testsuite/test_to_vect.py	                        (rev 0)
+++ grass/trunk/temporal/t.rast.to.vect/testsuite/test_to_vect.py	2015-02-06 12:49:25 UTC (rev 64481)
@@ -0,0 +1,174 @@
+"""Test t.rast.to.vect
+
+(C) 2014 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+ at author Soeren Gebbert
+"""
+
+import subprocess
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+
+class TestRasterToVector(TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        cls.use_temp_region()
+        cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
+
+        cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_2 = 200", overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_3 = 300", overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_4 = 400", overwrite=True)
+        cls.runModule("r.mapcalc", expression="a_5 = null()", overwrite=True)
+
+        cls.runModule("t.create", type="strds", temporaltype="absolute",  
+                                 output="A", title="A test", description="A test",  
+                                 overwrite=True)
+        cls.runModule("t.register",  flags="i", type="raster", input="A",  
+                                     maps="a_1,a_2,a_3,a_4,a_5", start="2001-01-01", 
+                                     increment="3 months", overwrite=True)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.runModule("t.remove", flags="rf", type="strds",  
+                                   inputs="A")
+        cls.del_temp_region()
+        
+    def tearDown(self):
+        """Remove generated data"""
+        self.runModule("t.remove", flags="rf", type="stvds",  
+                                   inputs="result")
+
+    def test_simple_points(self):
+        self.assertModule("t.rast.to.vect", input="A", output="result", 
+                          type="point", flags="n", column="values",
+                          basename="test",
+                          nprocs=1, overwrite=True, verbose=True)
+
+        #self.assertModule("t.info",  type="stvds", flags="g",  input="result")
+
+        tinfo_string="""start_time=2001-01-01 00:00:00
+                        end_time=2002-04-01 00:00:00
+                        granularity=3 months
+                        map_time=interval
+                        number_of_maps=5
+                        points=384
+                        primitives=384"""
+
+        info = SimpleModule("t.info", flags="g", type="stvds", input="result")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+
+    def test_simple_area(self):
+        self.assertModule("t.rast.to.vect", input="A", output="result", 
+                          type="area", flags="n", column="values",
+                          basename="test",
+                          nprocs=1, overwrite=True, verbose=True)
+                          
+        #self.assertModule("t.info",  type="stvds", flags="g",  input="result")
+        
+        tinfo_string="""start_time=2001-01-01 00:00:00
+                        end_time=2002-04-01 00:00:00
+                        granularity=3 months
+                        map_time=interval
+                        number_of_maps=5
+                        boundaries=4
+                        centroids=4
+                        primitives=8
+                        nodes=4
+                        areas=4
+                        islands=4"""
+
+        info = SimpleModule("t.info", flags="g", type="stvds", input="result")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+
+    def test_simple_area_smooth(self):
+        """Test areas with smooth corners"""
+        self.assertModule("t.rast.to.vect", input="A", output="result", 
+                          type="area", flags="s", column="values",
+                          basename="test",
+                          nprocs=1, overwrite=True, verbose=True)
+                          
+        #self.assertModule("t.info",  type="stvds", flags="g",  input="result")
+        
+        tinfo_string="""start_time=2001-01-01 00:00:00
+                        end_time=2002-01-01 00:00:00
+                        granularity=3 months
+                        map_time=interval
+                        number_of_maps=4
+                        boundaries=4
+                        centroids=4
+                        primitives=8
+                        nodes=4
+                        areas=4
+                        islands=4"""
+
+        info = SimpleModule("t.info", flags="g", type="stvds", input="result")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+
+    def test_parallel(self):
+        self.assertModule("t.rast.to.vect",  input="A", output="result", 
+                          type="point", flags="t", column="values",
+                          basename="test",
+                          nprocs=4, overwrite=True, verbose=True)
+
+        #self.assertModule("t.info",  type="stvds", flags="g",  input="result")
+
+        tinfo_string="""start_time=2001-01-01 00:00:00
+                        end_time=2002-01-01 00:00:00
+                        granularity=3 months
+                        number_of_maps=4
+                        points=384
+                        primitives=384"""
+
+        info = SimpleModule("t.info", flags="g", type="stvds", input="result")
+        self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+
+class TestRasterToVectorFails(TestCase):
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        cls.use_temp_region()
+        cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
+
+        cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True)
+
+        cls.runModule("t.create",  type="strds", temporaltype="absolute",  
+                                 output="A", title="A test", description="A test",  
+                                 overwrite=True)
+        cls.runModule("t.register",  flags="i", type="raster", input="A",  
+                                     maps="a_1", start="2001-01-01", 
+                                     increment="3 months", overwrite=True)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.runModule("t.remove", flags="rf", type="strds",
+                      inputs="A")
+        cls.del_temp_region()
+
+    def test_error_handling(self):
+        # No basename, no type
+        self.assertModuleFail("t.rast.to.vect", input="A", output="result")
+
+
+    def test_empty_strds(self):
+        """Test for empty strds"""
+        self.assertModule("t.rast.to.vect", input="A", output="result", 
+                          type="point", flags="n", column="values",
+                          basename="test",
+                          where="start_time > '2010-01-01'",
+                          nprocs=1, overwrite=True, verbose=True)
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()



More information about the grass-commit mailing list