Skip to content

Commit

Permalink
Make QgsVectorLayerImport use QgsCoordinateReferenceSystem
Browse files Browse the repository at this point in the history
references, not pointers
  • Loading branch information
nyalldawson committed Jul 14, 2016
1 parent e683101 commit a2efab0
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 97 deletions.
9 changes: 9 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ objects are implicitly shared, returning a copy helps simplify and make code mor
only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorLayerImport QgsVectorLayerImport

<ul>
<li>QgsVectorLayerImport 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 QgsVectorLayerImport.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter

<ul>
Expand Down
41 changes: 32 additions & 9 deletions python/core/qgsvectorlayerimport.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,50 @@ class QProgressDialog;
ErrUserCancelled, /*!< User cancelled the import*/
};

/** Write contents of vector layer to a different datasource */
/**
* Writes the contents of vector layer to a different datasource.
* @param layer source layer
* @param uri URI for destination data source
* @param providerKey string key for destination data provider
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
* not available
* @param onlySelected set to true to export only selected features
* @param errorMessage if non-null, will be set to any error messages
* @param skipAttributeCreation set to true to skip exporting feature attributes
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
* @returns NoError for a successful export, or encountered error
*/
static ImportError importLayer( QgsVectorLayer* layer,
const QString& uri,
const QString& providerKey,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS,
bool onlySelected = false,
QString *errorMessage /Out/ = 0,
QString *errorMessage /Out/ = nullptr,
bool skipAttributeCreation = false,
QMap<QString, QVariant> *options = 0,
QProgressDialog *progress = 0
QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
);

/** Create a empty layer and add fields to it */
/** Constructor for QgsVectorLayerImport.
* @param uri URI for destination data source
* @param provider string key for destination data provider
* @param fields fields to include in created layer
* @param geometryType destination geometry type
* @param crs desired CRS, or an invalid (default constructed) CRS if
* not available
* @param overwrite set to true to overwrite any existing data source
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
*/
QgsVectorLayerImport( const QString &uri,
const QString &provider,
const QgsFields &fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
const QgsCoordinateReferenceSystem& crs,
bool overwrite = false,
const QMap<QString, QVariant> *options = 0,
QProgressDialog *progress = 0
const QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
);

/** Checks whether there were any errors */
Expand Down
16 changes: 8 additions & 8 deletions src/core/qgsvectorlayerimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
const QString &uri,
const QgsFields &fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem &destCRS,
bool overwrite,
QMap<int, int> *oldToNewAttrIdx,
QString *errorMessage,
Expand All @@ -46,7 +46,7 @@ QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
const QString &providerKey,
const QgsFields& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
const QgsCoordinateReferenceSystem& crs,
bool overwrite,
const QMap<QString, QVariant> *options,
QProgressDialog *progress )
Expand Down Expand Up @@ -206,21 +206,21 @@ QgsVectorLayerImport::ImportError
QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
const QString& uri,
const QString& providerKey,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS,
bool onlySelected,
QString *errorMessage,
bool skipAttributeCreation,
QMap<QString, QVariant> *options,
QProgressDialog *progress )
{
const QgsCoordinateReferenceSystem* outputCRS;
QgsCoordinateReferenceSystem outputCRS;
QgsCoordinateTransform* ct = nullptr;
bool shallTransform = false;

if ( !layer )
return ErrInvalidLayer;

if ( destCRS && destCRS->isValid() )
if ( destCRS.isValid() )
{
// This means we should transform
outputCRS = destCRS;
Expand All @@ -229,7 +229,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
else
{
// This means we shouldn't transform, use source CRS as output (if defined)
outputCRS = &layer->crs();
outputCRS = layer->crs();
}


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

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

// Check for failure
if ( !ct )
Expand Down
31 changes: 27 additions & 4 deletions src/core/qgsvectorlayerimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,47 @@ class CORE_EXPORT QgsVectorLayerImport
ErrUserCancelled, /*!< User cancelled the import*/
};

/** Write contents of vector layer to a different datasource */
/**
* Writes the contents of vector layer to a different datasource.
* @param layer source layer
* @param uri URI for destination data source
* @param providerKey string key for destination data provider
* @param destCRS destination CRS, or an invalid (default constructed) CRS if
* not available
* @param onlySelected set to true to export only selected features
* @param errorMessage if non-null, will be set to any error messages
* @param skipAttributeCreation set to true to skip exporting feature attributes
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
* @returns NoError for a successful export, or encountered error
*/
static ImportError importLayer( QgsVectorLayer* layer,
const QString& uri,
const QString& providerKey,
const QgsCoordinateReferenceSystem *destCRS,
const QgsCoordinateReferenceSystem& destCRS,
bool onlySelected = false,
QString *errorMessage = nullptr,
bool skipAttributeCreation = false,
QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
);

/** Create a empty layer and add fields to it */
/** Constructor for QgsVectorLayerImport.
* @param uri URI for destination data source
* @param provider string key for destination data provider
* @param fields fields to include in created layer
* @param geometryType destination geometry type
* @param crs desired CRS, or an invalid (default constructed) CRS if
* not available
* @param overwrite set to true to overwrite any existing data source
* @param options optional provider dataset options
* @param progress optional progress dialog to show progress of export
*/
QgsVectorLayerImport( const QString &uri,
const QString &provider,
const QgsFields &fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
const QgsCoordinateReferenceSystem& crs,
bool overwrite = false,
const QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pE
QgsDebugMsg( "ogr provider loaded" );

typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem & );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
if ( createEmptyDataSource )
{
if ( geometrytype != QGis::WKBUnknown )
{
QgsCoordinateReferenceSystem srs = QgsCRSCache::instance()->crsBySrsId( crsId );
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs ) )
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs ) )
{
return QString::null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString& file )
{
return false;
}
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem& );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) );
if ( !createEmptyDataSource )
{
return false;
}
if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, &mFeaturePool->getLayer()->crs() ) )
if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, mFeaturePool->getLayer()->crs() ) )
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/db2/qgsdb2dataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ bool QgsDb2ConnectionItem::handleDrop( const QMimeData* data, const QString& toS

QgsVectorLayerImport::ImportError err;
QString importError;
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "DB2", &srcLayer->crs(), false, &importError, false, nullptr, progress );
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "DB2", srcLayer->crs(), false, &importError, false, nullptr, progress );
if ( err == QgsVectorLayerImport::NoError )
{
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
Expand Down
23 changes: 11 additions & 12 deletions src/providers/db2/qgsdb2provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,15 +1263,14 @@ bool QgsDb2Provider::changeGeometryValues( const QgsGeometryMap &geometry_map )
return true;
}

QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer(
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
const QMap<QString, QVariant> *options )
QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer( const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem& srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
const QMap<QString, QVariant> *options )
{
Q_UNUSED( options );

Expand All @@ -1297,8 +1296,8 @@ QgsVectorLayerImport::ImportError QgsDb2Provider::createEmptyLayer(
// srs->posgisSrid() seems to return the authority id which is
// most often the EPSG id. Hopefully DB2 has defined an SRS using this
// value as the srid / srs_id. If not, we are out of luck.
QgsDebugMsg( "srs: " + srs->toWkt() );
long srid = srs->postgisSrid();
QgsDebugMsg( "srs: " + srs.toWkt() );
long srid = srs.postgisSrid();
QgsDebugMsg( QString( "srid: %1" ).arg( srid ) );
if ( srid >= 0 )
{
Expand Down Expand Up @@ -1740,7 +1739,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/db2/qgsdb2provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class QgsDb2Provider : public QgsVectorDataProvider
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
const QgsCoordinateReferenceSystem& srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage = nullptr,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqldataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ bool QgsMssqlConnectionItem::handleDrop( const QMimeData* data, const QString& t

QgsVectorLayerImport::ImportError err;
QString importError;
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "mssql", &srcLayer->crs(), false, &importError, false, nullptr, progress );
err = QgsVectorLayerImport::importLayer( srcLayer, uri, "mssql", srcLayer->crs(), false, &importError, false, nullptr, progress );
if ( err == QgsVectorLayerImport::NoError )
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
else if ( err == QgsVectorLayerImport::ErrUserCancelled )
Expand Down
31 changes: 15 additions & 16 deletions src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,15 +1650,14 @@ QGis::WkbType QgsMssqlProvider::getWkbType( const QString& geometryType, int dim
}


QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
const QMap<QString, QVariant> *options )
QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer( const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem& srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
const QMap<QString, QVariant> *options )
{
Q_UNUSED( options );

Expand Down Expand Up @@ -1758,23 +1757,23 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(

// set up spatial reference id
int srid = 0;
if ( srs->isValid() )
if ( srs.isValid() )
{
srid = srs->srsid();
srid = srs.srsid();
QString auth_srid = "null";
QString auth_name = "null";
QStringList sl = srs->authid().split( ':' );
QStringList sl = srs.authid().split( ':' );
if ( sl.length() == 2 )
{
auth_name = '\'' + sl[0] + '\'';
auth_srid = sl[1];
}
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')" )
.arg( srs->srsid() )
.arg( srs.srsid() )
.arg( auth_name,
auth_srid,
srs->toWkt(),
srs->toProj4() );
srs.toWkt(),
srs.toProj4() );
if ( !q.exec( sql ) )
{
if ( errorMessage )
Expand Down Expand Up @@ -1958,7 +1957,7 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
const QString& uri,
const QgsFields &fields,
QGis::WkbType wkbType,
const QgsCoordinateReferenceSystem *srs,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage = nullptr,
Expand Down
Loading

0 comments on commit a2efab0

Please sign in to comment.