[Qgis-developer] How to get Provider default values for each attribute using Python

Alexandre Neto senhor.neto at gmail.com
Tue Feb 5 02:18:52 PST 2013


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.

I'm not very proficient neither with Python or QGIS API, but i'm trying to
create a plugin that allows one to transform a selected multipart feature
into singlepart features while editing in
QGIS?<http://gis.stackexchange.com/questions/44799/how-to-transform-a-selected-multipart-feature-into-singlepart-features-while-edi>,
a very simple but useful tool while editing.

So far I manage to create the code that works with regular shapefiles, you
can see it at the end of the email.

My problem now is to make it work with Postgis layers due to the unique
values sequences. I toke a look into
QgsVectorLayer::splitFeatures<http://qgis.org/api/1.8/qgsvectorlayer_8cpp_source.html#l02334>
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

02412         //use default value where possible (primary key issue),
otherwise the value from the original (splitted) feature02413
QgsAttributeMap
<http://qgis.org/api/1.8/qgsfeature_8h.html#a54fb9a5ab4cfae301a0415ce490f08c8>
newAttributes = select_it->attributeMap();02414         QVariant
defaultValue;02415         for ( int j = 0; j < newAttributes.size();
++j )02416         {02417           if ( mDataProvider
<http://qgis.org/api/1.8/classQgsVectorLayer.html#aa84c4fb5f4fb69cbfb706403ebe5109a>
)02418           {02419             defaultValue = mDataProvider
<http://qgis.org/api/1.8/classQgsVectorLayer.html#aa84c4fb5f4fb69cbfb706403ebe5109a>->defaultValue
<http://qgis.org/api/1.8/classQgsVectorDataProvider.html#ac5390ce5f17903e642d4659b9a3ea091>(
j );02420             if ( !defaultValue.isNull() )02421
{02422               newAttributes.insert( j, defaultValue );02423
        }02424           }02425         }


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.

layer.dataProvider().defaultValue(0)

<PyQt4.QtCore.QVariant object at 0x0DEC5BC8>



Thanks for the help,

Alexandre Neto



My actual plugin code is this:

------------------------------------------------------------

layer = self.iface.mapCanvas().currentLayer()
new_features = []
n_of_splitted_features = 0
n_of_new_features = 0

for feature in layer.selectedFeatures():
    geom = feature.geometry()
    # if feature geometry is multipart starts split processing
    if geom.isMultipart():
        n_of_splitted_features += 1
        #remove_list.append(feature.id())

        # Get attributes from original feature
        new_attributes = feature.attributeMap() ### Change to work with
Postgis

        # Get parts from original feature
        parts = geom.asGeometryCollection ()

        # from 2nd to last part create a new features using their
        # single geometry and the attributes of the original feature
        temp_feature = QgsFeature()
        temp_feature.setAttributeMap(new_attributes)
        for i in range(1,len(parts)):
            temp_feature.setGeometry(parts[i])
            new_features.append(QgsFeature(temp_feature))
        # update feature geometry to hold first part single geometry
        # (this way one of the output feature keeps the original Id)
        feature.setGeometry(parts[0])
        layer.updateFeature(feature)

# add new features to layer
n_of_new_features = len(new_features)
if n_of_new_features > 0:
    layer.addFeatures(new_features, False)

print ("Splited " + str(n_of_splitted_features) + " feature(s) into " +
str(n_of_new_features + n_of_splitted_features) + " new ones.")

------------------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130205/590b3ae5/attachment.html>


More information about the Qgis-developer mailing list