File: GdalTest.py

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (181 lines) | stat: -rw-r--r-- 6,542 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-

"""
***************************************************************************
    GdalTest.py
    ---------------------
    Date                 : April 2013
    Copyright            : (C) 2013 by Victor Olaya
    Email                : volayaf at gmail dot com
***************************************************************************
*                                                                         *
*   This program 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 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'April 2013'
__copyright__ = '(C) 2013, Victor Olaya'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os
import unittest
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly

import processing
from processing.tools import dataobjects
from processing.tools.system import *

from processing.tests.TestData import points, points2, polygons, polygons2, \
    lines, union, table, polygonsGeoJson, raster


class GdalTest(unittest.TestCase):

    def test_gdalogrsieve(self):
        outputs = processing.runalg('gdalogr:sieve', raster(), 2, 0, None)
        output = outputs['dst_filename']
        self.assertTrue(os.path.isfile(output))
        dataset = gdal.Open(output, GA_ReadOnly)
        strhash = hash(str(dataset.ReadAsArray(0).tolist()))
        self.assertEqual(strhash, -1353696889)

    def test_gdalogrsieveWithUnsupportedOutputFormat(self):
        outputs = processing.runalg('gdalogr:sieve', raster(), 2, 0,
                                    getTempFilename('img'))
        output = outputs['dst_filename']
        self.assertTrue(os.path.isfile(output))
        dataset = gdal.Open(output, GA_ReadOnly)
        strhash = hash(str(dataset.ReadAsArray(0).tolist()))
        self.assertEqual(strhash, -1353696889)

    def test_gdalogrwarpreproject(self):
        outputs = processing.runalg(
            'gdalogr:warpreproject',
            raster(),
            'EPSG:23030',
            'EPSG:4326',
            0,
            0,
            '',
            None,
            )
        output = outputs['OUTPUT']
        self.assertTrue(os.path.isfile(output))
        dataset = gdal.Open(output, GA_ReadOnly)
        strhash = hash(str(dataset.ReadAsArray(0).tolist()))
        self.assertEqual(strhash, -2021328784)

    def test_gdalogrmerge(self):
        outputs = processing.runalg('gdalogr:merge', raster(), False, False,
                                    None)
        output = outputs['OUTPUT']
        self.assertTrue(os.path.isfile(output))
        dataset = gdal.Open(output, GA_ReadOnly)
        strhash = hash(str(dataset.ReadAsArray(0).tolist()))
        self.assertEqual(strhash, -1353696889)

    def test_gdalogrogr2ogr(self):
        outputs = processing.runalg('gdalogr:ogr2ogr', union(), 3, '', None)
        output = outputs['OUTPUT_LAYER']
        layer = dataobjects.getObjectFromUri(output, True)
        fields = layer.pendingFields()
        expectednames = [
            'id',
            'poly_num_a',
            'poly_st_a',
            'id_2',
            'poly_num_b',
            'poly_st_b',
            ]
        expectedtypes = [
            'Integer',
            'Real',
            'String',
            'Integer',
            'Real',
            'String',
            ]
        names = [str(f.name()) for f in fields]
        types = [str(f.typeName()) for f in fields]
        self.assertEqual(expectednames, names)
        self.assertEqual(expectedtypes, types)
        features = processing.features(layer)
        self.assertEqual(8, len(features))
        feature = features.next()
        attrs = feature.attributes()
        expectedvalues = [
            '1',
            '1.1',
            'string a',
            '2',
            '1',
            'string a',
            ]
        values = [str(attr) for attr in attrs]
        self.assertEqual(expectedvalues, values)
        wkt = 'POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))'
        self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

    def test_gdalogrogr2ogrWrongExtension(self):
        outputs = processing.runalg('gdalogr:ogr2ogr', union(), 3, '',
                                    getTempFilename('wrongext'))
        output = outputs['OUTPUT_LAYER']
        layer = dataobjects.getObjectFromUri(output, True)
        fields = layer.pendingFields()
        expectednames = [
            'id',
            'poly_num_a',
            'poly_st_a',
            'id_2',
            'poly_num_b',
            'poly_st_b',
            ]
        expectedtypes = [
            'Integer',
            'Real',
            'String',
            'Integer',
            'Real',
            'String',
            ]
        names = [str(f.name()) for f in fields]
        types = [str(f.typeName()) for f in fields]
        self.assertEqual(expectednames, names)
        self.assertEqual(expectedtypes, types)
        features = processing.features(layer)
        self.assertEqual(8, len(features))
        feature = features.next()
        attrs = feature.attributes()
        expectedvalues = [
            '1',
            '1.1',
            'string a',
            '2',
            '1',
            'string a',
            ]
        values = [str(attr) for attr in attrs]
        self.assertEqual(expectedvalues, values)
        wkt = 'POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))'
        self.assertEqual(wkt, str(feature.geometry().exportToWkt()))


def suite():
    suite = unittest.makeSuite(GdalTest, 'test')
    return suite


def runtests():
    result = unittest.TestResult()
    testsuite = suite()
    testsuite.run(result)
    return result