Skip to content
Permalink
Browse files

Use reference rather than pointer for crs in QgsVectorFileWriter

Since QgsCoordinateReferenceSystem is implicitly shared, it's a
safer approach
  • Loading branch information
nyalldawson committed Jul 14, 2016
1 parent ce87fda commit fd42ed31452fea26e33441f4f078886fdc529701
@@ -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
@@ -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,
@@ -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(),
@@ -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(),
@@ -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;
@@ -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
@@ -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
@@ -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
@@ -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;
@@ -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;

@@ -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)

@@ -923,7 +923,7 @@ bool QgsGeometryAnalyzer::eventLayer( QgsVectorLayer* lineLayer, QgsVectorLayer*
eventLayer->dataProvider()->encoding(),
eventLayer->fields(),
memoryProviderType,
&( lineLayer->crs() ),
lineLayer->crs(),
outputFormat );
}
else
@@ -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;

@@ -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 )
@@ -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;
@@ -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;
@@ -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;
@@ -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,
@@ -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 )
@@ -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,
@@ -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,
@@ -178,7 +178,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
{
layerOptions.append( "SHPT=NULL" );
}
srs = nullptr;
srs = QgsCoordinateReferenceSystem();
}
else
{
@@ -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() );
}
@@ -369,7 +369,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
CPLSetConfigOption( "SHAPE_ENCODING", nullptr );
}

if ( srs )
if ( srs.isValid() )
{
if ( mOgrDriverName == "ESRI Shapefile" )
{
@@ -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
@@ -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,
@@ -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,
@@ -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() );
@@ -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
@@ -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,
@@ -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(),
@@ -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(),
@@ -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,
@@ -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 );
@@ -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 ) );
@@ -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 ) );
@@ -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 )
@@ -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;

0 comments on commit fd42ed3

Please sign in to comment.
You can’t perform that action at this time.