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 |
|
# 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) |
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. |
237 |
|
|
238 |
# DBF specific interface parts. |
# DBF specific interface parts. |
239 |
|
|
|
def Precision(self, col): |
|
|
"""Return column precision""" |
|
|
return self.column_map[col].prec |
|
|
|
|
240 |
def Width(self, col): |
def Width(self, col): |
241 |
"""Return column width""" |
"""Return column width""" |
242 |
return self.column_map[col].width |
return self.column_map[col].width |
375 |
def Width(self, col): |
def Width(self, col): |
376 |
"""Return the maximum width of values in the column |
"""Return the maximum width of values in the column |
377 |
|
|
378 |
The return value is the the maximum length of string representation |
The return value is the the maximum length of string |
379 |
of the values in the column (represented by index or name).""" |
representation of the values in the column (represented by index |
380 |
|
or name). |
381 |
|
""" |
382 |
max = 0 |
max = 0 |
383 |
|
|
384 |
type = self.column_map[col].type |
type = self.column_map[col].type |
385 |
index = self.column_map[col].index |
index = self.column_map[col].index |
386 |
values = [row[index] for row in self.data] |
values = [row[index] for row in self.data] |
388 |
return None |
return None |
389 |
|
|
390 |
if type == FIELDTYPE_DOUBLE: |
if type == FIELDTYPE_DOUBLE: |
391 |
prec = self.Precision(col) |
format = "%.12f" |
|
format = "%%.%df" % prec |
|
392 |
elif type == FIELDTYPE_INT: |
elif type == FIELDTYPE_INT: |
393 |
format = "%d" |
format = "%d" |
394 |
else: |
else: |
400 |
|
|
401 |
return max |
return max |
402 |
|
|
|
def Precision(self, col): |
|
|
"""Return the precision of the column |
|
|
|
|
|
The return value is the maximum number of numeric characters after the |
|
|
decimal if column type is double. Else precision zero is returned. |
|
|
The column can be represented by index or name. |
|
|
""" |
|
|
|
|
|
type = self.column_map[col].type |
|
|
if type == FIELDTYPE_DOUBLE: |
|
|
index = self.column_map[col].index |
|
|
values = [row[index] for row in self.data] |
|
|
if not values: |
|
|
return 0 |
|
|
|
|
|
max = 0 |
|
|
for value in values: |
|
|
l = len(str(value % 1)) |
|
|
if l > max: |
|
|
max = l |
|
|
if max > 2: |
|
|
return max - 2 |
|
|
else: |
|
|
return 0 |
|
|
else: |
|
|
return 0 |
|
|
|
|
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 |
""" |
""" |
423 |
|
|
424 |
# Initialise the header. Distinguish between DBFTable and others. |
# Initialise the header. Distinguish between DBFTable and others. |
425 |
for col in table.Columns(): |
for col in table.Columns(): |
|
prec = table.Precision(col.name) |
|
426 |
width = table.Width(col.name) |
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) |
dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec) |
432 |
|
|
433 |
for i in range(table.NumRows()): |
for i in range(table.NumRows()): |