[GRASS-SVN] r48077 - grass/branches/develbranch_6/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 2 16:07:07 EDT 2011


Author: martinl
Date: 2011-09-02 13:07:07 -0700 (Fri, 02 Sep 2011)
New Revision: 48077

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_vdigit.py
   grass/branches/develbranch_6/gui/wxpython/gui_modules/toolbars.py
   grass/branches/develbranch_6/gui/wxpython/gui_modules/wxvdigit.py
Log:
wxGUI/vdigit: implement missing 'copy attributes'
	      (merge r48076 from trunk)	      


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_vdigit.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_vdigit.py	2011-09-02 20:02:36 UTC (rev 48076)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/mapdisp_vdigit.py	2011-09-02 20:07:07 UTC (rev 48077)
@@ -928,20 +928,19 @@
                 return
             self._geomAttrbUpdate([fid,])
         elif action in ("copyCats", "copyAttrs"):
-            try:
-                if action == 'copyCats':
-                    if self.digit.CopyCats(self.copyCatsList,
-                                           self.copyCatsIds, copyAttrb = False) < 0:
-                        return
-                else:
-                    if self.digit.CopyCats(self.copyCatsList,
-                                           self.copyCatsIds, copyAttrb = True) < 0:
-                        return
+            print self.copyCatsList
+            print self.copyCatsIds
+            if action == 'copyCats':
+                if self.digit.CopyCats(self.copyCatsList,
+                                       self.copyCatsIds, copyAttrb = False) < 0:
+                    return
+            else:
+                if self.digit.CopyCats(self.copyCatsList,
+                                       self.copyCatsIds, copyAttrb = True) < 0:
+                    return
                 
-                del self.copyCatsList
-                del self.copyCatsIds
-            except AttributeError:
-                pass
+            del self.copyCatsList
+            del self.copyCatsIds
             
             self._updateATM()
                 

Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/toolbars.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/toolbars.py	2011-09-02 20:02:36 UTC (rev 48076)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/toolbars.py	2011-09-02 20:07:07 UTC (rev 48077)
@@ -762,7 +762,7 @@
              wx.ITEM_CHECK, self.OnCopyCats, "copyCats"),
             (_('Copy features from (background) vector map'),
              wx.ITEM_CHECK, self.OnCopy, "copyLine"),
