QgsFields,
QgsField)
import processing
+import re
class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
# Constants for feature field names
FATHER_ID = 'FatherId'
LINESTRING = 'LineString'
+
+ # RegExp for DN values in
+ DN_PATTERN = re.compile(r"DN(\d+)(-.*)?")
def tr(self, string):
"""
if not ('z2' in sourceNames): outputFields.append(QgsField('z2', QVariant.Double))
if not ('Length' in sourceNames): outputFields.append(QgsField('Length', QVariant.Double))
if not ('LineString' in sourceNames): outputFields.append(QgsField('LineString', QVariant.String))
+ if not ('DimensionDN' in sourceNames): outputFields.append(QgsField('DimensionDN', QVariant.Int))
(output, outputId) = self.parameterAsSink(
parameters,
length = lengths.get(id, None)
# Vertices
+ mypoints = list(feature.geometry().vertices())
mylist = strings.get(id, None)
if mylist == None:
feedback.pushInfo('No points for feature {}'.format(id))
- continue
+ mylist = [mypoints]
#feedback.pushInfo('Points: {}'.format("|".join(map(lambda x: ";".join(('{} {}'.format(p.x(), p.y()) for p in x)), mylist))))
- mypoints = list(feature.geometry().vertices())
head = feature.geometry().vertices().next()
resultList = [head]
mylist = list(map(lambda x: list(reversed(x)), mylist))
i = next((i for i, x in enumerate(mylist) if head.distance(x[0]) <= eps), None)
if i == None:
- feedback.pushInfo('No matching start vertex for feature {}'.format(id))
- continue
+ feedback.pushInfo('Warning: No matching start vertex for feature {}'.format(id))
+ mylist = [mypoints]
+ i = 0
+
+ vertices = mylist.pop(i)
while i != None:
- vertices = mylist.pop(i)
tail = vertices[-1]
resultList.extend(vertices[1:])
+ if tail.distance(mypoints[-1]) <= eps:
+ break
+
i = next((i for i, x in enumerate(mylist) if tail.distance(x[0]) <= eps), None)
+ if i != None:
+ vertices = mylist.pop(i)
+ else:
+ i = next((i for i, x in enumerate(mylist) if tail.distance(x[-1]) <= eps), None)
+ if i != None:
+ vertices = list(reversed(mylist.pop(i)))
# feedback.pushInfo(str(resultList))
# Convert to string
result = ";".join(('{} {}'.format(p.x(), p.y()) for p in resultList))
- feedback.pushInfo('Feature {}: {}'.format(id, result))
+ # feedback.pushInfo('Feature {}: {}'.format(id, result))
outputFeature = QgsFeature()
outputFeature.setFields(outputFields)
outputFeature['Length'] = feature['length'] # length
outputFeature['LineString'] = result
+ label = feature['Label']
+ m = self.DN_PATTERN.fullmatch(label)
+ if m:
+ outputFeature['DimensionDN'] = int(m.group(1))
+
output.addFeature(outputFeature)
feedback.pushInfo('Loop done')