Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 13, 2019
1 parent d167bab commit ddb9360
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 39 deletions.
Expand Up @@ -638,7 +638,7 @@ tests:
floatval: skip

- algorithm: qgis:rectanglesovalsdiamondsfixed
name: Create fixed distance rectange buffers around points
name: Create fixed distance rectangle buffers around points
params:
HEIGHT: 0.25
INPUT:
Expand Down Expand Up @@ -1386,7 +1386,7 @@ tests:
name: expected/single_sided_buffer_multiline_bevel.gml
type: vector

- algorithm: qgis:extractvertices
- algorithm: native:extractvertices
name: Extract vertices from multipolygons
params:
INPUT:
Expand All @@ -1403,7 +1403,7 @@ tests:
angle:
precision: 7

- algorithm: qgis:extractvertices
- algorithm: native:extractvertices
name: Extract vertices from polygons
params:
INPUT:
Expand All @@ -1420,7 +1420,7 @@ tests:
angle:
precision: 7

- algorithm: qgis:extractvertices
- algorithm: native:extractvertices
name: Extract vertices from multilines
params:
INPUT:
Expand All @@ -1437,7 +1437,7 @@ tests:
angle:
precision: 7

- algorithm: qgis:extractvertices
- algorithm: native:extractvertices
name: Extract vertices from lines
params:
INPUT:
Expand Down
23 changes: 23 additions & 0 deletions src/analysis/processing/qgsalgorithmextractspecificvertices.cpp
Expand Up @@ -111,6 +111,16 @@ QgsWkbTypes::Type QgsExtractSpecificVerticesAlgorithm::outputWkbType( QgsWkbType
return outputWkbType;
}

QgsProcessingFeatureSource::Flag QgsExtractSpecificVerticesAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

QgsFeatureSink::SinkFlags QgsExtractSpecificVerticesAlgorithm::sinkFlags() const
{
return QgsFeatureSink::RegeneratePrimaryKey;
}

void QgsExtractSpecificVerticesAlgorithm::initParameters( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "VERTICES" ), QObject::tr( "Vertex indices" ), QStringLiteral( "0" ) ) );
Expand Down Expand Up @@ -148,6 +158,19 @@ QgsFeatureList QgsExtractSpecificVerticesAlgorithm::processFeature( const QgsFea
QgsGeometry inputGeom = f.geometry();
if ( inputGeom.isNull() )
{
QgsAttributes attrs = f.attributes();
attrs << QVariant()
<< QVariant()
<< QVariant();
if ( mGeometryType == QgsWkbTypes::PolygonGeometry )
{
attrs << QVariant();
}
attrs << QVariant()
<< QVariant()
<< QVariant();

f.setAttributes( attrs );
outputFeatures << f;
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsalgorithmextractspecificvertices.h
Expand Up @@ -50,6 +50,8 @@ class QgsExtractSpecificVerticesAlgorithm : public QgsProcessingFeatureBasedAlgo
QgsFields outputFields( const QgsFields &inputFields ) const override;
QgsProcessing::SourceType outputLayerType() const override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
QgsFeatureSink::SinkFlags sinkFlags() const override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand Down
36 changes: 29 additions & 7 deletions src/analysis/processing/qgsalgorithmextractvertices.cpp
Expand Up @@ -72,16 +72,15 @@ QgsProcessing::SourceType QgsExtractVerticesAlgorithm::outputLayerType() const
QgsFields QgsExtractVerticesAlgorithm::outputFields( const QgsFields &inputFields ) const
{
QgsFields outputFields = inputFields;
outputFields.append( QgsField( QStringLiteral( "vertex_pos" ), QVariant::Int ) );
outputFields.append( QgsField( QStringLiteral( "vertex_index" ), QVariant::Int ) );
outputFields.append( QgsField( QStringLiteral( "vertex_part" ), QVariant::Int ) );
outputFields.append( QgsField( QStringLiteral( "vertex_index" ), QVariant::Int, QString(), 10, 0 ) );
outputFields.append( QgsField( QStringLiteral( "vertex_part" ), QVariant::Int, QString(), 10, 0 ) );
if ( mGeometryType == QgsWkbTypes::PolygonGeometry )
{
outputFields.append( QgsField( QStringLiteral( "vertex_part_ring" ), QVariant::Int ) );
outputFields.append( QgsField( QStringLiteral( "vertex_part_ring" ), QVariant::Int, QString(), 10, 0 ) );
}
outputFields.append( QgsField( QStringLiteral( "vertex_part_index" ), QVariant::Int ) );
outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double ) );
outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double ) );
outputFields.append( QgsField( QStringLiteral( "vertex_part_index" ), QVariant::Int, QString(), 10, 0 ) );
outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 14 ) );
outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 14 ) );

