[GRASS-user] Directed vector networks (e.g. river networks)

Pietro Zambelli peter.zamb at gmail.com
Fri Jun 14 03:49:42 PDT 2013


Hi Johannes,

On Friday 14 Jun 2013 11:03:04 Johannes Radinger wrote:
> Hi,
> 
> as I am not very familiar with GRASS' vector capabilities maybe someone of
> you has some ideas. Is there the concept of directed vector networks e.g
> describing river networks with flow direction implemented in GRASS?

Each line in GRASS is just an array of points, therefore for each line you 
have a direction from the first point to the last. 
If the direction is not correct you can change with reverse.

A possible way to check the right direction using pygrass is (note, not 
tested, and pygrass is only available in grass7):

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.raster import RasterRow
from grass.pygrass.gis.region import Region


def check_line(line, elevation, region):
    first = elevation.get_value(line[0], region)
    last = elevation.get_value(line[-1], region)
    if first < last:
        line.reverse()
    return line


def check_direction(vectorname, elevname, outputname):
    """Check if the direction of a river network is correct"""
    river = VectorTopo(vectorname)
    river.open()
    newriver = VectorTopo(outputname)
    newriver.open('w', tab_cols=river.table.columns.items())
    elevation = RasterRow(elevname)
    elevation.open()
    region = Region()
    for line in river:
        newline = check_line(line, elevation, region)
        newriver.write(newline, attrs=line.attrs.values())
    newriver.table.conn.commit()
    river.close()
    newriver.close()
    elevation.close()

check_direction('river', 'elevation', 'river_checked')

}}}

Of course you can implement the same in C.

For the other questions I'm not sure which is the best way to follow.

All the best.

Pietro


More information about the grass-user mailing list