v_net.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. ***************************************************************************
  3. v_net.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. This Python module handles pre-treatment operations for v.net.* GRASS7 modules.
  17. Before using a v.net module you often have to incorporate a points layer into
  18. the network vector map.
  19. """
  20. __author__ = 'Médéric Ribreux'
  21. __date__ = 'December 2015'
  22. __copyright__ = '(C) 2015, Médéric Ribreux'
  23. import os
  24. from qgis.core import QgsProcessingException
  25. from processing.tools.system import getTempFilename
  26. def incorporatePoints(alg, parameters, context, feedback, pointLayerName='points', networkLayerName='input'):
  27. """
  28. incorporate points with lines to form a GRASS network
  29. """
  30. # Grab the point layer and delete this parameter
  31. pointLayer = alg.parameterAsVectorLayer(parameters, pointLayerName, context)
  32. if pointLayer:
  33. # Create an intermediate GRASS layer which is the combination of network + centers
  34. intLayer = 'net' + os.path.basename(getTempFilename(context=context))
  35. pointLayer = alg.exportedLayers[pointLayerName]
  36. # Grab the network layer
  37. lineLayer = alg.parameterAsVectorLayer(parameters, networkLayerName, context)
  38. if lineLayer:
  39. lineLayer = alg.exportedLayers[networkLayerName]
  40. else:
  41. raise QgsProcessingException(
  42. alg.tr('GRASS GIS 7 v.net requires a lines layer!'))
  43. threshold = alg.parameterAsDouble(parameters, 'threshold', context)
  44. # Create the v.net connect command for point layer integration
  45. command = 'v.net -s input={} points={} output={} operation=connect threshold={}'.format(
  46. lineLayer, pointLayer, intLayer, threshold)
  47. alg.commands.append(command)
  48. # Connect the point layer database to the layer 2 of the network
  49. command = 'v.db.connect -o map={} table={} layer=2'.format(intLayer, pointLayer)
  50. alg.commands.append(command)
  51. # remove undesired parameters
  52. alg.removeParameter(pointLayerName)
  53. # Use temp layer for input
  54. alg.exportedLayers[networkLayerName] = intLayer
  55. # Process the command
  56. if 'threshold' in parameters:
  57. alg.removeParameter('threshold')
  58. alg.processCommand(parameters, context, feedback)
  59. def variableOutput(alg, layers, parameters, context, nocats=True):
  60. """ Handle variable data output for v.net modules:
  61. :param layers:
  62. layers is a dict of outputs:
  63. { 'outputName': ['srcLayer', 'output_type', output_layer_number, nocats],
  64. ...
  65. }
  66. where:
  67. - outputName is the name of the output in the description file.
  68. - srcLayer is the grass name of the layer to export.
  69. - output_type is the GRASS datatype (point/line/area/etc.).
  70. - output_layer_number is the GRASS layer number for multiple layers datasets.
  71. - nocats indicates weither we need to also export without categories items.
  72. :param parameters:
  73. :param context:
  74. :param nocats: do not add categories.
  75. """
  76. for outputName, typeList in layers.items():
  77. if not isinstance(typeList, list):
  78. continue
  79. file_name = alg.parameterAsOutputLayer(parameters, outputName, context)
  80. src_layer = typeList[0]
  81. output_type = typeList[1]
  82. output_layer_number = typeList[2]
  83. no_cats = typeList[3]
  84. grass_name = '{}{}'.format(src_layer, alg.uniqueSuffix)
  85. alg.exportVectorLayer(grassName=grass_name,
  86. fileName=file_name,
  87. layer=output_layer_number,
  88. exportnocat=no_cats,
  89. dataType=output_type)
  90. def processOutputs(alg, parameters, context, feedback):
  91. idx = alg.parameterAsInt(parameters, 'operation', context)
  92. operations = alg.parameterDefinition('operation').options()
  93. operation = operations[idx]
  94. if operation == 'nodes':
  95. outputParameter = {'output': ['output', 'point', 2, True]}
  96. elif operation == 'connect':
  97. outputParameter = {'output': ['output', 'line', 1, False]}
  98. elif operation == 'arcs':
  99. outputParameter = {'output': ['output', 'line', 1, True]}
  100. variableOutput(alg, outputParameter, parameters, context)