<div>I'm not sure if this is the right place to put this kind of questions, has all I see in here are much more complex debates about the actual developping of QGIS.</div><div><br></div>I'm not very proficient neither with Python or QGIS API, but i'm trying to create a plugin that allows one to <font face="arial, helvetica, sans-serif" style="margin:0px 0px 20px;padding:0px;border:0px;vertical-align:baseline"><a href="http://gis.stackexchange.com/questions/44799/how-to-transform-a-selected-multipart-feature-into-singlepart-features-while-edi" class="question-hyperlink" style="line-height:27px;color:rgb(109,79,31);margin:0px 0px 20px;padding:0px;border:0px;vertical-align:baseline">transform a selected multipart feature into singlepart features while editing in QGIS?</a><span style="line-height:27px">, a very simple but useful tool while editing.</span></font><div>
<br></div><div>So far I manage to create the code that works with regular shapefiles, you can see it at the end of the email.</div><div><br></div><div>My problem now is to make it work with Postgis layers due to the unique values sequences. I toke a look into <a href="http://qgis.org/api/1.8/qgsvectorlayer_8cpp_source.html#l02334">QgsVectorLayer::splitFeatures</a> for inspiration, has the objective is mostly the same, but I'm not being able to correctly translate the code to Python that checks for a provider and when possible replace the original attribute by the providers defaultValue</div>
<div><br></div><div><pre class="fragment" style="font-family:monospace,fixed;font-size:9pt;border:1px solid rgb(196,207,229);background-color:rgb(251,252,253);padding:4px 6px;margin:4px 8px 4px 2px;overflow:auto;word-wrap:break-word;line-height:15px">
02412         <span class="comment" style="color:rgb(128,0,0)">//use default value where possible (primary key issue), otherwise the value from the original (splitted) feature</span>
<a name="l02413" style="color:rgb(61,87,140)"></a>02413         <a class="code" href="http://qgis.org/api/1.8/qgsfeature_8h.html#a54fb9a5ab4cfae301a0415ce490f08c8" style="color:rgb(70,101,162);text-decoration:initial">QgsAttributeMap</a> newAttributes = select_it->attributeMap();
<a name="l02414" style="color:rgb(61,87,140)"></a>02414         QVariant defaultValue;
<a name="l02415" style="color:rgb(61,87,140)"></a>02415         <span class="keywordflow" style="color:rgb(224,128,0)">for</span> ( <span class="keywordtype" style="color:rgb(96,64,32)">int</span> j = 0; j < newAttributes.size(); ++j )
<a name="l02416" style="color:rgb(61,87,140)"></a>02416         {
<a name="l02417" style="color:rgb(61,87,140)"></a>02417           <span class="keywordflow" style="color:rgb(224,128,0)">if</span> ( <a class="code" href="http://qgis.org/api/1.8/classQgsVectorLayer.html#aa84c4fb5f4fb69cbfb706403ebe5109a" title="Pointer to data provider derived from the abastract base class QgsDataProvider." style="color:rgb(70,101,162);text-decoration:initial">mDataProvider</a> )
<a name="l02418" style="color:rgb(61,87,140)"></a>02418           {
<a name="l02419" style="color:rgb(61,87,140)"></a>02419             defaultValue = <a class="code" href="http://qgis.org/api/1.8/classQgsVectorLayer.html#aa84c4fb5f4fb69cbfb706403ebe5109a" title="Pointer to data provider derived from the abastract base class QgsDataProvider." style="color:rgb(70,101,162);text-decoration:initial">mDataProvider</a>-><a class="code" href="http://qgis.org/api/1.8/classQgsVectorDataProvider.html#ac5390ce5f17903e642d4659b9a3ea091" title="Returns the default value for field specified by fieldId." style="color:rgb(70,101,162);text-decoration:initial">defaultValue</a>( j );
<a name="l02420" style="color:rgb(61,87,140)"></a>02420             <span class="keywordflow" style="color:rgb(224,128,0)">if</span> ( !defaultValue.isNull() )
<a name="l02421" style="color:rgb(61,87,140)"></a>02421             {
<a name="l02422" style="color:rgb(61,87,140)"></a>02422               newAttributes.insert( j, defaultValue );
<a name="l02423" style="color:rgb(61,87,140)"></a>02423             }
<a name="l02424" style="color:rgb(61,87,140)"></a>02424           }
<a name="l02425" style="color:rgb(61,87,140)"></a>02425         }</pre></div><div><br></div><div>I'm not being able to check for mDataProvider or getting the mDataProvider.defaultValue(j). So far I could get to "see" the attribute default value with the code below but I'm not sure what to do with it.</div>
<div><br></div><div><p style="margin:0px">layer.dataProvider().defaultValue(0)</p>
<p style="margin:0px"><PyQt4.QtCore.QVariant object at 0x0DEC5BC8></p><p style="margin:0px"><br></p></div><div><br></div><div>Thanks for the help,</div><div><br></div><div>Alexandre Neto</div><div><br></div><div><br>
</div><div><br></div><div>My actual plugin code is this:</div><div><br></div><div><div>------------------------------------------------------------</div><div><br></div><div>layer = self.iface.mapCanvas().currentLayer()</div>
<div>new_features = []</div><div>n_of_splitted_features = 0</div><div>n_of_new_features = 0</div><div><br></div><div>for feature in layer.selectedFeatures():</div><div>    geom = feature.geometry()</div><div>    # if feature geometry is multipart starts split processing</div>
<div>    if geom.isMultipart():</div><div>        n_of_splitted_features += 1</div><div>        #remove_list.append(<a href="http://feature.id">feature.id</a>())</div><div>        </div><div>        # Get attributes from original feature</div>
<div>        new_attributes = feature.attributeMap() ### Change to work with Postgis</div><div>        </div><div>        # Get parts from original feature</div><div>        parts = geom.asGeometryCollection ()</div><div>
                </div><div>        # from 2nd to last part create a new features using their</div><div>        # single geometry and the attributes of the original feature</div><div>        temp_feature = QgsFeature()</div>
<div>        temp_feature.setAttributeMap(new_attributes)</div><div>        for i in range(1,len(parts)):</div><div>            temp_feature.setGeometry(parts[i])</div><div>            new_features.append(QgsFeature(temp_feature))</div>
<div>        # update feature geometry to hold first part single geometry</div><div>        # (this way one of the output feature keeps the original Id)</div><div>        feature.setGeometry(parts[0])</div><div>        layer.updateFeature(feature)</div>
<div><br></div><div># add new features to layer</div><div>n_of_new_features = len(new_features)</div><div>if n_of_new_features > 0:</div><div>    layer.addFeatures(new_features, False)</div><div><br></div><div>print ("Splited " + str(n_of_splitted_features) + " feature(s) into " +</div>
<div>str(n_of_new_features + n_of_splitted_features) + " new ones.")</div></div><div><br></div><div>------------------------------------------------------------------------------------------------------------</div>