Graphics Reference
In-Depth Information
at once, rather than subtracting one at a time. This will make the subtraction process much
faster:
combined_geometry = None
for geometry in geometries_2:
if combined_geometry == None:
combined_geometry = geometry
else:
combined_geometry = combined_geometry.combine(geometry)
We can now calculate the new set of geometries by subtracting one from the other:
dst_geometries = []
for geometry in geometries_1:
dst_geometry = geometry.difference(combined_geometry)
if not dst_geometry.isGeosValid(): continue
if dst_geometry.isGeosEmpty(): continue
dst_geometries.append(dst_geometry)
Notice that we check to see whether the destination geometry is mathematically valid and
is not empty.
Note
Invalid geometries are a common problem when manipulating complex shapes. There are
options for fixing them, such as splitting apart multi-geometries and performing a buffer
operation. However, doing this is beyond the scope of this topic.
Our last task is to save the resulting geometries into a new shapefile. We'll first ask the
user for the name of the destination shapefile:
dst_filename =
QFileDialog.getSaveFileName(iface.mainWindow(),
"Save results to:",
"~", "*.shp")
if not dst_filename:
return
We'll make use of a vector file writer to save the geometries into a shapefile. Let's start
by initializing the file writer object:
Search WWH ::




Custom Search