/[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 1176 by jonathan, Thu Jun 12 15:46:22 2003 UTC
# Line 23  on the mapping algorithm. Line 23  on the mapping algorithm.
23  # fix for people using python2.1  # fix for people using python2.1
24  from __future__ import nested_scopes  from __future__ import nested_scopes
25    
26  import copy  import copy, operator
27    
28  from Thuban import _  from Thuban import _
29    
30  from types import *  import types
31    
32  from messages import \  from messages import \
33      LAYER_PROJECTION_CHANGED, \      LAYER_PROJECTION_CHANGED, \
# Line 35  from messages import \ Line 35  from messages import \
35      LAYER_VISIBILITY_CHANGED      LAYER_VISIBILITY_CHANGED
36    
37  from Thuban.Model.color import Color  from Thuban.Model.color import Color
38    from Thuban.Model.range import Range
39    
40  import Thuban.Model.layer  import Thuban.Model.layer
41    
# Line 73  class Classification: Line 74  class Classification:
74      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
75          clazz = Classification()          clazz = Classification()
76    
77            # note: the only thing that isn't copied is the layer reference
78            clazz.field = self.field
79            clazz.fieldType = self.fieldType
80          clazz.__groups[0] = copy.deepcopy(self.__groups[0])          clazz.__groups[0] = copy.deepcopy(self.__groups[0])
81    
82          for i in range(1, len(self.__groups)):          for i in range(1, len(self.__groups)):
83              clazz.__groups.append(copy.deepcopy(self.__groups[i]))              clazz.__groups.append(copy.deepcopy(self.__groups[i]))
84    
         print "Classification.__deepcopy__"  
85          return clazz          return clazz
86    
87      def __SendNotification(self):      def __SendNotification(self):
# Line 194  class Classification: Line 197  class Classification:
197    
198          fill -- a Color object.          fill -- a Color object.
199          """          """
         assert isinstance(fill, Color)  
200          self.GetDefaultGroup().GetProperties().SetFill(fill)          self.GetDefaultGroup().GetProperties().SetFill(fill)
201          self.__SendNotification()          self.__SendNotification()
202                    
# Line 207  class Classification: Line 209  class Classification:
209    
210          color -- a Color object.          color -- a Color object.
211          """          """
         assert isinstance(color, Color)  
212          self.GetDefaultGroup().GetProperties().SetLineColor(color)          self.GetDefaultGroup().GetProperties().SetLineColor(color)
213          self.__SendNotification()          self.__SendNotification()
214                    
# Line 220  class Classification: Line 221  class Classification:
221    
222          lineWidth -- an integer > 0.          lineWidth -- an integer > 0.
223          """          """
224          assert isinstance(lineWidth, IntType)          assert isinstance(lineWidth, types.IntType)
225          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)
226          self.__SendNotification()          self.__SendNotification()
227                    
# Line 426  class ClassGroupProperties: Line 427  class ClassGroupProperties:
427                   and fill color = Color.Transparent                   and fill color = Color.Transparent
428          """          """
429    
430          self.stroke = None          #self.stroke = None
431          self.strokeWidth = 0          #self.strokeWidth = 0
432          self.fill = None          #self.fill = None
433    
434          if props is not None:          if props is not None:
435              self.SetProperties(props)              self.SetProperties(props)
# Line 447  class ClassGroupProperties: Line 448  class ClassGroupProperties:
448                    
449      def GetLineColor(self):      def GetLineColor(self):
450          """Return the line color as a Color object."""          """Return the line color as a Color object."""
451          return self.stroke          return self.__stroke
452    
453      def SetLineColor(self, color):      def SetLineColor(self, color):
454          """Set the line color.          """Set the line color.
# Line 455  class ClassGroupProperties: Line 456  class ClassGroupProperties:
456          color -- the color of the line. This must be a Color object.          color -- the color of the line. This must be a Color object.
457          """          """
458    
459          assert isinstance(color, Color)          self.__stroke = color
         self.stroke = color  
460    
461      def GetLineWidth(self):      def GetLineWidth(self):
462          """Return the line width."""          """Return the line width."""
463          return self.strokeWidth          return self.__strokeWidth
464    
465      def SetLineWidth(self, lineWidth):      def SetLineWidth(self, lineWidth):
466          """Set the line width.          """Set the line width.
467    
468          lineWidth -- the new line width. This must be > 0.          lineWidth -- the new line width. This must be > 0.
469          """          """
470          assert isinstance(lineWidth, IntType)          assert isinstance(lineWidth, types.IntType)
471          if (lineWidth < 1):          if (lineWidth < 1):
472              raise ValueError(_("lineWidth < 1"))              raise ValueError(_("lineWidth < 1"))
473    
474          self.strokeWidth = lineWidth          self.__strokeWidth = lineWidth
475    
476      def GetFill(self):      def GetFill(self):
477          """Return the fill color as a Color object."""          """Return the fill color as a Color object."""
478          return self.fill          return self.__fill
479    
480      def SetFill(self, fill):      def SetFill(self, fill):
481          """Set the fill color.          """Set the fill color.
# Line 483  class ClassGroupProperties: Line 483  class ClassGroupProperties:
483          fill -- the color of the fill. This must be a Color object.          fill -- the color of the fill. This must be a Color object.
484          """          """
485    
486          assert isinstance(fill, Color)          self.__fill = fill
         self.fill = fill  