-            (_('Duplicate attributes'),
+            (_('Copy attributes'),
              wx.ITEM_CHECK, self.OnCopyAttrb, "copyAttrs"),
             (_('Feature type conversion'),
              wx.ITEM_CHECK, self.OnTypeConversion, "typeConv"),

Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/wxvdigit.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/wxvdigit.py	2011-09-02 20:02:36 UTC (rev 48076)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/wxvdigit.py	2011-09-02 20:07:07 UTC (rev 48077)
@@ -922,7 +922,7 @@
         
         return ret
 
-    def CopyCats(self, fromId, toId, copyAttrb=False):
+    def CopyCats(self, fromId, toId, copyAttrb = False):
         """!Copy given categories to objects with id listed in ids
 
         @param cats ids of 'from' feature
@@ -931,16 +931,131 @@
         @return number of modified features
         @return -1 on error
         """
-        if len(fromId) == 0 or len(toId) == 0:
+        if len(fromId) < 1 or len(toId) < 1:
             return 0
         
-        ret = self.digit.CopyCats(fromId, toId, copyAttrb)
-
-        if ret > 0:
+        poCatsFrom = self.poCats
+        poCatsTo = Vect_new_cats_struct();
+        
+        nlines = 0
+        
+        for fline in fromId:
+            if not Vect_line_alive(self.poMapInfo, fline):
+                continue
+            
+            if Vect_read_line(self.poMapInfo, None, poCatsFrom, fline) < 0:
+                self._error.ReadLine(fline)
+                return -1
+            
+            for tline in toId:
+                if not Vect_line_alive(self.poMapInfo, tline):
+                    continue
+                
+                ltype = Vect_read_line(self.poMapInfo, self.poPoints, poCatsTo, tline)
+                if ltype < 0:
+                    self._error.ReadLine(fline)
+                    return -1
+                
+                catsFrom = poCatsFrom.contents
+                for i in range(catsFrom.n_cats):
+                    if not copyAttrb:
+                        # duplicate category
+                        cat = catsFrom.cat[i]
+                    else:
+                        # duplicate attributes
+                        cat = self.cats[catsFrom.field[i]] + 1
+                        self.cats[catsFrom.field[i]] = cat
+                        poFi = Vect_get_field(self.poMapInfo, catsFrom.field[i])
+                        if not poFi:
+                            self._error.DbLink(i)
+                            return -1
+                        
+                        fi = poFi.contents
+                        driver = db_start_driver(fi.driver)
+                        if not driver:
+                            self._error.Driver(fi.driver)
+                            return -1
+                        
+                        handle = dbHandle()
+                        db_init_handle(byref(handle))
+                        db_set_handle(byref(handle), fi.database, None)
+                        if db_open_database(driver, byref(handle)) != DB_OK:
+                            db_shutdown_driver(driver)
+                            self._error.Database(fi.driver, fi.database)
+                            return -1
+                        
+                        stmt = dbString()
+                        db_init_string(byref(stmt))
+                        db_set_string(byref(stmt),
+                                      "SELECT * FROM %s WHERE %s=%d" % (fi.table, fi.key,
+                                                                        catsFrom.cat[i]))
+                        
+                        cursor = dbCursor()
+                        if db_open_select_cursor(driver, byref(stmt), byref(cursor),
+                                                 DB_SEQUENTIAL) != DB_OK:
+                                db_close_database_shutdown_driver(driver)
+                                return -1
+                        
+                        table = db_get_cursor_table(byref(cursor))
+                        ncols = db_get_table_number_of_columns(table)
+                        
+                        sql = "INSERT INTO %s VALUES (" % fi.table
+                        # fetch the data
+                        more = c_int()
+                        while True:
+                            if db_fetch(byref(cursor), DB_NEXT, byref(more)) != DB_OK:
+                                db_close_database_shutdown_driver(driver)
+                                return -1
+                            if not more.value:
+                                break
+                            
+                            value_string = dbString()
+                            for col in range(ncols):
+                                if col > 0:
+                                    sql += ","
+                                    
+                                column = db_get_table_column(table, col)
+                                if db_get_column_name(column) == fi.key:
+                                    sql += "%d" % cat
+                                    continue
+                                
+                                value = db_get_column_value(column)
+                                db_convert_column_value_to_string(column, byref(value_string))
+                                if db_test_value_isnull(value):
+                                    sql += "NULL"
+                                else:
+                                    ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column))
+                                    if ctype != DB_C_TYPE_STRING:
+                                        sql += db_get_string(byref(value_string))
+                                    else:
+                                        sql += "'%s'" % db_get_string(byref(value_string))
+                        
+                        sql += ")"
+                        db_set_string(byref(stmt), sql)
+                        print db_get_string(byref(stmt))
+                        if db_execute_immediate(driver, byref(stmt)) != DB_OK:
+                            db_close_database_shutdown_driver(driver)
+                            return -1
+                        
+                        db_close_database_shutdown_driver(driver)
+                        G_free(poFi)
+                
+                if Vect_cat_set(poCatsTo, catsFrom.field[i], cat) < 1:
+                    continue
+                
+                if Vect_rewrite_line(self.poMapInfo, tline, ltype, self.poPoints, poCatsTo) < 0:
+                    self._error.WriteLine()
+                    return -1
+                
+                nlines +=1
+        
+        Vect_destroy_cats_struct(poCatsTo)
+        
+        if nlines > 0:
             self.toolbar.EnableUndo()
+        
+        return nlines
 
-        return ret
-
     def _selectLinesByQueryThresh(self):
         """!Generic method used for SelectLinesByQuery() -- to get
         threshold value"""
@@ -1453,7 +1568,7 @@
                 if Vect_get_point_in_area(self.poMapInfo, left, byref(x), byref(y)) == 0:
                     Vect_reset_line(bpoints)
                     Vect_append_point(bpoints, x.value, y.value, 0.0)
-                    newline =  Vect_write_line(byref(self.poMapInfo), GV_CENTROID,
+                    newline =  Vect_write_line(self.poMapInfo, GV_CENTROID,
                                                bpoints, self.poCats)
                     if newline < 0:
                         self._error.WriteLine()



More information about the grass-commit mailing list