Assigns a traversing cost to an array of geographic grids. Pass the returned gridcost value to a routing function to define impassable barriers or weighted traversal zones in grid-based shortest-path calculations.
Syntax
gridcost ST_SetCost(geomgrid[] barriers, smallint cost);Parameters
| Parameter | Type | Description |
|---|---|---|
barriers | geomgrid[] | The array of geographic grids to assign a traversing cost to. |
cost | smallint | The traversing cost to assign. Set to -1 to mark the grids as impassable. Any other value is interpreted as an equivalent distance—the number of barrier-free grids whose total traversal cost equals the cost of crossing one grid in the barriers array. |
How it works
ST_SetCost assigns a traversing cost value to an array of specified grids with barriers. Pass the returned gridcost value to routing functions that calculate shortest paths across a grid network.
The cost parameter has two distinct behaviors:
-1(impassable): The array of specified grids cannot be traversed. Routing functions treat the grids inbarriersas blocked cells and route around them.Any other value (weighted cost): The cost is expressed as an equivalent distance. The value of the cost parameter for the remaining grids is calculated based on the equivalent distance—equating the cost of traversing a grid with barriers to the cost of traversing a number of grids without barriers. A cost of
5means crossing one grid inbarrierscosts as much as crossing 5 barrier-free grids.
Example
Assign a traversing cost of 1 to two grids:
SELECT st_setcost(
ARRAY[st_gridfromtext('GZ01'), st_gridfromtext('GZ1')],
1
);Output:
("{01024002000001000000,0102410100000000}",1)What's next
After assigning costs with ST_SetCost, pass the resulting gridcost values to a routing function to compute shortest paths across the grid network.