Skip to content

Commit 493219c

Browse files
rouaultnyalldawson
authored andcommittedSep 29, 2023
[OGR provider] Fix issue when writing a multi-part multipolygon in a shapefile with GDAL >= 3.7 (fixes #54537)
1 parent 5786239 commit 493219c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎src/core/providers/ogr/qgsogrprovider.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,15 @@ OGRGeometryH QgsOgrProvider::ConvertGeometryIfNecessary( OGRGeometryH hGeom )
13641364
return OGR_G_ForceToMultiLineString( hGeom );
13651365
}
13661366

1367+
if ( flattenLayerGeomType == wkbPolygon && flattenGeomType == wkbMultiPolygon &&
1368+
mGDALDriverName == QLatin1String( "ESRI Shapefile" ) )
1369+
{
1370+
// Do not force multipolygon to polygon for shapefiles, otherwise it will
1371+
// cause issues with GDAL 3.7 that does honour the topological intent of
1372+
// multipolygon
1373+
return hGeom;
1374+
}
1375+
13671376
return OGR_G_ForceTo( hGeom, layerGeomType, nullptr );
13681377
}
13691378

‎tests/src/python/test_provider_shapefile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,24 @@ def testLayersOnSameOGRLayerWithAndWithoutFilter(self):
11111111
assert QgsGeometry.compare(vl3_extent.asPolygon()[0], reference.asPolygon()[0],
11121112
0.00001), f'Expected {reference.asWkt()}, got {vl3_extent.asWkt()}'
11131113

1114+
def testWritingMultiPolygon(self):
1115+
"""Test that a MultiPolygon written to a Shape Polygon layer doesn't get converted to Polygon"""
1116+
1117+
tmpfile = os.path.join(self.basetestpath, 'testWritingMultiPolygon.shp')
1118+
ds = osgeo.ogr.GetDriverByName('ESRI Shapefile').CreateDataSource(tmpfile)
1119+
ds.CreateLayer('testWritingMultiPolygon', geom_type=osgeo.ogr.wkbPolygon)
1120+
ds = None
1121+
1122+
vl = QgsVectorLayer(tmpfile, 'test')
1123+
f = QgsFeature()
1124+
f.setAttributes([200])
1125+
wkt = "MultiPolygon (((0 0, 0 1, 1 1, 0 0)),((10 0, 10 1, 11 1, 10 0)))"
1126+
f.setGeometry(QgsGeometry.fromWkt(wkt))
1127+
vl.dataProvider().addFeatures([f])
1128+
1129+
f = next(vl.getFeatures())
1130+
self.assertEqual(f.geometry().constGet().asWkt(), wkt)
1131+
11141132

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

0 commit comments

Comments
 (0)
Please sign in to comment.