3

I created a layer (e.g. point) in QGIS with QgsVectorLayer from memory:

mem_layer = QgsVectorLayer("point?crs=epsg:32630&field=id:string","TempFile","memory")
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

and save it with writeAsVectorFormat:

error = QgsVectorFileWriter.writeAsVectorFormat(mem_layer, "C:\\Temp\\TempFile", "CP1250", None, "ESRI Shapefile")

This all works well, but any features I add are not saved (only after save, remove, load again).

Is there anyone who can help me?


Here some more info. It is a tiny plugin to create a predefined SHP file:

(fileNameText,fileNameBase) = QInputDialog.getText(None,"QGIS in LUPMIS","Enter name of new point file:")  
mem_layer = QgsVectorLayer("point?crs=epsg:32630&field=id:string&field=caption:string&field=style:integer",fileNameText,"memory")  
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)  
temp1 = "C:\\LUPMIS\\LUPMISTemp\\" + fileNameText   
error = QgsVectorFileWriter.writeAsVectorFormat(mem_layer, temp1, "CP1250", None, "ESRI Shapefile")  
if error == QgsVectorFileWriter.NoError:  
    iface.messageBar().pushMessage("LUPMIS","Point layer saved as file " + fileNameText + ".shp",QgsMessageBar.INFO)

From here, users should continue with manual entry through toolbar icons: Toggle Editing - Save Layer Edits - Add Feature, etc

  • It seems they are saved, but not refreshed in QGIS. I have no access to qgis at the moment. – Andreas Müller Feb 9 '16 at 21:27
  • 1
    Might need to see some more code. How are you adding features? – Nathan W Feb 10 '16 at 1:06

Your Answer

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

Browse other questions tagged or ask your own question.