487    
488      def __eq__(self, other):      def __eq__(self, other):
489          """Return true if 'props' has the same attributes as this class"""          """Return true if 'props' has the same attributes as this class"""
490    
491            #
492            # using 'is' over '==' results in a huge performance gain
493            # in the renderer
494            #
495          return isinstance(other, ClassGroupProperties)   \          return isinstance(other, ClassGroupProperties)   \
496              and self.stroke      == other.GetLineColor() \              and (self.__stroke is other.__stroke or      \
497              and self.strokeWidth == other.GetLineWidth() \                   self.__stroke == other.__stroke)        \
498              and self.fill        == other.GetFill()              and (self.__fill is other.__fill or          \
499                     self.__fill == other.__fill)            \
500                and self.__strokeWidth == other.__strokeWidth
501    
502      def __ne__(self, other):      def __ne__(self, other):
503          return not self.__eq__(other)          return not self.__eq__(other)
# Line 503  class ClassGroupProperties: Line 508  class ClassGroupProperties:
508      def __deepcopy__(self):      def __deepcopy__(self):
509          return ClassGroupProperties(self)          return ClassGroupProperties(self)
510    
511        def __repr__(self):
512            return repr((self.__stroke, self.__strokeWidth, self.__fill))
513    
514  class ClassGroup:  class ClassGroup:
515      """A base class for all Groups within a Classification"""      """A base class for all Groups within a Classification"""
516    
517      def __init__(self, label = ""):      def __init__(self, label = "", props = None, group = None):
518          """Constructor.          """Constructor.
519    
520          label -- A string representing the Group's label          label -- A string representing the Group's label
521          """          """
522    
523          self.label = None          if group is not None:
524                self.SetLabel(copy.copy(group.GetLabel()))
525          self.SetLabel(label)              self.SetProperties(copy.copy(group.GetProperties()))
526                self.SetVisible(group.IsVisible())
527            else:
528                self.SetLabel(label)
529                self.SetProperties(props)
530                self.SetVisible(True)
531    
532      def GetLabel(self):      def GetLabel(self):
533          """Return the Group's label."""          """Return the Group's label."""
# Line 526  class ClassGroup: Line 539  class ClassGroup:
539          label -- a string representing the Group's label. This must          label -- a string representing the Group's label. This must
540                   not be None.                   not be None.
541          """          """
542          assert isinstance(label, StringType)          assert isinstance(label, types.StringTypes)
543          self.label = label          self.label = label
544    
545      def GetDisplayText(self):      def GetDisplayText(self):
# Line 542  class ClassGroup: Line 555  class ClassGroup:
555          return False          return False
556    
557      def GetProperties(self):      def GetProperties(self):
558          """Return the properties associated with the given value.          """Return the properties associated with the given value."""
559    
560          Returns None. This needs to be overridden by all subclasses.          return self.prop
         """  
         assert False, "GetProperties must be overridden by subclass!"  
         return None  
561    
562        def SetProperties(self, prop):
563            """Set the properties associated with this Group.
564    
565            prop -- a ClassGroupProperties object. if prop is None,
566                    a default set of properties is created.
567            """
568    
569            if prop is None: prop = ClassGroupProperties()
570            assert isinstance(prop, ClassGroupProperties)
571            self.prop = prop
572    
573        def IsVisible(self):
574            return self.visible
575    
576        def SetVisible(self, visible):
577            self.visible = visible
578    
579        def __eq__(self, other):
580            return isinstance(other, ClassGroup) \
581                and self.label == other.label \
582                and self.GetProperties() == other.GetProperties()
583    
584        def __ne__(self, other):
585            return not self.__eq__(other)
586    
587        def __repr__(self):
588            return repr(self.label) + ", " + repr(self.GetProperties())
589            
590  class ClassGroupSingleton(ClassGroup):  class ClassGroupSingleton(ClassGroup):
591      """A Group that is associated with a single value."""      """A Group that is associated with a single value."""
592    
593      def __init__(self, value = 0, prop = None, label = ""):      def __init__(self, value = 0, props = None, label = "", group = None):
594          """Constructor.          """Constructor.
595    
596          value -- the associated value.          value -- the associated value.
# Line 563  class ClassGroupSingleton(ClassGroup): Line 600  class ClassGroupSingleton(ClassGroup):
600    
601          label -- a label for this group.          label -- a label for this group.
602          """          """
603          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.prop = None  
         self.value = None  
604    
605          self.SetValue(value)          self.SetValue(value)
         self.SetProperties(prop)  
606    
607      def __copy__(self):      def __copy__(self):
608          return ClassGroupSingleton(self.GetValue(),          return ClassGroupSingleton(self.GetValue(),
# Line 577  class ClassGroupSingleton(ClassGroup): Line 610  class ClassGroupSingleton(ClassGroup):
610                                     self.GetLabel())                                     self.GetLabel())
611    
612      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
613          return ClassGroupSingleton(copy.copy(self.GetValue()),          return ClassGroupSingleton(self.GetValue(), group = self)
                                    copy.copy(self.GetProperties()),  
                                    copy.copy(self.GetLabel()))  
614    
615      def GetValue(self):      def GetValue(self):
616          """Return the associated value."""          """Return the associated value."""
617          return self.value          return self.__value
618    
619      def SetValue(self, value):      def SetValue(self, value):
620          """Associate this Group with the given value."""          """Associate this Group with the given value."""
621          self.value = value          self.__value = value
622    
623      def Matches(self, value):      def Matches(self, value):
624          """Determine if the given value matches the associated Group value."""          """Determine if the given value matches the associated Group value."""
625    
626          """Returns True if the value matches, False otherwise."""          """Returns True if the value matches, False otherwise."""
627    
628          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  
629    
630      def GetDisplayText(self):      def GetDisplayText(self):
631          label = self.GetLabel()          label = self.GetLabel()
# Line 620  class ClassGroupSingleton(ClassGroup): Line 635  class ClassGroupSingleton(ClassGroup):
635          return str(self.GetValue())          return str(self.GetValue())
636    
637      def __eq__(self, other):      def __eq__(self, other):
638          return isinstance(other, ClassGroupSingleton) \          return ClassGroup.__eq__(self, other) \
639              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupSingleton) \
640              and self.GetValue() == other.GetValue()              and self.__value == other.__value
641    
642      def __ne__(self, other):      def __repr__(self):
643          return not self.__eq__(other)          return "(" + repr(self.__value) + ", " + ClassGroup.__repr__(self) + ")"
644    
645  class ClassGroupDefault(ClassGroup):  class ClassGroupDefault(ClassGroup):
646      """The default Group. When values do not match any other      """The default Group. When values do not match any other
647         Group within a Classification, the properties from this         Group within a Classification, the properties from this
648         class are used."""         class are used."""
649    
650      def __init__(self, prop = None, label = ""):      def __init__(self, props = None, label = "", group = None):
651          """Constructor.          """Constructor.
652    
653          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 656  class ClassGroupDefault(ClassGroup):
656          label -- a label for this group.          label -- a label for this group.
657          """          """
658    
659          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
         self.SetProperties(prop)  
660    
661      def __copy__(self):      def __copy__(self):
662          return ClassGroupDefault(self.GetProperties(), self.GetLabel())          return ClassGroupDefault(self.GetProperties(), self.GetLabel())
663    
664      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
665          return ClassGroupDefault(copy.copy(self.GetProperties()),          return ClassGroupDefault(label = self.GetLabel(), group = self)
                                  copy.copy(self.GetLabel()))  
666    
667      def Matches(self, value):      def Matches(self, value):
668          return True          return True
669    
     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  
   
