Skip to content

Commit a2efab0

Browse files
committedJul 14, 2016
Make QgsVectorLayerImport use QgsCoordinateReferenceSystem
references, not pointers
1 parent e683101 commit a2efab0

20 files changed

+147
-97
lines changed
 

‎doc/api_break.dox

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ objects are implicitly shared, returning a copy helps simplify and make code mor
4141
only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
4242
</ul>
4343

44+
\subsection qgis_api_break_3_0_QgsVectorLayerImport QgsVectorLayerImport
45+
46+
<ul>
47+
<li>QgsVectorLayerImport now takes references instead of pointers to QgsCoordinateReferenceSystem objects. Since
48+
QgsCoordinateReferenceSystem is now implicitly shared, using references to QgsCoordinateReferenceSystem rather than
49+
pointers makes for more robust, safer code. Use an invalid (default constructed) QgsCoordinateReferenceSystem
50+
in code which previously passed a null pointer to QgsVectorLayerImport.</li>
51+
</ul>
52+
4453
\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter
4554

4655
<ul>

‎python/core/qgsvectorlayerimport.sip

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,50 @@ class QProgressDialog;
3131
ErrUserCancelled, /*!< User cancelled the import*/
3232
};
3333

34-
/** Write contents of vector layer to a different datasource */
34+
/**
35+
* Writes the contents of vector layer to a different datasource.
36+
* @param layer source layer
37+
* @param uri URI for destination data source
38+
* @param providerKey string key for destination data provider
39+
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
40+
* not available
41+
* @param onlySelected set to true to export only selected features
42+
* @param errorMessage if non-null, will be set to any error messages
43+
* @param skipAttributeCreation set to true to skip exporting feature attributes
44+
* @param options optional provider dataset options
45+
* @param progress optional progress dialog to show progress of export
46+
* @returns NoError for a successful export, or encountered error
47+
*/
3548
static ImportError importLayer( QgsVectorLayer* layer,
3649
const QString& uri,
3750
const QString& providerKey,
38-
const QgsCoordinateReferenceSystem *destCRS,
51+
const QgsCoordinateReferenceSystem& destCRS,
3952
bool onlySelected = false,
40-
QString *errorMessage /Out/ = 0,
53+
QString *errorMessage /Out/ = nullptr,
4154
bool skipAttributeCreation = false,
42-
QMap<QString, QVariant> *options = 0,
43-
QProgressDialog *progress = 0
55+
QMap<QString, QVariant> *options = nullptr,
56+
QProgressDialog *progress = nullptr
4457
);
4558

46-
/** Create a empty layer and add fields to it */
59+
/** Constructor for QgsVectorLayerImport.
60+
* @param uri URI for destination data source
61+
* @param provider string key for destination data provider
62+
* @param fields fields to include in created layer
63+
* @param geometryType destination geometry type
64+
* @param crs desired CRS, or an invalid (default constructed) CRS if
65+
* not available
66+
* @param overwrite set to true to overwrite any existing data source
67+
* @param options optional provider dataset options
68+
* @param progress optional progress dialog to show progress of export
69+
*/
4770
QgsVectorLayerImport( const QString &uri,
4871
const QString &provider,
4972
const QgsFields &fields,
5073
QGis::WkbType geometryType,
51-
const QgsCoordinateReferenceSystem* crs,
74+
const QgsCoordinateReferenceSystem& crs,
5275
bool overwrite = false,
53-
const QMap<QString, QVariant> *options = 0,
54-
QProgressDialog *progress = 0
76+
const QMap<QString, QVariant> *options = nullptr,
77+
QProgressDialog *progress = nullptr
5578
);
5679

5780
/** Checks whether there were any errors */

