File: OTBSpecific_XMLLoading.py

package info (click to toggle)
qgis 2.18.28%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,007,948 kB
  • sloc: cpp: 671,774; python: 158,539; xml: 35,690; ansic: 8,346; sh: 1,766; perl: 1,669; sql: 999; yacc: 836; lex: 461; makefile: 292
file content (356 lines) | stat: -rw-r--r-- 12,833 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
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
# -*- coding: utf-8 -*-

"""
***************************************************************************
    OTBSpecific_XMLLoading.py
    -------------------------
    Date                 : 11-12-13
    Copyright            : (C) 2013 by CS Systemes d'information (CS SI)
    Email                : otb at c-s dot fr (CS SI)
    Contributors         : Julien Malik (CS SI)  - creation of otbspecific
                           Oscar Picas (CS SI)   -
                           Alexia Mondot (CS SI) - split otbspecific into 2 files
                                           add functions
***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************

When an OTB algorithms is run, this file allows adapting user parameter to fit the otbapplication.

Most of the following functions are like follows :
    adaptNameOfTheOTBApplication(commands_list)
The command list is a list of all parameters of the given algorithm with all user values.
"""


__author__ = 'Julien Malik, Oscar Picas, Alexia Mondot'
__date__ = 'December 2013'
__copyright__ = '(C) 2013, CS Systemes d\'information  (CS SI)'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
__version__ = "3.8"

import os

try:
    import processing  # NOQA
except ImportError as e:
    raise Exception("Processing must be installed and available in PYTHONPATH")

from processing.core.ProcessingConfig import ProcessingConfig

from . import OTBUtils


def adaptBinaryMorphologicalOperation(commands_list):
    val = commands_list[commands_list.index("-filter") + 1]

    def replace_dilate(param, value):
        if ".dilate" in str(param):
            return param.replace("dilate", value)
        else:
            return param

    import functools
    com_list = map(functools.partial(replace_dilate, value=val), commands_list)

    val = com_list[com_list.index("-structype.ball.xradius") + 1]

    pos = com_list.index("-structype.ball.xradius") + 2

    com_list.insert(pos, '-structype.ball.yradius')
    com_list.insert(pos + 1, val)

    return com_list


def adaptEdgeExtraction(commands_list):
    """
    Add filter.touzi.yradius as the same value as filter.touzi.xradius
    """
    val = commands_list[commands_list.index("-filter") + 1]
    if val == 'touzi':
        bval = commands_list[commands_list.index("-filter.touzi.xradius") + 1]
        pos = commands_list.index("-filter.touzi.xradius") + 2
        commands_list.insert(pos, "-filter.touzi.yradius")
        commands_list.insert(pos + 1, bval)
    return commands_list


def adaptGrayScaleMorphologicalOperation(commands_list):
    """
    Add structype.ball.yradius as the same value as structype.ball.xradius (as it is a ball)
    """
    val = commands_list[commands_list.index("-structype.ball.xradius") + 1]
    pos = commands_list.index("-structype.ball.xradius") + 2
    commands_list.insert(pos, "-structype.ball.yradius")
    commands_list.insert(pos + 1, val)
    return commands_list


def adaptSplitImage(commands_list):
    """
    Ran by default, the extension of output file is .file. Replace it with ".tif"
    If no extension given, put ".tif" at the end of the filename.
    """
    commands_list2 = []
    for item in commands_list:
        if ".file" in item:
            item = item.replace(".file", ".tif")
        if item == "-out":
            index = commands_list.index(item)
            if "." not in os.path.basename(commands_list[index + 1]):
                commands_list[index + 1] = commands_list[index + 1][:-1] + ".tif" + commands_list[index + 1][-1]
        commands_list2.append(item)
    return commands_list2


def adaptLSMSVectorization(commands_list):
    """
    Ran by default, the extension of output file is .file. Replace it with ".shp"
    If no extension given, put ".shp" at the end of the filename.
    """
    commands_list2 = []
    for item in commands_list:
        if ".file" in item:
            item = item.replace(".file", ".shp")
        if item == "-out":
            index = commands_list.index(item)
            if "." not in os.path.basename(commands_list[index + 1]):
                commands_list[index + 1] = commands_list[index + 1][:-1] + ".shp" + commands_list[index + 1][-1]
        commands_list2.append(item)

    return commands_list2


def adaptComputeImagesStatistics(commands_list):
    """
    Ran by default, the extension of output file is .file. Replace it with ".xml"
    If no extension given, put ".shp" at the end of the filename.
    """
    commands_list2 = []
    for item in commands_list:
        if ".file" in item:
            item = item.replace(".file", ".xml")
        commands_list2.append(item)
        if item == "-out":
            index = commands_list.index(item)
            if "." not in os.path.basename(commands_list[index + 1]):
                commands_list[index + 1] = commands_list[index + 1][:-1] + ".xml" + commands_list[index + 1][-1]

    return commands_list2


def adaptKmzExport(commands_list):
    """
    Ran by default, the extension of output file is .file. Replace it with ".kmz"
    If no extension given, put ".kmz" at the end of the filename.
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    commands_list2 = []
    for item in commands_list:
        if ".file" in item:
            item = item.replace(".file", ".kmz")
        if item == "-out":
            index = commands_list.index(item)
            if "." not in os.path.basename(commands_list[index + 1]):
                commands_list[index + 1] = commands_list[index + 1][:-1] + ".kmz" + commands_list[index + 1][-1]

        commands_list2.append(item)
    return commands_list2


def adaptColorMapping(commands_list):
    """
    The output of this algorithm must be in uint8.
    """
    indexInput = commands_list.index("-out")
    commands_list[indexInput + 1] = commands_list[indexInput + 1] + '" "uint8"'
    return commands_list


def adaptStereoFramework(commands_list):
    """
    Remove parameter and user value instead of giving None.
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    commands_list2 = commands_list
    adaptGeoidSrtm(commands_list2)
    for item in commands_list:
        if "None" in item:
            index = commands_list2.index(item)
            argumentToRemove = commands_list2[index - 1]
            commands_list2.remove(item)
            commands_list2.remove(argumentToRemove)
    return commands_list2


