File: scalebar.py

package info (click to toggle)
thuban 1.2.2-14
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,176 kB
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 145
file content (58 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright (c) 2001, 2002 by Intevation GmbH
# Authors:
# Frank Koormann <frank.koormann@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

__version__ = "$Revision: 913 $"

from Thuban import _

def deriveInterval(width, scale):
    """Calculate scalebar interval and unit which fits width for scale."""
    try:
        interval = width / scale
    except ZeroDivisionError:
        return -1, ''

    if interval / 1000 > 1:
        interval = long(interval / 1000)
        unit = 'km'
    else: 
        interval = long(interval)
        unit = 'm'

    return interval, unit

def roundInterval(d):
    """Round float."""
    if d<.001: 
        interval = long(d*10000)/10000.0
        return interval, "%.4f" % interval
    if d<.01:
        interval = long(d*1000)/1000.0
        return interval, "%.3f" % interval
    if d<.1:
        interval = long(d*100)/100.0
        return interval, "%.2f" % interval
    if d<1:
        interval = long(d*10)/10.0
        return interval, "%.1f" % interval
    if d<10:
        return long(d), "%d" % d
    if d<100:
        interval = long(d/10) * 10
        return interval, "%d" % interval
    if d<1000:
        interval = long(d/100) * 100
        return interval, "%d" % interval
    if d<10000:
        interval = long(d/1000) * 1000
        return interval, "%d" % interval
    if d<100000:
        interval = long(d/10000) * 10000
        return interval, "%d" % interval

    return -1, ''