‎src/core/qgsvectorlayerimport.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
3434
const QString &uri,
3535
const QgsFields &fields,
3636
QGis::WkbType geometryType,
37-
const QgsCoordinateReferenceSystem *destCRS,
37+
const QgsCoordinateReferenceSystem &destCRS,
3838
bool overwrite,
3939
QMap<int, int> *oldToNewAttrIdx,
4040
QString *errorMessage,
@@ -46,7 +46,7 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
4646
const QString &providerKey,
4747
const QgsFields& fields,
4848
QGis::WkbType geometryType,
49-
const QgsCoordinateReferenceSystem* crs,
49+
const QgsCoordinateReferenceSystem& crs,
5050
bool overwrite,
5151
const QMap<QString, QVariant> *options,
5252
QProgressDialog *progress )
@@ -206,21 +206,21 @@ QgsVectorLayerImport::ImportError
206206
QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
207207
const QString& uri,
208208
const QString& providerKey,
209-
const QgsCoordinateReferenceSystem *destCRS,
209+
const QgsCoordinateReferenceSystem& destCRS,
210210
bool onlySelected,
211211
QString *errorMessage,
212212
bool skipAttributeCreation,
213213
QMap<QString, QVariant> *options,
214214
QProgressDialog *progress )
215215
{
216-
const QgsCoordinateReferenceSystem* outputCRS;
216+
QgsCoordinateReferenceSystem outputCRS;
217217
QgsCoordinateTransform* ct = nullptr;
218218
bool shallTransform = false;
219219

220220
if ( !layer )
221221
return ErrInvalidLayer;
222222

223-
if ( destCRS && destCRS->isValid() )
223+
if ( destCRS.isValid() )
224224
{
225225
// This means we should transform
226226
outputCRS = destCRS;
@@ -229,7 +229,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
229229
else
230230
{
231231
// This means we shouldn't transform, use source CRS as output (if defined)
232-
outputCRS = &layer->crs();
232+
outputCRS = layer->crs();
233233
}
234234

235235

@@ -314,8 +314,8 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
314314
const QgsFeatureIds& ids = layer->selectedFeaturesIds();
315315

316316
// Create our transform
317-
if ( destCRS )
318-
ct = new QgsCoordinateTransform( layer->crs(), *destCRS );
317+
if ( destCRS.isValid() )
318+
ct = new QgsCoordinateTransform( layer->crs(), destCRS );
319319

320320
// Check for failure
321321
if ( !ct )

‎src/core/qgsvectorlayerimport.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,47 @@ class CORE_EXPORT QgsVectorLayerImport
5151
ErrUserCancelled, /*!< User cancelled the import*/
5252
};
5353

54-
/** Write contents of vector layer to a different datasource */
54+
/**
55+
* Writes the contents of vector layer to a different datasource.
56+
* @param layer source layer
57+
* @param uri URI for destination data source
58+
* @param providerKey string key for destination data provider
59+
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
60+
* not available
61+
* @param onlySelected set to true to export only selected features
62+
* @param errorMessage if non-null, will be set to any error messages
63+
* @param skipAttributeCreation set to true to skip exporting feature attributes
64+
* @param options optional provider dataset options
65+
* @param progress optional progress dialog to show progress of export
66+
* @returns NoError for a successful export, or encountered error
67+
*/
5568
static ImportError importLayer( QgsVectorLayer* layer,
5669
const QString& uri,
5770
const QString& providerKey,
58-
const QgsCoordinateReferenceSystem *destCRS,
71+
const QgsCoordinateReferenceSystem& destCRS,
5972
bool onlySelected = false,
6073
QString *errorMessage = nullptr,
6174
bool skipAttributeCreation = false,
6275
QMap<QString, QVariant> *options = nullptr,
6376
QProgressDialog *progress = nullptr
6477
);
6578

66-
/** Create a empty layer and add fields to it */
79+
/** Constructor for QgsVectorLayerImport.
80+
* @param uri URI for destination data source
81+
* @param provider string key for destination data provider
82+
* @param fields fields to include in created layer
83+
* @param geometryType destination geometry type
84+
* @param crs desired CRS, or an invalid (default constructed) CRS if
85+
* not available
86+
* @param overwrite set to true to overwrite any existing data source
87+
* @param options optional provider dataset options
88+
* @param progress optional progress dialog to show progress of export
89+
*/
6790
QgsVectorLayerImport( const QString &uri,
6891
const QString &provider,
6992
const QgsFields &fields,
7093
QGis::WkbType geometryType,
71-
const QgsCoordinateReferenceSystem* crs,
94+
const QgsCoordinateReferenceSystem& crs,
7295
bool overwrite = false,
7396
const QMap<QString, QVariant> *options = nullptr,
7497
QProgressDialog *progress = nullptr

‎src/gui/qgsnewvectorlayerdialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pE
270270
QgsDebugMsg( "ogr provider loaded" );
271271

272272
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
273-
const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
273+
const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem & );
274274
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
275275
if ( createEmptyDataSource )
276276
{
277277
if ( geometrytype != QGis::WKBUnknown )
278278
{
279279
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( crsId );
280-
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs ) )
280+
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs ) )
281281
{
282282
return QString::null;
283283
}

