Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

# Copyright (C) 2010, 2011, 2014 Ben Elliston 

# 

# Latitude/longitude spherical geodesy formulae & scripts (C) Chris Veness 2002-2011 

# (www.movable-type.co.uk/scripts/latlong.html) 

# 

# This file is free software; you can redistribute it and/or modify it 

# under the terms of the GNU General Public License as published by 

# the Free Software Foundation; either version 3 of the License, or 

# (at your option) any later version. 

 

"""Latitude and longitude support for the BoM solar irradiance grids.""" 

import math 

 

 

cellsize = 0.05 

xllcorner = 112.025 

yllcorner = -43.925 

maxcols = 839 

maxrows = 679 

 

 

class LatLong: 

 

    """A point of latitude and logitude.""" 

 

    def __init__(self, arg1, arg2, isXY=False): 

        """Initialise a lat/long object. 

 

        >>> obj = LatLong(-35, 149) 

        >>> obj = LatLong(1, 10, True) 

        >>> obj = LatLong(1, 2, True) 

        >>> obj = LatLong(679, 839, True) 

        >>> obj = LatLong(839, 679, True) 

        Traceback (most recent call last): 

          ... 

        ValueError 

        >>> obj = LatLong (499, 739, True) 

        >>> obj 

        (-34.925, 148.975) 

        """ 

        if isXY: 

            if arg1 > maxrows or arg2 > maxcols: 

                raise ValueError 

            self.lat = yllcorner + cellsize * (maxrows - arg1) 

            self.lon = xllcorner + cellsize * arg2 

        else: 

            self.lat = arg1 

            self.lon = arg2 

 

    def xy(self): 

        """ 

        Return the Cartesian coordinate. 

 

        >>> obj = LatLong(-35, 149) 

        >>> obj.xy() 

        (499, 739) 

        >>> obj = LatLong(0, 0, True) 

        >>> obj 

        (-9.975, 112.025) 

        """ 

        col = int((self.lon - xllcorner) / cellsize) 

        assert col < maxcols 

        row = int(maxrows - ((self.lat - yllcorner) / cellsize)) - 1 

        assert row >= 0 

        return row, col 

 

    def distance(self, another): 

        """ 

        Compute the distance in kilometres between this position and another. 

 

        >>> obj = LatLong (-35, 149) 

        >>> obj2 = LatLong (-36, 150) 

        >>> obj.distance (obj) 

        0.0 

        >>> print '%.1f' % obj.distance (obj2) 

        143.4 

        """ 

        # Code adapted from Chris Veness 

        R = 6371  # km 

        dlat = math.radians(another.lat - self.lat) 

        dlon = math.radians(another.lon - self.lon) 

        lat1 = math.radians(self.lat) 

        lat2 = math.radians(another.lat) 

        a = math.sin(dlat / 2) * math.sin(dlat / 2) + \ 

            math.sin(dlon / 2) * math.sin(dlon / 2) * math.cos(lat1) * math.cos(lat2) 

        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) 

        return R * c 

 

    def __repr__(self): 

        """ 

        Print object representation. 

 

        >>> obj = LatLong(-35, 149) 

        >>> print obj 

        (-35, 149) 

        """ 

        return self.__str__() 

 

    def __str__(self): 

        """ 

        Return string representation of the object. 

 

        >>> obj = LatLong(-35, 149) 

        >>> str(obj) 

        '(-35, 149)' 

        """ 

        return '(' + str(self.lat) + ', ' + str(self.lon) + ')'