All Products
Search
Document Center

PolarDB:ST_SetCost

Last Updated:Mar 28, 2026

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

ParameterTypeDescription
barriersgeomgrid[]The array of geographic grids to assign a traversing cost to.
costsmallintThe 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 in barriers as 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 5 means crossing one grid in barriers costs 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.