Skip to content

Commit

Permalink
[processing] homogenize and improve Points along lines
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent 98dfda3 commit f59e3a3
Showing 1 changed file with 40 additions and 51 deletions.
91 changes: 40 additions & 51 deletions python/plugins/processing/algs/gdal/PointsAlongLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
QgsProcessingParameterNumber,
QgsProcessingParameterVectorDestination,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterDefinition,
QgsProcessing)

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
Expand All @@ -40,87 +41,75 @@

class PointsAlongLines(GdalAlgorithm):

OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
DISTANCE = 'DISTANCE'
GEOMETRY = 'GEOMETRY'
DISTANCE = 'DISTANCE'
OPTIONS = 'OPTIONS'
OUTPUT = 'OUTPUT'

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'), [QgsProcessing.TypeVectorLine], optional=False))
self.tr('Input layer'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
defaultValue='geometry', optional=False))
self.tr('Geometry column name'),
defaultValue='geometry'))
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, self.tr('Points along lines'), QgsProcessing.TypeVectorPoint))
self.tr('Distance from line start represented as fraction of line length'),
type=QgsProcessingParameterNumber.Double,
minValue=0,
maxValue=1,
defaultValue=0.5))

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
defaultValue='',
optional=True)
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
self.tr('Points along lines'),
QgsProcessing.TypeVectorPoint))

def name(self):
return 'pointsalonglines'

def displayName(self):
return self.tr('Create points along lines')
return self.tr('Points along lines')

def group(self):
return self.tr('Vector geoprocessing')

def getConsoleCommands(self, parameters, context, feedback):
fields = self.parameterAsSource(parameters, self.INPUT, context).fields()
ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
def commandName(self):
return 'ogr2ogr'

distance = str(self.parameterAsDouble(parameters, self.DISTANCE, context))
def getConsoleCommands(self, parameters, context, feedback):
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)

outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

output, format = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)

other_fields = []
for f in fields:
if f.name() == geometry:
continue

other_fields.append(f.name())
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)

arguments = []
if format:
arguments.append('-f {}'.format(format))
arguments.append(output)
arguments.append(ogrLayer)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')

arguments.append('-dialect sqlite -sql "SELECT ST_Line_Interpolate_Point(')
arguments.append(geometry)
arguments.append(',')
arguments.append(distance)
arguments.append(')')
arguments.append('AS')
arguments.append(geometry)
arguments.append(',')
arguments.append(','.join(other_fields))
arguments.append('FROM')
arguments.append(layername)
arguments.append('"')

if options is not None and len(options.strip()) > 0:
arguments.append(options)
sql = "SELECT ST_Line_Interpolate_Point({}, {}), * FROM '{}'".format(geometry, distance, layerName)
arguments.append(sql)

commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
if options:
arguments.append(options)

return commands
if outputFormat:
arguments.append('-f {}'.format(outputFormat))

def commandName(self):
return 'ogr2ogr'
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

0 comments on commit f59e3a3

Please sign in to comment.