Skip to content

Commit

Permalink
Use reference rather than pointer for crs in QgsVectorFileWriter
Browse files Browse the repository at this point in the history
Since QgsCoordinateReferenceSystem is implicitly shared, it's a
safer approach
  • Loading branch information
nyalldawson committed Jul 14, 2016
1 parent ce87fda commit fd42ed3
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 49 deletions.
12 changes: 12 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ with too big impact should be deferred to a major version release.

This page tries to maintain a list with incompatible changes that happened in previous releases.

\section qgis_api_break_3_0 QGIS 3.0

\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter

<ul>
<li>QgsVectorFileWriter now takes references instead of pointers to QgsCoordinateReferenceSystem objects. Since
QgsCoordinateReferenceSystem is now implicitly shared, using references to QgsCoordinateReferenceSystem rather than
pointers makes for more robust, safer code. Use an invalid (default constructed) QgsCoordinateReferenceSystem
in code which previously passed a null pointer to QgsVectorFileWriter.</li>
</ul>


\section qgis_api_break_2_4 QGIS 2.4

\subsection qgis_api_break_mtr Multi-threaded Rendering
Expand Down
6 changes: 3 additions & 3 deletions python/core/qgsvectorfilewriter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class QgsVectorFileWriter
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
bool onlySelected = false,
QString *errorMessage = 0,
Expand Down Expand Up @@ -224,7 +224,7 @@ class QgsVectorFileWriter
const QString& fileEncoding,
const QgsFields& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
const QStringList &datasourceOptions = QStringList(),
const QStringList &layerOptions = QStringList(),
Expand All @@ -237,7 +237,7 @@ class QgsVectorFileWriter
const QString& fileEncoding,
const QgsFields& fields,
QgsWKBTypes::Type geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
const QStringList &datasourceOptions = QStringList(),
const QStringList &layerOptions = QStringList(),
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/interpolation/DualEdgeTriangulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3105,7 +3105,7 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
}
}

QgsVectorFileWriter writer( shapeFileName, "Utf-8", fields, QGis::WKBLineString, nullptr );
QgsVectorFileWriter writer( shapeFileName, "Utf-8", fields, QGis::WKBLineString );
if ( writer.hasError() != QgsVectorFileWriter::NoError )
{
return false;
Expand Down
14 changes: 7 additions & 7 deletions src/analysis/vector/qgsgeometryanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool QgsGeometryAnalyzer::simplify( QgsVectorLayer* layer,
QGis::WkbType outputType = dp->geometryType();
QgsCoordinateReferenceSystem crs = layer->crs();

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, crs );
QgsFeature currentFeature;

//take only selection
Expand Down Expand Up @@ -165,7 +165,7 @@ bool QgsGeometryAnalyzer::centroids( QgsVectorLayer* layer, const QString& shape
QGis::WkbType outputType = QGis::WKBPoint;
QgsCoordinateReferenceSystem crs = layer->crs();

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, crs );
QgsFeature currentFeature;

//take only selection
Expand Down Expand Up @@ -293,7 +293,7 @@ bool QgsGeometryAnalyzer::extent( QgsVectorLayer* layer,
fields.append( QgsField( QString( "HEIGHT" ), QVariant::Double ) );
fields.append( QgsField( QString( "WIDTH" ), QVariant::Double ) );

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), fields, outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), fields, outputType, crs );

QgsRectangle rect;
if ( onlySelectedFeatures ) // take only selection
Expand Down Expand Up @@ -391,7 +391,7 @@ bool QgsGeometryAnalyzer::convexHull( QgsVectorLayer* layer, const QString& shap
QGis::WkbType outputType = QGis::WKBPolygon;
QgsCoordinateReferenceSystem crs = layer->crs();

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), fields, outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), fields, outputType, crs );
QgsFeature currentFeature;
QgsGeometry* dissolveGeometry = nullptr; //dissolve geometry
QMultiMap<QString, QgsFeatureId> map;
Expand Down Expand Up @@ -597,7 +597,7 @@ bool QgsGeometryAnalyzer::dissolve( QgsVectorLayer* layer, const QString& shapef
QGis::WkbType outputType = dp->geometryType();
QgsCoordinateReferenceSystem crs = layer->crs();

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, crs );
QgsFeature currentFeature;
QMultiMap<QString, QgsFeatureId> map;

