File: GdalAlgorithmsGeneralTest.py

package info (click to toggle)
qgis 3.40.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,185,400 kB
  • sloc: cpp: 1,616,418; python: 372,869; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,829; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 155
file content (413 lines) | stat: -rw-r--r-- 16,288 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
***************************************************************************
    GdalAlgorithmTests.py
    ---------------------
    Date                 : January 2016
    Copyright            : (C) 2016 by Matthias Kuhn
    Email                : matthias@opengis.ch
***************************************************************************
*                                                                         *
*   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__ = "Matthias Kuhn"
__date__ = "January 2016"
__copyright__ = "(C) 2016, Matthias Kuhn"

import nose2
import os
import shutil
import tempfile

from qgis.core import (
    QgsProcessingContext,
    QgsProcessingFeedback,
    QgsCoordinateReferenceSystem,
    QgsApplication,
    QgsFeature,
    QgsGeometry,
    QgsPointXY,
    QgsProject,
    QgsVectorLayer,
    QgsRectangle,
    QgsProjUtils,
    QgsProcessingException,
    QgsProcessingFeatureSourceDefinition,
)

from qgis.testing import QgisTestCase, start_app

from processing.algs.gdal.GdalUtils import GdalUtils
from processing.algs.gdal.ogr2ogr import ogr2ogr
from processing.algs.gdal.OgrToPostGis import OgrToPostGis

testDataPath = os.path.join(os.path.dirname(__file__), "testdata")


