[GRASS-SVN] r60017 - grass/trunk/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 1 12:17:11 PDT 2014


Author: huhabla
Date: 2014-05-01 12:17:11 -0700 (Thu, 01 May 2014)
New Revision: 60017

Modified:
   grass/trunk/lib/python/temporal/c_libraries_interface.py
   grass/trunk/lib/python/temporal/list_stds.py
Log:
Added stds and time stamped map list function that is sensitive to the user mapsets access rights

Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py	2014-05-01 18:49:46 UTC (rev 60016)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py	2014-05-01 19:17:11 UTC (rev 60017)
@@ -38,13 +38,41 @@
     READ_MAP_INFO=5
     MAP_EXISTS=6
     READ_MAP_INFO=7
+    AVAILABLE_MAPSETS = 8
 
     TYPE_RASTER=0
     TYPE_RASTER3D=1
     TYPE_VECTOR=2
 
 ###############################################################################
+def available_mapsets(lock, conn, data):
+    """!Return all available mapsets the user can access as a list of strings
+    
+       @param lock A multiprocessing.Lock instance
+       @param conn A multiprocessing.Pipe instance used to send True or False
+       @param data The list of data entries [function_id]
+       
+       @return Names of available mapsets as list of strings
+    """
+    
+    mapsets = libgis.G_available_mapsets()
 
+    count = 0
+    mapset_list = []
+    while mapsets[count]:
+        char_list = ""
+        mapset = mapsets[count]
+        if libgis.G__mapset_permissions(mapset) > 0:
+            count += 1
+            c = 0
+            while mapset[c] != "\x00":
+                char_list += mapset[c]
+                c += 1
+                
+        mapset_list.append(char_list)   
+        
+    conn.send(mapset_list) 
+
 def _has_timestamp(lock, conn, data):
     """!Check if the file based GRASS timestamp is present and send
        True or False using the provided pipe.
@@ -586,7 +614,7 @@
        @param conn A multiprocessing.Pipe
     """
     # Crerate the function array
-    functions = [0]*8
+    functions = [0]*9
     functions[RPCDefs.STOP] = _stop
     functions[RPCDefs.HAS_TIMESTAMP] = _has_timestamp
     functions[RPCDefs.WRITE_TIMESTAMP] = _write_timestamp
@@ -594,6 +622,7 @@
     functions[RPCDefs.REMOVE_TIMESTAMP] = _remove_timestamp
     functions[RPCDefs.READ_MAP_INFO] = _read_map_info
     functions[RPCDefs.MAP_EXISTS] = _map_exists
+    functions[RPCDefs.AVAILABLE_MAPSETS] = available_mapsets
 
     libgis.G_gisinit("c_library_server")
     libgis.G_debug(1, "Start C-interface server")
@@ -643,7 +672,12 @@
        >>> grass.run_command("v.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
        0
 
-
+       # Check mapsets
+       >>> ciface = tgis.CLibrariesInterface()
+       >>> mapsets = ciface.available_mapsets()
+       >>> print mapsets[0]
+       PERMANENT
+       
        # Raster map
        >>> ciface = tgis.CLibrariesInterface()
        >>> check = ciface.raster_map_exists("test", tgis.get_current_mapset())
@@ -1031,6 +1065,19 @@
                                name, mapset, layer, timestring])
         return self.client_conn.recv()
 
+    def available_mapsets(self):
+        """!Return all available mapsets the user can access as a list of strings
+        
+           @param lock A multiprocessing.Lock instance
+           @param conn A multiprocessing.Pipe instance used to send True or False
+           @param data Can be None
+           
+           @return Names of available mapsets as list of strings
+        """
+        self._check_restart_server()
+        self.client_conn.send([RPCDefs.AVAILABLE_MAPSETS, ])
+        return self.client_conn.recv()
+
     def stop(self):
         """!Stop the messenger server and close the pipe
         """

Modified: grass/trunk/lib/python/temporal/list_stds.py
===================================================================
--- grass/trunk/lib/python/temporal/list_stds.py	2014-05-01 18:49:46 UTC (rev 60016)
+++ grass/trunk/lib/python/temporal/list_stds.py	2014-05-01 19:17:11 UTC (rev 60017)
@@ -28,6 +28,83 @@
 
 ###############################################################################
 
+def get_dataset_list(type,  temporal_type,  columns=None,  where=None,  order=None):
+    """! Return a list of time stamped maps or space time datasets of a specific temporal type
+         that are registred in the temporal database
+    
+         This method returns a dictionary, the keys are the available mapsets, 
+         the values are the rows from the SQL database query.
+
+        @param type The type of the datasets (strds, str3ds, stvds, rast, rast3d, vect)
+        @param temporal_type The temporal type of the datasets (absolute, relative)
+        @param columns A comma separated list of columns that will be selected
+        @param where A where statement for selected listing without "WHERE"
+        @param order A comma separated list of columns to order the
+                               datasets by category
+                      
+        @return A dictionary with the rows of the SQL query for each available mapset
+        
+        >>> import grass.temporal as tgis
+        >>> tgis.init()
+        >>> name = "list_stds_test"
+        >>> sp = tgis.open_new_space_time_dataset(name=name, type="strds", 
+        ... temporaltype="absolute", title="title", descr="descr", semantic="mean", dbif=None, overwrite=True)
+        >>> mapset = tgis.get_current_mapset()
+        >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name")
+        >>> rows =  stds_list[mapset]
+        >>> for row in rows:
+        ...     if row["name"] == name:
+        ...         print True
+        True
+        >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name,mapset", where="mapset = '%s'"%(mapset))
+        >>> rows =  stds_list[mapset]
+        >>> for row in rows:
+        ...     if row["name"] == name and row["mapset"] == mapset:
+        ...         print True
+        True
+        >>> check = sp.delete()
+    """
+    id = None
+    sp = dataset_factory(type, id)
+
+    dbif = SQLDatabaseInterfaceConnection()
+    dbif.connect()
+    
+    mapsets = get_tgis_c_library_interface().available_mapsets()
+    
+    result = {}
+    
+    for mapset in mapsets:
+        
+        if temporal_type == "absolute":
+            table = sp.get_type() + "_view_abs_time"
+        else:
+            table = sp.get_type() + "_view_rel_time"
+
+        if columns and columns.find("all") == -1:
+            sql = "SELECT " + str(columns) + " FROM " + table
+        else:
+            sql = "SELECT * FROM " + table
+
+        if where:
+            sql += " WHERE " + where
+            sql += " AND mapset = '%s'"%(mapset)
+        else:
+            sql += " WHERE mapset = '%s'"%(mapset)
+
+        if order:
+            sql += " ORDER BY " + order
+
+        dbif.cursor.execute(sql)
+        rows = dbif.cursor.fetchall()
+        
+        if rows:
+            result[mapset] = rows
+        
+    return result
+        
+###############################################################################
+
 def list_maps_of_stds(type, input, columns, order, where, separator, method, header, gran=None):
     """! List the maps of a space time dataset using diffetent methods
 
@@ -35,7 +112,7 @@
         @param input Name of a space time raster dataset
         @param columns A comma separated list of columns to be printed to stdout
         @param order A comma separated list of columns to order the
-                      space time dataset by category
+                      maps by category
         @param where A where statement for selected listing without "WHERE"
                       e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
         @param separator The field separator character between the columns
@@ -185,3 +262,9 @@
                     print output
     if connected:
         dbif.close()
+
+###############################################################################
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()



More information about the grass-commit mailing list