Expand Down Expand Up @@ -750,7 +750,7 @@ bool QgsGeometryAnalyzer::buffer( QgsVectorLayer* layer, const QString& shapefil
}
QgsCoordinateReferenceSystem crs = layer->crs();

QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dp->encoding(), layer->fields(), outputType, crs );
QgsFeature currentFeature;
QgsGeometry *dissolveGeometry = nullptr; //dissolve geometry (if dissolve enabled)

Expand Down Expand Up @@ -923,7 +923,7 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
eventLayer->dataProvider()->encoding(),
eventLayer->fields(),
memoryProviderType,
&( lineLayer->crs() ),
lineLayer->crs(),
outputFormat );
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/vector/qgsoverlayanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
QgsFields fieldsB = layerB->fields();
combineFieldLists( fieldsA, fieldsB );

QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fieldsA, outputType, &crs );
QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fieldsA, outputType, crs );
QgsFeature currentFeature;
QgsSpatialIndex index;

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/vector/qgspointsample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int QgsPointSample::createRandomPoints( QProgressDialog* pd )
QgsVectorFileWriter writer( mOutputLayer, "UTF-8",
outputFields,
QGis::WKBPoint,
&( mInputLayer->crs() ) );
mInputLayer->crs() );

//check if creation of output layer successfull
if ( writer.hasError() != QgsVectorFileWriter::NoError )
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/vector/qgstransectsample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ int QgsTransectSample::createSample( QProgressDialog* pd )
outputPointFields.append( QgsField( "start_long", QVariant::Double ) );

QgsVectorFileWriter outputPointWriter( mOutputPointLayer, "utf-8", outputPointFields, QGis::WKBPoint,
&( mStrataLayer->crs() ) );
mStrataLayer->crs() );
if ( outputPointWriter.hasError() != QgsVectorFileWriter::NoError )
{
return 3;
}

outputPointFields.append( QgsField( "bearing", QVariant::Double ) ); //add bearing attribute for lines
QgsVectorFileWriter outputLineWriter( mOutputLineLayer, "utf-8", outputPointFields, QGis::WKBLineString,
&( mStrataLayer->crs() ) );
mStrataLayer->crs() );
if ( outputLineWriter.hasError() != QgsVectorFileWriter::NoError )
{
return 4;
Expand All @@ -107,7 +107,7 @@ int QgsTransectSample::createSample( QProgressDialog* pd )
usedBaselineFields.append( QgsField( "stratum_id", stratumIdType ) );
usedBaselineFields.append( QgsField( "ok", QVariant::String ) );
QgsVectorFileWriter usedBaselineWriter( mUsedBaselineLayer, "utf-8", usedBaselineFields, QGis::WKBLineString,
&( mStrataLayer->crs() ) );
mStrataLayer->crs() );
if ( usedBaselineWriter.hasError() != QgsVectorFileWriter::NoError )
{
return 5;
Expand All @@ -118,7 +118,7 @@ int QgsTransectSample::createSample( QProgressDialog* pd )
QString bufferClipLineOutput = outputPointInfo.absolutePath() + "/out_buffer_clip_line.shp";
QgsFields bufferClipLineFields;
bufferClipLineFields.append( QgsField( "id", stratumIdType ) );
QgsVectorFileWriter bufferClipLineWriter( bufferClipLineOutput, "utf-8", bufferClipLineFields, QGis::WKBLineString, &( mStrataLayer->crs() ) );
QgsVectorFileWriter bufferClipLineWriter( bufferClipLineOutput, "utf-8", bufferClipLineFields, QGis::WKBLineString, mStrataLayer->crs() );

//configure distanceArea depending on minDistance units and output CRS
QgsDistanceArea distanceArea;
Expand Down
30 changes: 15 additions & 15 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
const QString &theFileEncoding,
const QgsFields& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs,
const QString& driverName,
const QStringList &datasourceOptions,
const QStringList &layerOptions,
Expand All @@ -97,7 +97,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
srs, driverName, datasourceOptions, layerOptions, newFilename, nullptr );
}

QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName, const QString& fileEncoding, const QgsFields& fields, QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs, const QString& driverName, const QStringList& datasourceOptions, const QStringList& layerOptions, QString* newFilename, QgsVectorFileWriter::SymbologyExport symbologyExport )
QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName, const QString& fileEncoding, const QgsFields& fields, QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem& srs, const QString& driverName, const QStringList& datasourceOptions, const QStringList& layerOptions, QString* newFilename, QgsVectorFileWriter::SymbologyExport symbologyExport )
: mDS( nullptr )
, mLayer( nullptr )
, mOgrRef( nullptr )
Expand All @@ -117,7 +117,7 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName,
const QString& fileEncoding,
const QgsFields& fields,
QgsWKBTypes::Type geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs,
const QString& driverName,
const QStringList& datasourceOptions,
const QStringList& layerOptions,
Expand All @@ -143,7 +143,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
QString fileEncoding,
const QgsFields& fields,
QgsWKBTypes::Type geometryType,
const QgsCoordinateReferenceSystem* srs,
QgsCoordinateReferenceSystem srs,
const QString& driverName,
QStringList datasourceOptions,
QStringList layerOptions,
Expand Down Expand Up @@ -178,7 +178,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
{
layerOptions.append( "SHPT=NULL" );
}
srs = nullptr;
srs = QgsCoordinateReferenceSystem();
}
else
{
Expand Down Expand Up @@ -322,9 +322,9 @@ void QgsVectorFileWriter::init( QString vectorFileName,
}

// consider spatial reference system of the layer
if ( srs )
if ( srs.isValid() )
{
QString srsWkt = srs->toWkt();
QString srsWkt = srs.toWkt();
QgsDebugMsg( "WKT to save as is " + srsWkt );
mOgrRef = OSRNewSpatialReference( srsWkt.toLocal8Bit().constData() );
}
Expand Down Expand Up @@ -369,7 +369,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
CPLSetConfigOption( "SHAPE_ENCODING", nullptr );
}

