1

There is such a code:

from PyQt4.QtCore import QVariant
from qgis.core import (QgsFeature, QgsField, QgsFields,
QgsGeometry, QgsPoint, QgsVectorFileWriter)
from qgis.utils import QGis, iface
iface.legendInterface().layers()
sluoksniu_saras=iface.legendInterface().layers()
for cL in sluoksniu_saras:
    fn='D:\\Ko\\'+cL.name()
    ret = QgsVectorFileWriter.writeAsVectorFormat(cL, fn, "Windows-1257",\
    QgsCoordinateReferenceSystem(cL.crs()), driverName="ESRI Shapefile",\
    forceMulti =False, includeZ = False)
    if ret == QgsVectorFileWriter.NoError:
       print "Irasyta i " +fn + " !"
print 'viskas'

In theory, he should write all open layers of the project in a specific folder and reset the z-dimension. Layers are recorded, but the z-dimension is not reset. Where is the mistake? I'm using QGIS 2.16

2 Answers 2

1

It's mentioned in the API for QGIS 2.16 that you need to set the overrideGeometryType parameter:

includeZ - set to true to include z dimension in output. This option is only valid if overrideGeometryType is set.

Try including this parameter in your code.


Edit:

In response to your comment, you could try something like the following where I assumed you used a point shapefile for the geometry but you can check here for other geometry types:

ret = QgsVectorFileWriter.writeAsVectorFormat(cL, fn, "Windows-1257",\
    QgsCoordinateReferenceSystem(cL.crs()), driverName="ESRI Shapefile",\
    forceMulti =False, overrideGeometryType=QgsWKBTypes.PointZ, includeZ=False)

(Note: I no longer have QGIS 2.16 installed as it is quite old now so cannot test this. You should perhaps consider updating to the latest version)

4
  • all this is certainly wonderful, but I have 0 days experience with Python. I downloaded this code from the Internet, just added the missing and thought that it would work. It does not work. Would it not make it difficult for you to indicate how I should do this? Thanks.
    – Smaigas
    Commented Sep 27, 2019 at 14:07
  • @Smaigas - See the edited post.
    – Joseph
    Commented Sep 27, 2019 at 14:26
  • Thanks for the tips. Now my code is working. Below I will lay out the working code. P.S. I can not install a newer version, the latest version is 2.16, which works on Windows XP
    – Smaigas
    Commented Sep 28, 2019 at 7:59
  • @Smaigas - Most welcome, glad you got it working :)
    – Joseph
    Commented Oct 1, 2019 at 9:32
1

Working code:

from PyQt4.QtCore import QVariant
from qgis.core import (QgsFeature, QgsField, QgsFields,
QgsGeometry, QgsPoint, QgsVectorFileWriter)
from qgis.utils import QGis, iface
#iface.legendInterface().layers()
sluoksniu_saras=iface.legendInterface().layers()
for cL in sluoksniu_saras:
    fn='D:\\Ko\\'+cL.name()
    geom_name = {0: 1, 1: 2, 2: 3}
    geom_type = cL.geometryType()
    tipas=geom_name[geom_type]
    ret = QgsVectorFileWriter.writeAsVectorFormat(cL, fn, "Windows-1257",\
    QgsCoordinateReferenceSystem(cL.crs()), driverName="ESRI Shapefile",\
    overrideGeometryType=tipas, forceMulti = False, includeZ = False)
    if ret == QgsVectorFileWriter.NoError:
       print "Irasyta i " +fn + " !"
print 'viskas'

Thanks for the help. (Ačiū už pagalbą)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.