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 |
frank |
869 |
from Thuban.Model.scalebar import deriveInterval, roundInterval |
12 |
jonathan |
1253 |
from Thuban.Model.proj import PROJ_UNITS_METERS |
13 |
frank |
862 |
|
14 |
|
|
from wxPython.wx import * |
15 |
|
|
|
16 |
|
|
class ScaleBar: |
17 |
|
|
|
18 |
|
|
def __init__(self, map): |
19 |
|
|
self.map = map |
20 |
|
|
|
21 |
frank |
912 |
def DrawScaleBar(self, scale, dc, position, size): |
22 |
frank |
862 |
"""Draw a scalebar on a given DC""" |
23 |
|
|
|
24 |
|
|
# Only draw a legend if the corresponding map has a layer |
25 |
jonathan |
1253 |
if self.map is not None \ |
26 |
|
|
and self.map.projection is not None \ |
27 |
|
|
and len(self.map.layers) > 0 \ |
28 |
|
|
and scale > 0.0: |
29 |
frank |
862 |
|
30 |
jonathan |
1253 |
# We have a projection, draw the scalebar in bw |
31 |
|
|
BlackPen = wxBLACK_PEN |
32 |
|
|
BlackBrush = wxBLACK_BRUSH |
33 |
|
|
BlackText = wxBLACK |
34 |
frank |
862 |
|
35 |
|
|
# Get the dimension |
36 |
frank |
912 |
width, height = size |
37 |
|
|
posx, posy = position |
38 |
frank |
862 |
l1width, l1height = dc.GetTextExtent("%d"%0) |
39 |
|
|
|
40 |
|
|
# Make a first guess for the interval (to get the size we have |
41 |
|
|
# to reserve for the labels) |
42 |
|
|
interval, unit = deriveInterval(width, scale) |
43 |
|
|
l2width, l2height = dc.GetTextExtent("%d %s"%(interval,unit)) |
44 |
|
|
width = width - 4.0 - l1width/2.0 -l2width/2.0 |
45 |
|
|
|
46 |
|
|
# Having precised the width now the final interval can be calculated |
47 |
|
|
interval, unit = deriveInterval(width, scale) |
48 |
|
|
interval, label = roundInterval(interval) |
49 |
|
|
|
50 |
frank |
871 |
if interval > 0.0: |
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 |
frank |
862 |
|
57 |
frank |
871 |
dc.SetPen(BlackPen) |
58 |
frank |
862 |
|
59 |
frank |
871 |
brush = wxBrush(wxWHITE, wxSOLID) |
60 |
|
|
dc.SetBrush(brush) |
61 |
frank |
912 |
dc.DrawRectangle(posx+4,posy+2,width,8) |
62 |
frank |
862 |
|
63 |
frank |
871 |
dc.SetBrush(BlackBrush) |
64 |
frank |
912 |
dc.DrawRectangle(posx+width+4,posy+2,width,8) |
65 |
frank |
862 |
|
66 |
frank |
871 |
dc.SetTextForeground(BlackText) |
67 |
frank |
912 |
dc.DrawText("%d"%0, posx+ 4 - l1width/2, posy+12) |
68 |
frank |
862 |
|
69 |
frank |
871 |
l2width, l2height = dc.GetTextExtent("%s %s"%(label, unit)) |
70 |
frank |
912 |
dc.DrawText("%s %s"%(interval, unit), posx+ 2*width+4 - l2width/2, posy + 12) |
71 |
frank |
862 |
|