File: v_net_distance.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 (78 lines) | stat: -rw-r--r-- 3,310 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
77
78
"""
***************************************************************************
    v_net_distance.py
    ---------------------
    Date                 : December 2015
    Copyright            : (C) 2015 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__ = "December 2015"
__copyright__ = "(C) 2015, Médéric Ribreux"

import os
from .v_net import variableOutput
from processing.tools.system import getTempFilename
from qgis.core import QgsProcessingParameterString


def processCommand(alg, parameters, context, feedback):
    """Handle data preparation for v.net.distance:
    * Integrate point layers into network vector map.
    * Make v.net.distance use those layers.
    * Delete the threshold parameter.
    * If where statement, connect to the db
    """
    # Grab the point layer and delete this parameter
    lineLayer = alg.exportedLayers["input"]
    fromLayer = alg.exportedLayers["flayer"]
    toLayer = alg.exportedLayers["tlayer"]
    intLayer = "bufnet" + os.path.basename(getTempFilename(context=context))
    netLayer = "net" + os.path.basename(getTempFilename(context=context))
    threshold = alg.parameterAsDouble(parameters, "threshold", context)

    # Create the v.net connect command for from_layer integration
    command = "v.net -s input={} points={} output={} operation=connect threshold={} arc_layer=1 node_layer=2".format(
        lineLayer, fromLayer, intLayer, threshold
    )
    alg.commands.append(command)

    # Do it again with to_layer
    command = "v.net -s input={} points={} output={} operation=connect threshold={} arc_layer=1 node_layer=3".format(
        intLayer, toLayer, netLayer, threshold
    )
    alg.commands.append(command)

    # Connect the point layer database to the layer 2 of the network
    command = f"v.db.connect -o map={netLayer} table={fromLayer} layer=2"
    alg.commands.append(command)

    command = f"v.db.connect -o map={netLayer} table={toLayer} layer=3"
    alg.commands.append(command)

    # remove undesired parameters
    alg.removeParameter("flayer")
    alg.removeParameter("tlayer")
    alg.removeParameter("threshold")
    alg.exportedLayers["input"] = netLayer

    # Add the two new parameters
    fLayer = QgsProcessingParameterString("from_layer", None, 2, False, False)
    alg.addParameter(fLayer)
    tLayer = QgsProcessingParameterString("to_layer", None, 3, False, False)
    alg.addParameter(tLayer)
    alg.processCommand(parameters, context, feedback)


def processOutputs(alg, parameters, context, feedback):
    outputParameter = {"output": ["output", "line", 1, True]}
    variableOutput(alg, outputParameter, parameters, context)