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

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/Model/classification.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 397 - (hide annotations)
Tue Feb 11 14:23:32 2003 UTC (22 years ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/classification.py
File MIME type: text/x-python
File size: 6601 byte(s)
* Thuban/Model/classification.py: Added import line to fix
        feature conflicts between running on python2.2 and python2.1.

1 jonathan 371 # Copyright (c) 2001 by Intevation GmbH
2     # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     __version__ = "$Revision$"
9    
10     """
11     A Classification provides a mapping from an input value
12     to data. This mapping can be specified in two ways.
13     First, specific values can be associated with data.
14     Second, ranges can be associated with data such that if
15     an input value falls with a range that data is returned.
16 jonathan 388 If no mapping can be found then default data will
17 jonathan 371 be returned. Input values must be hashable objects
18    
19     See the description of getProperties() for more information
20     on the mapping algorithm.
21     """
22    
23 jonathan 397 # fix for people using python2.1
24     from __future__ import nested_scopes
25    
26 jonathan 388 from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
27     LAYER_VISIBILITY_CHANGED
28    
29 jan 374 from Thuban import _
30 jonathan 381 from Thuban.Model.color import Color
31 jan 374
32 jonathan 381 from wxPython.wx import *
33    
34 jonathan 371 # constants
35     RANGE_MIN = 0
36     RANGE_MAX = 1
37     RANGE_DATA = 2
38    
39     class Classification:
40    
41 jonathan 388 def __init__(self, layer, field = None):
42 jonathan 371 """Initialize a classification.
43    
44 jonathan 388 layer -- the layer object who owns this classification
45    
46 jonathan 371 field -- the name of the data table field that
47     is to be used to classify layer properties
48     """
49    
50 jonathan 388 self.layer = layer
51 jonathan 378 self.points = {}
52     self.ranges = []
53 jonathan 388 self.DefaultData = ClassData()
54     self.field = field
55     #self.SetField(field)
56    
57     def SetField(self, field):
58 jonathan 371 """Set the name of the data table field to use.
59    
60 jonathan 388 field -- if None then all values map to the default data
61 jonathan 371 """
62    
63     self.field = field
64 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
65 jonathan 371
66 jonathan 388 def GetField(self):
67     return self.field
68    
69     def SetDefaultData(self, data):
70 jonathan 371 """Set the data to be used when a value can't be classified.
71    
72     data -- data that the value maps to. See class description.
73     """
74    
75 jonathan 388 self.DefaultData = data
76 jonathan 371
77 jonathan 388 def GetDefaultData(self):
78     return self.DefaultData
79 jonathan 385
80 jonathan 388 def SetDefaultFill(self, fill):
81     self.DefaultData.SetFill(fill)
82     self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
83    
84     def GetDefaultFill(self):
85     return self.DefaultData.GetFill()
86    
87     def SetDefaultStroke(self, stroke):
88     self.DefaultData.SetStroke(stroke)
89     self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
90    
91     def GetDefaultStroke(self):
92     return self.DefaultData.GetStroke()
93    
94     def SetDefaultStrokeWidth(self, strokeWidth):
95     self.DefaultData.SetStrokeWidth(strokeWidth)
96     self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
97    
98     def GetDefaultStrokeWidth(self):
99     return self.DefaultData.GetStrokeWidth()
100    
101     def AddRange(self, min, max, data):
102 jonathan 371 """Add a new range to the classification.
103    
104     A range allows a value to be classified if it falls between
105     min and max. Specifically, min <= value < max
106    
107     min -- the lower bound.
108    
109     max -- the upper bound.
110    
111     data -- data that the value maps to. See class description.
112     """
113    
114     if min >= max:
115 jan 374 raise ValueError(_("Range minimum >= maximum!"))
116 jonathan 378 self.ranges.append([min, max, data])
117 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
118 jonathan 371
119 jonathan 388 def AddPoint(self, value, data):
120 jonathan 371 """Associate a single value with data.
121    
122     When this value is to be classified data will be returned.
123    
124     value -- classification value.
125    
126     data -- data that the value maps to. See class description.
127     """
128    
129 jonathan 378 self.points[value] = data
130 jonathan 388 self.layer.changed(LAYER_LEGEND_CHANGED, self.layer)
131 jonathan 371
132 jonathan 388 def GetProperties(self, value):
133     """Return the associated data, or the default data.
134 jonathan 371
135     The following search technique is used:
136 jonathan 388 (1) if the field is None, return the default data
137 jonathan 371 (2) check if the value exists as a single value
138     (3) check if the value falls within a range. Ranges
139     are checked in the order they were added to
140     the classification.
141    
142 jonathan 388 value -- the value to classify. If there is no mapping,
143     or value is None, return the default data
144     (which may be None)
145 jonathan 371 """
146    
147 jonathan 388 if self.field is not None and value is not None:
148 jonathan 371 #
149     # first check the discrete values
150     #
151 jonathan 378 if self.points.has_key(value):
152     return self.points[value]
153 jonathan 371
154     #
155     # now check the ranges
156     #
157 jonathan 378 for p in self.ranges:
158 jonathan 371 if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):
159     return p[RANGE_DATA]
160    
161    
162 jonathan 388 return self.DefaultData
163 jonathan 371
164 jonathan 381 def TreeInfo(self):
165     items = []
166 jonathan 378
167 jonathan 381 #
168 jonathan 388 # XXX: shouldn't print anything if there are no classifications
169 jonathan 381 #
170    
171    
172     def color_string(color):
173     if color is None:
174     return "None"
175     return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)
176    
177 jonathan 388 def build_item(data):
178 jonathan 381 i = []
179    
180 jonathan 388 v = data.GetStroke()
181     i.append((_("Stroke: %s") % color_string(v), v))
182     v = data.GetStrokeWidth()
183     i.append((_("Stroke Width: %s") % v))
184     v = data.GetFill()
185     i.append((_("Fill: %s") % color_string(v), v))
186     return i
187    
188     items.append((_("'DEFAULT'"), build_item(self.DefaultData)))
189    
190 jonathan 381 for name, data in self.points.items():
191 jonathan 388 items.append((_("%s") % name, build_item(data)))
192 jonathan 381
193     for p in self.ranges:
194     data = p[RANGE_DATA]
195 jonathan 388 items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX])),
196     build_item(data))
197    
198 jonathan 381 return (_("Classifications"), items)
199    
200 jonathan 388
201     class ClassData:
202    
203     def __init__(self):
204     self.stroke = None
205     self.stroke_width = 0
206     self.fill = None
207     self.label = ""
208    
209     def GetStroke(self):
210     return self.stroke
211    
212     def SetStroke(self, stroke):
213     self.stroke = stroke
214    
215     def GetStrokeWidth(self):
216     return self.stroke_width
217    
218     def SetStrokeWidth(self, stroke_width):
219     self.stroke_width = stroke_width
220    
221     def GetFill(self):
222     return self.fill
223    
224     def SetFill(self, fill):
225     self.fill = fill
226    
227     def GetLabel(self):
228     return self.label
229    
230     def SetLabel(self, label):
231     self.label = label
232    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26