2015-02-05 22:55:44 +00:00
# -*- coding: utf-8 -*-
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ogr2ogrpointsonlines . py
- - - - - - - - - - - - - - - - - - - - -
Date : Janaury 2015
Copyright : ( C ) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"""
2016-09-21 18:24:26 +02:00
from builtins import str
2015-02-05 22:55:44 +00:00
__author__ = ' Giovanni Manghi '
__date__ = ' January 2015 '
__copyright__ = ' (C) 2015, Giovanni Manghi '
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = ' $Format: % H$ '
2017-06-27 17:26:21 +10:00
from qgis . core import ( QgsProcessingParameterFeatureSource ,
QgsProcessingParameterString ,
QgsProcessingParameterNumber ,
QgsProcessingParameterVectorDestination ,
QgsProcessingOutputVectorLayer ,
QgsProcessing ,
QgsVectorFileWriter ,
QgsVectorLayer )
2015-02-05 22:55:44 +00:00
from processing . core . parameters import ParameterVector
from processing . core . parameters import ParameterString
from processing . core . parameters import ParameterNumber
from processing . core . outputs import OutputVector
2016-01-22 15:45:09 +02:00
from processing . algs . gdal . GdalAlgorithm import GdalAlgorithm
2015-02-05 22:55:44 +00:00
from processing . algs . gdal . GdalUtils import GdalUtils
2016-08-23 19:33:42 +03:00
from processing . tools import dataobjects
2016-01-22 15:45:09 +02:00
from processing . tools . system import isWindows
from processing . tools . vector import ogrConnectionString , ogrLayerName
2015-08-22 14:29:41 +02:00
2016-01-22 15:45:09 +02:00
class Ogr2OgrPointsOnLines ( GdalAlgorithm ) :
2015-02-05 22:55:44 +00:00
OUTPUT_LAYER = ' OUTPUT_LAYER '
INPUT_LAYER = ' INPUT_LAYER '
DISTANCE = ' DISTANCE '
GEOMETRY = ' GEOMETRY '
OPTIONS = ' OPTIONS '
2017-05-15 13:40:38 +10:00
def __init__ ( self ) :
super ( ) . __init__ ( )
2017-06-27 17:20:57 +10:00
def initAlgorithm ( self , config = None ) :
2017-06-27 17:26:21 +10:00
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 ) )
2015-02-05 22:55:44 +00:00
2017-05-15 13:40:38 +10:00
def name ( self ) :
return ' createpointsalonglines '
def displayName ( self ) :
return self . tr ( ' Create points along lines ' )
def group ( self ) :
return self . tr ( ' Vector geoprocessing ' )
2017-06-27 17:20:57 +10:00
def getConsoleCommands ( self , parameters , context , feedback ) :
2017-06-27 17:26:21 +10:00
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 ]
2016-01-22 15:45:09 +02:00
layername = " ' " + ogrLayerName ( inLayer ) + " ' "
2017-06-27 17:26:21 +10:00
distance = str ( self . parameterAsDouble ( parameters , self . DISTANCE , context ) )
geometry = str ( self . parameterAsString ( parameters , self . GEOMETRY , context ) )
2015-02-24 12:35:00 +02:00
2017-06-27 17:26:21 +10:00
outFile = self . parameterAsOutputLayer ( parameters , self . OUTPUT_LAYER , context )
2015-02-05 22:55:44 +00:00
2017-06-27 17:26:21 +10:00
output = ogrConnectionString ( outFile , context )
options = str ( self . parameterAsString ( parameters , self . OPTIONS , context ) )
2015-02-05 22:55:44 +00:00
arguments = [ ]
arguments . append ( output )
arguments . append ( ogrLayer )
2015-02-24 12:35:00 +02:00
2015-02-05 22:55:44 +00:00
arguments . append ( ' -dialect sqlite -sql " SELECT ST_Line_Interpolate_Point( ' )
arguments . append ( geometry )
arguments . append ( ' , ' )
arguments . append ( distance )
arguments . append ( ' ),* ' )
arguments . append ( ' FROM ' )
arguments . append ( layername )
arguments . append ( ' " ' )
2015-02-24 12:35:00 +02:00
2017-02-08 11:22:21 +00:00
if options is not None and len ( options . strip ( ) ) > 0 :
2015-02-05 22:55:44 +00:00
arguments . append ( options )
2015-02-24 12:35:00 +02:00
2015-02-05 22:55:44 +00:00
commands = [ ]
if isWindows ( ) :
commands = [ ' cmd.exe ' , ' /C ' , ' ogr2ogr.exe ' ,
GdalUtils . escapeAndJoin ( arguments ) ]
else :
commands = [ ' ogr2ogr ' , GdalUtils . escapeAndJoin ( arguments ) ]
2015-05-07 13:14:01 +02:00
return commands
2015-05-21 15:43:47 +02:00
def commandName ( self ) :
2015-08-22 14:29:41 +02:00
return " ogr2ogr "