Skip to content
Permalink
Browse files

QgsVectorFileWriter: fix DGN creation (fixes #19722, fixes #19723)

  • Loading branch information
rouault committed Sep 21, 2018
1 parent 35c954e commit 6b620193178a1f55e3c1113b46047f42a580b937
@@ -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 )
@@ -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
}

@@ -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() );
@@ -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
@@ -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
@@ -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
@@ -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(
@@ -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.
You can’t perform that action at this time.