670      def GetDisplayText(self):      def GetDisplayText(self):
671          label = self.GetLabel()          label = self.GetLabel()
672    
# Line 677  class ClassGroupDefault(ClassGroup): Line 675  class ClassGroupDefault(ClassGroup):
675          return _("DEFAULT")          return _("DEFAULT")
676    
677      def __eq__(self, other):      def __eq__(self, other):
678          return isinstance(other, ClassGroupDefault) \          return ClassGroup.__eq__(self, other) \
679                and isinstance(other, ClassGroupDefault) \
680              and self.GetProperties() == other.GetProperties()              and self.GetProperties() == other.GetProperties()
681    
682      def __ne__(self, other):      def __repr__(self):
683          return not self.__eq__(other)          return "(" + ClassGroup.__repr__(self) + ")"
684    
685  class ClassGroupRange(ClassGroup):  class ClassGroupRange(ClassGroup):
686      """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
687         set of properties."""         set of properties."""
688    
689      def __init__(self, min = 0, max = 1, prop = None, label = ""):      def __init__(self, min = 0, max = 1, props = None, label = "", group=None):
690          """Constructor.          """Constructor.
691    
692          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 701  class ClassGroupRange(ClassGroup):
701          label -- a label for this group.          label -- a label for this group.
702          """          """
703    
704          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.min = self.max = 0  
         self.prop = None  
705    
706            #self.__min = self.__max = 0
707            #self.__range = Range("[" + repr(float(min)) + ";" +
708                                       #repr(float(max)) + "[")
709          self.SetRange(min, max)          self.SetRange(min, max)
         self.SetProperties(prop)  
710    
711      def __copy__(self):      def __copy__(self):
712          return ClassGroupRange(self.GetMin(),          return ClassGroupRange(min = self.__range,
713                                 self.GetMax(),                                 max = None,
714                                 self.GetProperties(),                                 props = self.GetProperties(),
715                                 self.GetLabel())                                 label = self.GetLabel())
716    
717      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
718          return ClassGroupRange(copy.copy(self.GetMin()),          return ClassGroupRange(min = copy.copy(self.__range),
719                                 copy.copy(self.GetMax()),                                 max = copy.copy(self.GetMax()),
720                                 copy.copy(self.GetProperties()),                                 group = self)
                                copy.copy(self.GetLabel()))  
721    
722      def GetMin(self):      def GetMin(self):
723          """Return the range's minimum value."""          """Return the range's minimum value."""
724          return self.min          return self.__range.GetRange()[1]
725    
726      def SetMin(self, min):      def SetMin(self, min):
727          """Set the range's minimum value.          """Set the range's minimum value.
# Line 733  class ClassGroupRange(ClassGroup): Line 730  class ClassGroupRange(ClassGroup):
730                 maximum value. Use SetRange() to change both min and max values.                 maximum value. Use SetRange() to change both min and max values.
731          """          """
732            
733          self.SetRange(min, self.max)          self.SetRange(min, self.__range.GetRange()[2])
734    
735      def GetMax(self):      def GetMax(self):
736          """Return the range's maximum value."""          """Return the range's maximum value."""
737          return self.max          return self.__range.GetRange()[2]
738    
739      def SetMax(self, max):      def SetMax(self, max):
740          """Set the range's maximum value.          """Set the range's maximum value.
# Line 745  class ClassGroupRange(ClassGroup): Line 742  class ClassGroupRange(ClassGroup):
742          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
743                 minimum value. Use SetRange() to change both min and max values.                 minimum value. Use SetRange() to change both min and max values.
744          """          """
745          self.SetRange(self.min, max)          self.SetRange(self.__range.GetRange()[1], max)
746    
747      def SetRange(self, min, max):      def SetRange(self, min, max = None):
748          """Set a new range.          """Set a new range.
749    
750          Note that min must be strictly less than max.          Note that min must be strictly less than max.
# Line 756  class ClassGroupRange(ClassGroup): Line 753  class ClassGroupRange(ClassGroup):
753          min -- the new maximum value          min -- the new maximum value
754          """          """
755    
756          if min >= max:          if isinstance(min, Range):
757              raise ValueError(_("ClassGroupRange: %i(min) >= %i(max)!") %              self.__range = min
758                               (min, max))          else:
759          self.min = min              if max is None:
760          self.max = max                  raise ValueError()
761    
762                self.__range = Range(("[", min, max, "["))
763    
764      def GetRange(self):      def GetRange(self):
765          """Return the range as a tuple (min, max)"""          """Return the range as a string"""
766          return (self.min, self.max)          #return (self.__min, self.__max)
767            return self.__range.string(self.__range.GetRange())
768    
769      def Matches(self, value):      def Matches(self, value):
770          """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 772  class ClassGroupRange(ClassGroup):
772          The following check is used: min <= value < max.          The following check is used: min <= value < max.
773          """          """
774    
775          return self.min <= value < self.max          return operator.contains(self.__range, value)
776            #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  
777    
778      def GetDisplayText(self):      def GetDisplayText(self):
779          label = self.GetLabel()          label = self.GetLabel()
780    
781          if label != "": return label          if label != "": return label
782    
783          return _("%s - %s") % (self.GetMin(), self.GetMax())          #return _("%s - %s") % (self.GetMin(), self.GetMax())
784            #return repr(self.__range)
785            return self.__range.string(self.__range.GetRange())
786    
787      def __eq__(self, other):      def __eq__(self, other):
788          return isinstance(other, ClassGroupRange) \          return ClassGroup.__eq__(self, other) \
789              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupRange) \
790              and self.GetRange() == other.GetRange()              and self.__range == other.__range
791                #and self.__min == other.__min \
792      def __ne__(self, other):              #and self.__max == other.__max
793          return not self.__eq__(other)  
794        def __repr__(self):
795            return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")"
796            #return "(" + repr(self.__min) + ", " + repr(self.__max) + ", " + \
797                   #ClassGroup.__repr__(self) + ")"
798    
799  class ClassGroupMap(ClassGroup):  class ClassGroupMap(ClassGroup):
800      """Currently, this class is not used."""      """Currently, this class is not used."""

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26