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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 613 by jonathan, Mon Apr 7 08:55:55 2003 UTC revision 1249 by jonathan, Fri Jun 20 09:27:19 2003 UTC
# Line 20  See the description of FindGroup() for m Line 20  See the description of FindGroup() for m
20  on the mapping algorithm.  on the mapping algorithm.
21  """  """
22        
23  # fix for people using python2.1  import copy, operator, types
 from __future__ import nested_scopes  
   
 import copy  
24    
25  from Thuban import _  from Thuban import _
26    
 from types import *  
   
27  from messages import \  from messages import \
28      LAYER_PROJECTION_CHANGED, \      LAYER_PROJECTION_CHANGED, \
29      LAYER_LEGEND_CHANGED, \      LAYER_LEGEND_CHANGED, \
30      LAYER_VISIBILITY_CHANGED      LAYER_VISIBILITY_CHANGED
31    
32  from Thuban.Model.color import Color  from Thuban.Model.color import Color
33    from Thuban.Model.range import Range
34    
35  import Thuban.Model.layer  import Thuban.Model.layer
36    
# Line 73  class Classification: Line 69  class Classification:
69      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
70          clazz = Classification()          clazz = Classification()
71    
72            # note: the only thing that isn't copied is the layer reference
73            clazz.field = self.field
74            clazz.fieldType = self.fieldType
75          clazz.__groups[0] = copy.deepcopy(self.__groups[0])          clazz.__groups[0] = copy.deepcopy(self.__groups[0])
76    
77          for i in range(1, len(self.__groups)):          for i in range(1, len(self.__groups)):
78              clazz.__groups.append(copy.deepcopy(self.__groups[i]))              clazz.__groups.append(copy.deepcopy(self.__groups[i]))
79    
         print "Classification.__deepcopy__"  
80          return clazz          return clazz
81    
82      def __SendNotification(self):      def __SendNotification(self):
# Line 194  class Classification: Line 192  class Classification:
192    
193          fill -- a Color object.          fill -- a Color object.
194          """          """
         assert isinstance(fill, Color)  
195          self.GetDefaultGroup().GetProperties().SetFill(fill)          self.GetDefaultGroup().GetProperties().SetFill(fill)
196          self.__SendNotification()          self.__SendNotification()
197                    
# Line 207  class Classification: Line 204  class Classification:
204    
205          color -- a Color object.          color -- a Color object.
206          """          """
         assert isinstance(color, Color)  
207          self.GetDefaultGroup().GetProperties().SetLineColor(color)          self.GetDefaultGroup().GetProperties().SetLineColor(color)
208          self.__SendNotification()          self.__SendNotification()
209                    
# Line 220  class Classification: Line 216  class Classification:
216    
217          lineWidth -- an integer > 0.          lineWidth -- an integer > 0.
218          """          """
219          assert isinstance(lineWidth, IntType)          assert isinstance(lineWidth, types.IntType)
220          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)
221          self.__SendNotification()          self.__SendNotification()
222                    
# Line 426  class ClassGroupProperties: Line 422  class ClassGroupProperties:
422                   and fill color = Color.Transparent                   and fill color = Color.Transparent
423          """          """
424    
425          self.stroke = None          #self.stroke = None
426          self.strokeWidth = 0          #self.strokeWidth = 0
427          self.fill = None          #self.fill = None
428    
429          if props is not None:          if props is not None:
430              self.SetProperties(props)              self.SetProperties(props)
# Line 447  class ClassGroupProperties: Line 443  class ClassGroupProperties:
443                    
444      def GetLineColor(self):      def GetLineColor(self):
445          """Return the line color as a Color object."""          """Return the line color as a Color object."""
446          return self.stroke          return self.__stroke
447    
448      def SetLineColor(self, color):      def SetLineColor(self, color):
449          """Set the line color.          """Set the line color.
# Line 455  class ClassGroupProperties: Line 451  class ClassGroupProperties:
451          color -- the color of the line. This must be a Color object.          color -- the color of the line. This must be a Color object.
452          """          """
453    
454          assert isinstance(color, Color)          self.__stroke = color
         self.stroke = color  
