File: r_proj.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 (76 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download | duplicates (7)
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
"""
***************************************************************************
    r_proj.py
    ---------
    Date                 : October 2017
    Copyright            : (C) 2017 by Médéric Ribreux
    Email                : medspx at medspx dot fr
***************************************************************************
*                                                                         *
*   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__ = "Médéric Ribreux"
__date__ = "October 2017"
__copyright__ = "(C) 2017, Médéric Ribreux"

from qgis.core import QgsProcessingParameterString
from processing.tools.system import isWindows
from grassprovider.grass_utils import GrassUtils


def processInputs(alg, parameters, context, feedback):
    # Grab the projection from the input vector layer
    layer = alg.parameterAsLayer(parameters, "input", context)

    # Creates a new location with this Crs
    wkt_file_name = GrassUtils.exportCrsWktToFile(layer.crs(), context)
    newLocation = f"newProj{alg.uniqueSuffix}"
    alg.commands.append(f'g.proj wkt="{wkt_file_name}" location={newLocation}')

    # Go to the newly created location
    alg.commands.append(f"g.mapset mapset=PERMANENT location={newLocation}")

    # Import the layer
    alg.loadRasterLayerFromParameter("input", parameters, context, False)

    # Go back to default location
    alg.commands.append("g.mapset mapset=PERMANENT location=temp_location")

    # Grab the projected Crs
    crs = alg.parameterAsCrs(parameters, "crs", context)
    wkt_file_name = GrassUtils.exportCrsWktToFile(crs, context)
    alg.commands.append(f'g.proj -c wkt="{wkt_file_name}"')

    # Remove crs parameter
    alg.removeParameter("crs")

    # Add the location parameter with proper value
    location = QgsProcessingParameterString(
        "location", "new location", f"newProj{alg.uniqueSuffix}"
    )
    alg.addParameter(location)

    # And set the region
    grassName = alg.exportedLayers["input"]
    # We use the shell to capture the results from r.proj -g
    if isWindows():
        # TODO: make some tests under a non POSIX shell
        alg.commands.append("set regVar=")
        alg.commands.append(
            'for /f "delims=" %%a in (\'r.proj -g input^="{}" location^="{}"\') do @set regVar=%%a'.format(
                grassName, newLocation
            )
        )
        alg.commands.append("g.region -a %regVar%")
    else:
        alg.commands.append(
            'g.region -a $(r.proj -g input="{}" location="{}")'.format(
                grassName, newLocation
            )
        )