Driving distance

Driving distance functions use the Dijkstra algorithm to find all the edges that have the cumulative costs less or equal to the specified value. Our ways dataset has some properties that can be used to perform sensible driving distance analysis.

Let's first find the edges that are no farther than 1000 metres from Vienna's central train station:

select * from pgr_drivingDistance('select gid as id, source, target, length_m as cost from pgr.ways', 120829, 1000); 

Next, let's see how far we can get from the station in five minutes:

select * from pgr_drivingDistance('select gid as id, source, target, cost_s as cost from pgr.ways', 120829, 300); 

In both cases, the output is a dataset with the following columns: seq, node, edge, cost, and agg_cost. Query results visualized on a map look as follows (orange is the result of the first query and blue shows the results of the second one):

Pgr_drivingDistance can also accept multiple starting nodes, so it is possible to calculate more complex scenarios in one go. In such cases, start vertices are provided as an array, and the output gets one more column - from_v - that describes what the starting point for the data was.

Now, as we have calculated the driving distance along our network, let's create driving distance zones (also called drive time zones, catchment areas) for our five minute drive, so we can assess what area actually belongs to a certain driving distance. In order to do so, we will use a pgr_alphaShape function:

select pgr_alphaShape( 
'select
v.id::int4, v.lon::float8 as x, v.lat::float8 as y
from(
select * from pgr_drivingDistance(''select gid as id, source, target, cost_s as cost from pgr.ways'', 120829, 300)
) as dd
left outer join pgr.ways_vertices_pgr v on dd.node = v.id'
);

The returned data is not a polygon as one could expect, but a set of pgr_alphashaperesult; so, basically, points that make up a shape:

         pgr_alphashape
-----------------------
(16.4133473,48.1908108)
(16.4131929,48.1906814)
...
(,) <-ring separator
...

If the function outputs more than one outer/inner ring, then they are separated by a row with null values for both x and y.

Next, let's make a polygon out of the calculated points:

select pgr_pointsAsPolygon( 
'select
v.id::int4, v.lon::float8 as x, v.lat::float8 as y
from(
select * from pgr_drivingDistance(''''select gid as id, source, target, cost_s as cost from pgr.ways'''', 120829, 300)
) as dd
left outer join pgr.ways_vertices_pgr v on dd.node = v.id'
);
Because pgr_pointsAsPolygon accepts text as the vertices select query and passes it internally to pgr_alphaShape, it may sometimes be difficult to work out how to escape apostrophes properly. In such cases, it is worth having a look at the PostgreSQL's double dollar sign delimiter: $$. In our example, it would be enough to replace the inner apostrophes with $$. If you happen to have more levels of string nesting, you add an identifier between the dollar symbols: $lvl1$ some string $lvl2$ some nested quoted string $lvl2$ some text $lvl1$

Finally, our result is a MultiPolygon:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset