Skip to content

Commit

Permalink
Ask datum transformation if vector layer is saved in another CRS
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Oct 31, 2013
1 parent 08092cd commit 5a2285e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 25 deletions.
27 changes: 26 additions & 1 deletion python/core/qgsvectorfilewriter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ class QgsVectorFileWriter
ErrInvalidLayer, // added in 2.0
};

//added in 2.0
enum SymbologyExport
{
NoSymbology = 0, //export only data
FeatureSymbology, //Keeps the number of features and export symbology per feature
SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
};

static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateTransform* ct,
const QString& driverName = "ESRI Shapefile",
bool onlySelected = false,
QString *errorMessage = 0,
const QStringList &datasourceOptions = QStringList(), // added in 1.6
const QStringList &layerOptions = QStringList(), // added in 1.6
bool skipAttributeCreation = false, // added in 1.6
QString *newFilename = 0, // added in 1.9
SymbologyExport symbologyExport = NoSymbology, //added in 2.0
double symbologyScale = 1.0 // added in 2.0
);

/** Write contents of vector layer to an (OGR supported) vector formt
@note: this method was added in version 1.5*/
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
Expand All @@ -40,7 +63,9 @@ class QgsVectorFileWriter
const QStringList &datasourceOptions = QStringList(), // added in 1.6
const QStringList &layerOptions = QStringList(), // added in 1.6
bool skipAttributeCreation = false, // added in 1.6
QString *newFilename = 0 // added in 1.9
QString *newFilename = 0, // added in 1.9
SymbologyExport symbologyExport = NoSymbology, //added in 2.0
double symbologyScale = 1.0 // added in 2.0
);

/** create shapefile and initialize it */
Expand Down
33 changes: 32 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
#include "qgscustomization.h"
#include "qgscustomprojectiondialog.h"
#include "qgsdatasourceuri.h"
#include "qgsdatumtransformdialog.h"
#include "qgsdecorationcopyright.h"
#include "qgsdecorationnortharrow.h"
#include "qgsdecorationscalebar.h"
Expand Down Expand Up @@ -4598,10 +4599,13 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection, QgsVectorLayer* v
QString format = dialog->format();
QStringList datasourceOptions = dialog->datasourceOptions();

QgsCoordinateTransform* ct = 0;

switch ( dialog->crs() )
{
case -2: // Project CRS
destCRS = mMapCanvas->mapRenderer()->destinationCrs();

break;
case -1: // Layer CRS
destCRS = vlayer->crs();
Expand All @@ -4612,14 +4616,39 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection, QgsVectorLayer* v
break;
}

if ( destCRS.isValid() && destCRS != vlayer->crs() )
{
ct = new QgsCoordinateTransform( vlayer->crs(), destCRS );

//ask user about datum transformation
QList< QList< int > > dt = QgsCoordinateTransform::datumTransformations( vlayer->crs(), destCRS );
if ( dt.size() > 1 )
{
QgsDatumTransformDialog d( vlayer->name(), dt );
if ( d.exec() == QDialog::Accepted )
{
QList< int > sdt = d.selectedDatumTransform();
if ( sdt.size() > 0 )
{
ct->setSourceDatumTransform( sdt.at( 0 ) );
}
if ( sdt.size() > 1 )
{
ct->setDestinationDatumTransform( sdt.at( 1 ) );
}
ct->initialise();
}
}
}

// ok if the file existed it should be deleted now so we can continue...
QApplication::setOverrideCursor( Qt::WaitCursor );

QgsVectorFileWriter::WriterError error;
QString errorMessage;
QString newFilename;
error = QgsVectorFileWriter::writeAsVectorFormat(
vlayer, vectorFilename, encoding, &destCRS, format,
vlayer, vectorFilename, encoding, ct, format,
saveOnlySelection,
&errorMessage,
datasourceOptions, dialog->layerOptions(),
Expand All @@ -4628,6 +4657,8 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection, QgsVectorLayer* v
( QgsVectorFileWriter::SymbologyExport )( dialog->symbologyExport() ),
dialog->scaleDenominator() );

delete ct;

QApplication::restoreOverrideCursor();

if ( error == QgsVectorFileWriter::NoError )
Expand Down
53 changes: 30 additions & 23 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,26 +744,50 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
SymbologyExport symbologyExport,
double symbologyScale )
{
QgsDebugMsg( "fileName = " + fileName );
const QgsCoordinateReferenceSystem* outputCRS;
QgsCoordinateTransform* ct = 0;
int shallTransform = false;
if ( destCRS && layer )
{
ct = new QgsCoordinateTransform( layer->crs(), *destCRS );
}

QgsVectorFileWriter::WriterError error = writeAsVectorFormat( layer, fileName, fileEncoding, ct, driverName, onlySelected,
errorMessage, datasourceOptions, layerOptions, skipAttributeCreation, newFilename, symbologyExport, symbologyScale );
delete ct;
return error;
}

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateTransform* ct,
const QString& driverName,
bool onlySelected,
QString *errorMessage,
const QStringList &datasourceOptions, // added in 1.6
const QStringList &layerOptions, // added in 1.6
bool skipAttributeCreation, // added in 1.6
QString *newFilename, // added in 1.9
SymbologyExport symbologyExport, //added in 2.0
double symbologyScale // added in 2.0
)
{
if ( !layer )
{
return ErrInvalidLayer;
}

if ( destCRS && destCRS->isValid() )
bool shallTransform = false;
const QgsCoordinateReferenceSystem* outputCRS = 0;
if ( ct )
{
// This means we should transform
outputCRS = destCRS;
outputCRS = &( ct->destCRS() );
shallTransform = true;
}
else
{
// This means we shouldn't transform, use source CRS as output (if defined)
outputCRS = &layer->crs();
outputCRS = &( layer->crs() );
}
QgsVectorFileWriter* writer =
new QgsVectorFileWriter( fileName, fileEncoding, skipAttributeCreation ? QgsFields() : layer->pendingFields(), layer->wkbType(), outputCRS, driverName, datasourceOptions, layerOptions, newFilename, symbologyExport );
Expand Down Expand Up @@ -805,18 +829,6 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,

const QgsFeatureIds& ids = layer->selectedFeaturesIds();

// Create our transform
if ( destCRS )
{
ct = new QgsCoordinateTransform( layer->crs(), *destCRS );
}

// Check for failure
if ( ct == NULL )
{
shallTransform = false;
}

//create symbol table if needed
if ( writer->symbologyExport() != NoSymbology )
{
Expand Down Expand Up @@ -928,11 +940,6 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
writer->stopRender( layer );
delete writer;

if ( shallTransform )
{
delete ct;
}

if ( errors > 0 && errorMessage && n > 0 )
{
*errorMessage += QObject::tr( "\nOnly %1 of %2 features written." ).arg( n - errors ).arg( n );
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsvectorfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ class CORE_EXPORT QgsVectorFileWriter
double symbologyScale = 1.0 // added in 2.0
);

static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateTransform* ct,
const QString& driverName = "ESRI Shapefile",
bool onlySelected = false,
QString *errorMessage = 0,
const QStringList &datasourceOptions = QStringList(), // added in 1.6
const QStringList &layerOptions = QStringList(), // added in 1.6
bool skipAttributeCreation = false, // added in 1.6
QString *newFilename = 0, // added in 1.9
SymbologyExport symbologyExport = NoSymbology, //added in 2.0
double symbologyScale = 1.0 // added in 2.0
);

/** create shapefile and initialize it */
QgsVectorFileWriter( const QString& vectorFileName,
const QString& fileEncoding,
Expand Down

0 comments on commit 5a2285e

Please sign in to comment.