Graphics Reference
In-Depth Information
Manipulating vector data and saving it to a
shapefile
Let's create a program that takes two vector data sources, subtracts one set of vectors from
the other, and saves the resulting geometries into a new shapefile. Along the way, we'll
learn a few important things about the PyQGIS library.
We'll be making use of the QgsGeometry.difference() function. This function per-
forms a geometrical subtraction of one geometry from another, like this:
Let's start by asking the user to select the first shapefile and open up a vector data provider
for that file:
filename_1 = QFileDialog.getOpenFileName(iface.mainWindow(),
"First Shapefile",
"~", "*.shp")
if not filename_1:
return
registry = QgsProviderRegistry.instance()
provider_1 = registry.provider("ogr", filename_1)
We can then read the geometries from that file into memory:
Search WWH ::




Custom Search