title: Finding nearest xy-point in numpy array and second nearest with condition
body: <p>My problem is like the problem in the thread <a href="https://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-of-x-and-y-coordinates">Finding index of nearest point in numpy arrays of x and y coordinates</a>, but it's extended:</p>
  
  <p>For better visualization here's an image
  (manipulated image, original from: <em>by 112BKS - Eigenes WerkOriginal graph/Data from [.. ? ..], CC BY-SA 3.0</em>, <a href="https://commons.wikimedia.org/w/index.php?curid=5949185" rel="nofollow noreferrer">link to page</a>):
  <a href="https://i.stack.imgur.com/m3Udp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m3Udp.jpg" alt="source: Von 112BKS - Eigenes WerkOriginal graph/Data from [.. ? ..], CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=5949185"></a></p>
  
  <p>On the one hand there is a array <code>datafield</code>. It consists of a numpy array with elements <code>[value x y]</code>. That are the thin blue lines with the numbers (they are the <code>value</code>). On the other hand there is the array <code>orangeline</code> in a numpy array with elements <code>[x y]</code>. </p>
  
  <p>What I want to do is to calculate the <code>value</code> of any elements in <code>orangeline</code>.<br> I visualized one concrete element of <code>orangeline</code> with the green circle. The value for it can I interpolate with the two elements from <code>datafield</code>, visualized with the triangles. As result I get for the green circle a <code>value</code> between 225 and 230.</p>
  
  <p><strong>First step:</strong> <br>Find for every element in <code>orangeline</code> the closest element in <code>datafield</code>.<br>(In the example that is the pink triangle.)</p>
  
  <p><strong>Second step:</strong><br> Find for every element in 'orangeline' the closest element in <code>datafield</code> but with another <code>value</code> than the one from the first step. <br>(In the example that is the brown triangle.)</p>
  
  <p><strong>Third step:</strong> Interpolate the <code>value</code> for every element in <code>orangeline</code> from those the two founded values and the distances to those elements.</p>
  
  <p>First step can be solved with </p>
  
  <pre><code>mytree = scipy.spatial.cKDTree(datafield[:, 1:3])
  dist1, indexes1 = mytree.query(orangeline)
  </code></pre>
  
  <p>But now I don't know how to filter the datafield for the second step. Is there a solution?</p>