I performed a transect in which I sampled several animals. The location of these animals are represented by a point shapefile. I am interested in calculating a pairwise matrix of distance (in meters) between all sampled animals. These distances will be used subsequently in spatial autocorrelation analyses to assess animal's dispersal capability (using genetic data).

However, calculating the euclidean distance is not adequate for my case because there is a river at north of these locations and it is considered to be a barrier to movement for the sampled animals. Thus, I rendered a shapefile polygon containing all these points and I intend to calculate the pairwise distance of points within the borders of this polygon. I uploaded a figure to help you understand exactly what I want. This figure contains 4 sampled locations (blue dots) and a yellow polygon from which I want to calculate the minimum distances. The red line represents the euclidean distance between the pair of points displayed. However, this distance to is not realistic because the river is in-between. The green line within the polygon is roughly what I want to estimate (basically a minimum path distance).

Any idea of how to do this with shapefiles or is it only possible to do with rasters?

There is other topic "Length of line on specific point (from point layer)" with a similar problem but that does not do exactly what I want. I have many more locations (besides the four shown) and calculate distances along the border of the polygon would not be realist.

enter image description here

  • 2
    Please do not comment on your own post. If you take the Tour, you'll see that our purpose is to compile a list of questions with answers. Thanks and apologies detract from question quality, making it harder to understand what it is that you want. This particular question begs for a graphic to better explain what you want. – Vince Jun 1 '17 at 10:29
  • 1
    Possible duplicate of Length of line on specific point (from point layer) – mgri Jun 1 '17 at 11:03
  • If I understand your issue, it should be very similar to the question linked above. – mgri Jun 1 '17 at 11:03
  • @mgri The lines shown in the figure are not shapefiles but rather a raw representation of the type of distance that I want to calculate. Looking at the question linked above, the poster was looking for a distance along a shapefile polyline to a destination point. I could do that but I have tens of points and I would have to draw several polylines – André Lourenço Jun 1 '17 at 11:25
  • 1
    @AndréLourenço I got it. Maybe, the linked answer doesn't work only when it is possible to draw the distance using a straight line (i.e. without going outside the polygon boundary). In the other cases (for example for the two points on the most left part of the attached image) the problem should be solved as in the linked answer. If you can confirm that, I can slightly edit that code and post it here, otherwise I suggest you adding more information on your original question for better explaining the issue. – mgri Jun 1 '17 at 12:43

I have no idea on how to do it with shapefile with existing tools, but an algorithm that iteratively adds vertices of the polygon to the line that joins the two points (until the new line is fully inside the polygon) should work. This would be the opposite of Douglas-Peucker.

With existing tools, I suggest that you convert your polygon to raster and assign a value equal to your pixel size for all the pixels inside the polygon (no data or VERY large value outside of the polygon). Then you can compute the minimum cost path using either GRASS or SAGA tools in the processing toolbox (see Cost Distance Analyst In QGIS? ). This will yield an exact but unprecise value, and you can increase the precision by reducing the cell size (which will also increase the processing time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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