title: QGIS - Retrieve start and end features from a line
body: <p>I'm creating lines by selecting two features from various layers. When I create a line a form pops up. In this form I want to display data from the start and end features of the line.
  What I'm currently doing is retrieving the vertices as point :</p>
  
  <pre><code>geom = feature.geometry ()
  line = geom.asPolyline ()
  pointFather = ligne[0]
  pointChild = ligne[-1]
  </code></pre>
  
  <p>then I get the coordinates of each point :</p>
  
  <pre><code>xf = pointFather.x()
  yf = pointFather.y()
  </code></pre>
  
  <p>and then I look into each possible layer to find the features with the same coordinates, just to retrieve the features I just clicked on !</p>
  
  <pre><code>for layer in layerList:
      provider = layer.dataProvider()
      iter = provider.getFeatures()
      for feature in iter:
          geom = feature.geometry().asPoint()
          if geom.x() == xf and geom.y() == yf:    
  </code></pre>
  
  <p>It must be something easier to do to directly retrieve the start and end features, isn't it ?</p>
  
  <p><strong>EDIT 1 :</strong></p>
  
  <p>here is my try after PCamargo first answer :</p>
  
  <pre><code>def retrieve_feature_from_xy(geom,point,layerList):
    for layer in layerList:
      index = QgsSpatialIndex()
      iter = layer.getFeatures()
      for feat in iter:
          index.insertFeature(feat)
      ids = index.intersects(geom.boundingBox())
      request = QgsFeatureRequest()
      request.setFilterFids(ids)
      iter = layer.getFeatures(request)
      for feat in iter:
          geom2 = feat.geometry().asPoint()
          if geom2.x() == point.x() and geom2.y() == point.y():
              return feat 
  </code></pre>
  
  <p><strong>EDIT 2 :</strong></p>
  
  <p>Here is my try after PCamargo second comment :</p>
  
  <pre><code>def retrieve_feature_from_xy2(geom,point,layerList):
      allfeatures = {}
      indexes=[]
      ids=[]
      for layer in layerList:
          index = QgsSpatialIndex()
          iter = layer.getFeatures()
          for feat in iter:
              index.insertFeature(feat)
              allfeatures[feat.id()]=feat
          indexes.append(index)
      for index in indexes:
          intersect_ids = index.intersects(geom.boundingBox())
          ids.append(intersect_ids)
      for id in ids:
          for i in id:
              feat=allfeatures[i]
              geom2=feat.geometry().asPoint()
              if geom2.x() == point.x() and geom2.y() == point.y():
                  return feat  
  </code></pre>
  
  <p><strong>EDIT 3</strong> </p>
  
  <p>Here is my try after PCamargo third comment :</p>
  
  <pre><code>def retrieve_feature_from_xy3(geom,point,layerList):    
      allfeatures = {}
      indexes=[]
      ids=[]
      indexDict = {}
      intersectsIdsDict = {}
  
      for layer in layerList:
          index = QgsSpatialIndex()
          iter = layer.getFeatures()
          for feat in iter:
              index.insertFeature(feat)
              allfeatures[layer,feat.id()]=feat
          indexes.append(index)
          indexDict[layer]=index
  
      for layer, index in indexDict.items():
          intersectsIds = index.intersects(geom.boundingBox())
          intersectsIdsDict[layer]=intersectsIds      
  
      for layer, index in intersectsIdsDict.items():
          for id in index:
              feat = allfeatures[layer,id]    
              geom2=feat.geometry().asPoint()
              if geom2.x() == point.x() and geom2.y() == point.y():
                  return feat
  </code></pre>