I want to draw several lines between pixels (please see the image below) according to some criteria :

enter image description here

  • First Criterion: The distance between 2 pixels is less than 2000m and greater than 500m

  • Second Criterion: The absolute difference of Z values is less than 600m and greater than 400m

For example, let's consider two pixels of a DEM: I want to draw a line between their centroids if they satisfy both criteria.

I tested by converting my raster DEM to points and applying these criteria but it takes a lot of time. Have you any proposal or suggestions? I'm using GRASS, Postgis or Python

  • 1
    It is not apparent how these criteria determine any kind of linear feature. Could you provide an illustration or edit this post to clarify the question?
    – whuber
    Jun 2, 2015 at 15:07
  • I provide an illustration
    – elmo
    Jun 2, 2015 at 15:23
  • 1
    Thank you. I still cannot determine how these lines are related to the pixel values or your criteria, because the picture is so complex and it shows none of the values of the underlying pixels, which is crucial for understanding what's going on. Why not just clarify your criteria? They need to provide more information in order to be converted into any working algorithm.
    – whuber
    Jun 2, 2015 at 15:27
  • Thank you for your response. For example let's consider two pixels of a DEM : I want to draw a line between their centroids if the distance is upper than 2000 meter and the difference of the Z value(or the value of the pixel) is upper than 400 meter
    – elmo
    Jun 2, 2015 at 15:37
  • OK, that's good. But your illustration makes it apparent that you don't want to draw all such lines: there would be far too many of them. How exactly are you going to select a smaller subset of these line segments?
    – whuber
    Jun 2, 2015 at 15:44

2 Answers 2


You could potentially convert the DEM object into a Numpy array, and do your tests on that. For example, sorting by the height difference.

Unfortunately, the distance between each pixel to another pixel is something you have to do a lot of times, and as such, you can't really do anything to reduce the number of tests, aside from making a few rules based on difference in the indices between cells.

Having said that, what about this (in pseudo code):

  1. Get your raster as an array
  2. For each ROW and COL (this is your source)
  3. For each other ROW and COL (this is your destination)
  4. Ignore the destination if it's this cell
  5. Ignore the destination if the index is more than nMinX away in rows (find this by getting the your 500 m / cell width) and less than nMaxX
  6. Ignore the destination if the index is more than nMinY away in cols (similar to 5.) and nMaxY
  7. optional, but work out a diagonal ignore test if you need to?
  8. Check this cell is within the height constraint, and if it is, check the distance constraint.
  9. If checks in 8 pass, create your line and move along.
  10. You may want to store the line as a cell index, and also ignore the returning line from the destination cell (otherwise you'll have a from/to and to/from line for each pair.

I think doing this in Python will be fairly straight forward.


Please have a look at the r.connectivity.distance GRASS 6 AddOn for inspiration.

It does something similar to what you are after...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.