<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hello<div><br></div><div>My goal is to write some features and geometries to a JSON file. I searched for examples however in QGIS 2 (I am using 2.2) something seems to have changed so it does not work. Here is a very simple example of what I want to do:</div><div><br></div><div><br></div><div><div>fields = QgsFields()</div><div>fields.append(QgsField("name"))</div><div>fields.append(QgsField("description"))</div><div>writer = QgsVectorFileWriter("/path/to/file.geojson", "utf-8", fields, QGis.WKBPoint, None, "GeoJSON")</div><div>feature = QgsFeature(fields)</div><div>feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(1, 2)))</div><div>feature.setAttributes(["name", "xyz"])</div><div>writer.addFeature(newFeature)</div><div>del writer</div><div><br></div></div><div><br></div><div>The result is a JSON file with the following content:</div><div><br></div><div><br></div><div><div>{</div><div>"type": "FeatureCollection",</div><div>                                                                                </div><div>"features": [</div><div>{ "type": "Feature", "properties": { }, "geometry": null }</div><div>]</div><div>}</div></div><div><br></div><div><br></div><div>So the feature is added but attributes and geometry are ignored. Do you have any idea what I am doing wrong? I also tried to create a memory layer:</div><div><br></div><div><br></div><div><div>layer = QgsVectorLayer("Point", "temp", "memory")</div><div>provider = layer.dataProvider()</div><div>layer.startEditing()</div><div>provider.addAttributes([QgsField("name"), QgsField("description")])</div><div>feature = QgsFeature(fields)</div><div>feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(1, 2)))</div><div>feature.setAttributes(["namex", "xyz"])</div><div>provider.addFeatures([feature])</div><div>layer.updateFields()</div><div>QgsVectorFileWriter.writeAsVectorFormat(layer, "/path/to/file.geojson", "utf-8", None, "GeoJSON")</div></div><div><br></div><div><br></div><div>The result looks a little bit better:</div><div><br></div><div><br></div><div><div>{</div><div>"type": "FeatureCollection",</div><div>"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::21781" } },</div><div>                                                                                </div><div>"features": [</div><div>{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 1.0, 2.0 ] } }</div><div>]</div><div>}</div></div><div><br></div><div><br></div><div>But the attributes are still missing. Does anybody know how this is done correctly? Thank you very much!</div><div><br></div><div>Josua</div>                                       </div></body>
</html>