/[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 984 by bh, Thu May 22 16:37:48 2003 UTC revision 1043 by bh, Mon May 26 19:27:15 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 12  Classes for handling tables of data. Line 13  Classes for handling tables of data.
13    
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    
16    import os
17  import inspect  import inspect
18  import warnings  import warnings
19    
20    from base import TitledObject
21    
22  import dbflib  import dbflib
23    
24  # the field types supported by a Table instance.  # the field types supported by a Table instance.
# Line 107  class DBFColumn: Line 111  class DBFColumn:
111          self.index = index          self.index = index
112    
113    
114  class DBFTable(OldTableInterfaceMixin):  class DBFTable(TitledObject, OldTableInterfaceMixin):
115    
116      """      """
117      Table interface for the data in a DBF file      Table interface for the data in a DBF file
# Line 129  class DBFTable(OldTableInterfaceMixin): Line 133  class DBFTable(OldTableInterfaceMixin):
133    
134      def __init__(self, filename):      def __init__(self, filename):
135          self.filename = filename          self.filename = filename
136            title = os.path.basename(self.filename)
137            TitledObject.__init__(self, title)
138          self.dbf = dbflib.DBFFile(filename)          self.dbf = dbflib.DBFFile(filename)
139    
140          # If true, self.dbf is open for writing.          # If true, self.dbf is open for writing.
# Line 224  class DBFTable(OldTableInterfaceMixin): Line 230  class DBFTable(OldTableInterfaceMixin):
230    
231      # DBF specific interface parts.      # DBF specific interface parts.
232    
233        def Width(self, col):
234            """Return column width"""
235            return self.column_map[col].width
236    
237      def Destroy(self):      def Destroy(self):
238          self.dbf.close()          self.dbf.close()
239          self.dbf = None          self.dbf = None
# Line 249  class DBFTable(OldTableInterfaceMixin): Line 259  class DBFTable(OldTableInterfaceMixin):
259          self.dbf.write_record(record, values)          self.dbf.write_record(record, values)
260          self.dbf.commit()          self.dbf.commit()
261    
262        def FileName(self):
263            """Return the filename the DBFTable was instantiated with"""
264            return self.filename
265    
266    
267  class MemoryColumn:  class MemoryColumn:
# Line 258  class MemoryColumn: Line 271  class MemoryColumn:
271          self.type = type          self.type = type
272          self.index = index          self.index = index
273    
274  class MemoryTable(OldTableInterfaceMixin):  class MemoryTable(TitledObject, OldTableInterfaceMixin):
275    
276      """Very simple table implementation that operates on a list of tuples"""      """Very simple table implementation that operates on a list of tuples"""
277    
# Line 270  class MemoryTable(OldTableInterfaceMixin Line 283  class MemoryTable(OldTableInterfaceMixin
283          data -- List of tuples, one for each row of data          data -- List of tuples, one for each row of data
284          """          """
285          self.data = data          self.data = data
286            title = 'MemoryTable'
287            TitledObject.__init__(self, title)
288    
289          # Create the column information objects          # Create the column information objects
290          self.columns = []          self.columns = []
# Line 336  class MemoryTable(OldTableInterfaceMixin Line 351  class MemoryTable(OldTableInterfaceMixin
351          return min(values), max(values)          return min(values), max(values)
352    
353      def UniqueValues(self, col):      def UniqueValues(self, col):
354          """Return a sorted list of all unique values in the column col"""          """Return a sorted list of all unique values in the column col
355    
356            col can be either column index or name.
357            """
358          dict = {}          dict = {}
359    
360          for i in range(self.NumRows()):          for i in range(self.NumRows()):
# Line 347  class MemoryTable(OldTableInterfaceMixin Line 365  class MemoryTable(OldTableInterfaceMixin
365          values.sort()          values.sort()
366          return values          return values
367    
368        def Width(self, col):
369            """Return the maximum width of values in the column
370    
371            The return value is the the maximum length of string
372            representation of the values in the column (represented by index
373            or name).
374            """
375            max = 0
376    
377            type  = self.column_map[col].type
378            index = self.column_map[col].index
379            values = [row[index] for row in self.data]
380            if not values:
381                return None
382    
383            if type == FIELDTYPE_DOUBLE:
384                format = "%.12f"
385            elif type == FIELDTYPE_INT:
386                format = "%d"
387            else:
388                format = "%s"
389            for value in values:
390                l = len(format % value)
391                if l > max:
392                    max = l
393    
394            return max
395    
396      def Dependencies(self):      def Dependencies(self):
397          """Return an empty sequence. The MemoryTable doesn't depend on anything          """Return an empty sequence. The MemoryTable doesn't depend on anything
398          """          """
# Line 358  class MemoryTable(OldTableInterfaceMixin Line 404  class MemoryTable(OldTableInterfaceMixin
404          # TODO: Allow values to be a dictionary and write the single          # TODO: Allow values to be a dictionary and write the single
405          # fields that are specified.          # fields that are specified.
406          self.data[record] = values          self.data[record] = values
407    
408    
409    def table_to_dbf(table, filename):
410        """Create the dbf file filename from the table"""
411        dbf = dbflib.create(filename)
412    
413        dbflib_fieldtypes = {FIELDTYPE_STRING: dbflib.FTString,
414                             FIELDTYPE_INT: dbflib.FTInteger,
415                             FIELDTYPE_DOUBLE: dbflib.FTDouble}
416    
417        # Initialise the header. Distinguish between DBFTable and others.
418        for col in table.Columns():
419            width = table.Width(col.name)
420            if col.type == FIELDTYPE_DOUBLE:
421                prec = getattr(col, "prec", 12)
422            else:
423                prec = 0
424            dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec)
425    
426        for i in range(table.NumRows()):
427            record = table.ReadRowAsDict(i)
428            dbf.write_record(i, record)
429        dbf.close()
430    
431    def table_to_csv(table, filename):
432        """Export table to csv file."""
433    
434        file = open(filename,"w")
435        columns = table.Columns()
436        if columns:
437            header = "#%s" % columns[0].name
438            for col in columns[1:]:
439                header = header + ",%s" % col.name
440            header = header + "\n"
441            file.write(header)
442    
443            for i in range(table.NumRows()):
444                record = table.ReadRowAsDict(i)
445                if len(record):
446                    line = "%s" % record[columns[0].name]
447                    for col in columns[1:]:
448                        line = line + ",%s" % record[col.name]
449                line = line + "\n"
450                file.write(line)
451        file.close()
452    

Legend:
Removed from v.984  
changed lines
  Added in v.1043

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26