test_map_info.py 6.45 KB
Newer Older
1
2
3
#!/usr/bin/env python
# -*- coding: utf-8 -*-

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# py_tools_ds
#
# Copyright (C) 2019  Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
#
# This software was developed within the context of the GeoMultiSens project funded
# by the German Federal Ministry of Education and Research
# (project grant code: 01 IS 14 010 A-C).
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


26
27
28
29
30
31
32
33
34
35
"""
test_map_info
-------------

Tests for `py_tools_ds.geo.map_info` module.
"""

import unittest

from py_tools_ds.geo.map_info import geotransform2mapinfo, mapinfo2geotransform
36
from py_tools_ds.geo.projection import EPSG2WKT
37
38


39
40
41
42
43
geotransform_utm = (331185.0, 30.0, -0.0, 5840115.0, -0.0, -30.0)
geotransform_utm_rotated = (331185.0, 12.202099292274006, 27.406363729278027,
                            5840115.0, 27.406363729278027, -12.202099292274006)
geotransform_local = (0, 1, 0, 0, 0, -1)
geotransform_local_rotated = (0.0, 6.123233995736766e-17, 1.0, 0.0, 1.0, -6.123233995736766e-17)
44
45
map_info_utm = ['UTM', 1, 1, 331185.0, 5840115.0, 30.0, 30.0, 33, 'North', 'WGS-84']
map_info_utm_rotated = ['UTM', 1, 1, 331185.0, 5840115.0, 30.0, 30.0, 33, 'North', 'WGS-84', 'rotation=66.00000']
46
map_info_local = ['Arbitrary', 1, 1, 0, 0, 1, 1, 0, 'North']
47
map_info_local_rotated = ['Arbitrary', 1, 1, 0, 0, 1, 1, 0, 'North', 'rotation=90.00000']
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

wkt_utm = \
    """
    PROJCS["WGS 84 / UTM zone 33N",
           GEOGCS["WGS 84",
                  DATUM["WGS_1984",
                        SPHEROID["WGS 84", 6378137, 298.257223563,
                                 AUTHORITY["EPSG", "7030"]],
                        AUTHORITY["EPSG", "6326"]],
                  PRIMEM["Greenwich", 0,
                         AUTHORITY["EPSG", "8901"]],
                  UNIT["degree", 0.0174532925199433,
                       AUTHORITY["EPSG", "9122"]],
                  AUTHORITY["EPSG", "4326"]],
           PROJECTION["Transverse_Mercator"],
           PARAMETER["latitude_of_origin", 0],
           PARAMETER["central_meridian", 15],
           PARAMETER["scale_factor", 0.9996],
           PARAMETER["false_easting", 500000],
           PARAMETER["false_northing", 0],
           UNIT["metre", 1,
                AUTHORITY["EPSG", "9001"]],
           AXIS["Easting", EAST],
           AXIS["Northing", NORTH],
           AUTHORITY["EPSG", "32633"]]
    """
74
wkt_utm = ' '.join(wkt_utm.split())
75
76
77
78
79
80
81
82
83
84
85


class Test_geotransform2mapinfo(unittest.TestCase):

    # TODO implement test in case of geographic prj

    def test_UTM_gt_prj(self):
        map_info = geotransform2mapinfo(gt=geotransform_utm, prj=wkt_utm)
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, map_info_utm)

86
    def test_gt_is_none(self):
87
88
89
90
91
        # test gt=None
        map_info = geotransform2mapinfo(gt=None, prj=wkt_utm)
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, map_info_local)

92
    def test_gt_is_arbitrary(self):
93
94
95
96
97
98
99
100
101
102
103
        # test gt=[0, 1, 0, 0, 0, -1]
        map_info = geotransform2mapinfo(gt=geotransform_local, prj=wkt_utm)
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, map_info_local)

    def test_prj_is_empty(self):
        exp_map_info = ['Arbitrary', 1, 1, 331185.0, 5840115.0, 30.0, 30.0, 0, 'North']
        map_info = geotransform2mapinfo(gt=geotransform_utm, prj='')
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, exp_map_info)

104
105
106
107
108
109
110
111
112
113
114
115
    def test_prj_is_not_wgs84(self):
        exp_map_info = ['Lambert Azimuthal Equal Area', 1.0, 1.0, 4526026.0, 3284919.5, 10.0, 10.0]
        map_info = geotransform2mapinfo(gt=(4526026.0, 10.0, 0.0, 3284919.5, 0.0, -10.0), prj=EPSG2WKT(3035))
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, exp_map_info)

    def test_prj_is_not_wgs84_rotation(self):
        exp_map_info = ['Lambert Azimuthal Equal Area', 1.0, 1.0, 331185.0, 5840115.0, 30.0, 30.0, 'rotation=66']
        map_info = geotransform2mapinfo(gt=geotransform_utm_rotated, prj=EPSG2WKT(3035))
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, exp_map_info)

116
117
118
119
120
121
122
123
124
125
    def test_gt_contains_rotation(self):
        map_info = geotransform2mapinfo(gt=geotransform_utm_rotated, prj=wkt_utm)
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, map_info_utm_rotated)

    def test_gt_contains_rotation_prj_is_local(self):
        map_info = geotransform2mapinfo(gt=geotransform_local_rotated, prj='')
        self.assertTrue(isinstance(map_info, list))
        self.assertEqual(map_info, map_info_local_rotated)

126
127
128

class Test_mapinfo2geotransform(unittest.TestCase):

129
    def test_map_info_is_valid(self):
130
        gt = mapinfo2geotransform(map_info_utm)
131
        self.assertTrue(isinstance(gt, (tuple, list)))
132
133
        self.assertEqual(gt, geotransform_utm)

134
135
        # test
        gt = mapinfo2geotransform(['Arbitrary', 1, 1, 5, -7, 1, 1, 0, 'North'])
136
137
        self.assertTrue(isinstance(gt, (tuple, list)))
        self.assertEqual(gt, (5, 1, 0, -7, 0, -1))
138

139
140
    def test_map_info_is_empty(self):
        gt = mapinfo2geotransform(None)
141
        self.assertTrue(isinstance(gt, (tuple, list)))
142
        self.assertEqual(gt, geotransform_local)
143

144
145
146
147
148
    def test_map_info_contains_LAEA_proj(self):
        gt = mapinfo2geotransform(['Lambert Azimuthal Equal Area', 1.0, 1.0, 4526026.0, 3284919.5, 10.0, 10.0])
        self.assertTrue(isinstance(gt, (tuple, list)))
        self.assertEqual(gt, (4526026.0, 10.0, 0.0, 3284919.5, 0.0, -10.0))

149
150
    def test_map_info_contains_rotation(self):
        gt = mapinfo2geotransform(map_info_utm_rotated)
151
        self.assertTrue(isinstance(gt, (tuple, list)))
152
153
154
155
        self.assertEqual(gt, geotransform_utm_rotated)

    def test_map_info_is_local_contains_rotation(self):
        gt = mapinfo2geotransform(map_info_local_rotated)
156
        self.assertTrue(isinstance(gt, (tuple, list)))
157
        self.assertEqual(gt, geotransform_local_rotated)