File: gdaltindex.py

package info (click to toggle)
qgis 3.40.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,181,336 kB
  • sloc: cpp: 1,593,302; python: 370,494; xml: 23,474; perl: 3,664; sh: 3,482; ansic: 2,257; sql: 2,133; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 157
file content (202 lines) | stat: -rw-r--r-- 7,041 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
"""
***************************************************************************
    gdaltindex.py
    ---------------------
    Date                 : February 2015
    Copyright            : (C) 2015 by Pedro Venancio
    Email                : pedrongvenancio 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__ = "Pedro Venancio"
__date__ = "February 2015"
__copyright__ = "(C) 2015, Pedro Venancio"

import os

from qgis.PyQt.QtGui import QIcon

from qgis.core import (
    QgsMapLayer,
    QgsProcessing,
    QgsProcessingAlgorithm,
    QgsProcessingException,
    QgsProcessingParameterCrs,
    QgsProcessingParameterEnum,
    QgsProcessingParameterString,
    QgsProcessingParameterBoolean,
    QgsProcessingParameterDefinition,
    QgsProcessingParameterMultipleLayers,
    QgsProcessingParameterVectorDestination,
)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class gdaltindex(GdalAlgorithm):
    LAYERS = "LAYERS"
    PATH_FIELD_NAME = "PATH_FIELD_NAME"
    ABSOLUTE_PATH = "ABSOLUTE_PATH"
    PROJ_DIFFERENCE = "PROJ_DIFFERENCE"
    TARGET_CRS = "TARGET_CRS"
    CRS_FIELD_NAME = "CRS_FIELD_NAME"
    CRS_FORMAT = "CRS_FORMAT"
    OUTPUT = "OUTPUT"

    def __init__(self):
        super().__init__()

    def initAlgorithm(self, config=None):
        self.formats = (
            (self.tr("Auto"), "AUTO"),
            (self.tr("Well-known text (WKT)"), "WKT"),
            (self.tr("EPSG"), "EPSG"),
            (self.tr("Proj.4"), "PROJ"),
        )

        self.addParameter(
            QgsProcessingParameterMultipleLayers(
                self.LAYERS, self.tr("Input files"), QgsProcessing.SourceType.TypeRaster
            )
        )
        self.addParameter(
            QgsProcessingParameterString(
                self.PATH_FIELD_NAME,
                self.tr("Field name to hold the file path to the indexed rasters"),
                defaultValue="location",
            )
        )
        self.addParameter(
            QgsProcessingParameterBoolean(
                self.ABSOLUTE_PATH,
                self.tr("Store absolute path to the indexed rasters"),
                defaultValue=False,
            )
        )
        self.addParameter(
            QgsProcessingParameterBoolean(
                self.PROJ_DIFFERENCE,
                self.tr("Skip files with different projection reference"),
                defaultValue=False,
            )
        )

        target_crs_param = QgsProcessingParameterCrs(
            self.TARGET_CRS,
            self.tr("Transform geometries to the given CRS"),
            optional=True,
        )
        target_crs_param.setFlags(
            target_crs_param.flags()
            | QgsProcessingParameterDefinition.Flag.FlagAdvanced
        )
        self.addParameter(target_crs_param)

        crs_field_param = QgsProcessingParameterString(
            self.CRS_FIELD_NAME,
            self.tr("The name of the field to store the SRS of each tile"),
            optional=True,
        )
        crs_field_param.setFlags(
            crs_field_param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
        )
        self.addParameter(crs_field_param)

        crs_format_param = QgsProcessingParameterEnum(
            self.CRS_FORMAT,
            self.tr("The format in which the CRS of each tile must be written"),
            options=[i[0] for i in self.formats],
            allowMultiple=False,
            defaultValue=0,
        )
        crs_format_param.setFlags(
            crs_format_param.flags()
            | QgsProcessingParameterDefinition.Flag.FlagAdvanced
        )
        self.addParameter(crs_format_param)

        self.addParameter(
            QgsProcessingParameterVectorDestination(
                self.OUTPUT,
                self.tr("Tile index"),
                QgsProcessing.SourceType.TypeVectorPolygon,
            )
        )

    def name(self):
        return "tileindex"

    def displayName(self):
        return self.tr("Tile index")

    def group(self):
        return self.tr("Raster miscellaneous")

    def groupId(self):
        return "rastermiscellaneous"

    def icon(self):
        return QIcon(os.path.join(pluginPath, "images", "gdaltools", "tiles.png"))

    def commandName(self):
        return "gdaltindex"

    def getConsoleCommands(self, parameters, context, feedback, executing=True):
        crs_field = self.parameterAsString(parameters, self.CRS_FIELD_NAME, context)
        crs_format = self.parameterAsEnum(parameters, self.CRS_FORMAT, context)
        target_crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

        outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
        self.setOutputValue(self.OUTPUT, outFile)
        output_details = GdalUtils.gdal_connection_details_from_uri(outFile, context)

        arguments = [
            "-tileindex",
            self.parameterAsString(parameters, self.PATH_FIELD_NAME, context),
        ]

        if self.parameterAsBoolean(parameters, self.ABSOLUTE_PATH, context):
            arguments.append("-write_absolute_path")

        if self.parameterAsBoolean(parameters, self.PROJ_DIFFERENCE, context):
            arguments.append("-skip_different_projection")

        if crs_field:
            arguments.append(f"-src_srs_name {crs_field}")

        if crs_format:
            arguments.append(f"-src_srs_format {self.formats[crs_format][1]}")

        if target_crs.isValid():
            arguments.append("-t_srs")
            arguments.append(GdalUtils.gdal_crs_string(target_crs))

        if output_details.format:
            arguments.append(f"-f {output_details.format}")

        arguments.append(output_details.connection_string)

        # Always write input files to a text file in case there are many of them and the
        # length of the command will be longer then allowed in command prompt
        list_file = GdalUtils.writeLayerParameterToTextFile(
            filename="tile_index_files.txt",
            alg=self,
            parameters=parameters,
            parameter_name=self.LAYERS,
            context=context,
            quote=True,
            executing=executing,
        )
        arguments.append("--optfile")
        arguments.append(list_file)

        return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]