Skip to content

Commit

Permalink
[processing] Reverse line direction can work with multi(line/curve) g…
Browse files Browse the repository at this point in the history
…eometries
  • Loading branch information
nyalldawson committed Jul 23, 2018
1 parent 6554843 commit f685d11
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ reverse_multiline.xsd"
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>-1</gml:Y></gml:coord>
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:reverse_multiline fid="lines.1">
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>1,-1 -1,-1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
</ogr:reverse_multiline>
</gml:featureMember>
<gml:featureMember>
<ogr:reverse_multiline fid="lines.2">
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>5,1 3,1</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>5,1 5.02418426103647,2.4147792706334</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
</ogr:reverse_multiline>
</gml:featureMember>
<gml:featureMember>
<ogr:reverse_multiline fid="lines.3">
</ogr:reverse_multiline>
</gml:featureMember>
<gml:featureMember>
<ogr:reverse_multiline fid="lines.4">
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>3,3 3,2 2,2 2,0</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>5.4595009596929,4.11976967370441 2.94433781190019,4.04721689059501</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>5.58042226487524,2.9468330134357 3,3</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
</ogr:reverse_multiline>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="reverse_multiline" type="ogr:reverse_multiline_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="reverse_multiline_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometryProperty" type="gml:MultiLineStringPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
11 changes: 11 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -973,6 +973,17 @@ tests:
geometry:
precision: 7

- algorithm: native:reverselinedirection
name: Reverse multiline direction
params:
INPUT:
name: multilines.gml
type: vector
results:
OUTPUT:
name: expected/reverse_multiline.gml
type: vector

- algorithm: qgis:offsetline
name: Offset line positive
params:
Expand Down
40 changes: 33 additions & 7 deletions src/analysis/processing/qgsalgorithmreverselinedirection.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsalgorithmreverselinedirection.h"
#include "qgscurve.h"
#include "qgsgeometrycollection.h"

///@cond PRIVATE

Expand Down Expand Up @@ -87,16 +88,41 @@ QgsFeatureList QgsReverseLineDirectionAlgorithm ::processFeature( const QgsFeatu
if ( feature.hasGeometry() )
{
const QgsGeometry geom = feature.geometry();
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom.constGet() );
if ( curve )
if ( !geom.isMultipart() )
{
std::unique_ptr< QgsCurve > reversed( curve->reversed() );
if ( !reversed )
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom.constGet() );
if ( curve )
{
// can this even happen?
throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
std::unique_ptr< QgsCurve > reversed( curve->reversed() );
if ( !reversed )
{
// can this even happen?
throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
}
const QgsGeometry outGeom( std::move( reversed ) );
feature.setGeometry( outGeom );
}
const QgsGeometry outGeom( std::move( reversed ) );
}
else
{
std::unique_ptr< QgsAbstractGeometry > dest( geom.constGet()->createEmptyWithSameType() );
const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( geom.constGet() );
QgsGeometryCollection *destCollection = qgsgeometry_cast< QgsGeometryCollection * >( dest.get() );
for ( int i = 0; i < collection->numGeometries(); ++i )
{
const QgsCurve *curve = qgsgeometry_cast< const QgsCurve *>( collection->geometryN( i ) );
if ( curve )
{
std::unique_ptr< QgsCurve > reversed( curve->reversed() );
if ( !reversed )
{
// can this even happen?
throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
}
destCollection->addGeometry( reversed.release() );
}
}
const QgsGeometry outGeom( std::move( dest ) );
feature.setGeometry( outGeom );
}
}
Expand Down

0 comments on commit f685d11

Please sign in to comment.