Skip to content

Commit

Permalink
QgsVectorFileWriter: fix DGN creation (fixes #19722, fixes #19723)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Sep 21, 2018
1 parent 35c954e commit 6b62019
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -140,6 +140,10 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString &vectorFileName,

bool QgsVectorFileWriter::supportsFeatureStyles( const QString &driverName )
{
if ( driverName == QLatin1String( "MapInfo MIF" ) )
{
return true;
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,3,0)
GDALDriverH gdalDriver = GDALGetDriverByName( driverName.toLocal8Bit().constData() );
if ( !gdalDriver )
Expand All @@ -151,7 +155,7 @@ bool QgsVectorFileWriter::supportsFeatureStyles( const QString &driverName )

return CSLFetchBoolean( driverMetadata, GDAL_DCAP_FEATURE_STYLES, false );
#else
return driverName == QLatin1String( "DXF" ) || driverName == QLatin1String( "KML" ) || driverName == QLatin1String( "MapInfo File" ) || driverName == QLatin1String( "MapInfo MIF" );
return driverName == QLatin1String( "DXF" ) || driverName == QLatin1String( "KML" ) || driverName == QLatin1String( "MapInfo File" );
#endif
}

Expand Down Expand Up @@ -415,16 +419,16 @@ void QgsVectorFileWriter::init( QString vectorFileName,
// disable encoding conversion of OGR Shapefile layer
CPLSetConfigOption( "SHAPE_ENCODING", "" );

if ( driverName == QLatin1String( "DGN" ) )
{
mLayer = OGR_DS_GetLayerByName( mDS.get(), "elements" );
}
else if ( action == CreateOrOverwriteFile || action == CreateOrOverwriteLayer )
if ( action == CreateOrOverwriteFile || action == CreateOrOverwriteLayer )
{
mLayer = OGR_DS_CreateLayer( mDS.get(), layerName.toUtf8().constData(), mOgrRef, wkbType, options );
if ( newLayer )
if ( newLayer && mLayer )
*newLayer = OGR_L_GetName( mLayer );
}
else if ( driverName == QLatin1String( "DGN" ) )
{
mLayer = OGR_DS_GetLayerByName( mDS.get(), "elements" );
}
else
{
mLayer = OGR_DS_GetLayerByName( mDS.get(), layerName.toUtf8().constData() );
Expand Down
12 changes: 10 additions & 2 deletions src/gui/ogr/qgsvectorlayersaveasdialog.cpp
Expand Up @@ -758,7 +758,11 @@ QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
}
}

return options + mOgrDatasourceOptions->toPlainText().split( '\n' );
QString plainText = mOgrDatasourceOptions->toPlainText().trimmed();
if ( !plainText.isEmpty() )
options += plainText.split( '\n' );

return options;
}

QStringList QgsVectorLayerSaveAsDialog::layerOptions() const
Expand Down Expand Up @@ -813,7 +817,11 @@ QStringList QgsVectorLayerSaveAsDialog::layerOptions() const
}
}

return options + mOgrLayerOptions->toPlainText().split( '\n' );
QString plainText = mOgrLayerOptions->toPlainText().trimmed();
if ( !plainText.isEmpty() )
options += plainText.split( '\n' );

return options;
}

QgsAttributeList QgsVectorLayerSaveAsDialog::selectedAttributes() const
Expand Down
43 changes: 43 additions & 0 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -116,6 +116,9 @@ def testWriteWithLongLongField(self):
idx = vl.fields().indexFromName('fldlonglong')
self.assertEqual(vl.getFeature(1).attributes()[idx], 2262000000)

del vl
os.unlink(filename + '.gpkg')

def testWriteWithBoolField(self):

# init connection string
Expand Down Expand Up @@ -156,6 +159,9 @@ def testWriteWithBoolField(self):
self.assertEqual(vl.getFeature(1).attributes()[idx], 1)
self.assertEqual(vl.getFeature(2).attributes()[idx], 0)

del vl
os.unlink(filename + '.gpkg')

def testDateTimeWriteShapefile(self):
"""Check writing date and time fields to an ESRI shapefile."""
ml = QgsVectorLayer(
Expand Down Expand Up @@ -939,6 +945,43 @@ def testOverwriteGPKG(self):
options)
self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message)

def testCreateDGN(self):
ml = QgsVectorLayer('Point?crs=epsg:4326', 'test', 'memory')
provider = ml.dataProvider()
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10, 10)))
provider.addFeatures([feat])

filename = os.path.join(str(QDir.tempPath()), 'testCreateDGN.dgn')
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
rc, errmsg = QgsVectorFileWriter.writeAsVectorFormat(ml, filename, 'utf-8', crs, 'DGN')

# open the resulting file
vl = QgsVectorLayer(filename, '', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 1)
del vl

# append
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = 'DGN'
options.layerName = 'test'
options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields
write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat(
ml,
filename,
options)
self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message)

# open the resulting file
vl = QgsVectorLayer(filename, '', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 2)
del vl

os.unlink(filename)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6b62019

Please sign in to comment.