[GRASS-SVN] r42459 - grass-addons/raster/r.stream.order
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 3 07:33:08 EDT 2010
Author: mmetz
Date: 2010-06-03 07:33:08 -0400 (Thu, 03 Jun 2010)
New Revision: 42459
Modified:
grass-addons/raster/r.stream.order/global.h
grass-addons/raster/r.stream.order/io.c
grass-addons/raster/r.stream.order/main.c
grass-addons/raster/r.stream.order/order.c
Log:
format and clean up code
Modified: grass-addons/raster/r.stream.order/global.h
===================================================================
--- grass-addons/raster/r.stream.order/global.h 2010-06-03 11:32:18 UTC (rev 42458)
+++ grass-addons/raster/r.stream.order/global.h 2010-06-03 11:33:08 UTC (rev 42459)
@@ -56,8 +56,8 @@
#define INITS struct init
INITS {
- int r;
- int c;
+ int r;
+ int c;
};
/* functions.c */
Modified: grass-addons/raster/r.stream.order/io.c
===================================================================
--- grass-addons/raster/r.stream.order/io.c 2010-06-03 11:32:18 UTC (rev 42458)
+++ grass-addons/raster/r.stream.order/io.c 2010-06-03 11:33:08 UTC (rev 42459)
@@ -3,7 +3,6 @@
int open_raster(char *mapname)
{
-
int fd = 0;
char *mapset;
struct Cell_head cellhd; /* it stores region information, and header information of rasters */
@@ -35,10 +34,11 @@
int create_base_maps(void)
{
int r, c;
- CELL *r_dirs=NULL, *r_streams=NULL;
- DCELL *r_accum=NULL;
- int in_dir_fd=0, in_stm_fd=0, in_acc_fd=0; /* input file descriptors: indir_fd - direction.... etc */
+ CELL *r_dirs = NULL, *r_streams = NULL;
+ DCELL *r_accum = NULL;
+ int in_dir_fd = 0, in_stm_fd = 0, in_acc_fd = 0; /* input file descriptors: indir_fd - direction.... etc */
+
in_dir_fd = open_raster(in_dirs);
in_stm_fd = open_raster(in_streams);
if (in_accum)
@@ -104,29 +104,29 @@
}
G_free(r_streams);
G_free(r_dirs);
- if (in_accum)
- G_free(r_accum);
+ if (in_accum)
+ G_free(r_accum);
G_percent(r, nrows, 2);
G_close_cell(in_dir_fd);
G_close_cell(in_stm_fd);
- if (in_accum)
- G_close_cell(in_acc_fd);
+ if (in_accum)
+ G_close_cell(in_acc_fd);
return 0;
} /* end create base maps */
int stream_number(void)
{
+ char *cur_mapset;
+ CELL c_min, c_max;
+ struct Range stream_range;
- char *cur_mapset;
- CELL c_min, c_max;
- struct Range stream_range;
- G_init_range(&stream_range);
- cur_mapset = G_find_cell2(in_streams, "");
- G_read_range(in_streams,cur_mapset,&stream_range);
- G_get_range_min_max(&stream_range, &c_min, &c_max);
- if(c_max<1)
- G_fatal_error("No streams found" );
- return c_max;
+ G_init_range(&stream_range);
+ cur_mapset = G_find_cell2(in_streams, "");
+ G_read_range(in_streams, cur_mapset, &stream_range);
+ G_get_range_min_max(&stream_range, &c_min, &c_max);
+ if (c_max < 1)
+ G_fatal_error("No streams found");
+ return c_max;
}
@@ -162,8 +162,8 @@
G_fatal_error(_("Unable to create raster map <%s>"), out_horton);
horton_buf = G_allocate_cell_buf();
}
-
- if (out_topo) {
+
+ if (out_topo) {
if ((out_topo_fd = G_open_raster_new(out_topo, CELL_TYPE)) < 0)
G_fatal_error(_("Unable to create raster map <%s>"), out_topo);
topo_buf = G_allocate_cell_buf();
@@ -183,7 +183,7 @@
if (out_horton)
G_set_c_null_value(horton_buf, ncols);
if (out_topo)
- G_set_c_null_value(topo_buf, ncols);
+ G_set_c_null_value(topo_buf, ncols);
for (c = 0; c < ncols; ++c) {
if (!out_zero) {
@@ -196,7 +196,7 @@
hack_buf[c] = s_streams[streams[r][c]].hack;
if (out_horton)
horton_buf[c] = s_streams[streams[r][c]].horton;
- if (out_topo)
+ if (out_topo)
topo_buf[c] = s_streams[streams[r][c]].topo_dim;
} /* end if streams */
}
@@ -211,7 +211,7 @@
horton_buf[c] = s_streams[streams[r][c]].horton;
if (out_topo)
topo_buf[c] = s_streams[streams[r][c]].topo_dim;
-
+
}
} /* end for cols */
@@ -224,8 +224,8 @@
if (out_horton)
G_put_c_raster_row(out_hrt_fd, horton_buf);
if (out_topo)
- G_put_c_raster_row(out_topo_fd, topo_buf);
-
+ G_put_c_raster_row(out_topo_fd, topo_buf);
+
} /* end for r */
G_percent(r, nrows, 2);
@@ -235,7 +235,7 @@
G_free(strahler_buf);
G_short_history(out_strahler, "raster", &history);
G_write_history(out_strahler, &history);
- G_message(_("%s Done!"),out_strahler);
+ G_message(_("%s Done!"), out_strahler);
}
if (out_shreeve) {
@@ -243,14 +243,14 @@
G_free(shreeve_buf);
G_short_history(out_shreeve, "raster", &history);
G_write_history(out_shreeve, &history);
- G_message(_("%s Done!"),out_shreeve);
+ G_message(_("%s Done!"), out_shreeve);
}
if (out_hack) {
G_close_cell(out_hck_fd);
G_free(hack_buf);
G_short_history(out_hack, "raster", &history);
G_write_history(out_hack, &history);
- G_message(_("%s Done!"),out_hack);
+ G_message(_("%s Done!"), out_hack);
}
if (out_horton) {
@@ -258,216 +258,202 @@
G_free(horton_buf);
G_short_history(out_horton, "raster", &history);
G_write_history(out_horton, &history);
- G_message(_("%s Done!"),out_horton);
+ G_message(_("%s Done!"), out_horton);
}
-
- if (out_topo) {
+
+ if (out_topo) {
G_close_cell(out_topo_fd);
G_free(topo_buf);
G_short_history(out_topo, "raster", &history);
G_write_history(out_topo, &history);
- G_message(_("%s Done!"),out_topo);
- }
+ G_message(_("%s Done!"), out_topo);
+ }
return 0;
-
} /* end write_maps */
-int create_table (void)
+int create_table(void)
{
-
- int i,j;
- int index_cat=0;
- int max_trib=0;
- /*
- char *mapset;
- struct Map_info Map;
- char out_table[30];
- */
- char* out_table;
- dbConnection conn;
- dbDriver *driver;
- dbHandle handle;
- dbString table_name, db_sql, val_string;
- char buf[1000];
- char ins_prev_streams[50]; /* insert */
- char* cat_col_name="cat";
+ int i;
+ int max_trib = 0;
+ /*
+ char *mapset;
+ struct Map_info Map;
+ char out_table[30];
+ */
+ char *out_table;
+ dbConnection conn;
+ dbDriver *driver;
+ dbHandle handle;
+ dbString table_name, db_sql, val_string;
+ char buf[1000];
+ char ins_prev_streams[50]; /* insert */
+ char *cat_col_name = "cat";
+ /* table definition */
+ char *tab_cat_col_name = "cat integer";
+ char *tab_stream = "stream integer";
+ char *tab_next_stream = "next_stream integer";
+ char *tab_prev_streams;
+ char *tab_strahler = "strahler integer";
+ char *tab_horton = "horton integer";
+ char *tab_shreve = "shreve integer";
+ char *tab_hack = "hack integer";
+ char *tab_length = "length double precision";
+ char *tab_cumlength = "cum_length double precision";
+ char *tab_stright = "stright double precision";
+ char *tab_fractal = "fractal double precision";
+ char *tab_distance = "out_dist double precision";
+ char *tab_topo_dim = "topo_dim integer";
- /* table definition */
- char* tab_cat_col_name="cat integer";
- char* tab_stream="stream integer";
- char* tab_next_stream="next_stream integer";
- char* tab_prev_streams;
- char* tab_strahler="strahler integer";
- char* tab_horton="horton integer";
- char* tab_shreve="shreve integer";
- char* tab_hack="hack integer";
- char* tab_length="length double precision";
- char* tab_cumlength="cum_length double precision";
- char* tab_stright="stright double precision";
- char* tab_fractal="fractal double precision";
- char* tab_distance="out_dist double precision";
- char* tab_topo_dim="topo_dim integer";
-
- G_message("Adding table...");
-
- /* trib num */
- for (i=0;i<stream_num+1;++i) {
- if (s_streams[i].trib_num>max_trib)
- max_trib=s_streams[i].trib_num;
- }
-
- /*
- mapset = G_find_vector(in_vector, "");
- if (mapset == NULL)
- G_fatal_error(_("Vector map <%s> not found"), in_vector);
-
- if (Vect_open_update(&Map, in_vector, mapset) < 0)
- G_fatal_error("Cannot open vector map <%s>", in_vector);
+ G_message("Adding table...");
- if(in_table)
- sprintf(out_table, "%s",in_table);
- else
- sprintf(out_table, "%s_new",in_vector);
- */
+ /* trib num */
+ for (i = 0; i < stream_num + 1; ++i) {
+ if (s_streams[i].trib_num > max_trib)
+ max_trib = s_streams[i].trib_num;
+ }
- out_table=in_table;
+ /*
+ mapset = G_find_vector(in_vector, "");
+ if (mapset == NULL)
+ G_fatal_error(_("Vector map <%s> not found"), in_vector);
- /* init */
-
- db_init_string(&db_sql);
- db_init_string(&val_string);
- db_init_string(&table_name);
- db_init_handle(&handle);
-
- /* string to db */
-
- db_get_connection(&conn);
- driver=db_start_driver_open_database(conn.driverName, conn.databaseName);
-
- if(db_table_exists(conn.driverName, conn.databaseName,out_table)>0)
- G_fatal_error("table %s exists. Choose different table name or check and remove existing table",out_table);
+ if (Vect_open_update(&Map, in_vector, mapset) < 0)
+ G_fatal_error("Cannot open vector map <%s>", in_vector);
- /* creating table */
+ if(in_table)
+ sprintf(out_table, "%s",in_table);
+ else
+ sprintf(out_table, "%s_new",in_vector);
+ */
- switch (max_trib) {
- case 2:
- tab_prev_streams="prev_str01 integer, prev_str02 integer";
+ out_table = in_table;
+
+ /* init */
+
+ db_init_string(&db_sql);
+ db_init_string(&val_string);
+ db_init_string(&table_name);
+ db_init_handle(&handle);
+
+ /* string to db */
+
+ db_get_connection(&conn);
+ driver =
+ db_start_driver_open_database(conn.driverName, conn.databaseName);
+
+ if (db_table_exists(conn.driverName, conn.databaseName, out_table) > 0)
+ G_fatal_error
+ ("table %s exists. Choose different table name or check and remove existing table",
+ out_table);
+
+ /* creating table */
+
+ switch (max_trib) {
+ case 2:
+ tab_prev_streams = "prev_str01 integer, prev_str02 integer";
break;
- case 3:
- tab_prev_streams="prev_str01 integer, prev_str02 integer, prev_str03 integer";
+ case 3:
+ tab_prev_streams =
+ "prev_str01 integer, prev_str02 integer, prev_str03 integer";
break;
- case 4:
- tab_prev_streams="prev_str01 integer, prev_str02 integer, prev_str03 integer prev_str04 integer";
+ case 4:
+ tab_prev_streams =
+ "prev_str01 integer, prev_str02 integer, prev_str03 integer prev_str04 integer";
break;
- case 5:
- tab_prev_streams="prev_str01 integer, prev_str02 integer, prev_str03 integer prev_str04 integer, prev_str05 integer";
+ case 5:
+ tab_prev_streams =
+ "prev_str01 integer, prev_str02 integer, prev_str03 integer prev_str04 integer, prev_str05 integer";
break;
- default:
+ default:
G_fatal_error("Error with number of tributuaries");
break;
- }
-
-
- sprintf(buf,"create table %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
- out_table,
- tab_cat_col_name,
- tab_stream,
- tab_next_stream,
- tab_prev_streams,
- tab_strahler,
- tab_horton,
- tab_shreve,
- tab_hack,
- tab_topo_dim,
- tab_length,
- tab_cumlength,
- tab_distance,
- tab_stright,
- tab_fractal);
-
- db_set_string(&db_sql, buf);
+ }
- if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
- db_close_database(driver);
- db_shutdown_driver(driver);
- G_fatal_error("Cannot create table %s", db_get_string(&db_sql));
- }
-
- if (db_create_index2(driver,out_table,cat_col_name) !=DB_OK)
+
+ sprintf(buf,
+ "create table %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
+ out_table, tab_cat_col_name, tab_stream, tab_next_stream,
+ tab_prev_streams, tab_strahler, tab_horton, tab_shreve, tab_hack,
+ tab_topo_dim, tab_length, tab_cumlength, tab_distance,
+ tab_stright, tab_fractal);
+
+ db_set_string(&db_sql, buf);
+
+ if (db_execute_immediate(driver, &db_sql) != DB_OK) {
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+ G_fatal_error("Cannot create table %s", db_get_string(&db_sql));
+ }
+
+ if (db_create_index2(driver, out_table, cat_col_name) != DB_OK)
G_warning("cannot create index");
-
- if (db_grant_on_table(driver,out_table,DB_PRIV_SELECT, DB_GROUP |DB_PUBLIC) !=DB_OK)
+
+ if (db_grant_on_table
+ (driver, out_table, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK)
G_fatal_error("cannot grant privileges on table %s", out_table);
- db_begin_transaction(driver);
+ db_begin_transaction(driver);
- for (i=0;i<stream_num+1;++i) {
-
- if(s_streams[i].stream<0)
- continue;
-
- switch (max_trib) {
- case 2:
- sprintf(ins_prev_streams,"%d, %d",s_streams[i].trib[0],s_streams[i].trib[1]);
- break;
- case 3:
- sprintf(ins_prev_streams,"%d ,%d, %d",s_streams[i].trib[0],s_streams[i].trib[1],s_streams[i].trib[2]);
- break;
- case 4:
- sprintf(ins_prev_streams,"%d, %d, %d, %d",s_streams[i].trib[0],s_streams[i].trib[1],s_streams[i].trib[2],s_streams[i].trib[3]);
- break;
- case 5:
- sprintf(ins_prev_streams,"%d, %d, %d, %d, %d",s_streams[i].trib[0],s_streams[i].trib[1],s_streams[i].trib[2],s_streams[i].trib[3],s_streams[i].trib[4]);
- break;
- default:
- G_fatal_error("Error with number of tributuaries");
- break;
+ for (i = 0; i < stream_num + 1; ++i) {
+
+ if (s_streams[i].stream < 0)
+ continue;
+
+ switch (max_trib) {
+ case 2:
+ sprintf(ins_prev_streams, "%d, %d", s_streams[i].trib[0],
+ s_streams[i].trib[1]);
+ break;
+ case 3:
+ sprintf(ins_prev_streams, "%d ,%d, %d", s_streams[i].trib[0],
+ s_streams[i].trib[1], s_streams[i].trib[2]);
+ break;
+ case 4:
+ sprintf(ins_prev_streams, "%d, %d, %d, %d", s_streams[i].trib[0],
+ s_streams[i].trib[1], s_streams[i].trib[2],
+ s_streams[i].trib[3]);
+ break;
+ case 5:
+ sprintf(ins_prev_streams, "%d, %d, %d, %d, %d",
+ s_streams[i].trib[0], s_streams[i].trib[1],
+ s_streams[i].trib[2], s_streams[i].trib[3],
+ s_streams[i].trib[4]);
+ break;
+ default:
+ G_fatal_error("Error with number of tributuaries");
+ break;
}
-
- sprintf(buf,"insert into %s values \
- (%d, %d, %d, %s, %d, %d, %d, %d, %d, %f, %f , %f, %f, %f)",
- out_table,
- i,
- s_streams[i].stream,
- s_streams[i].next_stream,
- ins_prev_streams,
- s_streams[i].strahler,
- s_streams[i].horton,
- s_streams[i].shreeve,
- s_streams[i].hack,
- s_streams[i].topo_dim,
- s_streams[i].length,
- s_streams[i].accum,
- s_streams[i].distance,
- s_streams[i].stright,
- s_streams[i].fractal);
-
- db_set_string(&db_sql,buf);
-
- if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
- db_close_database(driver);
- db_shutdown_driver(driver);
- G_fatal_error("Cannot inset new row: %s", db_get_string(&db_sql));
- }
+
+ sprintf(buf, "insert into %s values \
+ (%d, %d, %d, %s, %d, %d, %d, %d, %d, %f, %f , %f, %f, %f)", out_table, i, s_streams[i].stream, s_streams[i].next_stream, ins_prev_streams, s_streams[i].strahler, s_streams[i].horton, s_streams[i].shreeve, s_streams[i].hack, s_streams[i].topo_dim, s_streams[i].length, s_streams[i].accum, s_streams[i].distance, s_streams[i].stright, s_streams[i].fractal);
+
+ db_set_string(&db_sql, buf);
+
+ if (db_execute_immediate(driver, &db_sql) != DB_OK) {
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+ G_fatal_error("Cannot inset new row: %s", db_get_string(&db_sql));
}
+ }
-db_commit_transaction(driver);
-db_close_database_shutdown_driver(driver);
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
-/*
- if(Vect_map_check_dblink(&Map,1))
- Vect_map_del_dblink(&Map,1);
-
- Vect_map_add_dblink(&Map, 1, NULL, out_table,
- tab_cat_col_name, conn.driverName, conn.databaseName);
-
- Vect_hist_command(&Map);
- Vect_build(&Map);
- Vect_close(&Map);
-*/
+ /*
+ if(Vect_map_check_dblink(&Map,1))
+ Vect_map_del_dblink(&Map,1);
-G_message("Table %s created. You can join it to vector created with r.stream.extract \
- using v.db.connect", out_table);
- return 0;
+ Vect_map_add_dblink(&Map, 1, NULL, out_table,
+ tab_cat_col_name, conn.driverName, conn.databaseName);
+
+ Vect_hist_command(&Map);
+ Vect_build(&Map);
+ Vect_close(&Map);
+ */
+
+ G_message("Table %s created. You can join it to vector created with r.stream.extract \
+ using v.db.connect",
+ out_table);
+ return 0;
}
Modified: grass-addons/raster/r.stream.order/main.c
===================================================================
--- grass-addons/raster/r.stream.order/main.c 2010-06-03 11:32:18 UTC (rev 42458)
+++ grass-addons/raster/r.stream.order/main.c 2010-06-03 11:33:08 UTC (rev 42459)
@@ -23,11 +23,9 @@
*/
int main(int argc, char *argv[])
{
-
struct GModule *module; /* GRASS module for parsing arguments */
- struct Option *in_dir_opt, *in_stm_opt, /* *in_vect_opt,*/ *in_table_opt, *in_acc_opt, *out_str_opt, *out_shr_opt, *out_hck_opt, *out_hrt_opt, *out_topo_opt ; /* options */
+ struct Option *in_dir_opt, *in_stm_opt, /* *in_vect_opt, */ *in_table_opt, *in_acc_opt, *out_str_opt, *out_shr_opt, *out_hck_opt, *out_hrt_opt, *out_topo_opt; /* options */
struct Flag *out_back; /* flags */
- int i;
/* initialize GIS environment */
G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
@@ -56,23 +54,22 @@
in_dir_opt->gisprompt = "old,cell,raster";
in_dir_opt->description =
"Name of direction input map (r.watershed or r.stream.extract)";
- /*
- in_vect_opt = G_define_option();
- in_vect_opt->key = "vector";
- in_vect_opt->type = TYPE_STRING;
- in_vect_opt->required = NO;
- in_vect_opt->gisprompt = "old,vector,vector";
- in_vect_opt->description =
- "Name of stream vector file (r.stream.extract output only)";
- */
- in_table_opt = G_define_option(); /* optional tabe name */
+ /*
+ in_vect_opt = G_define_option();
+ in_vect_opt->key = "vector";
+ in_vect_opt->type = TYPE_STRING;
+ in_vect_opt->required = NO;
+ in_vect_opt->gisprompt = "old,vector,vector";
+ in_vect_opt->description =
+ "Name of stream vector file (r.stream.extract output only)";
+ */
+ in_table_opt = G_define_option(); /* optional tabe name */
in_table_opt->key = "table";
in_table_opt->type = TYPE_STRING;
in_table_opt->required = NO;
in_table_opt->answer = NULL;
- in_table_opt->description =
- "Name of new table to create";
-
+ in_table_opt->description = "Name of new table to create";
+
in_acc_opt = G_define_option(); /* input stream mask file - optional */
in_acc_opt->key = "accum"; /* required if strahler stream order is calculated for existing stream network */
in_acc_opt->type = TYPE_STRING;
@@ -108,8 +105,7 @@
out_hrt_opt->required = NO;
out_hrt_opt->answer = NULL;
out_hrt_opt->gisprompt = "new,cell,raster";
- out_hrt_opt->description =
- "Name of Horton's stream order output map";
+ out_hrt_opt->description = "Name of Horton's stream order output map";
out_hrt_opt->guisection = _("Output options");
out_hck_opt = G_define_option();
@@ -118,18 +114,16 @@
out_hck_opt->required = NO;
out_hck_opt->answer = NULL;
out_hck_opt->gisprompt = "new,cell,raster";
- out_hck_opt->description =
- "Name of Hack's main streams output map";
+ out_hck_opt->description = "Name of Hack's main streams output map";
out_hck_opt->guisection = _("Output options");
-
+
out_topo_opt = G_define_option();
out_topo_opt->key = "topo";
out_topo_opt->type = TYPE_STRING;
out_topo_opt->required = NO;
out_topo_opt->answer = NULL;
out_topo_opt->gisprompt = "new,cell,raster";
- out_topo_opt->description =
- "Name of topological dimension output map";
+ out_topo_opt->description = "Name of topological dimension output map";
out_topo_opt->guisection = _("Output options");
/* Define flags */
@@ -143,15 +137,15 @@
G_get_window(&window);
if (!out_str_opt->answer && !out_shr_opt->answer && !out_hck_opt->answer
- && !out_hrt_opt->answer && !out_topo_opt->answer && !in_table_opt->answer)
+ && !out_hrt_opt->answer && !out_topo_opt->answer &&
+ !in_table_opt->answer)
G_fatal_error(_("You must select one or more output maps: strahler, horton, shreeve, hack, topo or insert the table name"));
-
/* stores input options to variables */
in_dirs = in_dir_opt->answer;
in_streams = in_stm_opt->answer;
/* in_vector = in_vect_opt->answer; */
- in_table = in_table_opt->answer;
+ in_table = in_table_opt->answer;
in_accum = in_acc_opt->answer;
/* stores output options to variables */
@@ -162,36 +156,35 @@
out_topo = out_topo_opt->answer;
out_zero = (out_back->answer != 0);
- if (out_strahler) {
- if (G_legal_filename(out_strahler) < 0)
- G_fatal_error("<%s> is an illegal file name", out_strahler);
- }
- if (out_shreeve) {
+ if (out_strahler) {
+ if (G_legal_filename(out_strahler) < 0)
+ G_fatal_error("<%s> is an illegal file name", out_strahler);
+ }
+ if (out_shreeve) {
if (G_legal_filename(out_shreeve) < 0)
- G_fatal_error("<%s> is an illegal file name", out_shreeve);
- }
- if (out_hack) {
+ G_fatal_error("<%s> is an illegal file name", out_shreeve);
+ }
+ if (out_hack) {
if (G_legal_filename(out_hack) < 0)
- G_fatal_error("<%s> is an illegal file name", out_hack);
- }
- if (out_horton) {
+ G_fatal_error("<%s> is an illegal file name", out_hack);
+ }
+ if (out_horton) {
if (G_legal_filename(out_horton) < 0)
- G_fatal_error("<%s> is an illegal file name", out_horton);
- }
-
+ G_fatal_error("<%s> is an illegal file name", out_horton);
+ }
+
nrows = G_window_rows();
ncols = G_window_cols();
create_base_maps();
- stream_num = stream_number();
+ stream_num = stream_number();
- stack_max = stream_num; /* stack's size depends on number of streams */
+ stack_max = stream_num; /* stack's size depends on number of streams */
init_streams(stream_num);
find_nodes(stream_num);
if (out_hack || out_horton || in_table || out_topo)
- do_cum_length();
+ do_cum_length();
-
if (out_strahler || out_horton || in_table)
strahler();
if (out_shreeve || in_table)
@@ -200,10 +193,10 @@
horton();
if (out_hack || out_topo || in_table)
hack();
-
- write_maps();
- if(in_table)
+ write_maps();
+
+ if (in_table)
create_table();
exit(EXIT_SUCCESS);
Modified: grass-addons/raster/r.stream.order/order.c
===================================================================
--- grass-addons/raster/r.stream.order/order.c 2010-06-03 11:32:18 UTC (rev 42458)
+++ grass-addons/raster/r.stream.order/order.c 2010-06-03 11:33:08 UTC (rev 42459)
@@ -4,11 +4,11 @@
#include "global.h"
int trib_nums(int r, int c)
-{ /* calcualte number of tributuaries */
-
+{ /* calculate number of tributuaries */
int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int trib = 0;
+
int i, j;
for (i = 1; i < 9; ++i) {
@@ -25,14 +25,15 @@
if (trib > 3)
G_warning(_("Stream network may be too dense..."));
- return trib;
+ return trib;
} /* end trib_num */
int init_streams(int stream_num)
{
int i;
- s_streams = (STREAM *) G_malloc((stream_num + 1) * sizeof(STREAM));
- for (i = 0; i <= stream_num; ++i) {
+
+ s_streams = (STREAM *) G_malloc((stream_num + 1) * sizeof(STREAM));
+ for (i = 0; i <= stream_num; ++i) {
s_streams[i].next_stream = -1;
s_streams[i].stream = -1;
s_streams[i].trib_num = -1;
@@ -44,8 +45,8 @@
s_streams[i].length = 0.;
s_streams[i].stright = 0.;
s_streams[i].fractal = 0.;
- s_streams[i].distance=0.;
- s_streams[i].topo_dim=0;
+ s_streams[i].distance = 0.;
+ s_streams[i].topo_dim = 0;
s_streams[i].trib[0] = 0;
s_streams[i].trib[1] = 0;
s_streams[i].trib[2] = 0;
@@ -57,21 +58,19 @@
int find_nodes(int stream_num)
{
-
int d, i, j; /* d: direction, i: iteration */
int r, c;
int trib_num, trib = 0;
int next_stream = -1, cur_stream;
-
- springs_num = 0, outlets_num = 0;
int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
G_message(_("Finding nodes..."));
+ springs_num = 0, outlets_num = 0;
outlets = (int *)G_malloc((stream_num) * sizeof(int));
springs = (int *)G_malloc((stream_num) * sizeof(int));
- s_inits = (INITS *)G_malloc((stream_num) *sizeof(INITS));
+ s_inits = (INITS *) G_malloc((stream_num) * sizeof(INITS));
for (r = 0; r < nrows; ++r) {
for (c = 0; c < ncols; ++c) {
@@ -79,7 +78,7 @@
trib_num = trib_nums(r, c);
trib = 0;
d = abs(dirs[r][c]); /* abs */
- if (r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||
+ if (r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||
c + nextc[d] < 0 || c + nextc[d] > (ncols - 1)) {
next_stream = -1;
}
@@ -88,12 +87,12 @@
if (next_stream < 1)
next_stream = -1;
}
- if (dirs[r][c]==0)
- next_stream=-1;
+ if (dirs[r][c] == 0)
+ next_stream = -1;
cur_stream = streams[r][c];
if (cur_stream != next_stream) { /* building hierarchy */
-
+
if (outlets_num > (stream_num - 1))
G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
@@ -109,16 +108,16 @@
G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
s_streams[cur_stream].trib_num = 0;
- s_inits[springs_num].r=r;
- s_inits[springs_num].c=c;
+ s_inits[springs_num].r = r;
+ s_inits[springs_num].c = c;
springs[springs_num++] = cur_stream; /* collecting springs */
}
if (trib_num > 1) { /* adding tributuaries */
s_streams[cur_stream].trib_num = trib_num;
- //G_message("HERE %d %d", cur_stream, s_streams[cur_stream].trib_num);
-
+ /* G_message("HERE %d %d", cur_stream, s_streams[cur_stream].trib_num); */
+
for (i = 1; i < 9; ++i) {
if (trib > 4)
G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
@@ -147,93 +146,99 @@
return 0;
}
-int do_cum_length (void) {
-
- int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
- int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+int do_cum_length(void)
+{
+ int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+ int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+ int i, s, d; /* s - streams index; d - direction */
+ int done = 1;
+ int r, c;
+ int next_r, next_c;
+ int cur_stream;
+ float cur_northing, cur_easting;
+ float next_northing, next_easting;
+ float init_northing, init_easting;
+ double cur_length = 0.;
+ double cur_accum = 0.;
- int i, s, d; /* s - streams index; d - direction */
- int done = 1;
- int r, c;
- int next_r, next_c;
- int cur_stream;
- float cur_northing, cur_easting;
- float next_northing, next_easting;
- float init_northing, init_easting;//
- double cur_length=0.;
- double cur_accum=0.;
-
- G_message(_("Finding longests streams..."));
- G_begin_distance_calculations();
-
- for (s=0; s<springs_num; ++s) { /* main loop on springs */
- r = s_inits[s].r;
- c = s_inits[s].c;
- cur_stream=streams[r][c];
- cur_length=0;
- done=1;
-
- init_northing = window.north - (r + .5) * window.ns_res; //
- init_easting = window.west + (c + .5) * window.ew_res; //
-
- while(done) {
+ G_message(_("Finding longests streams..."));
+ G_begin_distance_calculations();
- cur_northing = window.north - (r + .5) * window.ns_res;
- cur_easting = window.west + (c + .5) * window.ew_res;
-
- d=dirs[r][c];
- next_r=r+nextr[d];
- next_c=c+nextc[d];
-
- if (d<1|| /* end of route: sink */
- r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) || /*border*/
- c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) ||
- streams[next_r][next_c]<1) { /* mask */
-
- cur_length=(window.ns_res+window.ew_res)/2;
- s_streams[cur_stream].accum += cur_length;
- s_streams[cur_stream].length += cur_length;
- s_streams[cur_stream].stright =
- G_distance(cur_easting, cur_northing, init_easting, init_northing);
- s_streams[cur_stream].fractal =
- s_streams[cur_stream].length/s_streams[cur_stream].stright;
- break;
- }
+ for (s = 0; s < springs_num; ++s) { /* main loop on springs */
+ r = s_inits[s].r;
+ c = s_inits[s].c;
+ cur_stream = streams[r][c];
+ cur_length = 0;
+ done = 1;
- next_northing = window.north - (next_r + .5) * window.ns_res;
- next_easting = window.west + (next_c + .5) * window.ew_res;
- cur_length = G_distance(next_easting, next_northing, cur_easting, cur_northing);
- s_streams[cur_stream].accum += cur_length;
- s_streams[cur_stream].length += cur_length;
- r=next_r;
- c=next_c;
-
- if (streams[next_r][next_c] != cur_stream) {
- s_streams[cur_stream].stright =
- G_distance(next_easting, next_northing, init_easting, init_northing);
- s_streams[cur_stream].fractal =
- s_streams[cur_stream].length/s_streams[cur_stream].stright;
- init_northing = cur_northing;
- init_easting = cur_easting;
-
- cur_stream=streams[next_r][next_c];
- cur_accum=0;
-
- for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
- if (s_streams[s_streams[cur_stream].trib[i]].accum == 0) {
- done=0;
- cur_accum=0;
- break; /* do not pass accum*/
- }
- if(s_streams[s_streams[cur_stream].trib[i]].accum>cur_accum)
- cur_accum=s_streams[s_streams[cur_stream].trib[i]].accum;
- } /* end for i */
- if(!in_accum)
- s_streams[cur_stream].accum=cur_accum;
- }/* end if */
- } /* end while */
- } /* end for s*/
-return 0;
+ init_northing = window.north - (r + .5) * window.ns_res;
+ init_easting = window.west + (c + .5) * window.ew_res;
+
+ while (done) {
+
+ cur_northing = window.north - (r + .5) * window.ns_res;
+ cur_easting = window.west + (c + .5) * window.ew_res;
+
+ d = dirs[r][c];
+ next_r = r + nextr[d];
+ next_c = c + nextc[d];
+
+ if (d < 1 || /* end of route: sink */
+ r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) || /*border */
+ c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) || streams[next_r][next_c] < 1) { /* mask */
+
+ cur_length = (window.ns_res + window.ew_res) / 2;
+ s_streams[cur_stream].accum += cur_length;
+ s_streams[cur_stream].length += cur_length;
+ s_streams[cur_stream].stright =
+ G_distance(cur_easting, cur_northing, init_easting,
+ init_northing);
+ s_streams[cur_stream].fractal =
+ s_streams[cur_stream].length /
+ s_streams[cur_stream].stright;
+ break;
+ }
+
+ next_northing = window.north - (next_r + .5) * window.ns_res;
+ next_easting = window.west + (next_c + .5) * window.ew_res;
+ cur_length =
+ G_distance(next_easting, next_northing, cur_easting,
+ cur_northing);
+ s_streams[cur_stream].accum += cur_length;
+ s_streams[cur_stream].length += cur_length;
+ r = next_r;
+ c = next_c;
+
+ if (streams[next_r][next_c] != cur_stream) {
+ s_streams[cur_stream].stright =
+ G_distance(next_easting, next_northing, init_easting,
+ init_northing);
+ s_streams[cur_stream].fractal =
+ s_streams[cur_stream].length /
+ s_streams[cur_stream].stright;
+ init_northing = cur_northing;
+ init_easting = cur_easting;
+
+ cur_stream = streams[next_r][next_c];
+ cur_accum = 0;
+
+ for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
+ if (s_streams[s_streams[cur_stream].trib[i]].accum == 0) {
+ done = 0;
+ cur_accum = 0;
+ break; /* do not pass accum */
+ }
+ if (s_streams[s_streams[cur_stream].trib[i]].accum >
+ cur_accum)
+ cur_accum =
+ s_streams[s_streams[cur_stream].trib[i]].accum;
+ } /* end for i */
+ if (!in_accum)
+ s_streams[cur_stream].accum = cur_accum;
+ } /* end if */
+ } /* end while */
+ } /* end for s */
+ return 0;
}
@@ -247,7 +252,6 @@
int strahler(void)
{
-
int i, j, done = 1;
int cur_stream, next_stream;
int max_strahler = 0, max_strahler_num;
@@ -275,14 +279,14 @@
done = 0;
break; /* strahler is not determined, break for loop */
}
- else if (s_streams[s_streams[cur_stream].trib[i]].
- strahler > max_strahler) {
+ else if (s_streams[s_streams[cur_stream].trib[i]].strahler
+ > max_strahler) {
max_strahler =
s_streams[s_streams[cur_stream].trib[i]].strahler;
max_strahler_num = 1;
}
- else if (s_streams[s_streams[cur_stream].trib[i]].
- strahler == max_strahler) {
+ else if (s_streams[s_streams[cur_stream].trib[i]].strahler
+ == max_strahler) {
++max_strahler_num;
}
} /* end determining strahler */
@@ -302,7 +306,6 @@
int shreeve(void)
{
-
int i, j, done = 1;
int cur_stream, next_stream;
int max_shreeve = 0;
@@ -351,7 +354,6 @@
int horton(void)
{
-
int *stack;
int top, i, j;
int cur_stream, cur_horton;
@@ -385,25 +387,26 @@
for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
if (s_streams[s_streams[cur_stream].trib[i]].horton < 0) {
- if (s_streams[s_streams[cur_stream].trib[i]].
- strahler > max_strahler) {
+ if (s_streams[s_streams[cur_stream].trib[i]].strahler
+ > max_strahler) {
max_strahler =
- s_streams[s_streams[cur_stream].trib[i]].
- strahler;
+ s_streams[s_streams[cur_stream].
+ trib[i]].strahler;
max_accum =
- s_streams[s_streams[cur_stream].trib[i]].
- accum;
+ s_streams[s_streams[cur_stream].
+ trib[i]].accum;
up_stream = s_streams[cur_stream].trib[i];
}
- else if (s_streams[s_streams[cur_stream].trib[i]].
- strahler == max_strahler) {
+ else if (s_streams
+ [s_streams[cur_stream].trib[i]].strahler ==
+ max_strahler) {
- if (s_streams[s_streams[cur_stream].trib[i]].
- accum > max_accum) {
+ if (s_streams[s_streams[cur_stream].trib[i]].accum
+ > max_accum) {
max_accum =
- s_streams[s_streams[cur_stream].trib[i]].
- accum;
+ s_streams[s_streams[cur_stream].
+ trib[i]].accum;
up_stream = s_streams[cur_stream].trib[i];
}
}
@@ -432,17 +435,14 @@
return 0;
}
-int hack(void) /* also calculate topological dimension */
-{
-
+int hack(void)
+{ /* also calculate topological dimension */
int *stack;
int top, i, j;
int cur_stream, cur_hack;
double max_accum;
int up_stream = 0;
-
- double cur_distance=0;
- int cur_topo_dim=0;
+ double cur_distance = 0;
G_message(_("Calculating Hack's main streams and topological dimension..."));
stack = (int *)G_malloc(stack_max * sizeof(int));
@@ -454,9 +454,10 @@
stack[0] = 0;
stack[1] = cur_stream;
top = 1;
-
- s_streams[cur_stream].topo_dim=top;
- cur_distance=s_streams[cur_stream].distance=s_streams[cur_stream].length;
+
+ s_streams[cur_stream].topo_dim = top;
+ cur_distance = s_streams[cur_stream].distance =
+ s_streams[cur_stream].length;
do {
max_accum = 0;
@@ -474,38 +475,38 @@
if (s_streams[s_streams[cur_stream].trib[i]].accum >
max_accum) {
max_accum =
- s_streams[s_streams[cur_stream].trib[i]].
- accum;
+ s_streams[s_streams[cur_stream].
+ trib[i]].accum;
up_stream = s_streams[cur_stream].trib[i];
}
}
} /* end determining up_stream */
if (up_stream) { /* at least one branch is not assigned */
-
+
if (s_streams[cur_stream].hack < 0) {
s_streams[cur_stream].hack = cur_hack;
- }
+ }
else {
cur_hack = s_streams[cur_stream].hack;
++cur_hack;
-
+
}
- cur_distance=s_streams[cur_stream].distance;
+ cur_distance = s_streams[cur_stream].distance;
cur_stream = up_stream;
stack[++top] = cur_stream;
-
- s_streams[cur_stream].distance=cur_distance+s_streams[cur_stream].length;
- s_streams[cur_stream].topo_dim=top;
-
-
+
+ s_streams[cur_stream].distance =
+ cur_distance + s_streams[cur_stream].length;
+ s_streams[cur_stream].topo_dim = top;
+
+
}
else { /* all asigned, go downstream */
cur_stream = stack[--top];
-
} /* end up_stream */
} /* end spring/node */
} while (cur_stream);
More information about the grass-commit
mailing list