455    
456      def GetLineWidth(self):      def GetLineWidth(self):
457          """Return the line width."""          """Return the line width."""
458          return self.strokeWidth          return self.__strokeWidth
459    
460      def SetLineWidth(self, lineWidth):      def SetLineWidth(self, lineWidth):
461          """Set the line width.          """Set the line width.
462    
463          lineWidth -- the new line width. This must be > 0.          lineWidth -- the new line width. This must be > 0.
464          """          """
465          assert isinstance(lineWidth, IntType)          assert isinstance(lineWidth, types.IntType)
466          if (lineWidth < 1):          if (lineWidth < 1):
467              raise ValueError(_("lineWidth < 1"))              raise ValueError(_("lineWidth < 1"))
468    
469          self.strokeWidth = lineWidth          self.__strokeWidth = lineWidth
470    
471      def GetFill(self):      def GetFill(self):
472          """Return the fill color as a Color object."""          """Return the fill color as a Color object."""
473          return self.fill          return self.__fill
474    
475      def SetFill(self, fill):      def SetFill(self, fill):
476          """Set the fill color.          """Set the fill color.
# Line 483  class ClassGroupProperties: Line 478  class ClassGroupProperties:
478          fill -- the color of the fill. This must be a Color object.          fill -- the color of the fill. This must be a Color object.
479          """          """
480    
481          assert isinstance(fill, Color)          self.__fill = fill
         self.fill = fill  
482    
483      def __eq__(self, other):      def __eq__(self, other):
484          """Return true if 'props' has the same attributes as this class"""          """Return true if 'props' has the same attributes as this class"""
485    
486            #
487            # using 'is' over '==' results in a huge performance gain
488            # in the renderer
489            #
490          return isinstance(other, ClassGroupProperties)   \          return isinstance(other, ClassGroupProperties)   \
491              and self.stroke      == other.GetLineColor() \              and (self.__stroke is other.__stroke or      \
492              and self.strokeWidth == other.GetLineWidth() \                   self.__stroke == other.__stroke)        \
493              and self.fill        == other.GetFill()              and (self.__fill is other.__fill or          \
494                     self.__fill == other.__fill)            \
495                and self.__strokeWidth == other.__strokeWidth
496    
497      def __ne__(self, other):      def __ne__(self, other):
498          return not self.__eq__(other)          return not self.__eq__(other)
# Line 503  class ClassGroupProperties: Line 503  class ClassGroupProperties:
503      def __deepcopy__(self):      def __deepcopy__(self):
504          return ClassGroupProperties(self)          return ClassGroupProperties(self)
505    
506        def __repr__(self):
507            return repr((self.__stroke, self.__strokeWidth, self.__fill))
508    
509  class ClassGroup:  class ClassGroup:
510      """A base class for all Groups within a Classification"""      """A base class for all Groups within a Classification"""
511    
512      def __init__(self, label = ""):      def __init__(self, label = "", props = None, group = None):
513          """Constructor.          """Constructor.
514    
515          label -- A string representing the Group's label          label -- A string representing the Group's label
516          """          """
517    
518          self.label = None          if group is not None:
519                self.SetLabel(copy.copy(group.GetLabel()))
520          self.SetLabel(label)              self.SetProperties(copy.copy(group.GetProperties()))
521                self.SetVisible(group.IsVisible())
522            else:
523                self.SetLabel(label)
524                self.SetProperties(props)
525                self.SetVisible(True)
526    
527      def GetLabel(self):      def GetLabel(self):
528          """Return the Group's label."""          """Return the Group's label."""
# Line 526  class ClassGroup: Line 534  class ClassGroup:
534          label -- a string representing the Group's label. This must          label -- a string representing the Group's label. This must
535                   not be None.                   not be None.
536          """          """
537          assert isinstance(label, StringType)          assert isinstance(label, types.StringTypes)
538          self.label = label          self.label = label
539    
540      def GetDisplayText(self):      def GetDisplayText(self):
# Line 542  class ClassGroup: Line 550  class ClassGroup:
550          return False          return False
551    
552      def GetProperties(self):      def GetProperties(self):
553          """Return the properties associated with the given value.          """Return the properties associated with the given value."""
554    
555          Returns None. This needs to be overridden by all subclasses.          return self.prop
         """  
         assert False, "GetProperties must be overridden by subclass!"  
         return None  
