[QGIS-Developer] PyQGIS: Mixed Polygon/MultiPolygon shapefile

Javier Sánchez Portero javiersanp at gmail.com
Tue May 29 06:39:48 PDT 2018


Hello

My name is Javier Sanchez and I'm the developer of a QGIS based standalone
application to convert Spanish Cadastre data to OpenStreetMap format [1].
I'm trying to port this app from QGIS 2 to 3 and I would like this to work
also with the previous version.

In QGIS 2.x, I was able to write a shapefile with a mixed set of polygons
and multipolygons geometries. Now in QGIS 3.x I can't. Maybe this is a GDAL
issue. Probably the last behaviour is the correct one. But is there any way
in QGIS 3 to create a shapefile mixing polygons with multipolygons?

Here is an example code to illustrate what I meant:

        crs = QgsCoordinateReferenceSystem(32628)
        fn = 'xxx.shp'
        w = PolygonLayer.create_shp(fn, crs)
        p = [[QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(0,0)]]
        mp = [[[QgsPoint(2,0), QgsPoint(3,0), QgsPoint(3,1),
QgsPoint(2,0)]],
              [[QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,1),
QgsPoint(4,0)]]]
        f1 = QgsFeature(QgsFields())
        g1 = QgsGeometry().fromPolygon(p)
        f1.setGeometry(g1)
        w.addFeature(f1)
        f2 = QgsFeature(QgsFields())
        g2 = QgsGeometry().fromMultiPolygon(mp)
        f2.setGeometry(g2)
        w.addFeature(f2)
        del(w)
        l = QgsVectorLayer(fn, 'test', 'ogr')
        for f in l.getFeatures():
            g = f.geometry()
            print(g.isMultipart())

When I run this in QGIS 2, the output is:

    False
    True

When I run the same example in QGIS 3.x (replacing fromPolygon with
fromPolygonXY and fromMultiPolygon with fromMultiPolygonXY), the output
instead is:

    True
    True

The writer converts g1 from polygon to multipolygon and every geometry in
the resulting shapefile have the same type while before I had a bunch of
multipart and singlepart geometries.

Regards, Javier

[1] https://github.com/OSM-es/CatAtom2Osm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180529/1c8d2a59/attachment.html>


More information about the QGIS-Developer mailing list