# $Id: topology.awk,v 1.2 2002-09-26 15:20:38 jan Exp $ # # Copyright (C) 1999, 2002 by Jan-Oliver Wagner # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # # DESCRIPTION: # This script reads in a GREAT-ER '.drn' file # and creates a topology for the river segments: # For each connection of two stretches (node) the # corresponding StretchIDs are printed (into two # files, one as from-to list ('from_id.tmp') and # the other one as to-from list ('to_id.tmp'). function GetNodeID(x, y) { nodeID = nodes[x,y] if (nodeID < 1) { nodeID = node_counter nodes[x,y] = nodeID node_counter ++ } return nodeID } BEGIN { FS = "," # set field separator node_counter = arc_counter = 1 last_StretchID = "none" } /^#/ { next } # skip comments /^$/ { next } # skip empty lines NF == 1 { # new stretch StretchID = $1 getline line # first coordinate -> must be a node # get coords of node split(line, coords, ",") x = coords[1] y = coords[2] # eliminate trailing zeros sub(/0*$/, "", x) sub(/0*$/, "", y) # check whether node exists and determine nodeID nodeID = GetNodeID(x,y) # store stretch arcs[arc_counter,"StretchID"] = StretchID arcs[arc_counter,"fnode"] = nodeID arc_counter ++ # now lets have a look on the last stretch if (last_StretchID == "none") { # we read the first stretch of the file # set new last stretch last_StretchID = StretchID next } # check whether node exists and determine nodeID nodeID = GetNodeID(lastX,lastY) # set to-node arcID = arc_counter - 2 arcs[arcID,"tnode"] = nodeID # set new last stretch lastStretchID = StretchID } NF == 2 { # coordinate pair lastX = $1 lastY = $2 # eliminate trailing zeros sub(/0*$/, "", lastX) sub(/0*$/, "", lastY) } END { # now lets have a look on the last stretch if (last_StretchID == "none") { # file contains no stretch exit } # check whether node exists and determine nodeID nodeID = GetNodeID(lastX,lastY) # set to-node arcID = arc_counter - 1 arcs[arcID,"tnode"] = nodeID # write results for (i=1;i < arc_counter;i ++) { printf("%d %s\n", arcs[i,"fnode"], arcs[i,"StretchID"]) >> "from_id.tmp"; printf("%d %s\n", arcs[i,"tnode"], arcs[i,"StretchID"]) >> "to_id.tmp"; } }