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

Carlo A. Bertelli (Charta s.r.l.) carlo.bertelli at gmail.com
Tue May 29 07:43:26 PDT 2018


Hello Javier,
just force the multipolygon behaviour.
As you say it's a GDAL refinement, just use a Layer Creation Option (-lco).
As per the docs (http://www.gdal.org/drv_shapefile.html):

   - *SHPT=type*: Override the type of shapefile created. Can be one of
   NULL for a simple .dbf file with no .shp file, POINT, ARC, POLYGON or
   MULTIPOINT for 2D; POINTZ, ARCZ, POLYGONZ, MULTIPOINTZ or MULTIPATCH for
   3D; POINTM, ARCM, POLYGONM or MULTIPOINTM for measured geometries; and
   POINTZM, ARCZM, POLYGONZM or MULTIPOINTZM for 3D measured geometries. The
   measure support was added in GDAL 2.1. MULTIPATCH files are supported since
   GDAL 2.2.

I think there are consequences, it will be necessary to know if there are
multipolygons in cadastral parcels. This is almost always the case if you
have an entire map, but it could be necessary to scan data or at least to
issue a GDAL_SQL query to detect it.

c

On Tue, May 29, 2018 at 3:39 PM, Javier Sánchez Portero <
javiersanp at gmail.com> wrote:

> 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
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
--------------------------------------------------------------------------
Carlo A. Bertelli
   Charta servizi e sistemi per il territorio e la storia ambientale srl
          Dipendenze del palazzo Doria,
          vc. alla Chiesa della Maddalena 9/2 16124      Genova (Italy)
          tel./fax +39(0)10 2475439  +39 0108566195  mobile:+39 393 1590711
   e-mail: bertelli at chartasrl.eu      http://www.chartasrl.eu
--------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180529/7a2144e2/attachment-0001.html>


More information about the QGIS-Developer mailing list