# How to create least-cost path between two polygons with GRASS?

I am completely a beginner in GRASS but I would like to use it to have the cumulative cost of moving between two polygons. More exactly, I would like to get the smallest cost from any point situated at the edge of one polygon (polygon A) to any point situated at the edge of another polygon (polygon B).

Until now, I used the function `CostDistance` and `CostPath` of ArcGIS like this:

``````CostDistance("start_polygon", "costs.tif", "", "outCostLink.tif")
``````

I would like to do the same thing with GRASS. I think that the function `r.cost` allows to do this. But the problem is that I don't understand how to specify in parameters the two polygons in GRASS ?

I have :

• a cost raster where each cell has a cost value.
• a shapefile for the first polygon
• a shapefile for the second polygon

Have you got an example of how to use `r.cost` with two polygons ?

Thanks very much for your help.

• One can imagine many different interpretations of your question: is the "cumulative cost of moving between two polygons" equal to (a) the smallest cost to get from any point in one polygon (say polygon "A") to any point in the other (polygon "B"), or (b) the average cost to get from points in A to points in B, or (c) the average smallest cost to get from points in A to some point in B, or (d) the same as (c) but reversing the roles of A and B or (e) the maximum over A of the smallest costs to get from points in A to B, ... etc, etc. Please edit this question so it states what you need. Aug 31, 2014 at 15:06
• Thank you for the clarification. Do you know how to find a costdistance raster for a set of origination points? If so, you're practically done, because a zonal summary of that (using the other polygon as the zone) will finish the job. Aug 31, 2014 at 19:29
• Thank you very much whuber for your response. No, sorry I don't know to find a costdistance raster for a set of origination points under GRASS. Under ArcGIS, I already imported a shapefile of polygons (no points). Thanks very much for your help. Aug 31, 2014 at 19:36

I know this is an old question, but here's a crack at the answer. My approach seems a little convoluted to me, but it's the best I could think of. I used GRASS 6.4.

``````g.region s=0 w=0 e=100 n=100 res=1

# set up some polygons
echo "B 4
10 10
10 20
20 10
10 10" | v.in.ascii -n input=- output=polygonA format=standard

echo "B 4
90 90
90 80
80 90
90 90" | v.in.ascii -n input=- output=polygonB format=standard

r.mapcalc cost=1.0

# generate points on the edges of the polygons
# dmax should probably be set to less than your raster resolution
v.to.points -i input=polygonA output=polygonA_pts dmax=1
v.to.points -i input=polygonB output=polygonB_pts dmax=1

# compute the least cost path
# this starts the lcp from the center of polygon B, but I imagine the lcp will
# pass through the polygon boundary at the point that would result in the lcp
r.cost input=cost output=cummulative_cost start_points=polygonA_pts stop_points=polygonB_pts
v.centroids input=polygonB output=polygonB_cent
v.to.points input=polygonB_cent output=polygonB_cent_only type=centroid
r.drain input=cummulative_cost output=lcp voutput=lcp vector_points=polygonB_cent_only

# since the lcp starts at the center of polygonB,
# split the lcp at the polygon edge to get a new starting point on the edge of the polygonB
# then run r.drain again to get the final result from edge of polygon to edge of polygon
v.patch input=lcp,polygonB output=temp
v.clean input=temp output=lcp_from_edge tool=break error=new_starting_point
r.drain input=cummulative_cost output=lcp voutput=lcp vector_points=new_starting_point --o
``````