class TestGdalAlgorithms(QgisTestCase):

    @classmethod
    def setUpClass(cls):
        start_app()
        from processing.core.Processing import Processing

        Processing.initialize()
        cls.cleanup_paths = []

    @classmethod
    def tearDownClass(cls):
        for path in cls.cleanup_paths:
            shutil.rmtree(path)

    def testCommandName(self):
        # Test that algorithms report a valid commandName
        p = QgsApplication.processingRegistry().providerById("gdal")
        for a in p.algorithms():
            if a.id() in ("gdal:buildvirtualvector"):
                # build virtual vector is an exception
                continue
            self.assertTrue(a.commandName(), f"Algorithm {a.id()} has no commandName!")

    def testCommandNameInTags(self):
        # Test that algorithms commandName is present in provided tags
        p = QgsApplication.processingRegistry().providerById("gdal")
        for a in p.algorithms():
            if not a.commandName():
                continue
            self.assertTrue(
                a.commandName() in a.tags(),
                f"Algorithm {a.id()} commandName not found in tags!",
            )

    def testNoParameters(self):
        # Test that algorithms throw QgsProcessingException and not base Python
        # exceptions when no parameters specified
        p = QgsApplication.processingRegistry().providerById("gdal")
        context = QgsProcessingContext()
        feedback = QgsProcessingFeedback()
        for a in p.algorithms():
            try:
                a.getConsoleCommands({}, context, feedback)
            except QgsProcessingException:
                pass

    def testGetOgrCompatibleSourceFromMemoryLayer(self):
        # create a memory layer and add to project and context
        layer = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "testmem", "memory"
        )
        self.assertTrue(layer.isValid())
        pr = layer.dataProvider()
        f = QgsFeature()
        f.setAttributes(["test", 123])
        f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200)))
        f2 = QgsFeature()
        f2.setAttributes(["test2", 457])
        f2.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200)))
        self.assertTrue(pr.addFeatures([f, f2]))
        self.assertEqual(layer.featureCount(), 2)
        QgsProject.instance().addMapLayer(layer)
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())

        alg = QgsApplication.processingRegistry().createAlgorithmById(
            "gdal:buffervectors"
        )
        self.assertIsNotNone(alg)
        parameters = {"INPUT": "testmem"}
        feedback = QgsProcessingFeedback()
        # check that memory layer is automatically saved out to geopackage when required by GDAL algorithms
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, executing=True
        )
        self.assertTrue(input_details.connection_string)
        self.assertTrue(input_details.connection_string.endswith(".gpkg"))
        self.assertTrue(os.path.exists(input_details.connection_string))
        self.assertTrue(input_details.connection_string)

        # make sure that layer has correct features
        res = QgsVectorLayer(input_details.connection_string, "res")
        self.assertTrue(res.isValid())
        self.assertEqual(res.featureCount(), 2)

        # with memory layers - if not executing layer source should be ignored and replaced
        # with a dummy path, because:
        # - it has no meaning for the gdal command outside of QGIS, memory layers don't exist!
        # - we don't want to force an export of the whole memory layer to a temp file just to show the command preview
        # this might be very slow!
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, executing=False
        )
        self.assertEqual(input_details.connection_string, "path_to_data_file")
        self.assertEqual(input_details.layer_name, "layer_name")

        QgsProject.instance().removeMapLayer(layer)

    def testGetOgrCompatibleSourceFromOgrLayer(self):
        p = QgsProject()
        source = os.path.join(testDataPath, "points.gml")
        vl = QgsVectorLayer(source)
        self.assertTrue(vl.isValid())
        p.addMapLayer(vl)

        context = QgsProcessingContext()
        context.setProject(p)
        feedback = QgsProcessingFeedback()

        alg = ogr2ogr()
        alg.initAlgorithm()
        input_details = alg.getOgrCompatibleSource(
            "INPUT", {"INPUT": vl.id()}, context, feedback, True
        )
        self.assertEqual(input_details.connection_string, source)
        input_details = alg.getOgrCompatibleSource(
            "INPUT", {"INPUT": vl.id()}, context, feedback, False
        )
        self.assertEqual(input_details.connection_string, source)

        # with selected features only - if not executing, the 'selected features only' setting
        # should be ignored (because it has no meaning for the gdal command outside of QGIS!)
        parameters = {"INPUT": QgsProcessingFeatureSourceDefinition(vl.id(), True)}
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, False
        )
        self.assertEqual(input_details.connection_string, source)

        # with subset string
        vl.setSubsetString("x")
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, False
        )
        self.assertEqual(input_details.connection_string, source)
        # subset of layer must be exported
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, True
        )
        self.assertNotEqual(input_details.connection_string, source)
        self.assertTrue(input_details.connection_string)
        self.assertTrue(input_details.connection_string.endswith(".gpkg"))
        self.assertTrue(os.path.exists(input_details.connection_string))
        self.assertTrue(input_details.layer_name)

        # geopackage with layer
        source = os.path.join(testDataPath, "custom", "circular_strings.gpkg")
        vl2 = QgsVectorLayer(source + "|layername=circular_strings")
        self.assertTrue(vl2.isValid())
        p.addMapLayer(vl2)
        input_details = alg.getOgrCompatibleSource(
            "INPUT", {"INPUT": vl2.id()}, context, feedback, True
        )
        self.assertEqual(input_details.connection_string, source)
        self.assertEqual(input_details.layer_name, "circular_strings")
        vl3 = QgsVectorLayer(source + "|layername=circular_strings_with_line")
        self.assertTrue(vl3.isValid())
        p.addMapLayer(vl3)
        input_details = alg.getOgrCompatibleSource(
            "INPUT", {"INPUT": vl3.id()}, context, feedback, True
        )
        self.assertEqual(input_details.connection_string, source)
        self.assertEqual(input_details.layer_name, "circular_strings_with_line")

    def testGetOgrCompatibleSourceFromFeatureSource(self):
        # create a memory layer and add to project and context
        layer = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "testmem", "memory"
        )
        self.assertTrue(layer.isValid())
        pr = layer.dataProvider()
        f = QgsFeature()
        f.setAttributes(["test", 123])
        f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200)))
        f2 = QgsFeature()
        f2.setAttributes(["test2", 457])
        f2.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200)))
        self.assertTrue(pr.addFeatures([f, f2]))
        self.assertEqual(layer.featureCount(), 2)
        # select first feature
        layer.selectByIds([next(layer.getFeatures()).id()])
        self.assertEqual(len(layer.selectedFeatureIds()), 1)
        QgsProject.instance().addMapLayer(layer)
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())

        alg = QgsApplication.processingRegistry().createAlgorithmById(
            "gdal:buffervectors"
        )
        self.assertIsNotNone(alg)
        parameters = {"INPUT": QgsProcessingFeatureSourceDefinition("testmem", True)}
        feedback = QgsProcessingFeedback()
        # check that memory layer is automatically saved out to geopackage when required by GDAL algorithms
        input_details = alg.getOgrCompatibleSource(
            "INPUT", parameters, context, feedback, executing=True
        )
        self.assertTrue(input_details.connection_string)
        self.assertTrue(input_details.connection_string.endswith(".gpkg"))
        self.assertTrue(os.path.exists(input_details.connection_string))
        self.assertTrue(input_details.layer_name)

        # make sure that layer has only selected feature
        res = QgsVectorLayer(input_details.connection_string, "res")
        self.assertTrue(res.isValid())
        self.assertEqual(res.featureCount(), 1)

        QgsProject.instance().removeMapLayer(layer)

    def testOgrLayerNameExtraction(self):
        with tempfile.TemporaryDirectory() as outdir:

            def _copyFile(dst):
                shutil.copyfile(
                    os.path.join(testDataPath, "custom", "weighted.csv"), dst
                )

            # OGR provider - single layer
            _copyFile(os.path.join(outdir, "a.csv"))
            name = GdalUtils.ogrLayerName(outdir)
            self.assertEqual(name, "a")

            # OGR provider - multiple layers
            _copyFile(os.path.join(outdir, "b.csv"))
            name1 = GdalUtils.ogrLayerName(outdir + "|layerid=0")
            name2 = GdalUtils.ogrLayerName(outdir + "|layerid=1")
            self.assertEqual(sorted([name1, name2]), ["a", "b"])

            name = GdalUtils.ogrLayerName(outdir + "|layerid=2")
            self.assertIsNone(name)

            # OGR provider - layername takes precedence
            name = GdalUtils.ogrLayerName(outdir + "|layername=f")
            self.assertEqual(name, "f")

            name = GdalUtils.ogrLayerName(outdir + "|layerid=0|layername=f")
            self.assertEqual(name, "f")

            name = GdalUtils.ogrLayerName(outdir + "|layername=f|layerid=0")
            self.assertEqual(name, "f")

            # SQLite provider
            name = GdalUtils.ogrLayerName(
                "dbname='/tmp/x.sqlite' table=\"t\" (geometry) sql="
            )
            self.assertEqual(name, "t")

            # PostgreSQL provider
            name = GdalUtils.ogrLayerName(
                'port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql='
            )
            self.assertEqual(name, "city_data.edge")

    def test_gdal_connection_details_from_uri(self):
        context = QgsProcessingContext()
        output_details = GdalUtils.gdal_connection_details_from_uri(
            "d:/test/test.shp", context
        )
        self.assertEqual(output_details.connection_string, "d:/test/test.shp")
        self.assertEqual(output_details.format, '"ESRI Shapefile"')
        output_details = GdalUtils.gdal_connection_details_from_uri(
            "d:/test/test.mif", context
        )
        self.assertEqual(output_details.connection_string, "d:/test/test.mif")
        self.assertEqual(output_details.format, '"MapInfo File"')

    def testConnectionString(self):
        alg = OgrToPostGis()
        alg.initAlgorithm()
        parameters = {}
        feedback = QgsProcessingFeedback()
        context = QgsProcessingContext()

        # NOTE: defaults are debatable, see
        # https://github.com/qgis/QGIS/pull/3607#issuecomment-253971020
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "host=localhost port=5432 active_schema=public",
        )

        parameters["HOST"] = "remote"
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "host=remote port=5432 active_schema=public",
        )

        parameters["HOST"] = ""
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "port=5432 active_schema=public",
        )

        parameters["PORT"] = "5555"
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "port=5555 active_schema=public",
        )

        parameters["PORT"] = ""
        self.assertEqual(
            alg.getConnectionString(parameters, context), "active_schema=public"
        )

        parameters["USER"] = "usr"
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "active_schema=public user=usr",
        )

        parameters["PASSWORD"] = "pwd"
        self.assertEqual(
            alg.getConnectionString(parameters, context),
            "password=pwd active_schema=public user=usr",
        )

    def testCrsConversion(self):
        self.assertFalse(GdalUtils.gdal_crs_string(QgsCoordinateReferenceSystem()))
        self.assertEqual(
            GdalUtils.gdal_crs_string(QgsCoordinateReferenceSystem("EPSG:3111")),
            "EPSG:3111",
        )
        self.assertEqual(
            GdalUtils.gdal_crs_string(QgsCoordinateReferenceSystem("POSTGIS:3111")),
            "EPSG:3111",
        )
        self.assertEqual(
            GdalUtils.gdal_crs_string(
                QgsCoordinateReferenceSystem(
                    "proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs"
                )
            ),
            "EPSG:20936",
        )
        crs = QgsCoordinateReferenceSystem()
        crs.createFromProj(
            "+proj=utm +zone=36 +south +a=600000 +b=70000 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs"
        )
        self.assertTrue(crs.isValid())

        # proj 6, WKT should be used
        self.assertEqual(
            GdalUtils.gdal_crs_string(crs)[:40],
            'BOUNDCRS[SOURCECRS[PROJCRS["unknown",BAS',
        )

        self.assertEqual(
            GdalUtils.gdal_crs_string(QgsCoordinateReferenceSystem("ESRI:102003")),
            "ESRI:102003",
        )

    def testEscapeAndJoin(self):
        self.assertEqual(
            GdalUtils.escapeAndJoin([1, "a", "a b", "a&b", "a(b)", ";"]),
            '1 a "a b" "a&b" "a(b)" ";"',
        )
        self.assertEqual(
            GdalUtils.escapeAndJoin([1, "-srcnodata", "--srcnodata", "-9999 9999"]),
            '1 -srcnodata --srcnodata "-9999 9999"',
        )


if __name__ == "__main__":
    nose2.main()