[Qgis-developer] vectordataprovider.rewind() problem?

Carson Farmer carson.farmer at gmail.com
Tue Nov 25 15:14:52 EST 2008


Martin Dobias wrote:
> On Tue, Nov 25, 2008 at 3:49 PM, Carson Farmer <carson.farmer at gmail.com> wrote:
>   
>> Is anyone else have trouble with QgsVectorDataProvider.rewind() not actually
>> rewinding properly?
>>
>> I'm trying to run through one QgsVectorLayer using while
>> providerA.nextFeature(feata): blah blah blah
>> Then loop through another (in the same function) using
>> providerB,nextFeature(featb): blah blah blah... but for some reason it won't
>> loop through the second layer at all?
>>     
>
> Hi Carson,
>
> could you please provide a very simple snippet? I don't get the idea
> how this all is connected and where .rewind() is being called.
>   
Sure!:


        while vproviderA.nextFeature( inFeatA ):
            geom = QgsGeometry( inFeatA.geometry() )
            diffGeom = QgsGeometry( inFeatA.geometry() )
            atMapA = inFeatA.attributeMap()
            intersects = indexA.intersects( geom.boundingBox() )
            if len( intersects ) <= 0:
                outFeat.setGeometry( geom )
                outFeat.setAttributeMap( atMapA )
                tempProvider.addFeatures( [ outFeat ] )
            else:
                for _id in intersects:
                    vproviderB.featureAtId( int( _id ), inFeatB , True, 
allAttrsB )
                    atMapB = inFeatB.attributeMap()
                    if geom.intersects( inFeatB.geometry() ):
                        found = True
                        diffGeom = diffGeom.difference( inFeatB.geometry() )
                        result = geom.intersection( inFeatB.geometry() )
                        outFeat.setGeometry( result )
                        outFeat.setAttributeMap( 
ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
                        tempProvider.addFeatures( [ outFeat ] )
                if found:
                    outFeat.setGeometry( diffGeom )
                    outFeat.setAttributeMap( atMapA )
                    tempProvider.addFeatures( [ outFeat ] )
        length = len( atMapA.values() )
        vproviderB.rewind()
        while vproviderB.nextFeature( inFeatA ):
            geom = QgsGeometry( inFeatA.geometry() )
            atMap = inFeatA.attributeMap().values()
            atMap = dict( zip( range( length, length + len( atMap ) ), 
atMap ) )
            intersects = indexB.intersects( geom.boundingBox() )
            if len(intersects) <= 0:
                outFeat.setGeometry( geom )
                outFeat.setAttributeMap( atMapA )
                tempProvider.addFeatures( [ outFeat ] )
            else:
                for _id in intersects:
                    vproviderA.featureAtId( int( _id ), inFeatB , True, 
allAttrsA )
                    atMapB = inFeatB.attributeMap()
                    if geom.intersects( inFeatB.geometry() ):
                        geom = geom.difference( inFeatB.geometry() )
                outFeat.setGeometry( geom )
                outFeat.setAttributeMap( atMap )
                tempProvider.addFeatures( [ outFeat ] )
        tempLayer.updateExtents()
        return tempLayer

Essentially what all this does, is loop through layerA, and calculates 
differences and intersections with layerB, then loops through layerB, 
and calculates difference with layerA. Technically, I don't even think 
rewind() needs to be called here at all, but then again... it's not 
working that way either?

But what I'm no longer getting, is the interaction through layerB... the 
first bit runs fine, but the second bit won't run... it used too? I'm 
not entirely sure what's going on (abviously) so I'm going to continue 
to look into this using smaller examples to pint point the problem... I 
was really just interested in hearing if others were getting similar 
problems...

Carson

Carson


More information about the Qgis-developer mailing list