Inspired by this question &
answer<http://gis.stackexchange.com/questions/9261/switch-line-direction-in-qgis>
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 <http://qgis.org/api/1.8/> 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 layerif 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!


Alexandre Neto

On Thu, Dec 27, 2012 at 12:36 PM, Alexandre Neto <senhor.n...@gmail.com>wrote:

> There are several questions and respective answers for how to make a
> multipart features Layer into a singlepart features one using QGIS:
>
>    - How to break a single polygon into multiple 
> polys?<http://gis.stackexchange.com/questions/27825/how-to-break-a-single-polygon-into-multiple-polys>
>    - How to split non-contiguous feature in 
> QGIS?<http://gis.stackexchange.com/questions/23377/how-to-split-non-contiguous-feature-in-qgis>
>
> But all the solutions I saw, creates a entirely new layer and would
> transform ALL the multipart geometries into single ones.
>
> So the questions is, how could one, during an edit session, *selectively* 
> transform
> a multipart feature into a singlepart feature, without the need to create a
> new layer?
>
> I saw a ticket <http://trac.osgeo.org/qgis/ticket/3278> for this but it
> seems to have been misunderstood.
>
> It would be the opposite to the "merge selected features" tool, and
> similar to 
> explode<http://webhelp.esri.com/arcgisdesktop/9.2/index.cfm?TopicName=Merging_and_separating_features>
>  in
> ArcGIS.
>
> Is there any plugin doing this? How could this be done using the python
> console?
>
>
> Alexandre Neto
>
>
> PS: I have posted the same 
> question<http://gis.stackexchange.com/questions/44799/how-to-transform-a-selected-multipart-feature-into-singlepart-features-during-a>in
> gis.stackexchange.com In case anyone wants to answer there.
>
_______________________________________________
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to