Skip to content

Commit

Permalink
Resurrect OGR points along lines algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 13, 2017
1 parent ab079f9 commit 6301ff6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
30 changes: 15 additions & 15 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ def getConsoleCommands(self, parameters, context, feedback):

def processAlgorithm(self, parameters, context, feedback):
commands = self.getConsoleCommands(parameters, context, feedback)
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
supported = QgsVectorFileWriter.supportedFormatExtensions()
for i, c in enumerate(commands):
for layer in layers:
if layer.source() in c:
exported = dataobjects.exportVectorLayer(layer, supported)
exportedFileName = os.path.splitext(os.path.split(exported)[1])[0]
c = c.replace(layer.source(), exported)
if os.path.isfile(layer.source()):
fileName = os.path.splitext(os.path.split(layer.source())[1])[0]
c = re.sub('[\s]{}[\s]'.format(fileName), ' ' + exportedFileName + ' ', c)
c = re.sub('[\s]{}'.format(fileName), ' ' + exportedFileName, c)
c = re.sub('["\']{}["\']'.format(fileName), "'" + exportedFileName + "'", c)

commands[i] = c
#layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
#supported = QgsVectorFileWriter.supportedFormatExtensions()
#for i, c in enumerate(commands):
# for layer in layers:
# if layer.source() in c:
# exported = dataobjects.exportVectorLayer(layer, supported)
# exportedFileName = os.path.splitext(os.path.split(exported)[1])[0]
# c = c.replace(layer.source(), exported)
# if os.path.isfile(layer.source()):
# fileName = os.path.splitext(os.path.split(layer.source())[1])[0]
# c = re.sub('[\s]{}[\s]'.format(fileName), ' ' + exportedFileName + ' ', c)
# c = re.sub('[\s]{}'.format(fileName), ' ' + exportedFileName, c)
# c = re.sub('["\']{}["\']'.format(fileName), "'" + exportedFileName + "'", c)

# commands[i] = c
GdalUtils.runGdal(commands, feedback)

# auto generate outputs
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
# from .ogr2ogrclipextent import Ogr2OgrClipExtent
# from .ogr2ogrtopostgis import Ogr2OgrToPostGis
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
# from .ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
from .ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
# from .ogr2ogrbuffer import Ogr2OgrBuffer
# from .ogr2ogrdissolve import Ogr2OgrDissolve
# from .onesidebuffer import OneSideBuffer
Expand Down Expand Up @@ -182,7 +182,7 @@ def loadAlgorithms(self):
# Ogr2OgrClipExtent(),
# Ogr2OgrToPostGis(),
# Ogr2OgrToPostGisList(),
# Ogr2OgrPointsOnLines(),
Ogr2OgrPointsOnLines(),
# Ogr2OgrBuffer(),
# Ogr2OgrDissolve(),
# OneSideBuffer(),
Expand Down
54 changes: 34 additions & 20 deletions python/plugins/processing/algs/gdal/ogr2ogrpointsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterVectorDestination,
QgsProcessingOutputVectorLayer,
QgsProcessing,
QgsVectorFileWriter,
QgsVectorLayer)
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
Expand All @@ -51,18 +59,19 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE], False))
self.addParameter(ParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
'geometry', optional=False))
self.addParameter(ParameterNumber(self.DISTANCE,
self.tr('Distance from line start represented as fraction of line length'), 0, 1, 0.5))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options (see ogr2ogr manual)'),
'', optional=True))

self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Points along lines'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER,
self.tr('Input layer'), [QgsProcessing.TypeVectorLine], optional=False))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
defaultValue='geometry', optional=False))
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
self.tr('Distance from line start represented as fraction of line length'), type=QgsProcessingParameterNumber.Double, minValue=0, maxValue=1, defaultValue=0.5))
self.addParameter(QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options (see ogr2ogr manual)'),
defaultValue='', optional=True))

self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT_LAYER, self.tr('Points along lines'), QgsProcessing.TypeVectorPoint))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Points along lines"), QgsProcessing.TypeVectorPoint))

def name(self):
return 'createpointsalonglines'
Expand All @@ -74,17 +83,22 @@ def group(self):
return self.tr('Vector geoprocessing')

def getConsoleCommands(self, parameters, context, feedback):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
inLayer = self.parameterAsVectorLayer(parameters, self.INPUT_LAYER, context)
if inLayer is None or inLayer.dataProvider().name() == 'ogr':
ogrLayer = self.parameterAsCompatibleSourceLayerPath(parameters, self.INPUT_LAYER, context, QgsVectorFileWriter.supportedFormatExtensions(), feedback=feedback)
if inLayer is None:
inLayer = ogrLayer
else:
ogrLayer = ogrConnectionString(inLayer, context)[1:-1]

layername = "'" + ogrLayerName(inLayer) + "'"
distance = str(self.getParameterValue(self.DISTANCE))
geometry = str(self.getParameterValue(self.GEOMETRY))
distance = str(self.parameterAsDouble(parameters, self.DISTANCE, context))
geometry = str(self.parameterAsString(parameters, self.GEOMETRY, context))

output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT_LAYER, context)

output = ogrConnectionString(outFile)
options = str(self.getParameterValue(self.OPTIONS))
output = ogrConnectionString(outFile, context)
options = str(self.parameterAsString(parameters, self.OPTIONS, context))

arguments = []
arguments.append(output)
Expand Down
26 changes: 13 additions & 13 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ tests:
# geometry:
# precision: 7
#
# - algorithm: gdal:createpointsalonglines
# name: Points along lines
# params:
# DISTANCE: 0.25
# GEOMETRY: geometry
# INPUT_LAYER:
# name: lines.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/gdal/points_along_lines.gml
# type: vector
#
- algorithm: gdal:createpointsalonglines
name: Points along lines
params:
DISTANCE: 0.25
GEOMETRY: geometry
INPUT_LAYER:
name: lines.gml
type: vector
results:
OUTPUT_LAYER:
name: expected/gdal/points_along_lines.gml
type: vector

# - algorithm: gdal:offsetlinesforlines
# name: Offset lines for lines (right-handed)
# params:
Expand Down

0 comments on commit 6301ff6

Please sign in to comment.