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

Daniel danielvaz at gmail.com
Sat Oct 5 16:52:42 PDT 2013


Don't forgot to instantiate inFeatA e inFeatB

inFeatA = QgsFeature()
inFeatB = QgsFeature()


After vprovider.getFeatures() maybe you need to tests if inFeatB has a
valid geometry and if geometry touches the current selected one, something
like this:

geomB = QgsGeometry( inFeatB.geometry() )

if geomB.touches(geomA):
   # do something


http://www.qgis.org/api/classQgsGeometry.html#a6f4183fe331cc77ba3683c9d0d9c7181
[touches method]
http://www.qgis.org/api/classQgsVectorLayer.html#a33968fa939503a9e3873c88e5f0ea439
[selectedFeaturesIds]


PS: Sorry for the noise, but I sent the previous email without conclude it.
This completes the previous one.

Hope it helps



On Sat, Oct 5, 2013 at 8:45 PM, Daniel <danielvaz at gmail.com> wrote:

> 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
>



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


More information about the Qgis-developer mailing list