I have a single-part polygon containing thousands of features which I converted/dissolved into a multipart polygon. Which is faster to process (i.e. when I clip them using a simple rectangular polygon)? Is there any basis or explanation for that?
Processing performance: Singlepart vs Multipart Polygon
multipartqgissinglepart
Related Solutions
Inspired by this question & answer as an example of how "easily" can one make its own solutions when using Open Source, I have tried to create my own code to selectively "explode" multipart features during an editing session.
I have explored the QGIS 1.8 API for the first time, and came out with this piece of code that seams to do the job:
layer = qgis.utils.iface.mapCanvas().currentLayer()
remove_list = []
for feature in layer.selectedFeatures():
geom = feature.geometry()
# check if feature geometry is multipart
if geom.isMultipart():
remove_list.append(feature.id())
new_features = []
temp_feature = QgsFeature(feature)
# create a new feature using the geometry of each part
for part in geom.asGeometryCollection ():
temp_feature.setGeometry(part)
new_features.append(QgsFeature(temp_feature))
# add new features to layer
layer.addFeatures(new_features, False)
# remove the original (multipart) features from layer
if len(remove_list) > 0:
for id in remove_list:
layer.deleteFeature (id)
I'm not an experienced programmer, so the code might not be the most efficient one.
Next steep will be to make a plugin out of it... Or at least, try to!
UPDATE:
I was able to create the plugin. It's called Multipart Split, and can be found in the QGIS official repository.
There is an explode tool in QGIS, too. Explode means creating a separate line (feature) for each segment - thus each line consists of just two vertices: start- and endpoint.
Single parts, however, can consists of many vertices. Only limitation is that each feature consists of only one linestring, thus the line might not fork or not consist of two or more separate lines. Imagine it like a pen on a sheet of paper: what you draw in a single line, without removing the pen from the paper and not going back on a line you already drew.
A mulipart, however, can consist of as many independent lines as you wish, being topologically connected (like forking) or completely disjoint. Imagine it like a whole road- or river network integrated in one single feature.
Consider the following image:
- There is one multipart line, symbolized in black: one single feature consisting of all the lines you see on the image (1 feature: no. 1 to 31)
- If you convert this multipart to single parts, you get three parts: the red (no. 1 to 21), yellow (no. 22 to 26) and blue (no. 27 to 31) lines are each separate lines, thus 3 features in total.
- If you explode these lines, you get 31 parts: the ones labeled from 1 to 31. So each segment (connection from one vertex to the next vertex) is a separate feature. If you have n vertices, you get n-1 features (parts).
Best Answer
Clipping, intersection etc are performed two steps:
For step #1, the bounding box of each feature is typically used first, ideally stored in a spatial index. If using a layer with a single giant (multi)polygon, then the feature will be typically returned. If using a layer with many small (single)polygons, only the features truly intersecting will be found.
For step #2, the number of vertices in each feature to be process matters a lot. If a single giant (multi)polygon is to be processed, the computation becomes very complex and slow. If many small features were to be processed, the clipping/intersection becomes very fast.
Overall, it is faster to process many small polygons rather than bigger ones.