Skip to content

Commit

Permalink
Use weak layer pointer instead of layer id in QgsJsonExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 24, 2017
1 parent f63adfe commit 987f80a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions src/core/qgsjsonutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#include <QJsonDocument>
#include <QJsonArray>

QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer *vectorLayer, int precision )
QgsJSONExporter::QgsJSONExporter(QgsVectorLayer *vectorLayer, int precision )
: mPrecision( precision )
, mIncludeGeometry( true )
, mIncludeAttributes( true )
, mIncludeRelatedAttributes( false )
, mLayerId( vectorLayer ? vectorLayer->id() : QString() )
, mLayer( vectorLayer )
{
if ( vectorLayer )
{
Expand All @@ -42,9 +42,9 @@ QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer *vectorLayer, int precisi
mTransform.setDestinationCrs( QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ) );
}

void QgsJSONExporter::setVectorLayer( const QgsVectorLayer *vectorLayer )
void QgsJSONExporter::setVectorLayer( QgsVectorLayer *vectorLayer )
{
mLayerId = vectorLayer ? vectorLayer->id() : QString();
mLayer = vectorLayer;
if ( vectorLayer )
{
mCrs = vectorLayer->crs();
Expand All @@ -54,7 +54,7 @@ void QgsJSONExporter::setVectorLayer( const QgsVectorLayer *vectorLayer )

QgsVectorLayer *QgsJSONExporter::vectorLayer() const
{
return qobject_cast< QgsVectorLayer * >( QgsProject::instance()->mapLayer( mLayerId ) );
return mLayer.data();
}

void QgsJSONExporter::setSourceCrs( const QgsCoordinateReferenceSystem &crs )
Expand Down Expand Up @@ -151,10 +151,9 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
}

// related attributes
QgsVectorLayer *vl = vectorLayer();
if ( vl && mIncludeRelatedAttributes )
if ( mLayer.data() && mIncludeRelatedAttributes )
{
QList< QgsRelation > relations = QgsProject::instance()->relationManager()->referencedRelations( vl );
QList< QgsRelation > relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer.data() );
Q_FOREACH ( const QgsRelation &relation, relations )
{
if ( attributeCounter > 0 )
Expand Down
10 changes: 5 additions & 5 deletions src/core/qgsjsonutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransform.h"
#include "qgsfields.h"
#include "qgsvectorlayer.h"

class QTextCodec;
class QgsVectorLayer;

/** \ingroup core
* \class QgsJSONExporter
Expand All @@ -43,7 +43,7 @@ class CORE_EXPORT QgsJSONExporter
* \param precision maximum number of decimal places to use for geometry coordinates,
* the RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6
*/
QgsJSONExporter( const QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );
QgsJSONExporter( QgsVectorLayer *vectorLayer = nullptr, int precision = 6 );

/** Sets the maximum number of decimal places to use in geometry coordinates.
* The RFC 7946 GeoJSON specification recommends limiting coordinate precision to 6
Expand Down Expand Up @@ -97,7 +97,7 @@ class CORE_EXPORT QgsJSONExporter
* \param vectorLayer vector layer
* \see vectorLayer()
*/
void setVectorLayer( const QgsVectorLayer *vectorLayer );
void setVectorLayer( QgsVectorLayer *vectorLayer );

/** Returns the associated vector layer, if set.
* \see setVectorLayer()
Expand Down Expand Up @@ -193,8 +193,8 @@ class CORE_EXPORT QgsJSONExporter
//! Whether to include attributes from related features in JSON export
bool mIncludeRelatedAttributes;

//! Layer ID of associated vector layer. Required for related attribute export.
QString mLayerId;
//! Associated vector layer. Required for related attribute export.
QPointer< QgsVectorLayer > mLayer;

QgsCoordinateReferenceSystem mCrs;

Expand Down

0 comments on commit 987f80a

Please sign in to comment.