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

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

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

revision 859 by frank, Thu May 8 10:41:09 2003 UTC revision 913 by frank, Fri May 16 16:25:10 2003 UTC
# Line 9  __version__ = "$Revision$" Line 9  __version__ = "$Revision$"
9    
10  from Thuban import _  from Thuban import _
11    
12  from wxPython.wx import *  def deriveInterval(width, scale):
13        """Calculate scalebar interval and unit which fits width for scale."""
14        try:
15            interval = width / scale
16        except ZeroDivisionError:
17            return -1, ''
18    
19        if interval / 1000 > 1:
20            interval = long(interval / 1000)
21            unit = 'km'
22        else:
23            interval = long(interval)
24            unit = 'm'
25    
26        return interval, unit
27    
28    def roundInterval(d):
29        """Round float."""
30        if d<.001:
31            interval = long(d*10000)/10000.0
32            return interval, "%.4f" % interval
33        if d<.01:
34            interval = long(d*1000)/1000.0
35            return interval, "%.3f" % interval
36        if d<.1:
37            interval = long(d*100)/100.0
38            return interval, "%.2f" % interval
39        if d<1:
40            interval = long(d*10)/10.0
41            return interval, "%.1f" % interval
42        if d<10:
43            return long(d), "%d" % d
44        if d<100:
45            interval = long(d/10) * 10
46            return interval, "%d" % interval
47        if d<1000:
48            interval = long(d/100) * 100
49            return interval, "%d" % interval
50        if d<10000:
51            interval = long(d/1000) * 1000
52            return interval, "%d" % interval
53        if d<100000:
54            interval = long(d/10000) * 10000
55            return interval, "%d" % interval
56    
57  class ScaleBar:      return -1, ''
   
     def __init__(self, map):  
         self.map = map  
   
     def DrawScalebar(self, scale, dc):  
         """Draw a scalebar on a given DC"""  
   
         # Only draw a legend if the corresponding map has a layer  
         if self.map is not None and len(self.map.layers) > 0:  
   
             # If no projection is specified, the scale information _might_  
             # be reasonable. So gray out the scalebar in these cases.  
             if self.map.projection is None:  
                 BlackPen = wxGREY_PEN  
                 BlackBrush = wxGREY_BRUSH  
                 BlackText = wxColor(127,127,127)  
             else:  
                 BlackPen = wxBLACK_PEN  
                 BlackBrush = wxBLACK_BRUSH  
                 BlackText = wxBLACK  
                   
             # Get the dimension  
             width, height = dc.GetSizeTuple()  
             l1width, l1height = dc.GetTextExtent("%d"%0)  
   
             # Make a first guess for the length (to get the size we have  
             # to reserve for the labels)  
             length, unit = self.deriveLength(width, scale)  
             l2width, l2height = dc.GetTextExtent("%d %s"%(length,unit))  
             width = width - 4.0 - l1width/2.0 -l2width/2.0  
           
             # Having precised the width now the final length can be calculated  
             length, unit = self.deriveLength(width, scale)  
             length = self.roundInterval(length)  
               
             # We draw 2 rectangles with half the width  
             if unit == 'km':  
                 width = int(length*1000.0*scale/2)  
             else:  
                 width = int(length*scale/2)  
               
             dc.SetPen(BlackPen)  
   
             brush = wxBrush(wxWHITE, wxSOLID)  
             dc.SetBrush(brush)  
             dc.DrawRectangle(4,2,width,8)  
   
             dc.SetBrush(BlackBrush)  
             dc.DrawRectangle(width+4,2,width,8)  
   
             dc.SetTextForeground(BlackText)  
             dc.DrawText("%d"%0, 4 - l1width/2, 12)  
   
             l2width, l2height = dc.GetTextExtent("%d %s"%(length, unit))  
             dc.DrawText("%d %s"%(length, unit), 2*width+4 - l2width/2, 12)  
   
     def deriveLength(self, width, scale):  
   
         length = width / scale  
   
         if length / 1000 > 1:  
             length = int(length / 1000)  
             unit = 'km'  
         else:  
             length = int(length)  
             unit = 'm'  
   
         return length, unit  
   
     def roundInterval(self, d):  
         if d<.001:  
             return int(d*10000)/10000.0  
         if d<.01:  
             return int(d*1000)/1000.0  
         if d<.1:  
             return int(d*100)/100.0  
         if d<1:  
             return int(d*10)/10.0  
         if d<10:  
             return int(d)  
         if d<100:  
             return int(d/10) * 10  
         if d<1000:  
             return int(d/100) * 100  
         if d<10000:  
             return int(d/1000) * 1000  
         if d<100000:  
             return int(d/10000) * 10000  
   
         return -1  
58    

Legend:
Removed from v.859  
changed lines
  Added in v.913

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26