if ( srs )
if ( srs.isValid() )
{
if ( mOgrDriverName == "ESRI Shapefile" )
{
Expand All @@ -378,7 +378,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
if ( prjFile.open( QIODevice::WriteOnly ) )
{
QTextStream prjStream( &prjFile );
prjStream << srs->toWkt().toLocal8Bit().constData() << endl;
prjStream << srs.toWkt().toLocal8Bit().constData() << endl;
prjFile.close();
}
else
Expand Down Expand Up @@ -2099,7 +2099,7 @@ QgsVectorFileWriter::WriterError
QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS,
const QString& driverName,
bool onlySelected,
QString *errorMessage,
Expand All @@ -2117,9 +2117,9 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer,
FieldValueConverter* fieldValueConverter )
{
QgsCoordinateTransform* ct = nullptr;
if ( destCRS && layer )
if ( destCRS.isValid() && layer )
{
ct = new QgsCoordinateTransform( layer->crs(), *destCRS );
ct = new QgsCoordinateTransform( layer->crs(), destCRS );
}

QgsVectorFileWriter::WriterError error = writeAsVectorFormat( layer, fileName, fileEncoding, ct, driverName, onlySelected,
Expand Down Expand Up @@ -2157,17 +2157,17 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( QgsVe
}

bool shallTransform = false;
const QgsCoordinateReferenceSystem* outputCRS = nullptr;
QgsCoordinateReferenceSystem outputCRS;
if ( ct )
{
// This means we should transform
outputCRS = &( ct->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();
}

QgsWKBTypes::Type destWkbType = QGis::fromOldWkbType( layer->wkbType() );
Expand Down
12 changes: 6 additions & 6 deletions src/core/qgsvectorfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class CORE_EXPORT QgsVectorFileWriter
* @param layer layer to write
* @param fileName file name to write to
* @param fileEncoding encoding to use
* @param destCRS pointer to CRS to reproject exported geometries to
* @param destCRS CRS to reproject exported geometries to, or invalid CRS for no reprojection
* @param driverName OGR driver to use
* @param onlySelected write only selected features of layer
* @param errorMessage pointer to buffer fo error message
Expand All @@ -224,7 +224,7 @@ class CORE_EXPORT QgsVectorFileWriter
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
bool onlySelected = false,
QString *errorMessage = nullptr,
Expand Down Expand Up @@ -291,7 +291,7 @@ class CORE_EXPORT QgsVectorFileWriter
const QString& fileEncoding,
const QgsFields& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
const QStringList &datasourceOptions = QStringList(),
const QStringList &layerOptions = QStringList(),
Expand All @@ -304,7 +304,7 @@ class CORE_EXPORT QgsVectorFileWriter
const QString& fileEncoding,
const QgsFields& fields,
QgsWKBTypes::Type geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs = QgsCoordinateReferenceSystem(),
const QString& driverName = "ESRI Shapefile",
const QStringList &datasourceOptions = QStringList(),
const QStringList &layerOptions = QStringList(),
Expand Down Expand Up @@ -423,7 +423,7 @@ class CORE_EXPORT QgsVectorFileWriter
const QString& fileEncoding,
const QgsFields& fields,
QgsWKBTypes::Type geometryType,
const QgsCoordinateReferenceSystem* srs,
const QgsCoordinateReferenceSystem& srs,
const QString& driverName,
const QStringList &datasourceOptions,
const QStringList &layerOptions,
Expand All @@ -433,7 +433,7 @@ class CORE_EXPORT QgsVectorFileWriter
);

void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields,
QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs,
QgsWKBTypes::Type geometryType, QgsCoordinateReferenceSystem srs,
const QString& driverName, QStringList datasourceOptions,
QStringList layerOptions, QString* newFilename,
FieldValueConverter* fieldValueConverter );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void QgsGeometryCheckerSetupTab::runChecks()
}

QString errMsg;
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormat( layer, filename, layer->dataProvider()->encoding(), &layer->crs(), mOutputDriverName, selectedOnly, &errMsg );
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormat( layer, filename, layer->dataProvider()->encoding(), layer->crs(), mOutputDriverName, selectedOnly, &errMsg );
if ( err != QgsVectorFileWriter::NoError )
{
QMessageBox::critical( this, tr( "Layer Creation Failed" ), tr( "Failed to create the output layer: %1" ).arg( errMsg ) );
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/geometry_snapper/qgsgeometrysnapperdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void QgsGeometrySnapperDialog::run()
}

QString errMsg;
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormat( layer, filename, layer->dataProvider()->encoding(), &layer->crs(), mOutputDriverName, selectedOnly, &errMsg );
QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormat( layer, filename, layer->dataProvider()->encoding(), layer->crs(), mOutputDriverName, selectedOnly, &errMsg );
if ( err != QgsVectorFileWriter::NoError )
{
QMessageBox::critical( this, tr( "Layer Creation Failed" ), tr( "Failed to create the output layer: %1" ).arg( errMsg ) );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ QgsVectorLayerImport::ImportError QgsOgrProvider::createEmptyLayer(
QgsVectorFileWriter *writer = new QgsVectorFileWriter(
uri, encoding, fields, wkbType,
srs, driverName, dsOptions, layerOptions );
srs ? *srs : QgsCoordinateReferenceSystem(), driverName, dsOptions, layerOptions );
QgsVectorFileWriter::WriterError error = writer->hasError();
if ( error )
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wfs/qgswfsshareddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ bool QgsWFSSharedData::createCache()
mCacheTablename = CPLGetBasename( vsimemFilename.toStdString().c_str() );
VSIUnlink( vsimemFilename.toStdString().c_str() );
QgsVectorFileWriter* writer = new QgsVectorFileWriter( vsimemFilename, "",
cacheFields, QGis::WKBPolygon, nullptr, "SpatiaLite", datasourceOptions, layerOptions );
cacheFields, QGis::WKBPolygon, QgsCoordinateReferenceSystem(), "SpatiaLite", datasourceOptions, layerOptions );
if ( writer->hasError() == QgsVectorFileWriter::NoError )
{
delete writer;
Expand Down
Loading

0 comments on commit fd42ed3

Please sign in to comment.