/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/table.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/table.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1024 by jan, Fri May 23 12:56:34 2003 UTC revision 1025 by frank, Mon May 26 11:46:17 2003 UTC
# Line 2  Line 2 
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  # Jan-Oliver Wagner <[email protected]>  # Jan-Oliver Wagner <[email protected]>
5    # Frank Koormann <[email protected]>
6  #  #
7  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
8  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 229  class DBFTable(TitledObject, OldTableInt Line 230  class DBFTable(TitledObject, OldTableInt
230    
231      # DBF specific interface parts.      # DBF specific interface parts.
232    
233        def Precision(self, col):
234            """Return column precision"""
235            return self.column_map[col].prec
236    
237        def Width(self, col):
238            """Return column width"""
239            return self.column_map[col].width
240    
241      def Destroy(self):      def Destroy(self):
242          self.dbf.close()          self.dbf.close()
243          self.dbf = None          self.dbf = None
# Line 346  class MemoryTable(TitledObject, OldTable Line 355  class MemoryTable(TitledObject, OldTable
355          return min(values), max(values)          return min(values), max(values)
356    
357      def UniqueValues(self, col):      def UniqueValues(self, col):
358          """Return a sorted list of all unique values in the column col"""          """Return a sorted list of all unique values in the column col
359    
360            col can be either column index or name.
361            """
362          dict = {}          dict = {}
363    
364          for i in range(self.NumRows()):          for i in range(self.NumRows()):
# Line 357  class MemoryTable(TitledObject, OldTable Line 369  class MemoryTable(TitledObject, OldTable
369          values.sort()          values.sort()
370          return values          return values
371    
372        def Width(self, col):
373            """Return the maximum width of values in the column
374    
375            The return value is the the maximum length of string representation
376            of the values in the column (represented by index or name)."""
377            max = 0
378            
379            type  = self.column_map[col].type
380            index = self.column_map[col].index
381            values = [row[index] for row in self.data]
382            if not values:
383                return None
384    
385            if type == FIELDTYPE_DOUBLE:
386                prec = self.Precision(col)
387                format = "%%.%df" % prec
388            elif type == FIELDTYPE_INT:
389                format = "%d"
390            else:
391                format = "%s"
392            for value in values:
393                l = len(format % value)
394                if l > max:
395                    max = l
396    
397            return max
398    
399        def Precision(self, col):
400            """Return the precision of the column
401    
402            The return value is the maximum number of numeric characters after the
403            decimal if column type is double. Else precision zero is returned.
404            The column can be represented by index or name.
405            """
406        
407            type  = self.column_map[col].type
408            if type == FIELDTYPE_DOUBLE:
409                index = self.column_map[col].index
410                values = [row[index] for row in self.data]
411                if not values:
412                    return 0
413                
414                max = 0
415                for value in values:
416                    l = len(str(value % 1))
417                    if l > max:
418                        max = l
419                if max > 2:
420                    return max - 2
421                else:
422                    return 0
423            else:
424                return 0
425              
426      def Dependencies(self):      def Dependencies(self):
427          """Return an empty sequence. The MemoryTable doesn't depend on anything          """Return an empty sequence. The MemoryTable doesn't depend on anything
428          """          """
# Line 368  class MemoryTable(TitledObject, OldTable Line 434  class MemoryTable(TitledObject, OldTable
434          # TODO: Allow values to be a dictionary and write the single          # TODO: Allow values to be a dictionary and write the single
435          # fields that are specified.          # fields that are specified.
436          self.data[record] = values          self.data[record] = values
437    
438    
439    def table_to_dbf(table, filename):
440        """Create the dbf file filename from the table"""
441        dbf = dbflib.create(filename)
442    
443        dbflib_fieldtypes = {FIELDTYPE_STRING: dbflib.FTString,
444                             FIELDTYPE_INT: dbflib.FTInteger,
445                             FIELDTYPE_DOUBLE: dbflib.FTDouble}
446    
447        # Initialise the header. Distinguish between DBFTable and others.
448        for col in table.Columns():
449            prec  = table.Precision(col.name)
450            width = table.Width(col.name)
451            dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec)
452    
453        for i in range(table.NumRows()):
454            record = table.ReadRowAsDict(i)
455            dbf.write_record(i, record)
456        dbf.close()
457    
458    def table_to_csv(table, filename):
459        """Export table to csv file."""
460    
461        file = open(filename,"w")
462        columns = table.Columns()
463        if columns:
464            header = "#%s" % columns[0].name
465            for col in columns[1:]:
466                header = header + ",%s" % col.name
467            header = header + "\n"
468            file.write(header)
469    
470            for i in range(table.NumRows()):
471                record = table.ReadRowAsDict(i)
472                if len(record):
473                    line = "%s" % record[columns[0].name]
474                    for col in columns[1:]:
475                        line = line + ",%s" % record[col.name]
476                line = line + "\n"
477                file.write(line)
478        file.close()
479    

Legend:
Removed from v.1024  
changed lines
  Added in v.1025

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26