556    
557        def SetProperties(self, prop):
558            """Set the properties associated with this Group.
559    
560            prop -- a ClassGroupProperties object. if prop is None,
561                    a default set of properties is created.
562            """
563    
564            if prop is None: prop = ClassGroupProperties()
565            assert isinstance(prop, ClassGroupProperties)
566            self.prop = prop
567    
568        def IsVisible(self):
569            return self.visible
570    
571        def SetVisible(self, visible):
572            self.visible = visible
573    
574        def __eq__(self, other):
575            return isinstance(other, ClassGroup) \
576                and self.label == other.label \
577                and self.GetProperties() == other.GetProperties()
578    
579        def __ne__(self, other):
580            return not self.__eq__(other)
581    
582        def __repr__(self):
583            return repr(self.label) + ", " + repr(self.GetProperties())
584            
585  class ClassGroupSingleton(ClassGroup):  class ClassGroupSingleton(ClassGroup):
586      """A Group that is associated with a single value."""      """A Group that is associated with a single value."""
587    
588      def __init__(self, value = 0, prop = None, label = ""):      def __init__(self, value = 0, props = None, label = "", group = None):
589          """Constructor.          """Constructor.
590    
591          value -- the associated value.          value -- the associated value.
# Line 563  class ClassGroupSingleton(ClassGroup): Line 595  class ClassGroupSingleton(ClassGroup):
595    
596          label -- a label for this group.          label -- a label for this group.
597          """          """
598          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.prop = None  
         self.value = None  
599    
600          self.SetValue(value)          self.SetValue(value)
         self.SetProperties(prop)  
601    
602      def __copy__(self):      def __copy__(self):
603          return ClassGroupSingleton(self.GetValue(),          return ClassGroupSingleton(self.GetValue(),
# Line 577  class ClassGroupSingleton(ClassGroup): Line 605  class ClassGroupSingleton(ClassGroup):
605                                     self.GetLabel())                                     self.GetLabel())
606    
607      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
608          return ClassGroupSingleton(copy.copy(self.GetValue()),          return ClassGroupSingleton(self.GetValue(), group = self)
                                    copy.copy(self.GetProperties()),  
                                    copy.copy(self.GetLabel()))  
609    
610      def GetValue(self):      def GetValue(self):
611          """Return the associated value."""          """Return the associated value."""
612          return self.value          return self.__value
613    
614      def SetValue(self, value):      def SetValue(self, value):
615          """Associate this Group with the given value."""          """Associate this Group with the given value."""
616          self.value = value          self.__value = value
617    
618      def Matches(self, value):      def Matches(self, value):
619          """Determine if the given value matches the associated Group value."""          """Determine if the given value matches the associated Group value."""
620    
621          """Returns True if the value matches, False otherwise."""          """Returns True if the value matches, False otherwise."""
622    
623          return self.value == value          return self.__value == value
   
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
   
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
   
         if prop is None: prop = ClassGroupProperties()  
         assert isinstance(prop, ClassGroupProperties)  
         self.prop = prop  
624    
625      def GetDisplayText(self):      def GetDisplayText(self):
626          label = self.GetLabel()          label = self.GetLabel()
# Line 620  class ClassGroupSingleton(ClassGroup): Line 630  class ClassGroupSingleton(ClassGroup):
630          return str(self.GetValue())          return str(self.GetValue())
631    
632      def __eq__(self, other):      def __eq__(self, other):
633          return isinstance(other, ClassGroupSingleton) \          return ClassGroup.__eq__(self, other) \
634              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupSingleton) \
635              and self.GetValue() == other.GetValue()              and self.__value == other.__value
636    
637      def __ne__(self, other):      def __repr__(self):
638          return not self.__eq__(other)          return "(" + repr(self.__value) + ", " + ClassGroup.__repr__(self) + ")"
639    
640  class ClassGroupDefault(ClassGroup):  class ClassGroupDefault(ClassGroup):
641      """The default Group. When values do not match any other      """The default Group. When values do not match any other
642         Group within a Classification, the properties from this         Group within a Classification, the properties from this
643         class are used."""         class are used."""
644    
645      def __init__(self, prop = None, label = ""):      def __init__(self, props = None, label = "", group = None):
646          """Constructor.          """Constructor.
647    
648          prop -- a ClassGroupProperites object. If prop is None a default          prop -- a ClassGroupProperites object. If prop is None a default
# Line 641  class ClassGroupDefault(ClassGroup): Line 651  class ClassGroupDefault(ClassGroup):
651          label -- a label for this group.          label -- a label for this group.
652          """          """
653    
654          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
         self.SetProperties(prop)  
