PointsAlongLines.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. ***************************************************************************
  3. PointsAlongLines.py
  4. ---------------------
  5. Date : Janaury 2015
  6. Copyright : (C) 2015 by Giovanni Manghi
  7. Email : giovanni dot manghi at naturalgis dot pt
  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__ = 'Giovanni Manghi'
  18. __date__ = 'January 2015'
  19. __copyright__ = '(C) 2015, Giovanni Manghi'
  20. from qgis.core import (QgsProcessingException,
  21. QgsProcessingParameterFeatureSource,
  22. QgsProcessingParameterString,
  23. QgsProcessingParameterNumber,
  24. QgsProcessingParameterVectorDestination,
  25. QgsProcessingParameterDefinition,
  26. QgsProcessing)
  27. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  28. from processing.algs.gdal.GdalUtils import GdalUtils
  29. from processing.tools.system import isWindows
  30. class PointsAlongLines(GdalAlgorithm):
  31. INPUT = 'INPUT'
  32. GEOMETRY = 'GEOMETRY'
  33. DISTANCE = 'DISTANCE'
  34. OPTIONS = 'OPTIONS'
  35. OUTPUT = 'OUTPUT'
  36. def __init__(self):
  37. super().__init__()
  38. def initAlgorithm(self, config=None):
  39. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  40. self.tr('Input layer'),
  41. [QgsProcessing.TypeVectorLine]))
  42. self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
  43. self.tr('Geometry column name'),
  44. defaultValue='geometry'))
  45. self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
  46. self.tr('Distance from line start represented as fraction of line length'),
  47. type=QgsProcessingParameterNumber.Double,
  48. minValue=0,
  49. maxValue=1,
  50. defaultValue=0.5))
  51. options_param = QgsProcessingParameterString(self.OPTIONS,
  52. self.tr('Additional creation options'),
  53. defaultValue='',
  54. optional=True)
  55. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  56. self.addParameter(options_param)
  57. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  58. self.tr('Points along lines'),
  59. QgsProcessing.TypeVectorPoint))
  60. def name(self):
  61. return 'pointsalonglines'
  62. def displayName(self):
  63. return self.tr('Points along lines')
  64. def group(self):
  65. return self.tr('Vector geoprocessing')
  66. def groupId(self):
  67. return 'vectorgeoprocessing'
  68. def commandName(self):
  69. return 'ogr2ogr'
  70. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  71. source = self.parameterAsSource(parameters, self.INPUT, context)
  72. if source is None:
  73. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  74. fields = source.fields()
  75. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  76. distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
  77. geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
  78. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  79. self.setOutputValue(self.OUTPUT, outFile)
  80. options = self.parameterAsString(parameters, self.OPTIONS, context)
  81. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  82. other_fields_exist = any(
  83. f for f in fields
  84. if f.name() != geometry
  85. )
  86. other_fields = ',*' if other_fields_exist else ''
  87. arguments = [
  88. output,
  89. ogrLayer,
  90. '-dialect',
  91. 'sqlite',
  92. '-sql',
  93. f'SELECT ST_Line_Interpolate_Point({geometry}, {distance}) AS {geometry}{other_fields} FROM "{layerName}"'
  94. ]
  95. if options:
  96. arguments.append(options)
  97. if outputFormat:
  98. arguments.append(f'-f {outputFormat}')
  99. return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]