6

I'm trying to use a vector file writer and I'm getting an unexpected WKB type of my output layer.

I'm setting up the writer like this

writer = QgsVectorFileWriter( output_layer, 'UTF-8', fields,  QgsWKBTypes.PointZM , inlayer.crs() )

Below is information about my QGIS installation at the time.

enter image description here

When I check the properties of the shapefile that is generated, the geometry type is showing up as PointZ instead of PointZM. The simple script below should duplicate the problem.

##input_layer=vector
##output_layer=output vector

from qgis.core import *
from qgis.utils import *
from PyQt4.QtCore import *

inlayer = processing.getObject(input_layer)
provider = inlayer.dataProvider()
fields = provider.fields()
writer = QgsVectorFileWriter( output_layer, 'UTF-8', fields, QgsWKBTypes.PointZM, inlayer.crs() )
feature = QgsFeature()
feature.setGeometry(QgsGeometry(QgsPointV2(QgsWKBTypes.PointZM, 1, 2, 3, 4)))
writer.addFeature(feature)
del writer

It may be easier to test this in the python console with the following commands

>>> writer = QgsVectorFileWriter('c:/test/test_layer.shp', 'UTF8', QgsFields(), QgsWKBTypes.PointZM, QgsCoordinateReferenceSystem('EPSG:3857'))
>>> feature = QgsFeature()
>>> feature.setGeometry(QgsGeometry(QgsPointV2(QgsWKBTypes.PointZM, 1, 2, 3, 4)))
>>> writer.addFeature(feature)
True
>>> del writer

Just make sure you have a test folder on the C drive and then you can add the generated shapefile to QGIS to inspect.

3
  • Which GDAL version is your QGIS based off? (you can check in the help->about dialog)
    – ndawson
    Feb 9 '17 at 23:15
  • @ndawson I added a screenshot of that dialog just in case any of the other versions would be important. Feb 10 '17 at 14:14
  • is strange because the same thing occurs using MultiPointZM,the result shapefile is MultiPointZ
    – Fran Raga
    Oct 25 '17 at 9:46
3
+50

Interestingly, if you do not use del writer and check the metadata for the layer, it is still registered as a PointZM type (but the feature is not added).

I just tested the code you provided for use in the Python Console and can't say what the reasoning for this is but an alternative could be to first create a memory layer, populate this with your QgsPointV2 and then write this to a shapefile and force the WKBType as a PointZM:

from PyQt4.QtCore import QVariant

mem_layer = QgsVectorLayer("Point?crs=epsg:3857", "name", "memory")
provider = mem_layer.dataProvider()
provider.addAttributes([QgsField("FID", QVariant.Int)])
mem_layer.updateFields() 

feat = QgsFeature()
feat.setGeometry(QgsGeometry( QgsPointV2(QgsWKBTypes.PointZM, 1, 2, 3, 4)))
feat.setAttributes(["0"])
provider.addFeatures([feat])

QgsVectorFileWriter.writeAsVectorFormat(mem_layer, 'c:/test/result.shp', "utf-8", None, "ESRI Shapefile", layerOptions=['SHPT=POINTZM'])

Example:

Example


Tested on QGIS 2.18.13 for Win7 64-bit.


Edit:

As @TJ Rockefeller already mentioned in his comment, there's no need to create a memory layer. You can force the geometry type for the output to be PointZM by adding the following layer creation option:

layerOptions=['SHPT=POINTZM']

So the code could look like:

writer = QgsVectorFileWriter('c:/test/result.shp', 'UTF-8', QgsFields(), QgsWKBTypes.PointZM, QgsCoordinateReferenceSystem('EPSG:3857'), layerOptions=['SHPT=POINTZM'])
feature = QgsFeature()
feature.setGeometry(QgsGeometry(QgsPointV2(QgsWKBTypes.PointZM, 1, 2, 3, 4)))
writer.addFeature(feature)
del writer
2
  • 1
    It looks like you don't need the memory layer to get this to work. It looks like the magic is in layerOptions=['SHPT=POINTZM']. I added that to the definition of my QgsVectorFileWriter, and everything works now in the examples that I gave. Oct 26 '17 at 15:35
  • @TJRockefeller - Yes, you're right! Thanks for checking, edited post to reflect your comment :)
    – Joseph
    Oct 27 '17 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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