Skip to content

Commit 3c4fb26

Browse files
committedDec 6, 2017
Add option to control CRS for merge algorithm (fixes #9652)
The default behaviour of auto selecting the first layer's CRS can be unpredictable, especially in models
1 parent cb96e1b commit 3c4fb26

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed
 

‎src/analysis/processing/qgsalgorithmmergevector.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ QString QgsMergeVectorAlgorithm::group() const
4242
void QgsMergeVectorAlgorithm::initAlgorithm( const QVariantMap & )
4343
{
4444
addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ), QgsProcessing::TypeVector ) );
45+
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Destination CRS" ), QVariant(), true ) );
4546
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Merged" ) ) );
4647
}
4748

@@ -50,9 +51,10 @@ QString QgsMergeVectorAlgorithm::shortHelpString() const
5051
return QObject::tr( "This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n"
5152
"If attributes tables are different, the attribute table of the resulting layer will contain the attributes "
5253
"from all input layers. New attributes will be added for the original layer name and source.\n\n"
53-
"The layers will all be reprojected to match the coordinate reference system of the first input layer.\n\n"
5454
"If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, "
55-
"if any of the input layers are multi-part, the output layer will also be a multi-part layer." );
55+
"if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n"
56+
"Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be "
57+
"taken from the first input layer. All layers will all be reprojected to match this CRS." );
5658
}
5759

5860
QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance() const
@@ -67,7 +69,10 @@ QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parame
6769
QgsFields outputFields;
6870
long totalFeatureCount = 0;
6971
QgsWkbTypes::Type outputType = QgsWkbTypes::Unknown;
70-
QgsCoordinateReferenceSystem outputCrs;
72+
QgsCoordinateReferenceSystem outputCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
73+
74+
if ( outputCrs.isValid() )
75+
feedback->pushInfo( QObject::tr( "Using specified destination CRS %1" ).arg( outputCrs.authid() ) );
7176

7277
bool errored = false;
7378

@@ -92,8 +97,11 @@ QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parame
9297

9398
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer );
9499

95-
if ( !outputCrs.isValid() )
100+
if ( !outputCrs.isValid() && vl->crs().isValid() )
101+
{
96102
outputCrs = vl->crs();
103+
feedback->pushInfo( QObject::tr( "Taking destination CRS %1 from layer" ).arg( outputCrs.authid() ) );
104+
}
97105

98106
// check wkb type
99107
if ( outputType != QgsWkbTypes::Unknown && outputType != QgsWkbTypes::NoGeometry )
@@ -138,8 +146,8 @@ QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parame
138146
found = true;
139147
if ( destField.type() != sourceField.type() )
140148
{
141-
throw QgsProcessingException( QObject::tr( "%1 field in layer %2 has different data type than in other layers" )
142-
.arg( sourceField.name() ).arg( i ) );
149+
throw QgsProcessingException( QObject::tr( "%1 field in layer %2 has different data type than in other layers (%3 instead of %4)" )
150+
.arg( sourceField.name(), vl->name() ).arg( sourceField.typeName() ).arg( destField.typeName() ) );
143151
}
144152
break;
145153
}

0 commit comments

Comments
 (0)
Please sign in to comment.