Skip to content

Commit

Permalink
some fixes and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Sep 10, 2020
1 parent abd5fad commit 5ffc4f2
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 60 deletions.
Expand Up @@ -31,7 +31,7 @@ Constructor

~QgsMeshTriangulation();

bool addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback = 0, int featureCount = 1 );
bool addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback = 0, long featureCount = 1 );
%Docstring
Adds vertices to the triangulation from a feature iterator, return ``True`` if successful.

Expand All @@ -42,7 +42,7 @@ Adds vertices to the triangulation from a feature iterator, return ``True`` if s
:param featureCount: the count of feature to allow progress report of the feedback
%End

bool addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transformContext, QgsFeedback *feedback = 0, int featureCount = 1 );
bool addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transformContext, QgsFeedback *feedback = 0, long featureCount = 1 );
%Docstring
Adds break lines from a vector layer, return ``True`` if successful.

Expand Down
Expand Up @@ -14,9 +14,9 @@ class QgsProcessingParameterTinInputLayers: QgsProcessingParameterDefinition
A parameter for processing algorithms that need a list of input vector layers to construct a TIN
A valid value for this parameter is a list (QVariantList), where each item is a map (QVariantMap) in this form:
{
'Id': string hat represents the unique Id of the vector layer,
'Type': how the vector layer is used : as vertices or as break lines
'AttributeIndex' : the index of the attribute of the vector layer used to defined the Z value of vertices,
'source': string that represents identification of the vector layer,
'type': how the vector layer is used : as vertices, structure lines or break lines
'attributeIndex' : the index of the attribute of the vector layer used to defined the Z value of vertices,
if -1, the Z coordinates of features are used
}

Expand All @@ -31,13 +31,14 @@ if -1, the Z coordinates of features are used
enum Type
{
Vertices,
StructureLines,
BreakLines
};

struct InputLayer
{
QString layerId; //!The Id of the input layer
Type type; //!The sype of the input lyer (see Type)
QString source; //!The source of the input layer
Type type; //!The type of the input layer (see Type)
int attributeIndex; //! The attribute index used for Z value of vertices
};

Expand Down
30 changes: 15 additions & 15 deletions src/analysis/mesh/qgsmeshtriangulation.cpp
Expand Up @@ -34,7 +34,7 @@ QgsMeshTriangulation::QgsMeshTriangulation(): QObject()

QgsMeshTriangulation::~QgsMeshTriangulation() = default;

bool QgsMeshTriangulation::addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback, int featureCount )
bool QgsMeshTriangulation::addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback, long featureCount )
{
if ( !vertexFeatureIterator.isValid() || vertexFeatureIterator.isClosed() )
return false;
Expand All @@ -43,7 +43,7 @@ bool QgsMeshTriangulation::addVertices( QgsFeatureIterator &vertexFeatureIterato
feedback->setProgress( 0 );

QgsFeature feat;
int i = 0;
long i = 0;
while ( vertexFeatureIterator.nextFeature( feat ) )
{
if ( feedback )
Expand All @@ -61,7 +61,7 @@ bool QgsMeshTriangulation::addVertices( QgsFeatureIterator &vertexFeatureIterato
return true;
}

bool QgsMeshTriangulation::addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback, int featureCount )
bool QgsMeshTriangulation::addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback, long featureCount )
{
if ( !lineFeatureIterator.isValid() || lineFeatureIterator.isClosed() )
return false;
Expand All @@ -70,7 +70,7 @@ bool QgsMeshTriangulation::addBreakLines( QgsFeatureIterator &lineFeatureIterato
feedback->setProgress( 0 );

QgsFeature feat;
int i = 0;
long i = 0;
while ( lineFeatureIterator.nextFeature( feat ) )
{
if ( feedback )
Expand Down Expand Up @@ -219,29 +219,29 @@ void QgsMeshTriangulation::addBreakLinesFromFeature( const QgsFeature &feature,
}
}

int i = 0;
for ( const QgsCurve *curve : curves )
{
if ( !curve )
continue;

if ( feedback )
{
if ( feedback->isCanceled() )
break;

feedback->setProgress( 100 * i / curves.size() );
i++;
}
if ( feedback && feedback->isCanceled() )
break;

QgsPointSequence linePoints;
curve->points( linePoints );
bool hasZ = curve->is3D();
if ( valueAttribute >= 0 )
for ( QgsPoint &point : linePoints )
for ( int i = 0; i < linePoints.count(); ++i )
{
if ( feedback && feedback->isCanceled() )
break;
point.setZ( valueOnVertex );
if ( hasZ )
linePoints[i].setZ( valueOnVertex );
else
{
const QgsPoint &point = linePoints.at( i );
linePoints[i] = QgsPoint( point.x(), point.y(), valueOnVertex );
}
}

mTriangulation->addLine( linePoints, QgsInterpolator::SourceBreakLines );
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/mesh/qgsmeshtriangulation.h
Expand Up @@ -57,7 +57,7 @@ class ANALYSIS_EXPORT QgsMeshTriangulation : public QObject
* \param feedback feedback argument may be specified to allow cancellation and progress reports
* \param featureCount the count of feature to allow progress report of the feedback
*/
bool addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback = nullptr, int featureCount = 1 );
bool addVertices( QgsFeatureIterator &vertexFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transform, QgsFeedback *feedback = nullptr, long featureCount = 1 );

/**
* Adds break lines from a vector layer, return TRUE if successful.
Expand All @@ -69,7 +69,7 @@ class ANALYSIS_EXPORT QgsMeshTriangulation : public QObject
*
* \warning if the feature iterator contains only point geometries, the vertices will be added only without treating them as breaklines
*/
bool addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transformContext, QgsFeedback *feedback = nullptr, int featureCount = 1 );
bool addBreakLines( QgsFeatureIterator &lineFeatureIterator, int valueAttribute, const QgsCoordinateTransform &transformContext, QgsFeedback *feedback = nullptr, long featureCount = 1 );

//! Returns the triangulated mesh
QgsMesh triangulatedMesh() const;
Expand Down
24 changes: 13 additions & 11 deletions src/analysis/processing/qgsalgorithmtinmeshcreation.cpp
Expand Up @@ -72,7 +72,6 @@ void QgsTinMeshCreationAlgorithm::initAlgorithm( const QVariantMap &configuratio

bool QgsTinMeshCreationAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
Q_UNUSED( feedback );
const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "SOURCE_DATA" ) )->name() );
if ( layersVariant.type() != QVariant::List )
return false;
Expand All @@ -91,24 +90,27 @@ bool QgsTinMeshCreationAlgorithm::prepareAlgorithm( const QVariantMap &parameter
if ( layer.type() != QVariant::Map )
continue;
const QVariantMap layerMap = layer.toMap();
const QString layerId = layerMap.value( QStringLiteral( "Id" ) ).toString();
const QString layerSource = layerMap.value( QStringLiteral( "source" ) ).toString();
const QgsProcessingParameterTinInputLayers::Type type =
static_cast<QgsProcessingParameterTinInputLayers::Type>( layerMap.value( QStringLiteral( "Type" ) ).toInt() );
int attributeIndex = layerMap.value( QStringLiteral( "AttributeIndex" ) ).toInt();
static_cast<QgsProcessingParameterTinInputLayers::Type>( layerMap.value( QStringLiteral( "type" ) ).toInt() );
int attributeIndex = layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt();

QgsVectorLayer *vectorLayer = context.project()->mapLayer<QgsVectorLayer *>( layerId );
if ( !vectorLayer || !vectorLayer->isValid() )
QgsProcessingFeatureSource *featureSource = QgsProcessingUtils::variantToSource( layerSource, context );

if ( !featureSource )
continue;

const QgsCoordinateTransform transform( vectorLayer->crs(), destinationCrs, context.transformContext() );
int featureCount = vectorLayer->featureCount();
const QgsCoordinateTransform transform( featureSource->sourceCrs(), destinationCrs, context.transformContext() );
int featureCount = featureSource->featureCount();
switch ( type )
{
case QgsProcessingParameterTinInputLayers::Vertices:
mVerticesLayer.append( {vectorLayer->getFeatures(), transform, attributeIndex, featureCount} );
mVerticesLayer.append( {featureSource->getFeatures(), transform, attributeIndex, featureCount} );
break;
case QgsProcessingParameterTinInputLayers::BreakLines:
mBreakLinesLayer.append( {vectorLayer->getFeatures(), transform, attributeIndex, featureCount} );
mBreakLinesLayer.append( {featureSource->getFeatures(), transform, attributeIndex, featureCount} );
break;
default:
break;
}
}
Expand All @@ -123,7 +125,7 @@ QVariantMap QgsTinMeshCreationAlgorithm::processAlgorithm( const QVariantMap &pa
{
QgsMeshTriangulation triangulation;
QgsCoordinateReferenceSystem destinationCrs = parameterAsCrs( parameters, QStringLiteral( "CRS_OUTPUT" ), context );
if ( !destinationCrs.isValid() )
if ( !destinationCrs.isValid() && context.project() )
destinationCrs = context.project()->crs();
triangulation.setCrs( destinationCrs );

Expand Down
16 changes: 8 additions & 8 deletions src/core/processing/qgsprocessingparametertininputlayers.cpp
Expand Up @@ -43,21 +43,21 @@ bool QgsProcessingParameterTinInputLayers::checkValueIsAcceptable( const QVarian
return false;
const QVariantMap layerMap = variantLayer.toMap();

if ( !layerMap.contains( QStringLiteral( "Id" ) ) ||
!layerMap.contains( QStringLiteral( "Type" ) ) ||
!layerMap.contains( QStringLiteral( "AttributeIndex" ) ) )
if ( !layerMap.contains( QStringLiteral( "source" ) ) ||
!layerMap.contains( QStringLiteral( "type" ) ) ||
!layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
return false;

if ( !context )
continue; // when called without context, we will skip checking whether the layer can be resolved

QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "Id" ) ).toString(), *context );
QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "source" ) ).toString(), *context );
if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
return false;

QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );

if ( layerMap.value( QStringLiteral( "AttributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
return false;
}

Expand All @@ -73,9 +73,9 @@ QString QgsProcessingParameterTinInputLayers::valueAsPythonString( const QVarian
{
const QVariantMap layerMap = variantLayer.toMap();
QStringList layerDefParts;
layerDefParts << QStringLiteral( "'Id': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "Id" ) ) );
layerDefParts << QStringLiteral( "'Type': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "Type" ) ) );
layerDefParts << QStringLiteral( "'AttributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "AttributeIndex" ) ) );
layerDefParts << QStringLiteral( "'source': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "source" ) ) );
layerDefParts << QStringLiteral( "'type': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "type" ) ) );
layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "attributeIndex" ) ) );
QString layerDef = QStringLiteral( "{ %1 }" ).arg( layerDefParts.join( ',' ) );
parts.append( layerDef );
}
Expand Down
15 changes: 8 additions & 7 deletions src/core/processing/qgsprocessingparametertininputlayers.h
Expand Up @@ -23,9 +23,9 @@
* A parameter for processing algorithms that need a list of input vector layers to construct a TIN
* A valid value for this parameter is a list (QVariantList), where each item is a map (QVariantMap) in this form:
* {
* 'Id': string hat represents the unique Id of the vector layer,
* 'Type': how the vector layer is used : as vertices or as break lines
* 'AttributeIndex' : the index of the attribute of the vector layer used to defined the Z value of vertices,
* 'source': string that represents identification of the vector layer,
* 'type': how the vector layer is used : as vertices, structure lines or break lines
* 'attributeIndex' : the index of the attribute of the vector layer used to defined the Z value of vertices,
* if -1, the Z coordinates of features are used
* }
*
Expand All @@ -39,15 +39,16 @@ class CORE_EXPORT QgsProcessingParameterTinInputLayers: public QgsProcessingPara
//! Defines the type of input layer
enum Type
{
Vertices, //! Input that adds only vertices
BreakLines //! Input that adds vertices and break lines
Vertices, //!< Input that adds only vertices
StructureLines, //!< Input that adds add structure lines
BreakLines //!< Input that adds vertices and break lines
};

//! Used to store input layer Id and other associated parameters
struct InputLayer
{
QString layerId; //!The Id of the input layer
Type type; //!The sype of the input lyer (see Type)
QString source; //!The source of the input layer
Type type; //!The type of the input layer (see Type)
int attributeIndex; //! The attribute index used for Z value of vertices
};

Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingregistry.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsvectorfilewriter.h"
#include "qgsprocessingparametertypeimpl.h"
#include "qgsprocessingparametervectortilewriterlayers.h"
#include "qgsprocessingparametertininputlayers.h"
#include "qgsprocessingparameterfieldmap.h"
#include "qgsprocessingparameteraggregate.h"

Expand Down Expand Up @@ -66,6 +67,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
addParameterType( new QgsProcessingParameterTypeFieldMapping() );
addParameterType( new QgsProcessingParameterTypeAggregate() );
addParameterType( new QgsProcessingParameterTypeTinInputLayers() );
}

QgsProcessingRegistry::~QgsProcessingRegistry()
Expand Down
21 changes: 12 additions & 9 deletions src/gui/processing/qgsprocessingtininputlayerswidget.cpp
Expand Up @@ -42,9 +42,9 @@ QVariant QgsProcessingTinInputLayersWidget::value() const
for ( const QgsProcessingParameterTinInputLayers::InputLayer &layer : layers )
{
QVariantMap layerMap;
layerMap[QStringLiteral( "Id" )] = layer.layerId;
layerMap[QStringLiteral( "Type" )] = layer.type;
layerMap[QStringLiteral( "AttributeIndex" )] = layer.attributeIndex;
layerMap[QStringLiteral( "source" )] = layer.source;
layerMap[QStringLiteral( "type" )] = layer.type;
layerMap[QStringLiteral( "attributeIndex" )] = layer.attributeIndex;
list.append( layerMap );
}

Expand All @@ -65,9 +65,9 @@ void QgsProcessingTinInputLayersWidget::setValue( const QVariant &value )
continue;
const QVariantMap layerMap = layerValue.toMap();
QgsProcessingParameterTinInputLayers::InputLayer layer;
layer.layerId = layerMap.value( QStringLiteral( "Id" ) ).toString();
layer.type = static_cast<QgsProcessingParameterTinInputLayers::Type>( layerMap.value( QStringLiteral( "Type" ) ).toInt() );
layer.attributeIndex = layerMap.value( QStringLiteral( "AttributeIndex" ) ).toInt();
layer.source = layerMap.value( QStringLiteral( "source" ) ).toString();
layer.type = static_cast<QgsProcessingParameterTinInputLayers::Type>( layerMap.value( QStringLiteral( "type" ) ).toInt() );
layer.attributeIndex = layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt();
mInputLayersModel.addLayer( layer );
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
if ( !currentLayer )
return;
QgsProcessingParameterTinInputLayers::InputLayer layer;
layer.layerId = mComboLayers->currentLayer()->id();
layer.source = mComboLayers->currentLayer()->id();

switch ( currentLayer->geometryType() )
{
Expand Down Expand Up @@ -161,7 +161,7 @@ QVariant QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::da
{
case Qt::DisplayRole:
{
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( mInputLayers.at( index.row() ).layerId );
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( mInputLayers.at( index.row() ).source );
switch ( index.column() )
{
case 0:
Expand All @@ -179,6 +179,9 @@ QVariant QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::da
case QgsProcessingParameterTinInputLayers::BreakLines:
return tr( "Break Lines" );
break;
default:
return QString();
break;
}
break;
case 2:
Expand Down Expand Up @@ -343,7 +346,7 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingTinInputLayersWidgetWr
return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
}

QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget() SIP_FACTORY
QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
{
mWidget = new QgsProcessingTinInputLayersWidget( widgetContext().project() );
connect( mWidget, &QgsProcessingTinInputLayersWidget::changed, this, [ = ]
Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingtininputlayerswidget.h
Expand Up @@ -106,7 +106,7 @@ class QgsProcessingTinInputLayersWidgetWrapper : public QgsAbstractProcessingPa
protected:
QStringList compatibleParameterTypes() const override {return QStringList();}
QStringList compatibleOutputTypes() const override {return QStringList();}
QWidget *createWidget() override;
QWidget *createWidget() override SIP_FACTORY;
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;

Expand Down

0 comments on commit 5ffc4f2

Please sign in to comment.