Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Be more careful with output geometries added as a
result of clipping
  • Loading branch information
nyalldawson committed Jun 28, 2018
1 parent 31330dd commit 6f5405a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.

This file was deleted.

@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xsi:schemaLocation="http://ogr.maptools.org/ clip_lines_by_multipolygon.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>2</gml:X><gml:Y>-1</gml:Y></gml:coord>
<gml:coord><gml:X>2</gml:X><gml:Y>1</gml:Y></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:clip_lines_by_multipolygon fid="lines.2">
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>2,1 2,2</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>2,2 3,2</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>3,2 3,3</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
Expand All @@ -26,9 +26,4 @@
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>7,2 8,2</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
</ogr:clip_lines_by_multipolygon>
</gml:featureMember>
<gml:featureMember>
<ogr:clip_lines_by_multipolygon fid="lines.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:clip_lines_by_multipolygon>
</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="clip_lines_by_multipolygon" type="ogr:clip_lines_by_multipolygon_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="clip_lines_by_multipolygon_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>
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmclip.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsalgorithmclip.h"
#include "qgsgeometryengine.h"
#include "qgsoverlayutils.h"

///@cond PRIVATE

Expand Down Expand Up @@ -78,6 +79,7 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );

QString dest;
QgsWkbTypes::GeometryType sinkType = QgsWkbTypes::geometryType( featureSource->wkbType() );
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, featureSource->fields(), QgsWkbTypes::multiType( featureSource->wkbType() ), featureSource->sourceCrs() ) );

if ( !sink )
Expand Down Expand Up @@ -184,6 +186,9 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
newGeometry = inputFeature.geometry();
}

if ( !QgsOverlayUtils::sanitizeIntersectionResult( newGeometry, sinkType ) )
continue;

QgsFeature outputFeature;
outputFeature.setGeometry( newGeometry );
outputFeature.setAttributes( inputFeature.attributes() );
Expand Down
3 changes: 1 addition & 2 deletions src/analysis/processing/qgsoverlayutils.cpp
Expand Up @@ -20,8 +20,7 @@

///@cond PRIVATE

//! Makes sure that what came out from intersection of two geometries is good to be used in the output
static bool sanitizeIntersectionResult( QgsGeometry &geom, QgsWkbTypes::GeometryType geometryType )
bool QgsOverlayUtils::sanitizeIntersectionResult( QgsGeometry &geom, QgsWkbTypes::GeometryType geometryType )
{
if ( geom.isNull() )
{
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsoverlayutils.h
Expand Up @@ -17,6 +17,7 @@
#define QGSOVERLAYUTILS_H

#include <QList>
#include "qgswkbtypes.h"

#define SIP_NO_FILE

Expand All @@ -27,6 +28,7 @@ class QgsFeatureSink;
class QgsFields;
class QgsProcessingContext;
class QgsProcessingFeedback;
class QgsGeometry;

namespace QgsOverlayUtils
{
Expand All @@ -43,6 +45,9 @@ namespace QgsOverlayUtils

void intersection( const QgsFeatureSource &sourceA, const QgsFeatureSource &sourceB, QgsFeatureSink &sink, QgsProcessingContext &context, QgsProcessingFeedback *feedback, int &count, int totalCount, const QList<int> &fieldIndicesA, const QList<int> &fieldIndicesB );

//! Makes sure that what came out from intersection of two geometries is good to be used in the output
bool sanitizeIntersectionResult( QgsGeometry &geom, QgsWkbTypes::GeometryType geometryType );

/**
* Copies features from the source to the sink and resolves overlaps: for each pair of overlapping features A and B
* it will produce:
Expand Down

0 comments on commit 6f5405a

Please sign in to comment.