[GRASS-SVN] r55457 - in grass-addons/grass7/raster/r.agent: libagent tests

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 19 09:37:46 PDT 2013


Author: mic
Date: 2013-03-19 09:37:46 -0700 (Tue, 19 Mar 2013)
New Revision: 55457

Modified:
   grass-addons/grass7/raster/r.agent/libagent/grassland.py
   grass-addons/grass7/raster/r.agent/tests/test_grassland.py
Log:
start implementation of parsevectorlayer

Modified: grass-addons/grass7/raster/r.agent/libagent/grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/grassland.py	2013-03-19 16:16:21 UTC (rev 55456)
+++ grass-addons/grass7/raster/r.agent/libagent/grassland.py	2013-03-19 16:37:46 UTC (rev 55457)
@@ -74,7 +74,25 @@
             self.layers[layername].write(self.grassmapnames[layername],
                                                         overwrite=force)
 
-    def parsevectorlayer(self, layername, grassmapname=False, force=False):
-#TODO
-        return []
+    def parsevectorlayer(self, layername, grassmapname, value=1, force=False):
+        """
+        Take point information from a vector layer, mark them on the
+        layer specified and return them as a list
+        @param string name of the layer to be exported
+        @param string name of the GRASS map file to be created
+        @param int value to be set
+        @param boolean optional, whether an existing file may be overwritten
+        """
+        vectors = []
+        if grass.find_file(name = grassmapname, element = 'vector')['file']:
+            layer = grass.vector_db_select(grassmapname)['values']
+            # TODO only points are supported, ask some expert how to test this
+            # TODO indexing seems to start at "1".. verify!
+            for v in layer.values():
+                if len(v) == 4 and v[0] == v[3]:
+                    x = ( v[1] - self.region["s"] ) / self.region["ewres"]
+                    y = ( v[1] - self.region["s"] ) / self.region["nsres"]
+                    vectors.append([x, y])
+                    self.layers[layername][y][x] = value
+        return vectors
 

Modified: grass-addons/grass7/raster/r.agent/tests/test_grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_grassland.py	2013-03-19 16:16:21 UTC (rev 55456)
+++ grass-addons/grass7/raster/r.agent/tests/test_grassland.py	2013-03-19 16:37:46 UTC (rev 55457)
@@ -8,7 +8,9 @@
 
 class TestGrassland(unittest.TestCase):
     def setUp(self):
-        self.layername = "r.agent.testmap"
+        # TODO check if there is a nicer way to do this..
+        self.layername = "r.agent.cell.testmap"
+        self.vlayername = "r.agent.vector.testmap"
         self.pg = grassland.Grassland()
 
     def test_getregion(self):
@@ -92,11 +94,20 @@
         pass
 
     def test_parsegrasslayer(self):
-        # grass.vector_db_select('sites')
-        pass
+        if self.vlayername:
+            if grass.find_file(name = self.vlayername,
+                               element = 'vector')['file']:
+                print "We need a file to play with in this test, but it"
+                print "seems to exist already: '" + self.vlayername + "'"
+                # show error if arrived here
+                self.assertTrue(False)
+            #TODO find a way to write vector files..
 
     def tearDown(self):
         if self.layername:
             grass.try_remove(grass.find_file(name = self.layername,
                                              element = 'cell')['file'])
+        if self.vlayername:
+            grass.try_remove(grass.find_file(name = self.vlayername,
+                                             element = 'vector')['file'])
 



More information about the grass-commit mailing list