4 @brief Support classes for dbm.py
9 (C) 2007-2011 by the GRASS Development Team
11 This program is free software under the GNU General Public
12 License (>=v2). Read the file COPYING that comes with GRASS
15 @author Martin Landa <landa.martin gmail.com>
25 from preferences
import globalSettings
as UserSettings
31 if type(value) == types.UnicodeType:
34 enc = UserSettings.Get(group =
'atm', key =
'encoding', subkey =
'value')
36 value = unicode(value, enc)
37 elif 'GRASS_DB_ENCODING' in os.environ:
38 value = unicode(value, os.environ[
'GRASS_DB_ENCODING'])
41 value = unicode(value,
'ascii')
42 except UnicodeDecodeError:
43 value = _(
"Unable to decode value. Set encoding in GUI preferences ('Attributes').")
48 """!Create database connection information content"""
49 infoFlexSizer = wx.FlexGridSizer (cols = 2, hgap = 1, vgap = 1)
50 infoFlexSizer.AddGrowableCol(1)
52 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
54 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
55 label = mapDBInfo.layers[layer][
'driver']))
56 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
58 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
59 label = mapDBInfo.layers[layer][
'database']))
60 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
62 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
63 label = mapDBInfo.layers[layer][
'table']))
64 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
66 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
67 label = mapDBInfo.layers[layer][
'key']))
72 """!Class providing information about attribute tables
73 linked to the vector map"""
75 gselect.VectorDBInfo.__init__(self, map)
78 """!Return list of columns names (based on their index)"""
80 names = [
''] * len(self.
tables[table].keys())
84 for name, desc
in self.
tables[table].iteritems():
85 names[desc[
'index']] = name
90 """!Get attributes by coordinates (all available layers)
92 Return line id or None if no line is found"""
96 data = grass.vector_what(map = self.
map,
97 coord = (float(queryCoords[0]), float(queryCoords[1])),
98 distance = float(qdist))
100 if len(data) < 1
or 'Table' not in data[0]:
104 table = data[0][
'Table']
105 for key, value
in data[0][
'Attributes'].iteritems():
109 if self.
tables[table][key][
'ctype'] != types.StringType:
110 value = self.
tables[table][key][
'ctype'] (value)
113 self.
tables[table][key][
'values'].append(value)
116 for key, value
in data[0].iteritems():
117 if key ==
'Attributes':
120 ret[key].append(value)
125 """!Select records from the table
127 Return number of selected records, -1 on error
134 table = self.
layers[layer][
"table"]
136 if where
is None or where
is '':
137 sql =
"SELECT %s FROM %s" % (cols, table)
139 sql =
"SELECT %s FROM %s WHERE %s" % (cols, table, where)
141 ret = gcmd.RunCommand(
'db.select',
147 database = self.
layers[layer][
"database"],
148 driver = self.
layers[layer][
"driver"])
152 for line
in ret.splitlines():
153 name, value = line.split(
'|')
156 if self.
tables[table][name][
'ctype'] !=
type(
''):
157 value = self.
tables[table][name][
'ctype'] (value)
162 self.
tables[table][name][
'values'].append(value)