def adaptComputeConfusionMatrix(commands_list):
    """
    Ran by default, the extension of output file is .file. Replace it with ".csv"
    If no extension given, put ".csv" at the end of the filename.
    """
    commands_list2 = []
    for item in commands_list:
        if ".file" in item:
            item = item.replace(".file", ".csv")
        if item == "-out":
            index = commands_list.index(item)
            if "." not in os.path.basename(commands_list[index + 1]):
                commands_list[index + 1] = commands_list[index + 1][:-1] + ".csv" + commands_list[index + 1][-1]

        commands_list2.append(item)
    return commands_list2


def adaptRadiometricIndices(commands_list):
    """
    Replace indice nickname by its corresponding entry in the following dictionary :
    indices = {"ndvi" : "Vegetation:NDVI", "tndvi" : "Vegetation:TNDVI",  "rvi" : "Vegetation:RVI",  "savi" : "Vegetation:SAVI",
           "tsavi" : "Vegetation:TSAVI", "msavi" : "Vegetation:MSAVI",  "msavi2" : "Vegetation:MSAVI2",  "gemi" : "Vegetation:GEMI",
           "ipvi" : "Vegetation:IPVI",
           "ndwi" : "Water:NDWI", "ndwi2" : "Water:NDWI2", "mndwi" :"Water:MNDWI" , "ndpi" : "Water:NDPI",
           "ndti" : "Water:NDTI",
           "ri" : "Soil:RI", "ci" : "Soil:CI", "bi" : "Soil:BI", "bi2" : "Soil:BI2"}
    """
#                 "laindvilog" : , "lairefl" : , "laindviformo" : ,
    indices = {"ndvi": "Vegetation:NDVI", "tndvi": "Vegetation:TNDVI", "rvi": "Vegetation:RVI", "savi": "Vegetation:SAVI",
               "tsavi": "Vegetation:TSAVI", "msavi": "Vegetation:MSAVI", "msavi2": "Vegetation:MSAVI2", "gemi": "Vegetation:GEMI",
               "ipvi": "Vegetation:IPVI",
               "ndwi": "Water:NDWI", "ndwi2": "Water:NDWI2", "mndwi": "Water:MNDWI", "ndpi": "Water:NDPI",
               "ndti": "Water:NDTI",
               "ri": "Soil:RI", "ci": "Soil:CI", "bi": "Soil:BI", "bi2": "Soil:BI2"}
    for item in commands_list:
        if item in indices:
            commands_list[commands_list.index(item)] = indices[item]
    return commands_list


def adaptDisparityMapToElevationMap(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    return commands_list


def adaptConnectedComponentSegmentation(commands_list):
    """
    Remove parameter and user value instead of giving None.
    """
    commands_list2 = commands_list
    adaptGeoidSrtm(commands_list2)
    for item in commands_list:
        if "None" in item:
            index = commands_list2.index(item)
            argumentToRemove = commands_list2[index - 1]
            commands_list2.remove(item)
            commands_list2.remove(argumentToRemove)
        #commands_list2.append(item)
    return commands_list2


def adaptSuperimpose(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    return commands_list


def adaptOrthoRectification(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    return commands_list


def adaptExtractROI(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    return commands_list


def adaptTrainImagesClassifier(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    adaptGeoidSrtm(commands_list)
    return commands_list


def adaptGeoidSrtm(commands_list):
    """
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    srtm, geoid = ckeckGeoidSrtmSettings()

    if srtm:
        if commands_list[0].endswith("ExtractROI"):
            commands_list.append("-mode.fit.elev.dem")
            commands_list.append(srtm)
        else:
            commands_list.append("-elev.dem")
            commands_list.append(srtm)
    if geoid:
        if commands_list[0].endswith("ExtractROI"):
            commands_list.append("-mode.fit.elev.geoid")
            commands_list.append(geoid)
        else:
            commands_list.append("-elev.geoid")
            commands_list.append(geoid)


def adaptComputePolylineFeatureFromImage(commands_list):
    """
    Remove parameter and user value instead of giving None.
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    commands_list2 = commands_list
    adaptGeoidSrtm(commands_list2)
    for item in commands_list:
        if "None" in item:
            index = commands_list2.index(item)
            argumentToRemove = commands_list2[index - 1]
            commands_list2.remove(item)
            commands_list2.remove(argumentToRemove)
        # commands_list2.append(item)
    return commands_list2


def adaptComputeOGRLayersFeaturesStatistics(commands_list):
    """
    Remove parameter and user value instead of giving None.
    Check geoid file, srtm folder and given elevation and manage arguments.
    """
    commands_list2 = commands_list
    adaptGeoidSrtm(commands_list2)
    for item in commands_list:
        if "None" in item:
            index = commands_list2.index(item)
            argumentToRemove = commands_list2[index - 1]
            commands_list2.remove(item)
            commands_list2.remove(argumentToRemove)
        # commands_list2.append(item)
    return commands_list2


def ckeckGeoidSrtmSettings():
    folder = ProcessingConfig.getSetting(OTBUtils.OTB_SRTM_FOLDER)
    if folder is None:
        folder = ""

    filepath = ProcessingConfig.getSetting(OTBUtils.OTB_GEOID_FILE)
    if filepath is None:
        filepath = ""

    return folder, filepath