v_net_distance.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. ***************************************************************************
  3. v_net_distance.py
  4. ---------------------
  5. Date : December 2015
  6. Copyright : (C) 2015 by Médéric Ribreux
  7. Email : medspx at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'December 2015'
  19. __copyright__ = '(C) 2015, Médéric Ribreux'
  20. import os
  21. from .v_net import variableOutput
  22. from processing.tools.system import getTempFilename
  23. from qgis.core import QgsProcessingParameterString
  24. def processCommand(alg, parameters, context, feedback):
  25. """ Handle data preparation for v.net.distance:
  26. * Integrate point layers into network vector map.
  27. * Make v.net.distance use those layers.
  28. * Delete the threshold parameter.
  29. * If where statement, connect to the db
  30. """
  31. # Grab the point layer and delete this parameter
  32. lineLayer = alg.exportedLayers['input']
  33. fromLayer = alg.exportedLayers['flayer']
  34. toLayer = alg.exportedLayers['tlayer']
  35. intLayer = 'bufnet' + os.path.basename(getTempFilename(context=context))
  36. netLayer = 'net' + os.path.basename(getTempFilename(context=context))
  37. threshold = alg.parameterAsDouble(parameters, 'threshold', context)
  38. # Create the v.net connect command for from_layer integration
  39. command = 'v.net -s input={} points={} output={} operation=connect threshold={} arc_layer=1 node_layer=2'.format(
  40. lineLayer, fromLayer, intLayer, threshold)
  41. alg.commands.append(command)
  42. # Do it again with to_layer
  43. command = 'v.net -s input={} points={} output={} operation=connect threshold={} arc_layer=1 node_layer=3'.format(
  44. intLayer, toLayer, netLayer, threshold)
  45. alg.commands.append(command)
  46. # Connect the point layer database to the layer 2 of the network
  47. command = 'v.db.connect -o map={} table={} layer=2'.format(netLayer, fromLayer)
  48. alg.commands.append(command)
  49. command = 'v.db.connect -o map={} table={} layer=3'.format(netLayer, toLayer)
  50. alg.commands.append(command)
  51. # remove undesired parameters
  52. alg.removeParameter('flayer')
  53. alg.removeParameter('tlayer')
  54. alg.removeParameter('threshold')
  55. alg.exportedLayers['input'] = netLayer
  56. # Add the two new parameters
  57. fLayer = QgsProcessingParameterString('from_layer', None, 2, False, False)
  58. alg.addParameter(fLayer)
  59. tLayer = QgsProcessingParameterString('to_layer', None, 3, False, False)
  60. alg.addParameter(tLayer)
  61. alg.processCommand(parameters, context, feedback)
  62. def processOutputs(alg, parameters, context, feedback):
  63. outputParameter = {'output': ['output', 'line', 1, True]}
  64. variableOutput(alg, outputParameter, parameters, context)