655    
656      def __copy__(self):      def __copy__(self):
657          return ClassGroupDefault(self.GetProperties(), self.GetLabel())          return ClassGroupDefault(self.GetProperties(), self.GetLabel())
658    
659      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
660          return ClassGroupDefault(copy.copy(self.GetProperties()),          return ClassGroupDefault(label = self.GetLabel(), group = self)
                                  copy.copy(self.GetLabel()))  
661    
662      def Matches(self, value):      def Matches(self, value):
663          return True          return True
664    
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
   
         if prop is None: prop = ClassGroupProperties()  
         assert isinstance(prop, ClassGroupProperties)  
         self.prop = prop  
   
665      def GetDisplayText(self):      def GetDisplayText(self):
666          label = self.GetLabel()          label = self.GetLabel()
667    
# Line 677  class ClassGroupDefault(ClassGroup): Line 670  class ClassGroupDefault(ClassGroup):
670          return _("DEFAULT")          return _("DEFAULT")
671    
672      def __eq__(self, other):      def __eq__(self, other):
673          return isinstance(other, ClassGroupDefault) \          return ClassGroup.__eq__(self, other) \
674                and isinstance(other, ClassGroupDefault) \
675              and self.GetProperties() == other.GetProperties()              and self.GetProperties() == other.GetProperties()
676    
677      def __ne__(self, other):      def __repr__(self):
678          return not self.__eq__(other)          return "(" + ClassGroup.__repr__(self) + ")"
679    
680  class ClassGroupRange(ClassGroup):  class ClassGroupRange(ClassGroup):
681      """A Group that represents a range of values that map to the same      """A Group that represents a range of values that map to the same
682         set of properties."""         set of properties."""
683    
684      def __init__(self, min = 0, max = 1, prop = None, label = ""):      def __init__(self, min = 0, max = 1, props = None, label = "", group=None):
685          """Constructor.          """Constructor.
686    
687          The minumum value must be strictly less than the maximum.          The minumum value must be strictly less than the maximum.
# Line 702  class ClassGroupRange(ClassGroup): Line 696  class ClassGroupRange(ClassGroup):
696          label -- a label for this group.          label -- a label for this group.
697          """          """
698    
699          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.min = self.max = 0  
         self.prop = None  
700    
701            #self.__min = self.__max = 0
702            #self.__range = Range("[" + repr(float(min)) + ";" +
703                                       #repr(float(max)) + "[")
704          self.SetRange(min, max)          self.SetRange(min, max)
         self.SetProperties(prop)  
705    
706      def __copy__(self):      def __copy__(self):
707          return ClassGroupRange(self.GetMin(),          return ClassGroupRange(min = self.__range,
708                                 self.GetMax(),                                 max = None,
709                                 self.GetProperties(),                                 props = self.GetProperties(),
710                                 self.GetLabel())                                 label = self.GetLabel())
711    
712      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
713          return ClassGroupRange(copy.copy(self.GetMin()),          return ClassGroupRange(min = copy.copy(self.__range),
714                                 copy.copy(self.GetMax()),                                 max = copy.copy(self.GetMax()),
715                                 copy.copy(self.GetProperties()),                                 group = self)
                                copy.copy(self.GetLabel()))  
