Skip to content
Permalink
Browse files
Use weak layer pointer instead of layer id in QgsJsonExporter
  • Loading branch information
nyalldawson committed May 24, 2017
1 parent f63adfe commit 987f80a4cedfcfb7c2ab6a1a972051ff17db9632
Showing with 12 additions and 13 deletions.
  1. +7 −8 src/core/qgsjsonutils.cpp
  2. +5 −5 src/core/qgsjsonutils.h
@@ -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 )
{
@@ -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();
@@ -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 )
@@ -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 )
@@ -21,9 +21,9 @@
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransform.h"
#include "qgsfields.h"
#include "qgsvectorlayer.h"

class QTextCodec;
class QgsVectorLayer;

/** \ingroup core
* \class QgsJSONExporter
@@ -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
@@ -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()
@@ -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;

0 comments on commit 987f80a

Please sign in to comment.