/[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 385 - (hide annotations)
Mon Feb 3 11:43:56 2003 UTC (22 years, 1 month ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/classification.py
File MIME type: text/x-python
File size: 5012 byte(s)
Added getNull() to return the NullData reference

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     If no mapping can be found then a NullData data will
17     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 jan 374 from Thuban import _
24 jonathan 381 from Thuban.Model.color import Color
25 jan 374
26 jonathan 381 from wxPython.wx import *
27    
28 jonathan 371 # constants
29     RANGE_MIN = 0
30     RANGE_MAX = 1
31     RANGE_DATA = 2
32    
33     class Classification:
34    
35    
36     def __init__(self, field = None):
37     """Initialize a classification.
38    
39     field -- the name of the data table field that
40     is to be used to classify layer properties
41     """
42    
43 jonathan 378 self.points = {}
44     self.ranges = []
45 jonathan 371 self.setField(field)
46     self.setNull(None)
47    
48     def setField(self, field):
49     """Set the name of the data table field to use.
50    
51     field -- if None then all values map to NullData
52     """
53    
54     self.field = field
55    
56     def setNull(self, data):
57     """Set the data to be used when a value can't be classified.
58    
59     data -- data that the value maps to. See class description.
60     """
61    
62     self.NullData = data
63    
64 jonathan 385 def getNull(self):
65     return self.NullData
66    
67 jonathan 371 def addRange(self, min, max, data):
68     """Add a new range to the classification.
69    
70     A range allows a value to be classified if it falls between
71     min and max. Specifically, min <= value < max
72    
73     min -- the lower bound.
74    
75     max -- the upper bound.
76    
77     data -- data that the value maps to. See class description.
78     """
79    
80     if min >= max:
81 jan 374 raise ValueError(_("Range minimum >= maximum!"))
82 jonathan 378 self.ranges.append([min, max, data])
83 jonathan 371
84     def addPoint(self, value, data):
85     """Associate a single value with data.
86    
87     When this value is to be classified data will be returned.
88    
89     value -- classification value.
90    
91     data -- data that the value maps to. See class description.
92     """
93    
94 jonathan 378 self.points[value] = data
95 jonathan 371
96     def getProperties(self, value):
97     """Return the associated data, or the NullData.
98    
99     The following search technique is used:
100     (1) if the field is None, return NullData
101     (2) check if the value exists as a single value
102     (3) check if the value falls within a range. Ranges
103     are checked in the order they were added to
104     the classification.
105    
106     value -- the value to classify. If there is no mapping
107     return the NullData (which may be None)
108     """
109    
110     if self.field is not None:
111     #
112     # first check the discrete values
113     #
114 jonathan 378 if self.points.has_key(value):
115     return self.points[value]
116 jonathan 371
117     #
118     # now check the ranges
119     #
120 jonathan 378 for p in self.ranges:
121 jonathan 371 if (p[RANGE_MIN] <= value) and (value < p[RANGE_MAX]):
122     return p[RANGE_DATA]
123    
124    
125     return self.NullData
126    
127 jonathan 381 def TreeInfo(self):
128     items = []
129 jonathan 378
130 jonathan 381 #
131     # shouldn't print anything if there are no classifications
132     #
133    
134    
135     def color_string(color):
136     if color is None:
137     return "None"
138     return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue)
139    
140     if self.NullData is not None:
141     i = []
142     for key, value in self.NullData.items():
143     if isinstance(value, Color):
144     i.append((_("%s: %s") % (key, color_string(value)), value))
145     else:
146     i.append(_("%s: %s") % (key, value))
147     items.append((_("'NULL'"), i))
148    
149     for name, data in self.points.items():
150     i = []
151     for key, value in data.items():
152     if isinstance(value, Color):
153     i.append((_("%s: %s") % (key, color_string(value)), value))
154     else:
155     i.append(_("%s: %s") % (key, value))
156     items.append((_("%s") % name, i))
157    
158     for p in self.ranges:
159     i = []
160     data = p[RANGE_DATA]
161     for key, value in data.items():
162     if isinstance(value, Color):
163     i.append((_("%s: %s") % (key, color_string(value)), value))
164     else:
165     i.append(_("%s: %s") % (key, value))
166     items.append((_("%s-%s") % (p[RANGE_MIN], p[RANGE_MAX], i)))
167    
168     return (_("Classifications"), items)
169    

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26