/[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 994 by bh, Thu May 22 18:05:16 2003 UTC revision 1078 by bh, Wed May 28 09:07:30 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    
137            # Omit the extension in the title as it's not really needed and
138            # it can be confusing because dbflib removes extensions and
139            # appends some variations of '.dbf' before it tries to open the
140            # file. So the title could be e.g. myshapefile.shp when the real
141            # filename is myshapefile.dbf
142            title = os.path.splitext(os.path.basename(self.filename))[0]
143            TitledObject.__init__(self, title)
144    
145          self.dbf = dbflib.DBFFile(filename)          self.dbf = dbflib.DBFFile(filename)
146    
147          # If true, self.dbf is open for writing.          # If true, self.dbf is open for writing.
# Line 224  class DBFTable(OldTableInterfaceMixin): Line 237  class DBFTable(OldTableInterfaceMixin):
237    
238      # DBF specific interface parts.      # DBF specific interface parts.
239    
240        def Width(self, col):
241            """Return column width"""
242            return self.column_map[col].width
243    
244      def Destroy(self):      def Destroy(self):
245          self.dbf.close()          self.dbf.close()
246          self.dbf = None          self.dbf = None
# Line 261  class MemoryColumn: Line 278  class MemoryColumn:
278          self.type = type          self.type = type
279          self.index = index          self.index = index
280    
281  class MemoryTable(OldTableInterfaceMixin):  class MemoryTable(TitledObject, OldTableInterfaceMixin):
282    
283      """Very simple table implementation that operates on a list of tuples"""      """Very simple table implementation that operates on a list of tuples"""
284    
# Line 273  class MemoryTable(OldTableInterfaceMixin Line 290  class MemoryTable(OldTableInterfaceMixin
290          data -- List of tuples, one for each row of data          data -- List of tuples, one for each row of data
291          """          """
292          self.data = data          self.data = data
293            title = 'MemoryTable'
294            TitledObject.__init__(self, title)
295    
296          # Create the column information objects          # Create the column information objects
297          self.columns = []          self.columns = []
# Line 339  class MemoryTable(OldTableInterfaceMixin Line 358  class MemoryTable(OldTableInterfaceMixin
358          return min(values), max(values)          return min(values), max(values)
359    
360      def UniqueValues(self, col):      def UniqueValues(self, col):
361          """Return a sorted list of all unique values in the column col"""          """Return a sorted list of all unique values in the column col
362    
363            col can be either column index or name.
364            """
365          dict = {}          dict = {}
366    
367          for i in range(self.NumRows()):          for i in range(self.NumRows()):
# Line 350  class MemoryTable(OldTableInterfaceMixin Line 372  class MemoryTable(OldTableInterfaceMixin
372          values.sort()          values.sort()
373          return values          return values
374    
375        def Width(self, col):
376            """Return the maximum width of values in the column
377    
378            The return value is the the maximum length of string
379            representation of the values in the column (represented by index
380            or name).
381            """
382            max = 0
383    
384            type  = self.column_map[col].type
385            index = self.column_map[col].index
386            values = [row[index] for row in self.data]
387            if not values:
388                return None
389    
390            if type == FIELDTYPE_DOUBLE:
391                format = "%.12f"
392            elif type == FIELDTYPE_INT:
393                format = "%d"
394            else:
395                format = "%s"
396            for value in values:
397                l = len(format % value)
398                if l > max:
399                    max = l
400    
401            return max
402    
403      def Dependencies(self):      def Dependencies(self):
404          """Return an empty sequence. The MemoryTable doesn't depend on anything          """Return an empty sequence. The MemoryTable doesn't depend on anything
405          """          """
# Line 361  class MemoryTable(OldTableInterfaceMixin Line 411  class MemoryTable(OldTableInterfaceMixin
411          # TODO: Allow values to be a dictionary and write the single          # TODO: Allow values to be a dictionary and write the single
412          # fields that are specified.          # fields that are specified.
413          self.data[record] = values          self.data[record] = values
414    
415    
416    def table_to_dbf(table, filename):
417        """Create the dbf file filename from the table"""
418        dbf = dbflib.create(filename)
419    
420        dbflib_fieldtypes = {FIELDTYPE_STRING: dbflib.FTString,
421                             FIELDTYPE_INT: dbflib.FTInteger,
422                             FIELDTYPE_DOUBLE: dbflib.FTDouble}
423    
424        # Initialise the header. Distinguish between DBFTable and others.
425        for col in table.Columns():
426            width = table.Width(col.name)
427            if col.type == FIELDTYPE_DOUBLE:
428                prec = getattr(col, "prec", 12)
429            else:
430                prec = 0
431            dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec)
432    
433        for i in range(table.NumRows()):
434            record = table.ReadRowAsDict(i)
435            dbf.write_record(i, record)
436        dbf.close()
437    
438    def table_to_csv(table, filename):
439        """Export table to csv file."""
440    
441        file = open(filename,"w")
442        columns = table.Columns()
443        if columns:
444            header = "#%s" % columns[0].name
445            for col in columns[1:]:
446                header = header + ",%s" % col.name
447            header = header + "\n"
448            file.write(header)
449    
450            for i in range(table.NumRows()):
451                record = table.ReadRowAsDict(i)
452                if len(record):
453                    line = "%s" % record[columns[0].name]
454                    for col in columns[1:]:
455                        line = line + ",%s" % record[col.name]
456                line = line + "\n"
457                file.write(line)
458        file.close()
459    

Legend:
Removed from v.994  
changed lines
  Added in v.1078

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26