Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
QgsVectorLayerSaveAsDialog: make it work with custom CRS
The current approach was based on a srsid, that that might be unset for custom
CRS. So store a full QgsCoordinateReferenceSystem object internally and use
that for exchanges with QgsProjectionSelectionWidget

Consequence: we need to deprecate the "long crs()" method, and replace it
by a "QgsCoordinateReferenceSystem crsObject()" one. I'm not particularly
happy with the name but the existing one was not very well named...

Mark also the QgsVectorLayerSaveAsDialog( long srsid, ... ) constructor as
deprecated

Fixes the use case of #34471 (comment)
  • Loading branch information
rouault authored and nyalldawson committed May 27, 2020
1 parent b76f39e commit 535c253
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -8874,7 +8874,7 @@ QString QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbology
QgsWkbTypes::Type forcedGeometryType = dialog->geometryType();

QgsCoordinateTransform ct;
destCRS = QgsCoordinateReferenceSystem::fromSrsId( dialog->crs() );
destCRS = dialog->crsObject();

if ( destCRS.isValid() )
{
Expand Down
24 changes: 14 additions & 10 deletions src/gui/ogr/qgsvectorlayersaveasdialog.cpp
Expand Up @@ -39,7 +39,7 @@ static const int COLUMN_IDX_EXPORT_AS_DISPLAYED_VALUE = 2;

QgsVectorLayerSaveAsDialog::QgsVectorLayerSaveAsDialog( long srsid, QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mCRS( srsid )
, mSelectedCrs( QgsCoordinateReferenceSystem::fromSrsId( srsid ) )
, mAttributeTableItemChangedSlotEnabled( true )
, mReplaceRawFieldValuesStateChangedSlotEnabled( true )
, mActionOnExistingFile( QgsVectorFileWriter::CreateOrOverwriteFile )
Expand All @@ -57,7 +57,7 @@ QgsVectorLayerSaveAsDialog::QgsVectorLayerSaveAsDialog( QgsVectorLayer *layer, i
{
if ( layer )
{
mCRS = layer->crs().srsid();
mSelectedCrs = layer->crs();
mLayerExtent = layer->extent();
}
setup();
Expand Down Expand Up @@ -149,9 +149,8 @@ void QgsVectorLayerSaveAsDialog::setup()
idx = 0;
}

QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem::fromSrsId( mCRS );
mCrsSelector->setCrs( srs );
mCrsSelector->setLayerCrs( srs );
mCrsSelector->setCrs( mSelectedCrs );
mCrsSelector->setLayerCrs( mSelectedCrs );
mCrsSelector->setMessage( tr( "Select the coordinate reference system for the vector file. "
"The data points will be transformed from the layer coordinate reference system." ) );

Expand All @@ -165,8 +164,8 @@ void QgsVectorLayerSaveAsDialog::setup()
mSymbologyExportComboBox_currentIndexChanged( mSymbologyExportComboBox->currentText() );

// extent group box
mExtentGroupBox->setOutputCrs( srs );
mExtentGroupBox->setOriginalExtent( mLayerExtent, srs );
mExtentGroupBox->setOutputCrs( mSelectedCrs );
mExtentGroupBox->setOriginalExtent( mLayerExtent, mSelectedCrs );
mExtentGroupBox->setOutputExtentFromOriginal();
mExtentGroupBox->setCheckable( true );
mExtentGroupBox->setChecked( false );
Expand Down Expand Up @@ -726,8 +725,8 @@ void QgsVectorLayerSaveAsDialog::mAttributeTable_itemChanged( QTableWidgetItem *

void QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs )
{
mCRS = crs.srsid();
mExtentGroupBox->setOutputCrs( crs );
mSelectedCrs = crs;
mExtentGroupBox->setOutputCrs( mSelectedCrs );
}

QString QgsVectorLayerSaveAsDialog::filename() const
Expand All @@ -752,7 +751,12 @@ QString QgsVectorLayerSaveAsDialog::format() const

long QgsVectorLayerSaveAsDialog::crs() const
{
return mCRS;
return mSelectedCrs.srsid();
}

QgsCoordinateReferenceSystem QgsVectorLayerSaveAsDialog::crsObject() const
{
return mSelectedCrs;
}

QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
Expand Down
15 changes: 12 additions & 3 deletions src/gui/ogr/qgsvectorlayersaveasdialog.h
Expand Up @@ -56,8 +56,10 @@ class GUI_EXPORT QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVec

/**
* Construct a new QgsVectorLayerSaveAsDialog
*
* \deprecated since QGIS 3.14 - will be removed in QGIS 4.0
*/
QgsVectorLayerSaveAsDialog( long srsid, QWidget *parent = nullptr, Qt::WindowFlags fl = nullptr );
Q_DECL_DEPRECATED QgsVectorLayerSaveAsDialog( long srsid, QWidget *parent = nullptr, Qt::WindowFlags fl = nullptr );

/**
* Construct a new QgsVectorLayerSaveAsDialog
Expand Down Expand Up @@ -100,8 +102,15 @@ class GUI_EXPORT QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVec
/**
* Returns the internal CRS ID.
* \see QgsCoordinateReferenceSystem::srsid()
* \deprecated since QGIS 3.14 - will be removed in QGIS 4.0. Use crsObject() instead.
*/
Q_DECL_DEPRECATED long crs() const;

/**
* Returns the CRS chosen for export
* \since QGIS 3.14
*/
long crs() const;
QgsCoordinateReferenceSystem crsObject() const;

/**
* Returns a list of attributes which are selected for saving.
Expand Down Expand Up @@ -221,7 +230,7 @@ class GUI_EXPORT QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVec
void setup();
QList< QPair< QLabel *, QWidget * > > createControls( const QMap<QString, QgsVectorFileWriter::Option *> &options );

long mCRS;
QgsCoordinateReferenceSystem mSelectedCrs;

QgsRectangle mLayerExtent;
QgsCoordinateReferenceSystem mLayerCrs;
Expand Down
8 changes: 8 additions & 0 deletions tests/src/app/testqgsvectorlayersaveasdialog.cpp
Expand Up @@ -72,6 +72,11 @@ void TestQgsVectorLayerSaveAsDialog::testAttributesAsDisplayedValues()
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "none?field=code:int&field=regular:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

// Assign a custom CRS to the layer
QgsCoordinateReferenceSystem crs;
crs.createFromString( "PROJ:+proj=merc +a=1" );
tempLayer->setCrs( crs );

// Set a widget
tempLayer->setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "ValueRelation" ), QVariantMap() ) );

Expand Down Expand Up @@ -128,6 +133,9 @@ void TestQgsVectorLayerSaveAsDialog::testAttributesAsDisplayedValues()
QCOMPARE( mAttributeTable->item( 0, 2 )->checkState(), Qt::Unchecked );
QCOMPARE( mAttributeTable->item( 0, 2 )->flags(), Qt::ItemIsUserCheckable );

// Check that we can get a custom CRS with crsObject()
QCOMPARE( d.crsObject(), crs ) ;

//d.exec();
}

Expand Down

0 comments on commit 535c253

Please sign in to comment.