Skip to content
Permalink
Browse files

Fix writing triangle geometry type via QgsVectorFileWriter fails

Refs #36638
  • Loading branch information
nyalldawson committed Jun 10, 2020
1 parent d3a54ee commit e0c03f9f0848490f358876ae5be6973961700bc9
Showing with 37 additions and 2 deletions.
  1. +1 −1 src/core/qgsvectorfilewriter.cpp
  2. +36 −1 tests/src/python/test_qgsvectorfilewriter.py
@@ -2622,7 +2622,7 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
}
else // wkb type matches
{
QByteArray wkb( geom.asWkb() );
QByteArray wkb( geom.asWkb( QgsAbstractGeometry::FlagExportTrianglesAsPolygons ) );
OGRGeometryH ogrGeom = createEmptyGeometry( mWkbType );
OGRErr err = OGR_G_ImportFromWkb( ogrGeom, reinterpret_cast<unsigned char *>( const_cast<char *>( wkb.constData() ) ), wkb.length() );
if ( err != OGRERR_NONE )
@@ -26,7 +26,10 @@
QgsProject,
QgsWkbTypes,
QgsRectangle,
QgsCoordinateTransform
QgsCoordinateTransform,
QgsMultiPolygon,
QgsTriangle,
QgsPoint
)
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QByteArray
import os
@@ -1202,6 +1205,38 @@ def testWriteGpkgWithFID(self):
self.assertEqual(f.attributes(), [123, 'text1'])
self.assertEqual(f.id(), 123)

def testWriteTriangle(self):
"""Check we can write geometries with triangle types."""
layer = QgsVectorLayer(
('MultiPolygonZ?crs=epsg:4326&field=name:string(20)'),
'test',
'memory')

ft = QgsFeature()
geom = QgsMultiPolygon()
geom.addGeometry(QgsTriangle(QgsPoint(1, 2, 3), QgsPoint(2, 2, 4), QgsPoint(2, 3, 4)))
ft.setGeometry(geom)
ft.setAttributes(['Johny'])
myResult, myFeatures = layer.dataProvider().addFeatures([ft])
self.assertTrue(myResult)
self.assertTrue(myFeatures)

dest_file_name = os.path.join(str(QDir.tempPath()), 'testWriteTriangle.gpkg')
write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat(
layer,
dest_file_name,
'utf-8',
layer.crs(),
'GPKG')
self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message)

# Open result and check
created_layer = QgsVectorLayer(dest_file_name, 'test', 'ogr')
self.assertTrue(created_layer.isValid())
f = next(created_layer.getFeatures(QgsFeatureRequest()))
self.assertEqual(f.geometry().asWkt(), 'MultiPolygonZ (((1 2 3, 2 2 4, 2 3 4, 1 2 3)))')
self.assertEqual(f.attributes(), [1, 'Johny'])


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

0 comments on commit e0c03f9

Please sign in to comment.
You can’t perform that action at this time.