Skip to content

Commit

Permalink
vector file writer: defer destroying spatial reference (fixes #10515)
Browse files Browse the repository at this point in the history
(cherry picked from commit cee63c8)
  • Loading branch information
jef-n committed Nov 26, 2015
1 parent e9d2a0e commit 4476749
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -69,6 +69,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
)
: mDS( NULL )
, mLayer( NULL )
, mOgrRef( NULL )
, mGeom( NULL )
, mError( NoError )
, mCodec( 0 )
Expand Down Expand Up @@ -258,12 +259,11 @@ QgsVectorFileWriter::QgsVectorFileWriter(
}

// consider spatial reference system of the layer
OGRSpatialReferenceH ogrRef = NULL;
if ( srs )
{
QString srsWkt = srs->toWkt();
QgsDebugMsg( "WKT to save as is " + srsWkt );
ogrRef = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
mOgrRef = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
}

// datasource created, now create the output layer
Expand All @@ -283,7 +283,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
// disable encoding conversion of OGR Shapefile layer
CPLSetConfigOption( "SHAPE_ENCODING", "" );

mLayer = OGR_DS_CreateLayer( mDS, TO8F( layerName ), ogrRef, wkbType, options );
mLayer = OGR_DS_CreateLayer( mDS, TO8F( layerName ), mOgrRef, wkbType, options );

if ( options )
{
Expand Down Expand Up @@ -316,8 +316,6 @@ QgsVectorFileWriter::QgsVectorFileWriter(
QgsDebugMsg( "Couldn't open file " + layerName + ".qpj" );
}
}

OSRDestroySpatialReference( ogrRef );
}

if ( mLayer == NULL )
Expand Down Expand Up @@ -1810,6 +1808,11 @@ QgsVectorFileWriter::~QgsVectorFileWriter()
{
OGR_DS_Destroy( mDS );
}

if ( mOgrRef )
{
OSRDestroySpatialReference( mOgrRef );
}
}

QgsVectorFileWriter::WriterError
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorfilewriter.h
Expand Up @@ -275,6 +275,7 @@ class CORE_EXPORT QgsVectorFileWriter

OGRDataSourceH mDS;
OGRLayerH mLayer;
OGRSpatialReferenceH mOgrRef;
OGRGeometryH mGeom;

QgsFields mFields;
Expand Down

0 comments on commit 4476749

Please sign in to comment.