|
30 | 30 | QgsProcessingParameterNumber, |
31 | 31 | QgsProcessingParameterVectorDestination, |
32 | 32 | QgsProcessingOutputVectorLayer, |
| 33 | + QgsProcessingParameterDefinition, |
33 | 34 | QgsProcessing) |
34 | 35 |
|
35 | 36 | from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm |
|
40 | 41 |
|
41 | 42 | class PointsAlongLines(GdalAlgorithm): |
42 | 43 |
|
43 | | - OUTPUT = 'OUTPUT' |
44 | 44 | INPUT = 'INPUT' |
45 | | - DISTANCE = 'DISTANCE' |
46 | 45 | GEOMETRY = 'GEOMETRY' |
| 46 | + DISTANCE = 'DISTANCE' |
47 | 47 | OPTIONS = 'OPTIONS' |
| 48 | + OUTPUT = 'OUTPUT' |
48 | 49 |
|
49 | 50 | def __init__(self): |
50 | 51 | super().__init__() |
51 | 52 |
|
52 | 53 | def initAlgorithm(self, config=None): |
53 | 54 | self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, |
54 | | - self.tr('Input layer'), [QgsProcessing.TypeVectorLine], optional=False)) |
| 55 | + self.tr('Input layer'), |
| 56 | + [QgsProcessing.TypeVectorLine])) |
55 | 57 | self.addParameter(QgsProcessingParameterString(self.GEOMETRY, |
56 | | - self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'), |
57 | | - defaultValue='geometry', optional=False)) |
| 58 | + self.tr('Geometry column name'), |
| 59 | + defaultValue='geometry')) |
58 | 60 | self.addParameter(QgsProcessingParameterNumber(self.DISTANCE, |
59 | | - self.tr('Distance from line start represented as fraction of line length'), type=QgsProcessingParameterNumber.Double, minValue=0, maxValue=1, defaultValue=0.5)) |
60 | | - self.addParameter(QgsProcessingParameterString(self.OPTIONS, |
61 | | - self.tr('Additional creation options (see ogr2ogr manual)'), |
62 | | - defaultValue='', optional=True)) |
63 | | - |
64 | | - self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT, self.tr('Points along lines'), QgsProcessing.TypeVectorPoint)) |
| 61 | + self.tr('Distance from line start represented as fraction of line length'), |
| 62 | + type=QgsProcessingParameterNumber.Double, |
| 63 | + minValue=0, |
| 64 | + maxValue=1, |
| 65 | + defaultValue=0.5)) |
| 66 | + |
| 67 | + options_param = QgsProcessingParameterString(self.OPTIONS, |
| 68 | + self.tr('Additional creation options'), |
| 69 | + defaultValue='', |
| 70 | + optional=True) |
| 71 | + options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) |
| 72 | + self.addParameter(options_param) |
| 73 | + |
| 74 | + self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT, |
| 75 | + self.tr('Points along lines'), |
| 76 | + QgsProcessing.TypeVectorPoint)) |
65 | 77 |
|
66 | 78 | def name(self): |
67 | 79 | return 'pointsalonglines' |
68 | 80 |
|
69 | 81 | def displayName(self): |
70 | | - return self.tr('Create points along lines') |
| 82 | + return self.tr('Points along lines') |
71 | 83 |
|
72 | 84 | def group(self): |
73 | 85 | return self.tr('Vector geoprocessing') |
74 | 86 |
|
75 | | - def getConsoleCommands(self, parameters, context, feedback): |
76 | | - fields = self.parameterAsSource(parameters, self.INPUT, context).fields() |
77 | | - ogrLayer, layername = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback) |
| 87 | + def commandName(self): |
| 88 | + return 'ogr2ogr' |
78 | 89 |
|
79 | | - distance = str(self.parameterAsDouble(parameters, self.DISTANCE, context)) |
| 90 | + def getConsoleCommands(self, parameters, context, feedback): |
| 91 | + ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback) |
| 92 | + distance = self.parameterAsDouble(parameters, self.DISTANCE, context) |
80 | 93 | geometry = self.parameterAsString(parameters, self.GEOMETRY, context) |
81 | | - |
82 | 94 | outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) |
83 | | - |
84 | | - output, format = GdalUtils.ogrConnectionStringAndFormat(outFile, context) |
85 | 95 | options = self.parameterAsString(parameters, self.OPTIONS, context) |
86 | 96 |
|
87 | | - other_fields = [] |
88 | | - for f in fields: |
89 | | - if f.name() == geometry: |
90 | | - continue |
91 | | - |
92 | | - other_fields.append(f.name()) |
| 97 | + output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context) |
93 | 98 |
|
94 | 99 | arguments = [] |
95 | | - if format: |
96 | | - arguments.append('-f {}'.format(format)) |
97 | 100 | arguments.append(output) |
98 | 101 | arguments.append(ogrLayer) |
| 102 | + arguments.append('-dialect') |
| 103 | + arguments.append('sqlite') |
| 104 | + arguments.append('-sql') |
99 | 105 |
|
100 | | - arguments.append('-dialect sqlite -sql "SELECT ST_Line_Interpolate_Point(') |
101 | | - arguments.append(geometry) |
102 | | - arguments.append(',') |
103 | | - arguments.append(distance) |
104 | | - arguments.append(')') |
105 | | - arguments.append('AS') |
106 | | - arguments.append(geometry) |
107 | | - arguments.append(',') |
108 | | - arguments.append(','.join(other_fields)) |
109 | | - arguments.append('FROM') |
110 | | - arguments.append(layername) |
111 | | - arguments.append('"') |
112 | | - |
113 | | - if options is not None and len(options.strip()) > 0: |
114 | | - arguments.append(options) |
| 106 | + sql = "SELECT ST_Line_Interpolate_Point({}, {}), * FROM '{}'".format(geometry, distance, layerName) |
| 107 | + arguments.append(sql) |
115 | 108 |
|
116 | | - commands = [] |
117 | | - if isWindows(): |
118 | | - commands = ['cmd.exe', '/C ', 'ogr2ogr.exe', |
119 | | - GdalUtils.escapeAndJoin(arguments)] |
120 | | - else: |
121 | | - commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] |
| 109 | + if options: |
| 110 | + arguments.append(options) |
122 | 111 |
|
123 | | - return commands |
| 112 | + if outputFormat: |
| 113 | + arguments.append('-f {}'.format(outputFormat)) |
124 | 114 |
|
125 | | - def commandName(self): |
126 | | - return 'ogr2ogr' |
| 115 | + return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] |
0 commit comments