return outputFields;
}
Expand All @@ -101,6 +100,16 @@ QgsWkbTypes::Type QgsExtractVerticesAlgorithm::outputWkbType( QgsWkbTypes::Type
return outputWkbType;
}

QgsProcessingFeatureSource::Flag QgsExtractVerticesAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}

QgsFeatureSink::SinkFlags QgsExtractVerticesAlgorithm::sinkFlags() const
{
return QgsFeatureSink::RegeneratePrimaryKey;
}

bool QgsExtractVerticesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
Expand All @@ -116,6 +125,18 @@ QgsFeatureList QgsExtractVerticesAlgorithm::processFeature( const QgsFeature &fe
QgsGeometry inputGeom = f.geometry();
if ( inputGeom.isNull() )
{
QgsAttributes attrs = f.attributes();
attrs << QVariant()
<< QVariant();
if ( mGeometryType == QgsWkbTypes::PolygonGeometry )
{
attrs << QVariant();
}
attrs << QVariant()
<< QVariant()
<< QVariant();

f.setAttributes( attrs );
outputFeatures << f;
}
else
Expand All @@ -137,6 +158,7 @@ QgsFeatureList QgsExtractVerticesAlgorithm::processFeature( const QgsFeature &fe
attrs << vertexId.vertex
<< cumulativeDistance
<< angle;

QgsFeature outputFeature = QgsFeature();
outputFeature.setAttributes( attrs );
outputFeature.setGeometry( QgsGeometry( ( *vi ).clone() ) );
Expand Down
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsalgorithmextractvertices.h
Expand Up @@ -51,6 +51,8 @@ class QgsExtractVerticesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QgsFields outputFields( const QgsFields &inputFields ) const override;
QgsProcessing::SourceType outputLayerType() const override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
QgsFeatureSink::SinkFlags sinkFlags() const override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand Down
39 changes: 12 additions & 27 deletions src/analysis/processing/qgsalgorithmrandomextract.cpp
Expand Up @@ -78,7 +78,7 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &para

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(),
source->wkbType(), source->sourceCrs() ) );
source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );

Expand Down Expand Up @@ -118,39 +118,24 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &para
break;
}

if ( idsCount.contains( id ) )
{
idsCount.insert( id, idsCount.value( id ) + 1 );
}
else
{
idsCount.insert( id, 1 );
}
idsCount[ id ] += 1;
}

QgsFeatureIds ids = QSet< QgsFeatureId >::fromList( idsCount.keys() );
QgsFeatureIterator fit = source->getFeatures( QgsFeatureRequest().setFilterFids( ids ), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
sink->addFeatures( fit, QgsFeatureSink::FastInsert );

// add duplicated features if any
if ( ids.size() != number )
QgsFeature f;
while ( fit.nextFeature( f ) )
{
QgsFeature f;
for ( auto it = idsCount.constBegin(); it != idsCount.constEnd(); ++it )
if ( feedback->isCanceled() )
{
break;
}

const int count = idsCount.value( f.id() );
for ( int i = 0; i < count; ++i )
{
if ( feedback->isCanceled() )
{
break;
}

if ( it.value() > 1 )
{
source->getFeatures( QgsFeatureRequest( it.key() ), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks ).nextFeature( f );
for ( int j = 1; j < it.value(); j++ )
{
sink->addFeature( f, QgsFeatureSink::FastInsert );
}
}
sink->addFeature( f, QgsFeatureSink::FastInsert );
}
}

Expand Down

0 comments on commit ddb9360

Please sign in to comment.