‎src/plugins/geometry_checker/ui/qgsgeometrycheckerresulttab.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,13 @@ bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString& file )
245245
{
246246
return false;
247247
}
248-
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
248+
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem& );
249249
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) );
250250
if ( !createEmptyDataSource )
251251
{
252252
return false;
253253
}
254-
if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, &mFeaturePool->getLayer()->crs() ) )
254+
if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, mFeaturePool->getLayer()->crs() ) )
255255
{
256256
return false;
257257
}

‎src/providers/db2/qgsdb2dataitems.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ bool QgsDb2ConnectionItem::handleDrop( const QMimeData* data, const QString& toS
361361

362362
QgsVectorLayerImport::ImportError err;
363363
QString importError;
364-
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "DB2", &srcLayer->crs(), false, &importError, false, nullptr, progress );
364+
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "DB2", srcLayer->crs(), false, &importError, false, nullptr, progress );
365365
if ( err == QgsVectorLayerImport::NoError )
366366
{
367367
importResults.append( tr( "%1: OK!" ).arg( u.name ) );

‎src/providers/db2/qgsdb2provider.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,15 +1263,14 @@ bool QgsDb2Provider::changeGeometryValues( const QgsGeometryMap &geometry_map )
12631263
return true;
12641264
}
12651265

1266-
QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer(
1267-
const QString& uri,
1268-
const QgsFields &fields,
1269-
QGis::WkbType wkbType,
1270-
const QgsCoordinateReferenceSystem *srs,
1271-
bool overwrite,
1272-
QMap<int, int> *oldToNewAttrIdxMap,
1273-
QString *errorMessage,
1274-
const QMap<QString, QVariant> *options )
1266+
QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer( const QString& uri,
1267+
const QgsFields &fields,
1268+
QGis::WkbType wkbType,
1269+
const QgsCoordinateReferenceSystem& srs,
1270+
bool overwrite,
1271+
QMap<int, int> *oldToNewAttrIdxMap,
1272+
QString *errorMessage,
1273+
const QMap<QString, QVariant> *options )
12751274
{
12761275
Q_UNUSED( options );
12771276

@@ -1297,8 +1296,8 @@ QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer(
12971296
// srs->posgisSrid() seems to return the authority id which is
12981297
// most often the EPSG id. Hopefully DB2 has defined an SRS using this
12991298
// value as the srid / srs_id. If not, we are out of luck.
1300-
QgsDebugMsg( "srs: " + srs->toWkt() );
1301-
long srid = srs->postgisSrid();
1299+
QgsDebugMsg( "srs: " + srs.toWkt() );
1300+
long srid = srs.postgisSrid();
13021301
QgsDebugMsg( QString( "srid: %1" ).arg( srid ) );
13031302
if ( srid >= 0 )
13041303
{
@@ -1740,7 +1739,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
17401739
const QString& uri,
17411740
const QgsFields &fields,
17421741
QGis::WkbType wkbType,
1743-
const QgsCoordinateReferenceSystem *srs,
1742+
const QgsCoordinateReferenceSystem &srs,
17441743
bool overwrite,
17451744
QMap<int, int> *oldToNewAttrIdxMap,
17461745
QString *errorMessage,

‎src/providers/db2/qgsdb2provider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class QgsDb2Provider : public QgsVectorDataProvider
124124
const QString& uri,
125125
const QgsFields &fields,
126126
QGis::WkbType wkbType,
127-
const QgsCoordinateReferenceSystem *srs,
127+
const QgsCoordinateReferenceSystem& srs,
128128
bool overwrite,
129129
QMap<int, int> *oldToNewAttrIdxMap,
130130
QString *errorMessage = nullptr,

‎src/providers/mssql/qgsmssqldataitems.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ bool QgsMssqlConnectionItem::handleDrop( const QMimeData* data, const QString& t
426426

427427
QgsVectorLayerImport::ImportError err;
428428
QString importError;
429-
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "mssql", &srcLayer->crs(), false, &importError, false, nullptr, progress );
429+
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "mssql", srcLayer->crs(), false, &importError, false, nullptr, progress );
430430
if ( err == QgsVectorLayerImport::NoError )
431431
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
432432
else if ( err == QgsVectorLayerImport::ErrUserCancelled )

‎src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,15 +1650,14 @@ QGis::WkbType QgsMssqlProvider::getWkbType( const QString& geometryType, int dim
16501650
}
16511651

16521652

1653-
QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
1654-
const QString& uri,
1655-
const QgsFields &fields,
1656-
QGis::WkbType wkbType,
1657-
const QgsCoordinateReferenceSystem *srs,
1658-
bool overwrite,
1659-
QMap<int, int> *oldToNewAttrIdxMap,
1660-
QString *errorMessage,
1661-
const QMap<QString, QVariant> *options )
1653+
QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer( const QString& uri,
1654+
const QgsFields &fields,
1655+
QGis::WkbType wkbType,
1656+
const QgsCoordinateReferenceSystem& srs,
1657+
bool overwrite,
1658+
QMap<int, int> *oldToNewAttrIdxMap,
1659+
QString *errorMessage,
1660+
const QMap<QString, QVariant> *options )
16621661
{
16631662
Q_UNUSED( options );
16641663

@@ -1758,23 +1757,23 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
17581757

17591758
// set up spatial reference id
17601759
int srid = 0;
1761-
if ( srs->isValid() )
1760+
if ( srs.isValid() )
17621761
{
1763-
srid = srs->srsid();
1762+
srid = srs.srsid();
17641763
QString auth_srid = "null";
17651764
QString auth_name = "null";
1766-
QStringList sl = srs->authid().split( ':' );
1765+
QStringList sl = srs.authid().split( ':' );
17671766
if ( sl.length() == 2 )
17681767
{
17691768
auth_name = '\'' + sl[0] + '\'';
17701769
auth_srid = sl[1];
17711770
}
17721771
sql = QString( "IF NOT EXISTS (SELECT * FROM spatial_ref_sys WHERE srid=%1) INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text) VALUES (%1, %2, %3, '%4', '%5')" )
1773-
.arg( srs->srsid() )
1772+
.arg( srs.srsid() )
17741773
.arg( auth_name,
17751774
auth_srid,
1776-
srs->toWkt(),
1777-
srs->toProj4() );
1775+
srs.toWkt(),
1776+
srs.toProj4() );
17781777
if ( !q.exec( sql ) )
17791778
{
17801779
if ( errorMessage )
@@ -1958,7 +1957,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
19581957
const QString& uri,
19591958
const QgsFields &fields,
19601959
QGis::WkbType wkbType,
1961-
const QgsCoordinateReferenceSystem *srs,
1960+
const QgsCoordinateReferenceSystem &srs,
19621961
bool overwrite,
19631962
QMap<int, int> *oldToNewAttrIdxMap,
19641963
QString *errorMessage,

‎src/providers/mssql/qgsmssqlprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
192192
const QString& uri,
193193
const QgsFields &fields,
194194
QGis::WkbType wkbType,
195-
const QgsCoordinateReferenceSystem *srs,
195+
const QgsCoordinateReferenceSystem &srs,
196196
bool overwrite,
197197
QMap<int, int> *oldToNewAttrIdxMap,
198198
QString *errorMessage = nullptr,

0 commit comments

Comments
 (0)
Please sign in to comment.