Graphics Reference
In-Depth Information
fields = QgsFields()
writer = QgsVectorFileWriter(dst_filename, "ASCII", fields,
dst_geometries[0].wkbType(),
None, "ESRI Shapefile")
if writer.hasError() != QgsVectorFileWriter.NoError:
print "Error!"
return
We don't have any attributes in our shapefile, so the fields list is empty. Now that the
writer has been set up, we can save the geometries into the file:
for geometry in dst_geometries:
feature = QgsFeature()
feature.setGeometry(geometry)
writer.addFeature(feature)
Now that all the data has been written to the disk, let's display a message box that informs
the user that we've finished:
QMessageBox.information(iface.mainWindow(), "",
"Subtracted features saved to disk.")
As you can see, creating a new shapefile is very straightforward in PyQGIS, and it's easy
to manipulate geometries using Python—just so long as you copy the QgsGeometry ob-
jects you want to keep around. If your Python code starts to crash while manipulating geo-
metries, this is probably the first thing you should look for.
Search WWH ::




Custom Search