716    
717      def GetMin(self):      def GetMin(self):
718          """Return the range's minimum value."""          """Return the range's minimum value."""
719          return self.min          return self.__range.GetRange()[1]
720    
721      def SetMin(self, min):      def SetMin(self, min):
722          """Set the range's minimum value.          """Set the range's minimum value.
# Line 733  class ClassGroupRange(ClassGroup): Line 725  class ClassGroupRange(ClassGroup):
725                 maximum value. Use SetRange() to change both min and max values.                 maximum value. Use SetRange() to change both min and max values.
726          """          """
727            
728          self.SetRange(min, self.max)          self.SetRange(min, self.__range.GetRange()[2])
729    
730      def GetMax(self):      def GetMax(self):
731          """Return the range's maximum value."""          """Return the range's maximum value."""
732          return self.max          return self.__range.GetRange()[2]
733    
734      def SetMax(self, max):      def SetMax(self, max):
735          """Set the range's maximum value.          """Set the range's maximum value.
# Line 745  class ClassGroupRange(ClassGroup): Line 737  class ClassGroupRange(ClassGroup):
737          max -- the new maximum. Note that this must be greater than the current          max -- the new maximum. Note that this must be greater than the current
738                 minimum value. Use SetRange() to change both min and max values.                 minimum value. Use SetRange() to change both min and max values.
739          """          """
740          self.SetRange(self.min, max)          self.SetRange(self.__range.GetRange()[1], max)
741    
742      def SetRange(self, min, max):      def SetRange(self, min, max = None):
743          """Set a new range.          """Set a new range.
744    
745          Note that min must be strictly less than max.          Note that min must be strictly less than max.
# Line 756  class ClassGroupRange(ClassGroup): Line 748  class ClassGroupRange(ClassGroup):
748          min -- the new maximum value          min -- the new maximum value
749          """          """
750    
751          if min >= max:          if isinstance(min, Range):
752              raise ValueError(_("ClassGroupRange: %i(min) >= %i(max)!") %              self.__range = min
753                               (min, max))          else:
754          self.min = min              if max is None:
755          self.max = max                  raise ValueError()
756    
757                self.__range = Range(("[", min, max, "["))
758    
759      def GetRange(self):      def GetRange(self):
760          """Return the range as a tuple (min, max)"""          """Return the range as a string"""
761          return (self.min, self.max)          #return (self.__min, self.__max)
762            return self.__range.string(self.__range.GetRange())
763    
764      def Matches(self, value):      def Matches(self, value):
765          """Determine if the given value lies with the current range.          """Determine if the given value lies with the current range.
# Line 772  class ClassGroupRange(ClassGroup): Line 767  class ClassGroupRange(ClassGroup):
767          The following check is used: min <= value < max.          The following check is used: min <= value < max.
768          """          """
769    
770          return self.min <= value < self.max          return operator.contains(self.__range, value)
771            #return self.__min <= value < self.__max
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
         if prop is None: prop = ClassGroupProperties()  
         assert isinstance(prop, ClassGroupProperties)  
         self.prop = prop  
772    
773      def GetDisplayText(self):      def GetDisplayText(self):
774          label = self.GetLabel()          label = self.GetLabel()
775    
776          if label != "": return label          if label != "": return label
777    
778          return _("%s - %s") % (self.GetMin(), self.GetMax())          #return _("%s - %s") % (self.GetMin(), self.GetMax())
779            #return repr(self.__range)
780            return self.__range.string(self.__range.GetRange())
781    
782      def __eq__(self, other):      def __eq__(self, other):
783          return isinstance(other, ClassGroupRange) \          return ClassGroup.__eq__(self, other) \
784              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupRange) \
785              and self.GetRange() == other.GetRange()              and self.__range == other.__range
786                #and self.__min == other.__min \
787      def __ne__(self, other):              #and self.__max == other.__max
788          return not self.__eq__(other)  
789        def __repr__(self):
790            return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")"
791            #return "(" + repr(self.__min) + ", " + repr(self.__max) + ", " + \
792                   #ClassGroup.__repr__(self) + ")"
793    
794  class ClassGroupMap(ClassGroup):  class ClassGroupMap(ClassGroup):
795      """Currently, this class is not used."""      """Currently, this class is not used."""

Legend:
Removed from v.613  
changed lines
  Added in v.1249

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26