Skip to content
Permalink
Browse files

Port Reverse Line Direction to new API, add test

  • Loading branch information
nyalldawson committed Jul 16, 2017
1 parent 9cbc8cc commit 6487fbb2d1e85b9f12b0046e386eea8db12e5836
@@ -82,6 +82,7 @@
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RegularPoints import RegularPoints
from .ReverseLineDirection import ReverseLineDirection
from .Ruggedness import Ruggedness
from .SaveSelectedFeatures import SaveSelectedFeatures
from .SelectByAttribute import SelectByAttribute
@@ -142,7 +143,6 @@
# from .FieldsMapper import FieldsMapper
# from .Datasources2Vrt import Datasources2Vrt
# from .OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
# from .ReverseLineDirection import ReverseLineDirection
# from .SpatialIndex import SpatialIndex
# from .DefineProjection import DefineProjection
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
@@ -212,7 +212,7 @@ def getAlgs(self):
# SplitWithLines(), CreateConstantRaster(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
# SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# Translate(),
@@ -271,6 +271,7 @@ def getAlgs(self):
RandomExtract(),
RandomExtractWithinSubsets(),
RegularPoints(),
ReverseLineDirection(),
Ruggedness(),
SaveSelectedFeatures(),
SelectByAttribute(),
@@ -25,22 +25,20 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsGeometry,
from qgis.core import (QgsGeometry,
QgsFeature,
QgsFeatureSink,
QgsProcessingUtils)
QgsProcessingException,
QgsProcessing,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects


class ReverseLineDirection(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Vector geometry tools')
@@ -49,9 +47,10 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Reversed'), datatype=[dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(
QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Reversed'), QgsProcessing.TypeVectorLine))

def name(self):
return 'reverselinedirection'
@@ -60,30 +59,28 @@ def displayName(self):
return self.tr('Reverse line direction')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
source = self.parameterAsSource(parameters, self.INPUT, context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

outFeat = QgsFeature()

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, inFeat in enumerate(features):
inGeom = inFeat.geometry()
attrs = inFeat.attributes()
if feedback.isCanceled():
break

outGeom = None
if not inGeom.isNull():
outFeat = inFeat
if inFeat.geometry():
inGeom = inFeat.geometry()
reversedLine = inGeom.geometry().reversed()
if not reversedLine:
raise GeoAlgorithmExecutionException(
raise QgsProcessingException(
self.tr('Error reversing line'))
outGeom = QgsGeometry(reversedLine)

outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
outFeat.setGeometry(outGeom)
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>lines_reversed</Name>
<ElementPath>lines_reversed</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>11.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>5.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
<gml:coord><gml:X>11</gml:X><gml:Y>5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:lines_reversed fid="lines.0">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>11,5 9,3 9,2 6,2</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>1,-1 -1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.2">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,3 3,2 2,2 2,0</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>5,1 3,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>10,-3 7,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>10,1 6,-3</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:lines_reversed>
</gml:featureMember>
<gml:featureMember>
<ogr:lines_reversed fid="lines.6">
</ogr:lines_reversed>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -555,6 +555,20 @@ tests:
name: expected/point_on_line.gml
type: vector

- algorithm: qgis:reverselinedirection
name: Reverse line direction
params:
INPUT:
name: lines.gml
type: vector
results:
OUTPUT:
name: expected/lines_reversed.gml
type: vector
compare:
geometry:
precision: 7

- algorithm: qgis:offsetline
name: Offset line positive
params:

0 comments on commit 6487fbb

Please sign in to comment.
You can’t perform that action at this time.