19 |
def DrawScalebar(self, scale, dc): |
def DrawScalebar(self, scale, dc): |
20 |
"""Draw a scalebar on a given DC""" |
"""Draw a scalebar on a given DC""" |
21 |
|
|
22 |
# Get the dimension |
# Only draw a legend if the corresponding map has a layer |
23 |
width, height = dc.GetSizeTuple() |
if self.map is not None and len(self.map.layers) > 0: |
24 |
l1width, l1height = dc.GetTextExtent("%d"%0) |
|
25 |
|
# If no projection is specified, the scale information _might_ |
26 |
# Make a first guess for the length (to get the size we have to reserve |
# be reasonable. So gray out the scalebar in these cases. |
27 |
# for the labels) |
if self.map.projection is None: |
28 |
length, unit = self.__deriveLength(width, scale) |
BlackPen = wxGREY_PEN |
29 |
l2width, l2height = dc.GetTextExtent("%d %s"%(length,unit)) |
BlackBrush = wxGREY_BRUSH |
30 |
width = width - 4.0 - l1width/2.0 -l2width/2.0 |
BlackText = wxColor(127,127,127) |
31 |
|
else: |
32 |
|
BlackPen = wxBLACK_PEN |
33 |
|
BlackBrush = wxBLACK_BRUSH |
34 |
|
BlackText = wxBLACK |
35 |
|
|
36 |
|
# Get the dimension |
37 |
|
width, height = dc.GetSizeTuple() |
38 |
|
l1width, l1height = dc.GetTextExtent("%d"%0) |
39 |
|
|
40 |
|
# Make a first guess for the length (to get the size we have |
41 |
|
# to reserve for the labels) |
42 |
|
length, unit = self.deriveLength(width, scale) |
43 |
|
l2width, l2height = dc.GetTextExtent("%d %s"%(length,unit)) |
44 |
|
width = width - 4.0 - l1width/2.0 -l2width/2.0 |
45 |
|
|
46 |
# Having precised the width now the final length can be calculated |
# Having precised the width now the final length can be calculated |
47 |
length, unit = self.__deriveLength(width, scale) |
length, unit = self.deriveLength(width, scale) |
48 |
|
length = self.roundInterval(length) |
49 |
# We draw 2 rectangles with half the width |
|
50 |
width = int(width/2) |
# We draw 2 rectangles with half the width |
51 |
|
if unit == 'km': |
52 |
brush = wxBrush(wxWHITE, wxSOLID) |
width = int(length*1000.0*scale/2) |
53 |
dc.SetBrush(brush) |
else: |
54 |
dc.DrawRectangle(4,2,width,8) |
width = int(length*scale/2) |
55 |
|
|
56 |
brush = wxBrush(wxBLACK, wxSOLID) |
dc.SetPen(BlackPen) |
57 |
dc.SetBrush(brush) |
|
58 |
dc.DrawRectangle(width+4,2,width,8) |
brush = wxBrush(wxWHITE, wxSOLID) |
59 |
|
dc.SetBrush(brush) |
60 |
|
dc.DrawRectangle(4,2,width,8) |
61 |
|
|
62 |
|
dc.SetBrush(BlackBrush) |
63 |
|
dc.DrawRectangle(width+4,2,width,8) |
64 |
|
|
65 |
dc.DrawText("%d"%0, 4 - l1width/2, 12) |
dc.SetTextForeground(BlackText) |
66 |
|
dc.DrawText("%d"%0, 4 - l1width/2, 12) |
67 |
|
|
68 |
l2width, l2height = dc.GetTextExtent("%d %s"%(length, unit)) |
l2width, l2height = dc.GetTextExtent("%d %s"%(length, unit)) |
69 |
dc.DrawText("%d %s"%(length, unit), 2*width+4 - l2width/2, 12) |
dc.DrawText("%d %s"%(length, unit), 2*width+4 - l2width/2, 12) |
70 |
|
|
71 |
def __deriveLength(self, width, scale): |
def deriveLength(self, width, scale): |
72 |
|
|
73 |
length = width / scale |
length = width / scale |
74 |
|
|
80 |
unit = 'm' |
unit = 'm' |
81 |
|
|
82 |
return length, unit |
return length, unit |
83 |
|
|
84 |
|
def roundInterval(self, d): |
85 |
|
if d<.001: |
86 |
|
return int(d*10000)/10000.0 |
87 |
|
if d<.01: |
88 |
|
return int(d*1000)/1000.0 |
89 |
|
if d<.1: |
90 |
|
return int(d*100)/100.0 |
91 |
|
if d<1: |
92 |
|
return int(d*10)/10.0 |
93 |
|
if d<10: |
94 |
|
return int(d) |
95 |
|
if d<100: |
96 |
|
return int(d/10) * 10 |
97 |
|
if d<1000: |
98 |
|
return int(d/100) * 100 |
99 |
|
if d<10000: |
100 |
|
return int(d/1000) * 1000 |
101 |
|
if d<100000: |
102 |
|
return int(d/10000) * 10000 |
103 |
|
|
104 |
|
return -1 |
105 |
|
|