/[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

trunk/thuban/Thuban/Model/scalebar.py revision 853 by frank, Wed May 7 18:23:58 2003 UTC branches/WIP-pyshapelib-bramz/Thuban/Model/scalebar.py revision 2734 by bramz, Thu Mar 1 12:42:59 2007 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, ''
58    
     def __init__(self, map):  
         self.map = map  
   
     def DrawScalebar(self, scale, dc):  
         """Draw a scalebar on a given DC"""  
   
         # 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)  
   
         # We draw 2 rectangles with half the width  
         width = int(width/2)  
   
         brush = wxBrush(wxWHITE, wxSOLID)  
         dc.SetBrush(brush)  
         dc.DrawRectangle(4,2,width,8)  
   
         brush = wxBrush(wxBLACK, wxSOLID)  
         dc.SetBrush(brush)  
         dc.DrawRectangle(width+4,2,width,8)  
   
         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  

Legend:
Removed from v.853  
changed lines
  Added in v.2734

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26