Re: [Qgis-developer] Python: Select features that touch other features
Thank you Daniel, Nathan and Marco for all your hints. I am sure I will manage to do what I want with all of your suggestions! Andreas Am 06.10.2013 02:00, schrieb Daniel: Using map() is the pythonic way Maybe ftools need an upgrade :) Thanks for the tip On Sat, Oct 5, 2013 at 8:53 PM, Nathan Woodrow madman...@gmail.com wrote: On Sun, Oct 6, 2013 at 9:45 AM, Daniel daniel...@gmail.com wrote: def createIndex( provider ): feat = QgsFeature() index = QgsSpatialIndex() fit = provider.getFeatures() while fit.nextFeature( feat ): index.insertFeature( feat ) return index Try something like this for your createIndex which will work with any collection of features: def createIndex( features ): index = QgsSpatialIndex() map(index.insertFeature, features) return index index = createIndex(provider.getFeatures()) and also because I hate seeing while nextFeature() used :) - Nathan ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Python: Select features that touch other features
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.neum...@carto.netwrote: 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@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer -- Daniel Vaz ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Python: Select features that touch other features
On Sun, Oct 6, 2013 at 9:45 AM, Daniel daniel...@gmail.com wrote: def createIndex( provider ): feat = QgsFeature() index = QgsSpatialIndex() fit = provider.getFeatures() while fit.nextFeature( feat ): index.insertFeature( feat ) return index Try something like this for your createIndex which will work with any collection of features: def createIndex( features ): index = QgsSpatialIndex() map(index.insertFeature, features) return index index = createIndex(provider.getFeatures()) and also because I hate seeing while nextFeature() used :) - Nathan ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [Qgis-developer] Python: Select features that touch other features
Using map() is the pythonic way Maybe ftools need an upgrade :) Thanks for the tip On Sat, Oct 5, 2013 at 8:53 PM, Nathan Woodrow madman...@gmail.com wrote: On Sun, Oct 6, 2013 at 9:45 AM, Daniel daniel...@gmail.com wrote: def createIndex( provider ): feat = QgsFeature() index = QgsSpatialIndex() fit = provider.getFeatures() while fit.nextFeature( feat ): index.insertFeature( feat ) return index Try something like this for your createIndex which will work with any collection of features: def createIndex( features ): index = QgsSpatialIndex() map(index.insertFeature, features) return index index = createIndex(provider.getFeatures()) and also because I hate seeing while nextFeature() used :) - Nathan -- Daniel Vaz ___ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer
[Qgis-developer] Python: Select features that touch other features
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@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer