[Qgis-developer] Python: Select features that touch other features

Daniel danielvaz at gmail.com
Sat Oct 5 16:45:01 PDT 2013


Hi Andreas,

First create a spatial index over layer features (I got this code from
ftools_utils.py)

# Convinience function to create a spatial index for input
QgsVectorDataProvider
def createIndex( provider ):
    feat = QgsFeature()
    index = QgsSpatialIndex()
    fit = provider.getFeatures()
    while fit.nextFeature( feat ):
        index.insertFeature( feat )
    return index

Let's suppose that you have a vector layer "vlayer"



get the selected features:

# vector data provider
vprovider = vlayer.dataProvider()

# spatial index
index = createIndex(vprovider)

# selected features
selection = vlayer.selectedFeaturesIds()


for inFeatA in selection:
    geomA = QgsGeometry( inFeatA.geometry() )
    intersects = index.intersects( geomA.boundingBox() )
    for feat_id in intersects:
        # probably you want to discard some intersected feature that has in
selection so...
        if feat_id in selection:
             continue
        vprovider.getFeatures( QgsFeatureRequest().setFilterFid( int( id )
) ).nextFeature( inFeatB )




On Fri, Oct 4, 2013 at 11:48 AM, Andreas Neumann <a.neumann at carto.net>wrote:

> Hi,
>
> In my little python plugin I would like to do the following:
>
> In a parcel layer the user can interactively select a street parcel
> (typically long parcels). The user could select one or more street parcels.
>
> My little python script should then select all parcels that touch the
> selected parcels, so the touching features are in the same table than
> the originally selected features.
>
> I did not work with spatial operators and selections so far. Does
> someone have some code around to start from?
>
> Thanks a lot!
>
> Andreas
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Daniel Vaz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131005/ab0a34d1/attachment.html>


More information about the Qgis-developer mailing list