Ejemplo n.º 1
0
                    " where cat=" + str(i + 1))
        # Points
        cur.execute("update streams set x1=" + str(points_in_streams[i][0].x) +
                    " where cat=" + str(i + 1))
        cur.execute("update streams set y1=" + str(points_in_streams[i][0].y) +
                    " where cat=" + str(i + 1))
        cur.execute("update streams set x2=" +
                    str(points_in_streams[i][-1].x) + " where cat=" +
                    str(i + 1))
        cur.execute("update streams set y2=" +
                    str(points_in_streams[i][-1].y) + " where cat=" +
                    str(i + 1))
    print i
#streamsTopo.write()
streamsTopo.table.conn.commit()
streamsTopo.build()

# Note on adding columns
# sch.table.columns.add('elevation','double precision')

# THEN:
# 5. Every line should have
"""
# Get line segment endpoints
# xy1: UPSTREAM
# xy2: DOWNSTREAM
v.db_addcolumn(map='streams', columns='x1 double precision, y1 double precision, x2 double precision, y2 double precision')
v.to_db(map='streams', option='start', columns='x1, y1')
v.to_db(map='streams', option='end', columns='x2, y2')
"""
"""
Ejemplo n.º 2
0
def main():
    """
    Links each river segment to the next downstream segment in a tributary 
    network by referencing its category (cat) number in a new column. "0"
    means that the river exits the map.
    """

    options, flags = gscript.parser()
    streams = options['map']
    x1 = options['upstream_easting_column']
    y1 = options['upstream_northing_column']
    x2 = options['downstream_easting_column']
    y2 = options['downstream_northing_column']

    streamsTopo = VectorTopo(streams)
    #streamsTopo.build()

    # 1. Get vectorTopo
    streamsTopo.open(mode='rw')
    """
    points_in_streams = []
    cat_of_line_segment = []

    # 2. Get coordinates
    for row in streamsTopo:
        cat_of_line_segment.append(row.cat)
        if type(row) == vector.geometry.Line:
            points_in_streams.append(row)
    """

    # 3. Coordinates of points: 1 = start, 2 = end
    try:
        streamsTopo.table.columns.add(x1, 'double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add(y1, 'double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add(x2, 'double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add(y2, 'double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add('tostream', 'int')
    except:
        pass
    streamsTopo.table.conn.commit()

    # Is this faster than v.to.db?
    """
    cur = streamsTopo.table.conn.cursor()
    for i in range(len(points_in_streams)):
        cur.execute("update streams set x1="+str(points_in_streams[i][0].x)+" where cat="+str(cat_of_line_segment[i]))
        cur.execute("update streams set y1="+str(points_in_streams[i][0].y)+" where cat="+str(cat_of_line_segment[i]))
        cur.execute("update streams set x2="+str(points_in_streams[i][-1].x)+" where cat="+str(cat_of_line_segment[i]))
        cur.execute("update streams set y2="+str(points_in_streams[i][-1].y)+" where cat="+str(cat_of_line_segment[i]))
    streamsTopo.table.conn.commit()
    streamsTopo.build()
    """
    # v.to.db Works more consistently, at least
    streamsTopo.close()
    v.to_db(map=streams, option='start', columns=x1 + ',' + y1)
    v.to_db(map=streams, option='end', columns=x2 + ',' + y2)

    # 4. Read in and save the start and end coordinate points
    colNames = np.array(vector_db_select(streams)['columns'])
    colValues = np.array(vector_db_select(streams)['values'].values())
    cats = colValues[:,
                     colNames == 'cat'].astype(int).squeeze()  # river number
    xy1 = colValues[:, (colNames == 'x1') + (colNames == 'y1')].astype(
        float)  # upstream
    xy2 = colValues[:, (colNames == 'x2') + (colNames == 'y2')].astype(
        float)  # downstream

    # 5. Build river network
    tocat = []
    for i in range(len(cats)):
        tosegment_mask = np.prod(xy1 == xy2[i], axis=1)
        if np.sum(tosegment_mask) == 0:
            tocat.append(0)
        else:
            tocat.append(cats[tosegment_mask.nonzero()[0][0]])
    tocat = np.asarray(tocat).astype(int)

    # This gives us a set of downstream-facing adjacencies.
    # We will update the database with it.
    streamsTopo.build()
    streamsTopo.open('rw')
    cur = streamsTopo.table.conn.cursor()
    # Default to 0 if no stream flows to it
    cur.execute("update " + streams + " set tostream=0")
    for i in range(len(tocat)):
        cur.execute("update " + streams + " set tostream=" + str(tocat[i]) +
                    " where cat=" + str(cats[i]))
    streamsTopo.table.conn.commit()
    #streamsTopo.build()
    streamsTopo.close()

    gscript.message('')
    gscript.message(
        'Drainage topology built. Check "tostream" column for the downstream cat.'
    )
    gscript.message('A cat value of 0 indicates the downstream-most segment.')
    gscript.message('')
Ejemplo n.º 3
0
def main():
    """
    Links each river segment to the next downstream segment in a tributary 
    network by referencing its category (cat) number in a new column. "0"
    means that the river exits the map.
    """

    options, flags = gscript.parser()
    streams = options['streams']

    streamsTopo = VectorTopo(streams)
    streamsTopo.build()

    # Is this faster than v.to.db?
    # Works more consistently, at least
    v.to_db(map=streams, option='start', columns='x1,y1')
    v.to_db(map=streams, option='end', columns='x2,y2')

    # 1. Get vectorTopo
    streamsTopo.open(mode='rw')
    points_in_streams = []
    cat_of_line_segment = []
    """
    # 2. Get coordinates
    for row in streamsTopo:
        cat_of_line_segment.append(row.cat)
        if type(row) == vector.geometry.Line:
            points_in_streams.append(row)

    # 3. Coordinates of points: 1 = start, 2 = end
    try:
        streamsTopo.table.columns.add('x1','double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add('y1','double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add('x2','double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add('y2','double precision')
    except:
        pass
    try:
        streamsTopo.table.columns.add('tostream','int')
    except:
        pass
    #streamsTopo.table.conn.commit()
    #streamsTopo.build()
    #streamsTopo.close()
    """

    cur = streamsTopo.table.conn.cursor()
    for i in range(len(points_in_streams)):
        cur.execute("update streams set x1=" + str(points_in_streams[i][0].x) +
                    " where cat=" + str(cat_of_line_segment[i]))
        cur.execute("update streams set y1=" + str(points_in_streams[i][0].y) +
                    " where cat=" + str(cat_of_line_segment[i]))
        cur.execute("update streams set x2=" +
                    str(points_in_streams[i][-1].x) + " where cat=" +
                    str(cat_of_line_segment[i]))
        cur.execute("update streams set y2=" +
                    str(points_in_streams[i][-1].y) + " where cat=" +
                    str(cat_of_line_segment[i]))
    streamsTopo.table.conn.commit()
    streamsTopo.build()

    colNames = np.array(vector_db_select('streams')['columns'])
    colValues = np.array(vector_db_select('streams')['values'].values())
    cats = colValues[:,
                     colNames == 'cat'].astype(int).squeeze()  # river number
    print colValues
    xy1 = colValues[:, (colNames == 'x1') + (colNames == 'y1')].astype(
        float)  # upstream
    xy2 = colValues[:, (colNames == 'x2') + (colNames == 'y2')].astype(
        float)  # downstream

    # Build river network
    tocat = []
    for i in range(len(cats)):
        tosegment_mask = np.prod(xy1 == xy2[i], axis=1)
        if np.sum(tosegment_mask) == 0:
            tocat.append(0)
        else:
            tocat.append(cats[tosegment_mask.nonzero()[0][0]])
    tocat = np.asarray(tocat).astype(int)

    # This gives us a set of downstream-facing adjacencies.
    # We will update the database with it.
    streamsTopo.build()
    streamsTopo.open('rw')
    cur = streamsTopo.table.conn.cursor()
    for i in range(len(tocat)):
        cur.execute("update streams set tostream=" + str(tocat[i]) +
                    " where cat=" + str(cats[i]))
    streamsTopo.table.conn.commit()
    streamsTopo.build()

    print ""
    print "Done."
    print ""