1 |
frank |
862 |
# 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 |
|
|
from Thuban.Model.scalebar import * |
12 |
|
|
|
13 |
|
|
from wxPython.wx import * |
14 |
|
|
|
15 |
|
|
class ScaleBar: |
16 |
|
|
|
17 |
|
|
def __init__(self, map): |
18 |
|
|
self.map = map |
19 |
|
|
|
20 |
|
|
def DrawScalebar(self, scale, dc): |
21 |
|
|
"""Draw a scalebar on a given DC""" |
22 |
|
|
|
23 |
|
|
# Only draw a legend if the corresponding map has a layer |
24 |
|
|
if self.map is not None and len(self.map.layers) > 0 and scale > 0.0: |
25 |
|
|
|
26 |
|
|
# If no projection is specified, the scale information _might_ |
27 |
|
|
# be reasonable. So gray out the scalebar in these cases. |
28 |
|
|
if self.map.projection is None: |
29 |
|
|
BlackPen = wxGREY_PEN |
30 |
|
|
BlackBrush = wxGREY_BRUSH |
31 |
|
|
BlackText = wxColor(127,127,127) |
32 |
|
|
else: |
33 |
|
|
BlackPen = wxBLACK_PEN |
34 |
|
|
BlackBrush = wxBLACK_BRUSH |
35 |
|
|
BlackText = wxBLACK |
36 |
|
|
|
37 |
|
|
# Get the dimension |
38 |
|
|
width, height = dc.GetSizeTuple() |
39 |
|
|
l1width, l1height = dc.GetTextExtent("%d"%0) |
40 |
|
|
|
41 |
|
|
# Make a first guess for the interval (to get the size we have |
42 |
|
|
# to reserve for the labels) |
43 |
|
|
interval, unit = deriveInterval(width, scale) |
44 |
|
|
l2width, l2height = dc.GetTextExtent("%d %s"%(interval,unit)) |
45 |
|
|
width = width - 4.0 - l1width/2.0 -l2width/2.0 |
46 |
|
|
|
47 |
|
|
# Having precised the width now the final interval can be calculated |
48 |
|
|
interval, unit = deriveInterval(width, scale) |
49 |
|
|
interval, label = roundInterval(interval) |
50 |
|
|
|
51 |
|
|
# We draw 2 rectangles with half the width |
52 |
|
|
if unit == 'km': |
53 |
|
|
width = int(interval*1000.0*scale/2) |
54 |
|
|
else: |
55 |
|
|
width = int(interval*scale/2) |
56 |
|
|
|
57 |
|
|
dc.SetPen(BlackPen) |
58 |
|
|
|
59 |
|
|
brush = wxBrush(wxWHITE, wxSOLID) |
60 |
|
|
dc.SetBrush(brush) |
61 |
|
|
dc.DrawRectangle(4,2,width,8) |
62 |
|
|
|
63 |
|
|
dc.SetBrush(BlackBrush) |
64 |
|
|
dc.DrawRectangle(width+4,2,width,8) |
65 |
|
|
|
66 |
|
|
dc.SetTextForeground(BlackText) |
67 |
|
|
dc.DrawText("%d"%0, 4 - l1width/2, 12) |
68 |
|
|
|
69 |
|
|
l2width, l2height = dc.GetTextExtent("%d %s"%(label, unit)) |
70 |
|
|
dc.DrawText("%d %s"%(interval, unit), 2*width+4 - l2width/2, 12) |
71 |
|
|
|