-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransectByDistance.py
208 lines (156 loc) · 6.54 KB
/
TransectByDistance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
***************************************************************************
* *
* 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; version 2 of the License. *
* *
***************************************************************************
"""
import math
from qgis.PyQt.QtCore import (QCoreApplication,
QVariant)
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsGeometry,
QgsFeature,
QgsField,
QgsFields,
QgsLineString,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber,
QgsWkbTypes)
from qgis import processing
class TransectDistance(QgsProcessingAlgorithm):
INPUT = 'INPUT'
DISTANCE = 'DISTANCE'
LENGTH = 'LENGTH'
OUTPUT = 'OUTPUT'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return TransectDistance()
def name(self):
return 'transectdistance'
def displayName(self):
return self.tr('Transect By Distance')
def group(self):
return self.tr('Vector Geometry')
def groupId(self):
return 'rve'
def shortHelpString(self):
return self.tr("Generates perpendicular transects along a polyline at a given distance and of a given length."
" The length is the entire length of the transect."
" All units are in the units of the layer's CRS.")
def initAlgorithm(self, config=None):
########################
# Algorithm Parameters #
########################
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Centerlines'),
[QgsProcessing.TypeVectorLine]
)
)
self.addParameter (
QgsProcessingParameterNumber(
self.DISTANCE,
self.tr('Distance offset'),
QgsProcessingParameterNumber.Double,
minValue=0,
defaultValue=100
)
)
self.addParameter (
QgsProcessingParameterNumber(
self.LENGTH,
self.tr('Transect length'),
QgsProcessingParameterNumber.Double,
minValue=0,
defaultValue=100
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Transects')
)
)
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(
parameters,
self.INPUT,
context
)
if source is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
offset_distance = self.parameterAsDouble(
parameters,
self.DISTANCE,
context
)
if offset_distance is None:
raise QgsProcessingException('Error: Distance offset is required.')
transect_length = self.parameterAsDouble(
parameters,
self.LENGTH,
context
)
transect_length = transect_length / 2.0
if transect_length is None:
raise QgsProcessingException('Error: Transect length is required.')
fields = source.fields()
fields.append(QgsField('trans_dist', QVariant.Double))
(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT,
context,
fields,
QgsWkbTypes.LineString,
source.sourceCrs()
)
if sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
# Compute the number of steps to display within the progress bar and
# get features from source
# feedback.pushInfo('Number of features: {}'.format(source.featureCount()))
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
geometries = []
feat_geom = feature.geometry()
if feat_geom.isMultipart():
for part in feat_geom.constParts():
geometries.append(QgsGeometry(part))
else:
geometries.append(feature.geometry())
for geom in geometries:
#feedback.pushInfo('Geom= {}'.format(geom.length()))
#raise QgsProcessingException('Debug: bailout...')
geom_as_polyline = QgsLineString(geom.asPolyline())
i = 0
while i < geom.length():
angle = geom.interpolateAngle(i) * 180 / math.pi
currentPoint = geom_as_polyline.interpolatePoint(i)
new_transect = QgsLineString(
currentPoint.project(transect_length, angle - 90),
currentPoint.project(transect_length, angle + 90)
)
newFeature = QgsFeature(fields)
newFeature.setGeometry(new_transect)
newFeature.setAttribute(fields.indexOf('trans_dist'), i)
sink.addFeature(
newFeature,
QgsFeatureSink.FastInsert
)
i += offset_distance
# Update the progress bar
feedback.setProgress(int(current * total))
return {self.OUTPUT: dest_id}