1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
3 |
# Frank Koormann <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
__version__ = "$Revision$" |
9 |
|
10 |
from Thuban import _ |
11 |
|
12 |
from wxPython.wx import * |
13 |
|
14 |
class ScaleBar: |
15 |
|
16 |
def __init__(self, map): |
17 |
self.map = map |
18 |
|
19 |
def DrawScalebar(self, scale, dc): |
20 |
"""Draw a scalebar on a given DC""" |
21 |
|
22 |
# Only draw a legend if the corresponding map has a layer |
23 |
if self.map is not None and len(self.map.layers) > 0: |
24 |
|
25 |
# If no projection is specified, the scale information _might_ |
26 |
# be reasonable. So gray out the scalebar in these cases. |
27 |
if self.map.projection is None: |
28 |
BlackPen = wxGREY_PEN |
29 |
BlackBrush = wxGREY_BRUSH |
30 |
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 |
47 |
length, unit = self.deriveLength(width, scale) |
48 |
length = self.roundInterval(length) |
49 |
|
50 |
# We draw 2 rectangles with half the width |
51 |
if unit == 'km': |
52 |
width = int(length*1000.0*scale/2) |
53 |
else: |
54 |
width = int(length*scale/2) |
55 |
|
56 |
dc.SetPen(BlackPen) |
57 |
|
58 |
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.SetTextForeground(BlackText) |
66 |
dc.DrawText("%d"%0, 4 - l1width/2, 12) |
67 |
|
68 |
l2width, l2height = dc.GetTextExtent("%d %s"%(length, unit)) |
69 |
dc.DrawText("%d %s"%(length, unit), 2*width+4 - l2width/2, 12) |
70 |
|
71 |
def deriveLength(self, width, scale): |
72 |
|
73 |
length = width / scale |
74 |
|
75 |
if length / 1000 > 1: |
76 |
length = int(length / 1000) |
77 |
unit = 'km' |
78 |
else: |
79 |
length = int(length) |
80 |
unit = 'm' |
81 |
|
82 |
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 |
|