[GRASS-SVN] r46127 - in grass-addons/grass7/raster/r.stream:
r.stream.basins r.stream.channel r.stream.distance
r.stream.order r.stream.segment r.stream.slope r.stream.snap
r.stream.stats
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Apr 28 08:12:21 EDT 2011
Author: mmetz
Date: 2011-04-28 05:12:21 -0700 (Thu, 28 Apr 2011)
New Revision: 46127
Modified:
grass-addons/grass7/raster/r.stream/r.stream.basins/basins_fill.c
grass-addons/grass7/raster/r.stream/r.stream.basins/basins_inputs.c
grass-addons/grass7/raster/r.stream/r.stream.basins/io.c
grass-addons/grass7/raster/r.stream/r.stream.basins/io.h
grass-addons/grass7/raster/r.stream/r.stream.basins/local_proto.h
grass-addons/grass7/raster/r.stream/r.stream.basins/main.c
grass-addons/grass7/raster/r.stream/r.stream.channel/io.c
grass-addons/grass7/raster/r.stream/r.stream.channel/io.h
grass-addons/grass7/raster/r.stream/r.stream.channel/local_proto.h
grass-addons/grass7/raster/r.stream/r.stream.channel/main.c
grass-addons/grass7/raster/r.stream/r.stream.channel/stream_topology.c
grass-addons/grass7/raster/r.stream/r.stream.channel/stream_write.c
grass-addons/grass7/raster/r.stream/r.stream.distance/distance_calc.c
grass-addons/grass7/raster/r.stream/r.stream.distance/distance_init.c
grass-addons/grass7/raster/r.stream/r.stream.distance/io.c
grass-addons/grass7/raster/r.stream/r.stream.distance/io.h
grass-addons/grass7/raster/r.stream/r.stream.distance/local_proto.h
grass-addons/grass7/raster/r.stream/r.stream.distance/main.c
grass-addons/grass7/raster/r.stream/r.stream.order/io.c
grass-addons/grass7/raster/r.stream/r.stream.order/io.h
grass-addons/grass7/raster/r.stream/r.stream.order/main.c
grass-addons/grass7/raster/r.stream/r.stream.order/stream_init.c
grass-addons/grass7/raster/r.stream/r.stream.order/stream_order.c
grass-addons/grass7/raster/r.stream/r.stream.order/stream_raster_close.c
grass-addons/grass7/raster/r.stream/r.stream.order/stream_topology.c
grass-addons/grass7/raster/r.stream/r.stream.order/stream_vector.c
grass-addons/grass7/raster/r.stream/r.stream.segment/io.c
grass-addons/grass7/raster/r.stream/r.stream.segment/io.h
grass-addons/grass7/raster/r.stream/r.stream.segment/local_proto.h
grass-addons/grass7/raster/r.stream/r.stream.segment/main.c
grass-addons/grass7/raster/r.stream/r.stream.segment/stream_segment.c
grass-addons/grass7/raster/r.stream/r.stream.segment/stream_topology.c
grass-addons/grass7/raster/r.stream/r.stream.segment/stream_vector.c
grass-addons/grass7/raster/r.stream/r.stream.slope/main.c
grass-addons/grass7/raster/r.stream/r.stream.snap/io.c
grass-addons/grass7/raster/r.stream/r.stream.snap/io.h
grass-addons/grass7/raster/r.stream/r.stream.snap/main.c
grass-addons/grass7/raster/r.stream/r.stream.snap/points_io.c
grass-addons/grass7/raster/r.stream/r.stream.snap/snap.c
grass-addons/grass7/raster/r.stream/r.stream.stats/io.c
grass-addons/grass7/raster/r.stream/r.stream.stats/io.h
grass-addons/grass7/raster/r.stream/r.stream.stats/local_proto.h
grass-addons/grass7/raster/r.stream/r.stream.stats/main.c
grass-addons/grass7/raster/r.stream/r.stream.stats/stats_calculate.c
grass-addons/grass7/raster/r.stream/r.stream.stats/stats_prepare.c
grass-addons/grass7/raster/r.stream/r.stream.stats/stats_print.c
Log:
code cleanup and bug fixes
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/basins_fill.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/basins_fill.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/basins_fill.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -42,7 +42,7 @@
head = -1;
}
fifo_count--;
-
+
return fifo_points[++head];
}
@@ -56,31 +56,31 @@
continue;
It is simple trick but allow gives module its real funcionality
-
+
Buffer is an correction wchich allow to delinate basins even if vector point
or coordinates do not lie exactly on stream. In that case a small one pixel buffer
is created. This is little risk functionality and must be used carefully.
*/
-int ram_add_outlets(CELL** basins, int outlets_num)
+int ram_add_outlets(CELL **basins, int outlets_num)
{
- int i;
+ int i;
- for (i = 0; i < outlets_num; ++i)
+ for (i = 0; i < outlets_num; ++i)
basins[outlets[i].r][outlets[i].c] = outlets[i].val;
- return 0;
+ return 0;
}
-int seg_add_outlets(SEGMENT* basins, int outlets_num)
+int seg_add_outlets(SEGMENT *basins, int outlets_num)
{
- int i;
- int* basins_cell;
-
- for (i = 0; i < outlets_num; ++i) {
- basins_cell=&outlets[i].val;
- segment_put(basins,basins_cell,outlets[i].r,outlets[i].c);
+ int i;
+ int *basins_cell;
+
+ for (i = 0; i < outlets_num; ++i) {
+ basins_cell = &outlets[i].val;
+ segment_put(basins, basins_cell, outlets[i].r, outlets[i].c);
}
return 0;
}
@@ -89,7 +89,7 @@
algorithm uses fifo queue for determining basins area.
*/
-int ram_fill_basins(OUTLET outlet, CELL** basins, CELL** dirs)
+int ram_fill_basins(OUTLET outlet, CELL **basins, CELL **dirs)
{
int next_r, next_c;
int r, c, val, i, j;
@@ -98,8 +98,8 @@
tail = 0;
head = -1;
fifo_count = 0;
- r=outlet.r;
- c=outlet.c;
+ r = outlet.r;
+ c = outlet.c;
val = outlet.val;
G_debug(1, "processing outlet at row %d col %d", r, c);
@@ -111,69 +111,70 @@
next_r = NR(i);
next_c = NC(i);
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue;
- j = DIAG(i);
+ j = DIAG(i);
- /* countributing cell, not yet assigned to a basin */
- if (dirs[next_r][next_c] == j && basins[next_r][next_c] == 0) {
- basins[next_r][next_c] = val;
- n_cell.r = next_r;
- n_cell.c = next_c;
- fifo_insert(n_cell);
- }
+ /* countributing cell, not yet assigned to a basin */
+ if (dirs[next_r][next_c] == j && basins[next_r][next_c] == 0) {
+ basins[next_r][next_c] = val;
+ n_cell.r = next_r;
+ n_cell.c = next_c;
+ fifo_insert(n_cell);
+ }
} /* end for i... */
n_cell = fifo_return_del();
r = n_cell.r;
c = n_cell.c;
- } /* end while */
+ } /* end while */
return 0;
}
-int seg_fill_basins(OUTLET outlet, SEGMENT* basins, SEGMENT* dirs)
+int seg_fill_basins(OUTLET outlet, SEGMENT *basins, SEGMENT *dirs)
{
int next_r, next_c;
int r, c, val, i, j;
POINT n_cell;
- int dirs_cell, basins_cell;
+ int dirs_cell, basins_cell;
+
tail = 0;
head = -1;
fifo_count = 0;
- r=outlet.r;
- c=outlet.c;
+ r = outlet.r;
+ c = outlet.c;
val = outlet.val;
G_debug(1, "processing outlet at row %d col %d", r, c);
- segment_put(basins,&val,r,c);
-
+ segment_put(basins, &val, r, c);
+
while (tail != head) {
for (i = 1; i < 9; i++) {
next_r = NR(i);
next_c = NC(i);
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue;
- j = DIAG(i);
+ j = DIAG(i);
- /* countributing cell, not yet assigned to a basin */
- segment_get(basins,&basins_cell,next_r,next_c);
- segment_get(dirs,&dirs_cell,next_r,next_c);
-
- if (dirs_cell == j && basins_cell == 0) {
- segment_put(basins,&val,next_r,next_c);
- n_cell.r = next_r;
- n_cell.c = next_c;
- fifo_insert(n_cell);
- }
+ /* countributing cell, not yet assigned to a basin */
+ segment_get(basins, &basins_cell, next_r, next_c);
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+
+ if (dirs_cell == j && basins_cell == 0) {
+ segment_put(basins, &val, next_r, next_c);
+ n_cell.r = next_r;
+ n_cell.c = next_c;
+ fifo_insert(n_cell);
+ }
} /* end for i... */
n_cell = fifo_return_del();
r = n_cell.r;
c = n_cell.c;
- } /* end while */
+ } /* end while */
return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/basins_inputs.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/basins_inputs.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/basins_inputs.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -2,41 +2,40 @@
int process_coors(char **answers)
{
- int n, outlets_num;
- int r, c;
- double X, Y;
- struct Cell_head window;
-
+ int n, outlets_num;
+ double X, Y;
+ struct Cell_head window;
+
if (!answers)
G_fatal_error(_("At least one pair of coordinates must pe send"));
-
- G_get_window(&window);
-
- for (n = 0, outlets_num = 0; answers[n] != NULL; n += 2, outlets_num++);
- outlets = (OUTLET *)G_malloc(outlets_num * sizeof(OUTLET));
+ G_get_window(&window);
- for (n = 0, outlets_num = 0; answers[n] != NULL; n += 2, outlets_num++) {
-
+ for (n = 0, outlets_num = 0; answers[n] != NULL; n += 2, outlets_num++) ;
+
+ outlets = (OUTLET *) G_malloc(outlets_num * sizeof(OUTLET));
+
+ for (n = 0, outlets_num = 0; answers[n] != NULL; n += 2, outlets_num++) {
+
if (!G_scan_easting(answers[n], &X, G_projection()))
G_fatal_error("Wrong coordinate <%s>", answers[n]);
-
+
if (!answers[n + 1])
G_fatal_error("Missing north coordinate for east %g", X);
-
+
if (!G_scan_northing(answers[n + 1], &Y, G_projection()))
G_fatal_error("Wrong coordinate <%s>", answers[n + 1]);
- if (X < window.west || X > window.east ||
- Y < window.south || Y > window.north)
- G_fatal_error("Coordinates outside window");
+ if (X < window.west || X > window.east ||
+ Y < window.south || Y > window.north)
+ G_fatal_error("Coordinates outside window");
outlets[outlets_num].r = (window.north - Y) / window.ns_res;
outlets[outlets_num].c = (X - window.west) / window.ew_res;
outlets[outlets_num].val = outlets_num + 1;
}
- return outlets_num;
+ return outlets_num;
}
int process_vector(char *in_point)
@@ -46,8 +45,7 @@
struct Map_info Map;
struct bound_box box;
int num_point = 0;
- int type, i,j, cat;
- int r,c;
+ int type, i, cat;
struct line_pnts *sites;
struct line_cats *cats;
@@ -65,18 +63,18 @@
Vect_region_box(&window, &box);
while ((type = Vect_read_next_line(&Map, sites, cats)) > -1) {
- if (type != GV_POINT)
- continue;
- if (Vect_point_in_box(sites->x[0], sites->y[0], sites->z[0], &box)) {
+ if (type != GV_POINT)
+ continue;
+ if (Vect_point_in_box(sites->x[0], sites->y[0], sites->z[0], &box)) {
num_point++;
- }
+ }
}
outlets = (OUTLET *) G_malloc(num_point * sizeof(OUTLET));
- for (j = 0; j < num_point; ++j) {
-
- type = Vect_read_line(&Map, sites, cats, j + 1);
+ Vect_rewind(&Map);
+ i = 0;
+ while ((type = Vect_read_next_line(&Map, sites, cats)) > -1) {
if (type != GV_POINT)
continue;
@@ -88,179 +86,184 @@
outlets[i].r = (int)Rast_northing_to_row(sites->y[0], &window);
outlets[i].c = (int)Rast_easting_to_col(sites->x[0], &window);
outlets[i].val = cat;
+ i++;
}
- i++;
- return num_point;
+
+ return num_point;
}
-int ram_process_streams(char** cat_list, CELL** streams, int number_of_streams, CELL** dirs, int lasts, int cats)
+int ram_process_streams(char **cat_list, CELL **streams,
+ int number_of_streams, CELL **dirs, int lasts,
+ int cats)
{
- int i, cat;
- int r, c, d; /* d: direction */
- int outlets_num=0, border_outlets_num=0;
- int next_stream = -1, cur_stream;
- int out_max = ncols + nrows;
-
- categories=NULL;
+ int i, cat;
+ int r, c, d; /* d: direction */
+ int outlets_num = 0;
+ int next_stream = -1, cur_stream;
+ int out_max = ncols + nrows;
- if (cat_list) { /* only if there are at least one category */
+ categories = NULL;
+
+ if (cat_list) { /* only if there are at least one category */
categories = G_malloc((number_of_streams) * sizeof(int));
- memset(categories,-1,(number_of_streams) * sizeof(int));
+ memset(categories, -1, (number_of_streams) * sizeof(int));
for (i = 0; cat_list[i] != NULL; ++i) {
- cat = atoi(cat_list[i]);
- if (cat < 1 || cat > number_of_streams)
- G_fatal_error(_
- ("Stream categories must be > 0 and < maximum stream category"));
- categories[cat] = cat;
+ cat = atoi(cat_list[i]);
+ if (cat < 1 || cat > number_of_streams)
+ G_fatal_error(_("Stream categories must be > 0 and < maximum stream category"));
+ categories[cat] = cat;
}
- }
+ }
- G_message("Finding nodes...");
- outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
+ G_message("Finding nodes...");
+ outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c]>0) {
- if (outlets_num > 6 * (out_max - 1))
- G_fatal_error(_
- ("Stream and direction maps probably do not match"));
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c] > 0) {
+ if (outlets_num > 6 * (out_max - 1))
+ G_fatal_error(_("Stream and direction maps probably do not match"));
- if (outlets_num > (out_max - 1))
- outlets = (OUTLET *)G_realloc(outlets,out_max*6*sizeof(OUTLET));
+ if (outlets_num > (out_max - 1))
+ outlets =
+ (OUTLET *) G_realloc(outlets,
+ out_max * 6 * sizeof(OUTLET));
d = abs(dirs[r][c]); /* r.watershed */
- if (NOT_IN_REGION(d))
- next_stream = -1; /* border */
- else
- next_stream = (streams[NR(d)][NC(d)]>0) ?
- streams[NR(d)][NC(d)]: -1;
+ if (NOT_IN_REGION(d))
+ next_stream = -1; /* border */
+ else
+ next_stream = (streams[NR(d)][NC(d)] > 0) ?
+ streams[NR(d)][NC(d)] : -1;
- if (d == 0)
- next_stream = -1;
+ if (d == 0)
+ next_stream = -1;
cur_stream = streams[r][c];
- if (lasts) {
- if (next_stream < 0) { /* is outlet! */
+ if (lasts) {
+ if (next_stream < 0) { /* is outlet! */
if (categories)
- if (categories[cur_stream] == -1) /* but not in list */
- continue;
+ if (categories[cur_stream] == -1) /* but not in list */
+ continue;
- outlets[outlets_num].r = r;
- outlets[outlets_num].c = c;
- outlets[outlets_num].val =
- (cats) ? outlets_num + 1 : streams[r][c];
- outlets_num++;
+ outlets[outlets_num].r = r;
+ outlets[outlets_num].c = c;
+ outlets[outlets_num].val =
+ (cats) ? outlets_num + 1 : streams[r][c];
+ outlets_num++;
}
- } else { /* not lasts */
+ }
+ else { /* not lasts */
- if (cur_stream != next_stream) { /* is node or outlet! */
+ if (cur_stream != next_stream) { /* is node or outlet! */
if (categories)
- if (categories[cur_stream] == -1) /* but not in list */
- continue;
-
- outlets[outlets_num].r = r;
- outlets[outlets_num].c = c;
- outlets[outlets_num].val =
- (cats) ? outlets_num + 1 : streams[r][c];
- outlets_num++;
+ if (categories[cur_stream] == -1) /* but not in list */
+ continue;
+
+ outlets[outlets_num].r = r;
+ outlets[outlets_num].c = c;
+ outlets[outlets_num].val =
+ (cats) ? outlets_num + 1 : streams[r][c];
+ outlets_num++;
}
- } /* end if else lasts */
+ } /* end if else lasts */
} /* end if streams */
return outlets_num;
}
-int seg_process_streams(char **cat_list, SEGMENT* streams, int number_of_streams, SEGMENT* dirs, int lasts, int cats)
+int seg_process_streams(char **cat_list, SEGMENT *streams,
+ int number_of_streams, SEGMENT *dirs, int lasts,
+ int cats)
{
- int i, cat;
- int r, c, d; /* d: direction */
- int outlets_num;
- int next_stream = -1, cur_stream;
- int streams_cell, dirs_cell,streams_next_cell;
- int out_max = ncols + nrows;
+ int i, cat;
+ int r, c, d; /* d: direction */
+ int outlets_num;
+ int next_stream = -1, cur_stream;
+ int streams_cell, dirs_cell, streams_next_cell;
+ int out_max = ncols + nrows;
- categories=NULL;
+ categories = NULL;
- if (cat_list) {
+ if (cat_list) {
categories = G_malloc((number_of_streams) * sizeof(int));
- memset(categories,-1,(number_of_streams) * sizeof(int));
+ memset(categories, -1, (number_of_streams) * sizeof(int));
for (i = 0; cat_list[i] != NULL; ++i) {
- cat = atoi(cat_list[i]);
- if (cat < 1 || cat > number_of_streams)
- G_fatal_error(_
- ("Stream categories must be > 0 and < maximum stream category"));
- categories[cat] = cat;
- }
- }
- G_message("Finding nodes...");
- outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
- outlets_num = 0;
+ cat = atoi(cat_list[i]);
+ if (cat < 1 || cat > number_of_streams)
+ G_fatal_error(_("Stream categories must be > 0 and < maximum stream category"));
+ categories[cat] = cat;
+ }
+ }
+ G_message("Finding nodes...");
+ outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
+ outlets_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- if (streams_cell>0) {
- if (outlets_num > 6 * (out_max - 1))
- G_fatal_error
- ("Stream and direction maps probably do not match");
+ segment_get(streams, &streams_cell, r, c);
+ if (streams_cell > 0) {
+ if (outlets_num > 6 * (out_max - 1))
+ G_fatal_error
+ ("Stream and direction maps probably do not match");
- if (outlets_num > (out_max - 1))
- outlets = (OUTLET *)G_realloc(outlets,out_max*6*sizeof(OUTLET));
+ if (outlets_num > (out_max - 1))
+ outlets =
+ (OUTLET *) G_realloc(outlets,
+ out_max * 6 * sizeof(OUTLET));
- segment_get(dirs,&dirs_cell,r,c);
+ segment_get(dirs, &dirs_cell, r, c);
d = abs(dirs_cell); /* abs */
- if (NOT_IN_REGION(d))
- next_stream = -1; /* border */
- else {
- segment_get(streams,&streams_next_cell,NR(d),NC(d));
- next_stream = (streams_next_cell > 0) ? streams_next_cell : -1;
- }
+ if (NOT_IN_REGION(d))
+ next_stream = -1; /* border */
+ else {
+ segment_get(streams, &streams_next_cell, NR(d), NC(d));
+ next_stream =
+ (streams_next_cell > 0) ? streams_next_cell : -1;
+ }
- if (d == 0)
- next_stream = -1;
+ if (d == 0)
+ next_stream = -1;
cur_stream = streams_cell;
- if (lasts) {
+ if (lasts) {
- if (next_stream < 0) { /* is outlet! */
- if (categories)
- if (categories[cur_stream] == -1) /* but not in list */
- continue;
+ if (next_stream < 0) { /* is outlet! */
+ if (categories)
+ if (categories[cur_stream] == -1) /* but not in list */
+ continue;
- outlets[outlets_num].r = r;
- outlets[outlets_num].c = c;
- outlets[outlets_num].val =
- (cats) ? outlets_num + 1 : cur_stream;
- outlets_num++;
+ outlets[outlets_num].r = r;
+ outlets[outlets_num].c = c;
+ outlets[outlets_num].val =
+ (cats) ? outlets_num + 1 : cur_stream;
+ outlets_num++;
}
- } else { /* not lasts */
+ }
+ else { /* not lasts */
- if (cur_stream != next_stream) { /* is outlet or node! */
- if (categories)
- if (categories[cur_stream] == -1) /* but not in list */
- continue;
-
- outlets[outlets_num].r = r;
- outlets[outlets_num].c = c;
- outlets[outlets_num].val =
- (cats) ? outlets_num + 1 : cur_stream;
- outlets_num++;
- }
- } /* end if else lasts */
- } /* end if streams */
- } /* end for */
+ if (cur_stream != next_stream) { /* is outlet or node! */
+ if (categories)
+ if (categories[cur_stream] == -1) /* but not in list */
+ continue;
+
+ outlets[outlets_num].r = r;
+ outlets[outlets_num].c = c;
+ outlets[outlets_num].val =
+ (cats) ? outlets_num + 1 : cur_stream;
+ outlets_num++;
+ }
+ } /* end if else lasts */
+ } /* end if streams */
+ } /* end for */
return outlets_num;
}
-
-
-
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,523 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- /*
- * set all cells in the map to value
- */
- int r;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
- for (r=0;r<map->nrows;++r)
- memset((map->map)[r],value,map->ncols*map->data_size);
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
-
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
Rast_put_row(output_fd, (map->map)[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
@@ -7,20 +10,21 @@
#include <grass/raster.h>
#include <grass/segment.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(r,c) (r)*ncols+(c)
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4));
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
+#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -28,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -39,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/local_proto.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/local_proto.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,17 @@
#include "io.h"
#include "local_vars.h"
+int process_coors(char **answers);
+int process_vector(char *in_point);
+
+int ram_fill_basins(OUTLET outlet, CELL **basins, CELL **dirs);
+int ram_add_outlets(CELL **basins, int outlets_num);
+int ram_process_streams(char **cat_list, CELL **streams,
+ int number_of_streams, CELL **dirs, int lasts,
+ int cats);
+
+int seg_fill_basins(OUTLET outlet, SEGMENT *basins, SEGMENT *dirs);
+int seg_add_outlets(SEGMENT *basins, int outlets_num);
+int seg_process_streams(char **cat_list, SEGMENT *streams,
+ int number_of_streams, SEGMENT *dirs, int lasts,
+ int cats);
Modified: grass-addons/grass7/raster/r.stream/r.stream.basins/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.basins/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.basins/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,4 @@
+
/****************************************************************************
*
* MODULE: r.stream.basins
@@ -27,225 +28,226 @@
int main(int argc, char *argv[])
{
-
- struct GModule *module;
- struct Option *in_dir_opt,
- *in_coor_opt,
- *in_stm_opt,
- *in_stm_cat_opt,
- *in_point_opt,
- *opt_basins,
- *opt_swapsize;
- struct Flag *flag_zerofill,
- *flag_cats,
- *flag_lasts,
- *flag_segmentation;
-
- int b_test = 0; /* test which option have been choosed: like chmod */
- int segmentation, zerofill, lasts, cats;
- int i, outlets_num;
- int max_number_of_streams;
-
- G_gisinit(argv[0]);
+ struct GModule *module;
+ struct Option *in_dir_opt,
+ *in_coor_opt,
+ *in_stm_opt,
+ *in_stm_cat_opt, *in_point_opt, *opt_basins, *opt_swapsize;
- module = G_define_module();
- module->description = _("Delineate basins according user' input. \
+ struct Flag *flag_zerofill, *flag_cats, *flag_lasts, *flag_segmentation;
+
+ int b_test = 0; /* test which option have been choosed: like chmod */
+ int segmentation, zerofill, lasts, cats;
+ int i, outlets_num = 0;
+ int max_number_of_streams;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->description = _("Delineate basins according user' input. \
Input can be stream network, point file with outlets or outlet coordinates");
- G_add_keyword("basins creation");
-
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT); /* input directon file */
- in_dir_opt->key = "dirs";
- in_dir_opt->description = _("Name of flow direction input map");
-
- in_coor_opt = G_define_option(); /* input coordinates de outlet */
- in_coor_opt->key = "coors";
- in_coor_opt->type = TYPE_STRING;
- in_coor_opt->key_desc = "x,y";
- in_coor_opt->answers = NULL;
- in_coor_opt->required = NO;
- in_coor_opt->multiple = YES;
- in_coor_opt->description = _("Basin's outlet's coordinates: E,N");
-
- in_stm_opt = G_define_standard_option(G_OPT_R_INPUT); /* input stream file file */
- in_stm_opt->key = "streams";
- in_stm_opt->required = NO;
- in_stm_opt->description = _("Name of stream mask input map");
-
- in_stm_cat_opt = G_define_option(); /* input stream category - optional */
- in_stm_cat_opt->key = "cats";
- in_stm_cat_opt->type = TYPE_STRING;
- in_stm_cat_opt->required = NO;
- in_stm_cat_opt->description = _("Create basins only for these categories:");
-
- in_point_opt = G_define_standard_option(G_OPT_V_INPUT); /* input point outputs - optional */
- in_point_opt->key = "points";
- in_point_opt->required = NO;
- in_point_opt->description = _("Name of vector points map");
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
-
- opt_basins = G_define_standard_option(G_OPT_R_OUTPUT);
- opt_basins->key = "basins";
- opt_basins->description = _("Output basin map");
-
- /*flags */
- flag_zerofill = G_define_flag();
- flag_zerofill->key = 'z';
- flag_zerofill->description = _("Create zero-value background instead of NULL");
+ G_add_keyword("basins creation");
- flag_cats = G_define_flag();
- flag_cats->key = 'c';
- flag_cats->description =
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT); /* input directon file */
+ in_dir_opt->key = "dirs";
+ in_dir_opt->description = _("Name of flow direction input map");
+
+ in_coor_opt = G_define_option(); /* input coordinates de outlet */
+ in_coor_opt->key = "coors";
+ in_coor_opt->type = TYPE_STRING;
+ in_coor_opt->key_desc = "x,y";
+ in_coor_opt->answers = NULL;
+ in_coor_opt->required = NO;
+ in_coor_opt->multiple = YES;
+ in_coor_opt->description = _("Basin's outlet's coordinates: E,N");
+
+ in_stm_opt = G_define_standard_option(G_OPT_R_INPUT); /* input stream file file */
+ in_stm_opt->key = "streams";
+ in_stm_opt->required = NO;
+ in_stm_opt->description = _("Name of stream mask input map");
+
+ in_stm_cat_opt = G_define_option(); /* input stream category - optional */
+ in_stm_cat_opt->key = "cats";
+ in_stm_cat_opt->type = TYPE_STRING;
+ in_stm_cat_opt->required = NO;
+ in_stm_cat_opt->description =
+ _("Create basins only for these categories:");
+
+ in_point_opt = G_define_standard_option(G_OPT_V_INPUT); /* input point outputs - optional */
+ in_point_opt->key = "points";
+ in_point_opt->required = NO;
+ in_point_opt->description = _("Name of vector points map");
+
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
+
+ opt_basins = G_define_standard_option(G_OPT_R_OUTPUT);
+ opt_basins->key = "basins";
+ opt_basins->description = _("Output basin map");
+
+ /*flags */
+ flag_zerofill = G_define_flag();
+ flag_zerofill->key = 'z';
+ flag_zerofill->description =
+ _("Create zero-value background instead of NULL");
+
+ flag_cats = G_define_flag();
+ flag_cats->key = 'c';
+ flag_cats->description =
_("Use unique category sequence instead of input streams");
- flag_lasts = G_define_flag();
- flag_lasts->key = 'l';
- flag_lasts->description = _("Create basins only for last stream links");
-
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
+ flag_lasts = G_define_flag();
+ flag_lasts->key = 'l';
+ flag_lasts->description = _("Create basins only for last stream links");
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
- zerofill = (flag_zerofill->answer == 0);
- cats = (flag_cats->answer != 0);
- lasts = (flag_lasts->answer != 0);
- segmentation=(flag_segmentation->answer != 0);
-
+ zerofill = (flag_zerofill->answer == 0);
+ cats = (flag_cats->answer != 0);
+ lasts = (flag_lasts->answer != 0);
+ segmentation = (flag_segmentation->answer != 0);
+
if (!in_coor_opt->answers && !in_stm_opt->answer && !in_point_opt->answer)
G_fatal_error(_("One basin's outlet definition is required"));
- if (in_stm_cat_opt->answers && !in_stm_opt->answer)
+ if (in_stm_cat_opt->answers && !in_stm_opt->answer)
G_fatal_error(_("If cats stream file is required"));
- if (in_coor_opt->answers)
+ if (in_coor_opt->answers)
b_test += 1;
if (in_stm_opt->answer)
b_test += 2;
if (in_point_opt->answer)
b_test += 4;
- if (b_test != 1 && b_test != 2 && b_test != 4)
+ if (b_test != 1 && b_test != 2 && b_test != 4)
G_fatal_error("Only one outlet definition is allowed");
-
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
-
- if (G_legal_filename(opt_basins->answer) < 0)
+
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+
+ if (G_legal_filename(opt_basins->answer) < 0)
G_fatal_error(_("<%s> is an illegal basin name"), opt_basins->answer);
-
-
- /* ALL IN RAM VERSION */
-if (!segmentation) {
+
+
+ /* ALL IN RAM VERSION */
+ if (!segmentation) {
MAP map_dirs, map_streams, map_basins;
- CELL **streams=NULL, **dirs, **basins;
-
+ CELL **streams = NULL, **dirs, **basins;
+
G_message("ALL IN RAM CALCULATION");
-
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- dirs=(CELL**)map_dirs.map;
-
- switch (b_test) {
- case 1:
- G_message("Calculate basins using coordinates...");
- outlets_num = process_coors(in_coor_opt->answers);
- break;
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ dirs = (CELL **) map_dirs.map;
- case 2:
- G_message("Calculate basins using streams...");
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- streams=(CELL**)map_streams.map;
- max_number_of_streams=(int)map_streams.max+1;
- outlets_num=ram_process_streams(in_stm_cat_opt->answers,
- streams, max_number_of_streams, dirs,lasts,cats);
- ram_release_map (&map_streams);
- break;
- case 4:
- G_message("Calculate basins using point file...");
- outlets_num = process_vector(in_point_opt->answer);
- break;
- }
+ switch (b_test) {
+ case 1:
+ G_message("Calculate basins using coordinates...");
+ outlets_num = process_coors(in_coor_opt->answers);
+ break;
- ram_create_map(&map_basins,CELL_TYPE);
- basins=(CELL**)map_basins.map;
- ram_add_outlets(basins, outlets_num);
+ case 2:
+ G_message("Calculate basins using streams...");
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ streams = (CELL **) map_streams.map;
+ max_number_of_streams = (int)map_streams.max + 1;
+ outlets_num = ram_process_streams(in_stm_cat_opt->answers,
+ streams, max_number_of_streams,
+ dirs, lasts, cats);
+ ram_release_map(&map_streams);
+ break;
+
+ case 4:
+ G_message("Calculate basins using point file...");
+ outlets_num = process_vector(in_point_opt->answer);
+ break;
+ }
+
+ ram_create_map(&map_basins, CELL_TYPE);
+ basins = (CELL **) map_basins.map;
+ ram_add_outlets(basins, outlets_num);
fifo_max = 4 * (nrows + ncols);
fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
- for (i = 0; i < outlets_num; ++i)
- ram_fill_basins(outlets[i],basins,dirs);
-
+ for (i = 0; i < outlets_num; ++i)
+ ram_fill_basins(outlets[i], basins, dirs);
+
G_free(fifo_points);
- ram_write_map (&map_basins, opt_basins->answer, CELL_TYPE, zerofill, 0);
- ram_release_map (&map_dirs);
- ram_release_map (&map_basins);
-
-} /* end ram */
+ ram_write_map(&map_basins, opt_basins->answer, CELL_TYPE, zerofill,
+ 0);
+ ram_release_map(&map_dirs);
+ ram_release_map(&map_basins);
- /* SEGMENT VERSION */
+ } /* end ram */
-if (segmentation) {
+ /* SEGMENT VERSION */
+
+ if (segmentation) {
SEG map_dirs, map_streams, map_basins;
- SEGMENT *streams=NULL, *dirs, *basins;
+ SEGMENT *streams = NULL, *dirs, *basins;
int number_of_segs;
G_message("MEMORY SWAP CALCULATION: MAY TAKE SOME TIME!");
-
+
number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32/0.12) : number_of_segs/0.12;
-
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
+
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
dirs = &map_dirs.seg;
-
+
switch (b_test) {
- case 1:
- G_message("Calculate basins using coordinates...");
- outlets_num = process_coors(in_coor_opt->answers);
- break;
+ case 1:
+ G_message("Calculate basins using coordinates...");
+ outlets_num = process_coors(in_coor_opt->answers);
+ break;
- case 2:
- G_message("Calculate basins using streams...");
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- streams = &map_streams.seg;
- max_number_of_streams=(int)map_streams.max+1;
- outlets_num=seg_process_streams(in_stm_cat_opt->answers,
- streams,max_number_of_streams,dirs,lasts,cats);
- seg_release_map (&map_streams);
- break;
+ case 2:
+ G_message("Calculate basins using streams...");
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs,
+ CELL_TYPE);
+ seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ streams = &map_streams.seg;
+ max_number_of_streams = (int)map_streams.max + 1;
+ outlets_num = seg_process_streams(in_stm_cat_opt->answers,
+ streams, max_number_of_streams,
+ dirs, lasts, cats);
+ seg_release_map(&map_streams);
+ break;
- case 4:
- G_message("Calculate basins using point file...");
- outlets_num = process_vector(in_point_opt->answer);
- break;
- }
-
+ case 4:
+ G_message("Calculate basins using point file...");
+ outlets_num = process_vector(in_point_opt->answer);
+ break;
+ }
+
seg_create_map(&map_basins, SROWS, SCOLS, number_of_segs, CELL_TYPE);
- basins=&map_basins.seg,
- seg_add_outlets(basins, outlets_num);
+ basins = &map_basins.seg;
+ seg_add_outlets(basins, outlets_num);
fifo_max = 4 * (nrows + ncols);
fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
- for (i = 0; i < outlets_num; ++i)
- seg_fill_basins(outlets[i],basins, dirs);
+ for (i = 0; i < outlets_num; ++i)
+ seg_fill_basins(outlets[i], basins, dirs);
G_free(fifo_points);
- seg_write_map (&map_basins, opt_basins->answer, CELL_TYPE, zerofill, 0);
- seg_release_map (&map_dirs);
- seg_release_map (&map_basins);
+ seg_write_map(&map_basins, opt_basins->answer, CELL_TYPE, zerofill,
+ 0);
+ seg_release_map(&map_dirs);
+ seg_release_map(&map_basins);
+ }
+
+ exit(EXIT_SUCCESS);
}
-
-exit(EXIT_SUCCESS);
- }
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,516 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- return 0;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
+
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
- Rast_put_row(output_fd, map->map[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
+ Rast_put_row(output_fd, (map->map)[r], output_data_type);
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,28 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/segment.h>
-#include <grass/vector.h>
-#include <grass/dbmi.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(x,y) (x)*ncols+(y)
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -30,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -41,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/local_proto.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/local_proto.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,57 @@
#include "io.h"
#include "local_vars.h"
+int free_attributes(int number_of_streams);
+
+
+int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
+ int number_of_streams);
+int ram_number_of_streams(CELL **streams, CELL **dirs);
+int ram_find_contributing_cell(int r, int c, CELL **dirs, FCELL **elevation);
+
+int ram_set_null_output(DCELL **output);
+int ram_calculate_identifiers(CELL **identifier, int number_of_streams,
+ int downstream);
+int ram_calculate_difference(DCELL **output, int number_of_streams,
+ int downstream);
+int ram_calculate_drop(DCELL **output, int number_of_streams, int downstream);
+int ram_calculate_local_distance(DCELL **output, int number_of_streams,
+ int downstream);
+int ram_calculate_distance(DCELL **output, int number_of_streams,
+ int downstream);
+int ram_calculate_cell(DCELL **output, int number_of_streams, int downstream);
+int ram_calculate_local_gradient(DCELL **output, int number_of_streams,
+ int downstream);
+int ram_calculate_gradient(DCELL **output, int number_of_streams,
+ int downstream);
+int ram_calculate_curvature(DCELL **output, int number_of_streams,
+ int downstream);
+
+
+
+int seg_build_streamlines(SEGMENT *streams, SEGMENT *dirs,
+ SEGMENT *elevation, int number_of_streams);
+int seg_number_of_streams(SEGMENT *streams, SEGMENT *dirs);
+
+int seg_find_contributing_cell(int r, int c, SEGMENT *dirs,
+ SEGMENT *elevation);
+
+int seg_set_null_output(SEGMENT *output);
+int seg_calculate_identifiers(SEGMENT *identifier, int number_of_streams,
+ int downstream);
+int seg_calculate_difference(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_drop(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_local_distance(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_distance(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_cell(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_local_gradient(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_gradient(SEGMENT *output, int number_of_streams,
+ int downstream);
+int seg_calculate_curvature(SEGMENT *output, int number_of_streams,
+ int downstream);
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -20,304 +20,321 @@
#include <grass/glocale.h>
#include "local_proto.h"
- 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 nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int main(int argc, char *argv[])
{
- /*
- IO output[] = {
- {"local_diff",NO,"Local elevation difference"},
- {"out_dist",NO,"Upstream distance form init"},
- */
+ /*
+ IO output[] = {
+ {"local_diff",NO,"Local elevation difference"},
+ {"out_dist",NO,"Upstream distance form init"},
+ */
- struct GModule *module;
- struct Option *in_dir_opt, /* options */
- *in_stm_opt,
- *in_elev_opt,
- *out_identifier_opt,
- *out_distance_opt,
- *out_difference_opt,
- *out_gradient_opt,
- *out_curvature_opt,
- *opt_swapsize;
-
- struct Flag *flag_segmentation,
- *flag_local,
- *flag_cells,
- *flag_downstream;
+ struct GModule *module;
+ struct Option *in_dir_opt, /* options */
+ *in_stm_opt,
+ *in_elev_opt,
+ *out_identifier_opt,
+ *out_distance_opt,
+ *out_difference_opt,
+ *out_gradient_opt, *out_curvature_opt, *opt_swapsize;
- char *method_name[] = { "UPSTREAM", "DOWNSTREAM" };
- int number_of_segs;
- int number_of_streams;
- int outlets_num;
- int order_max;
- int segmentation, downstream, local, cells; /*flags */
+ struct Flag *flag_segmentation,
+ *flag_local, *flag_cells, *flag_downstream;
+ char *method_name[] = { "UPSTREAM", "DOWNSTREAM" };
+ int number_of_segs;
+ int number_of_streams;
+ int segmentation, downstream, local, cells; /*flags */
+
/* initialize GIS environment */
- G_gisinit(argv[0]);
+ G_gisinit(argv[0]);
/* initialize module */
- module = G_define_module();
- G_add_keyword("Horton statisctics");
- module->description =
+ module = G_define_module();
+ G_add_keyword("Horton statisctics");
+ module->description =
_("Calculate local parameters for individual streams");
- G_add_keyword("Stream parameters");
- G_add_keyword("Stream gradient");
- G_add_keyword("Stream curvature");
+ G_add_keyword("Stream parameters");
+ G_add_keyword("Stream gradient");
+ G_add_keyword("Stream curvature");
- in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_stm_opt->key = "streams";
- in_stm_opt->description = "Name of streams mask input map";
+ in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_stm_opt->key = "streams";
+ in_stm_opt->description = "Name of streams mask input map";
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_dir_opt->key = "dirs";
- in_dir_opt->description = "Name of flow direction input map";
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_dir_opt->key = "dirs";
+ in_dir_opt->description = "Name of flow direction input map";
- in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_elev_opt->key = "elevation";
- in_elev_opt->description = "Name of elevation map";
-
- out_identifier_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_identifier_opt->key = "identifier";
- out_identifier_opt->required = NO;
- out_identifier_opt->description = "Unique identifier for stream";
-
- out_distance_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_distance_opt->key = "distance";
- out_distance_opt->required = NO;
- out_distance_opt->description = "Distance from init/join/outlet";
+ in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_elev_opt->key = "elevation";
+ in_elev_opt->description = "Name of elevation map";
- out_difference_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_difference_opt->key = "difference";
- out_difference_opt->required = NO;
- out_difference_opt->description = "Elevation differecne from init/join/outlet";
+ out_identifier_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_identifier_opt->key = "identifier";
+ out_identifier_opt->required = NO;
+ out_identifier_opt->description = "Unique identifier for stream";
- out_gradient_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_gradient_opt->key = "gradient";
- out_gradient_opt->required = NO;
- out_gradient_opt->description = "Mean gradient of stream from init/join/outlet";
-
- out_curvature_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_curvature_opt->key = "curvature";
- out_curvature_opt->required = NO;
- out_curvature_opt->description = "Local stream curvature";
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
+ out_distance_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_distance_opt->key = "distance";
+ out_distance_opt->required = NO;
+ out_distance_opt->description = "Distance from init/join/outlet";
- flag_downstream = G_define_flag();
- flag_downstream->key = 'd';
- flag_downstream->description = _("Calculate parameters from outlet (downstream values)");
-
- flag_local = G_define_flag();
- flag_local->key = 'l';
- flag_local->description = _("Calculate local values (for current cell)");
-
- flag_cells = G_define_flag();
- flag_cells->key = 'c';
- flag_cells->description = _("Calculate distance in cell (ignored local)");
+ out_difference_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_difference_opt->key = "difference";
+ out_difference_opt->required = NO;
+ out_difference_opt->description =
+ "Elevation differecne from init/join/outlet";
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
-
+ out_gradient_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_gradient_opt->key = "gradient";
+ out_gradient_opt->required = NO;
+ out_gradient_opt->description =
+ "Mean gradient of stream from init/join/outlet";
+
+ out_curvature_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_curvature_opt->key = "curvature";
+ out_curvature_opt->required = NO;
+ out_curvature_opt->description = "Local stream curvature";
+
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
+
+ flag_downstream = G_define_flag();
+ flag_downstream->key = 'd';
+ flag_downstream->description =
+ _("Calculate parameters from outlet (downstream values)");
+
+ flag_local = G_define_flag();
+ flag_local->key = 'l';
+ flag_local->description = _("Calculate local values (for current cell)");
+
+ flag_cells = G_define_flag();
+ flag_cells->key = 'c';
+ flag_cells->description = _("Calculate distance in cell (ignored local)");
+
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
- segmentation = (flag_segmentation->answer != 0);
- downstream = (flag_downstream->answer != 0);
+ segmentation = (flag_segmentation->answer != 0);
+ downstream = (flag_downstream->answer != 0);
- if (!out_identifier_opt->answer &&
- !out_distance_opt->answer &&
- !out_difference_opt->answer &&
- !out_gradient_opt->answer &&
- !out_curvature_opt->answer)
+ if (!out_identifier_opt->answer &&
+ !out_distance_opt->answer &&
+ !out_difference_opt->answer &&
+ !out_gradient_opt->answer && !out_curvature_opt->answer)
G_fatal_error(_("You must select at least one output maps"));
-
- local = (flag_local->answer != 0);
- cells = (flag_cells->answer != 0);
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
- G_get_window(&window);
- G_begin_distance_calculations();
- if(out_identifier_opt->answer)
- if (G_legal_filename(out_identifier_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal difference map name"),
- out_identifier_opt->answer);
+ local = (flag_local->answer != 0);
+ cells = (flag_cells->answer != 0);
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+ G_get_window(&window);
+ G_begin_distance_calculations();
- if(out_difference_opt->answer)
- if (G_legal_filename(out_difference_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal difference map name"),
- out_difference_opt->answer);
-
- if(out_distance_opt->answer)
- if (G_legal_filename(out_distance_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal distance map name"),
- out_distance_opt->answer);
-
- if(out_gradient_opt->answer)
- if (G_legal_filename(out_gradient_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal gradient map name"),
- out_gradient_opt->answer);
-
- if(out_curvature_opt->answer )
- if (G_legal_filename(out_curvature_opt->answer ) < 0)
- G_fatal_error(_("<%s> is an illegal gradient map name"),
- out_curvature_opt->answer );
-
-if(!segmentation) {
+ if (out_identifier_opt->answer)
+ if (G_legal_filename(out_identifier_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal difference map name"),
+ out_identifier_opt->answer);
+
+ if (out_difference_opt->answer)
+ if (G_legal_filename(out_difference_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal difference map name"),
+ out_difference_opt->answer);
+
+ if (out_distance_opt->answer)
+ if (G_legal_filename(out_distance_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal distance map name"),
+ out_distance_opt->answer);
+
+ if (out_gradient_opt->answer)
+ if (G_legal_filename(out_gradient_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal gradient map name"),
+ out_gradient_opt->answer);
+
+ if (out_curvature_opt->answer)
+ if (G_legal_filename(out_curvature_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal gradient map name"),
+ out_curvature_opt->answer);
+
+ if (!segmentation) {
MAP map_dirs, map_streams, map_elevation, map_output, map_identifier;
- CELL **streams, **dirs, **identifier=NULL;
+ CELL **streams, **dirs, **identifier = NULL;
FCELL **elevation;
DCELL **output;
- G_message(_("ALL IN RAM CALCULATION - DIRECTION: %s"),method_name[downstream]);
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_elevation,FCELL_TYPE);
- ram_read_map(&map_elevation,in_elev_opt->answer,0,-1);
-
- streams=(CELL**)map_streams.map;
- dirs=(CELL**)map_dirs.map;
- elevation=(FCELL**)map_elevation.map;
-
- number_of_streams=ram_number_of_streams(streams, dirs)+1;
- ram_build_streamlines(streams,dirs,elevation,number_of_streams);
+ G_message(_("ALL IN RAM CALCULATION - DIRECTION: %s"),
+ method_name[downstream]);
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_elevation, FCELL_TYPE);
+ ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+
+ streams = (CELL **) map_streams.map;
+ dirs = (CELL **) map_dirs.map;
+ elevation = (FCELL **) map_elevation.map;
+
+ number_of_streams = ram_number_of_streams(streams, dirs) + 1;
+ ram_build_streamlines(streams, dirs, elevation, number_of_streams);
ram_release_map(&map_streams);
ram_release_map(&map_dirs);
- ram_create_map(&map_output,DCELL_TYPE);
- output=(DCELL**)map_output.map; /* one output for all maps */
-
- if(out_identifier_opt->answer) {
- ram_create_map(&map_identifier,CELL_TYPE);
- identifier=(CELL**)map_identifier.map;
- ram_calculate_identifiers(identifier,number_of_streams,downstream);
- ram_write_map(&map_identifier,out_identifier_opt->answer,CELL_TYPE,1,0);
- ram_release_map(&map_identifier);
- }
-
- if(out_difference_opt->answer) {
- ram_set_null_output(output);
- if (local)
- ram_calculate_difference(output,number_of_streams,downstream);
- else
- ram_calculate_drop(output,number_of_streams,downstream);
- ram_write_map(&map_output,out_difference_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_distance_opt->answer) {
- ram_set_null_output(output);
- if(local && !cells)
- ram_calculate_local_distance(output,number_of_streams,downstream);
- if (!local && !cells)
- ram_calculate_distance(output,number_of_streams,downstream);
- if(cells)
- ram_calculate_cell(output,number_of_streams,downstream);
- ram_write_map(&map_output,out_distance_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_gradient_opt->answer) {
- ram_set_null_output(output);
- if(local)
- ram_calculate_local_gradient(output,number_of_streams,downstream);
- else
- ram_calculate_gradient(output,number_of_streams,downstream);
- ram_write_map(&map_output,out_gradient_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_curvature_opt->answer) {
- ram_set_null_output(output);
- ram_calculate_curvature(output,number_of_streams,downstream);
- ram_write_map(&map_output,out_curvature_opt->answer,DCELL_TYPE,0,0);
- }
-
+ ram_create_map(&map_output, DCELL_TYPE);
+ output = (DCELL **) map_output.map; /* one output for all maps */
+
+ if (out_identifier_opt->answer) {
+ ram_create_map(&map_identifier, CELL_TYPE);
+ identifier = (CELL **) map_identifier.map;
+ ram_calculate_identifiers(identifier, number_of_streams,
+ downstream);
+ ram_write_map(&map_identifier, out_identifier_opt->answer,
+ CELL_TYPE, 1, 0);
+ ram_release_map(&map_identifier);
+ }
+
+ if (out_difference_opt->answer) {
+ ram_set_null_output(output);
+ if (local)
+ ram_calculate_difference(output, number_of_streams,
+ downstream);
+ else
+ ram_calculate_drop(output, number_of_streams, downstream);
+ ram_write_map(&map_output, out_difference_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_distance_opt->answer) {
+ ram_set_null_output(output);
+ if (local && !cells)
+ ram_calculate_local_distance(output, number_of_streams,
+ downstream);
+ if (!local && !cells)
+ ram_calculate_distance(output, number_of_streams, downstream);
+ if (cells)
+ ram_calculate_cell(output, number_of_streams, downstream);
+ ram_write_map(&map_output, out_distance_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_gradient_opt->answer) {
+ ram_set_null_output(output);
+ if (local)
+ ram_calculate_local_gradient(output, number_of_streams,
+ downstream);
+ else
+ ram_calculate_gradient(output, number_of_streams, downstream);
+ ram_write_map(&map_output, out_gradient_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_curvature_opt->answer) {
+ ram_set_null_output(output);
+ ram_calculate_curvature(output, number_of_streams, downstream);
+ ram_write_map(&map_output, out_curvature_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
ram_release_map(&map_output);
-}
+ }
-if(segmentation) {
+ if (segmentation) {
SEG map_dirs, map_streams, map_elevation, map_output, map_identifier;
SEGMENT *streams, *dirs, *elevation, *output, *identifier;
-
+
G_message(_("SEGMENT CALCULATION: MAY TAKE SOME TIME- DIRECTION: %s"),
- method_name[downstream]);
-
+ method_name[downstream]);
+
number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32/0.18) : number_of_segs/0.18;
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs,CELL_TYPE);
- seg_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_elevation,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
- seg_read_map(&map_elevation,in_elev_opt->answer,0,-1);
-
- streams=&map_streams.seg;
- dirs=&map_dirs.seg;
- elevation=&map_elevation.seg;
-
- number_of_streams=seg_number_of_streams(streams, dirs)+1;
- seg_build_streamlines(streams,dirs,elevation,number_of_streams);
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
+ FCELL_TYPE);
+ seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+
+ streams = &map_streams.seg;
+ dirs = &map_dirs.seg;
+ elevation = &map_elevation.seg;
+
+ number_of_streams = seg_number_of_streams(streams, dirs) + 1;
+ seg_build_streamlines(streams, dirs, elevation, number_of_streams);
seg_release_map(&map_streams);
seg_release_map(&map_dirs);
- seg_create_map(&map_output,SROWS, SCOLS, number_of_segs, DCELL_TYPE);
- output=&map_output.seg; /* one output for all maps */
-
- if(out_identifier_opt->answer) {
- seg_create_map(&map_identifier,SROWS, SCOLS, number_of_segs,CELL_TYPE);
- identifier=&map_identifier.seg;
- seg_calculate_identifiers(identifier,number_of_streams,downstream);
- seg_write_map(&map_identifier,out_identifier_opt->answer,CELL_TYPE,1,0);
- seg_release_map(&map_identifier);
- }
+ seg_create_map(&map_output, SROWS, SCOLS, number_of_segs, DCELL_TYPE);
+ output = &map_output.seg; /* one output for all maps */
- if(out_difference_opt->answer) {
- seg_set_null_output(output);
- if (local)
- seg_calculate_difference(output,number_of_streams,downstream);
- else
- seg_calculate_drop(output,number_of_streams,downstream);
- seg_write_map(&map_output,out_difference_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_distance_opt->answer) {
- seg_set_null_output(output);
- if(local && !cells)
- seg_calculate_local_distance(output,number_of_streams,downstream);
- if (!local && !cells)
- seg_calculate_distance(output,number_of_streams,downstream);
- if(cells)
- seg_calculate_cell(output,number_of_streams,downstream);
- seg_write_map(&map_output,out_distance_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_gradient_opt->answer) {
- seg_set_null_output(output);
- if(local)
- seg_calculate_local_gradient(output,number_of_streams,downstream);
- else
- seg_calculate_gradient(output,number_of_streams,downstream);
- seg_write_map(&map_output,out_gradient_opt->answer,DCELL_TYPE,0,0);
- }
-
- if(out_curvature_opt->answer) {
- seg_set_null_output(output);
- seg_calculate_curvature(output,number_of_streams,downstream);
- seg_write_map(&map_output,out_curvature_opt->answer,DCELL_TYPE,0,0);
- }
+ if (out_identifier_opt->answer) {
+ seg_create_map(&map_identifier, SROWS, SCOLS, number_of_segs,
+ CELL_TYPE);
+ identifier = &map_identifier.seg;
+ seg_calculate_identifiers(identifier, number_of_streams,
+ downstream);
+ seg_write_map(&map_identifier, out_identifier_opt->answer,
+ CELL_TYPE, 1, 0);
+ seg_release_map(&map_identifier);
+ }
+ if (out_difference_opt->answer) {
+ seg_set_null_output(output);
+ if (local)
+ seg_calculate_difference(output, number_of_streams,
+ downstream);
+ else
+ seg_calculate_drop(output, number_of_streams, downstream);
+ seg_write_map(&map_output, out_difference_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_distance_opt->answer) {
+ seg_set_null_output(output);
+ if (local && !cells)
+ seg_calculate_local_distance(output, number_of_streams,
+ downstream);
+ if (!local && !cells)
+ seg_calculate_distance(output, number_of_streams, downstream);
+ if (cells)
+ seg_calculate_cell(output, number_of_streams, downstream);
+ seg_write_map(&map_output, out_distance_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_gradient_opt->answer) {
+ seg_set_null_output(output);
+ if (local)
+ seg_calculate_local_gradient(output, number_of_streams,
+ downstream);
+ else
+ seg_calculate_gradient(output, number_of_streams, downstream);
+ seg_write_map(&map_output, out_gradient_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
+ if (out_curvature_opt->answer) {
+ seg_set_null_output(output);
+ seg_calculate_curvature(output, number_of_streams, downstream);
+ seg_write_map(&map_output, out_curvature_opt->answer, DCELL_TYPE,
+ 0, 0);
+ }
+
seg_release_map(&map_output);
-}
- free_attributes(number_of_streams);
- exit(EXIT_SUCCESS);
+ }
+ free_attributes(number_of_streams);
+ exit(EXIT_SUCCESS);
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/stream_topology.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/stream_topology.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,53 +1,53 @@
#include "local_proto.h"
double get_distance(int r, int c, int d)
{
- double northing, easting, next_northing, next_easting;
- int next_r, next_c;
- next_r=NR(d);
- next_c=NC(d);
- northing = window.north - (r +.5) * window.ns_res;
- easting = window.west + (c +.5) * window.ew_res;
- next_northing = window.north - (next_r +.5) * window.ns_res;
- next_easting = window.west + (next_c +.5) * window.ew_res;
- return G_distance(easting,northing,next_easting,next_northing);
+ double northing, easting, next_northing, next_easting;
+ int next_r, next_c;
+
+ next_r = NR(d);
+ next_c = NC(d);
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (next_r + .5) * window.ns_res;
+ next_easting = window.west + (next_c + .5) * window.ew_res;
+ return G_distance(easting, northing, next_easting, next_northing);
}
-int ram_trib_nums(int r, int c, CELL** streams, CELL** dirs)
+int ram_trib_nums(int r, int c, CELL ** streams, CELL ** dirs)
{ /* calculate number of tributuaries */
- int trib_num = 0;
- int i, j;
- int next_r, next_c;
+ int trib_num = 0;
+ int i, j;
+ int next_r, next_c;
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
- if (streams[next_r][next_c] > 0 && dirs[next_r][next_c] == j)
- trib_num++;
- }
+ if (streams[next_r][next_c] > 0 && dirs[next_r][next_c] == j)
+ trib_num++;
+ }
- if (trib_num > 1)
+ if (trib_num > 1)
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
if (streams[next_r][next_c] == streams[r][c] &&
- dirs[next_r][next_c] == j)
+ dirs[next_r][next_c] == j)
trib_num--;
}
-
+
if (trib_num > 5)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
if (trib_num > 3)
G_warning(_("Stream network may be too dense..."));
@@ -56,366 +56,369 @@
-int ram_number_of_streams(CELL** streams, CELL** dirs)
+int ram_number_of_streams(CELL **streams, CELL **dirs)
{
- int r, c;
- int stream_num=0;
+ int r, c;
+ int stream_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c]>0)
- if (ram_trib_nums(r, c, streams, dirs) != 1)
- stream_num++;
- return stream_num;
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c] > 0)
+ if (ram_trib_nums(r, c, streams, dirs) != 1)
+ stream_num++;
+ return stream_num;
}
-int ram_build_streamlines(CELL** streams, CELL** dirs, FCELL** elevation, int number_of_streams)
+int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
+ int number_of_streams)
{
- int r, c, i;
- int d, next_d;
- int next_r, next_c;
- int prev_r, prev_c;
- int stream_num=1, cell_num=0;
- int contrib_cell;
- STREAM* SA;
- int tmp=0;
- stream_num = 1;
- int v;
- Rast_get_window(&window);
+ int r, c, i;
+ int d, next_d;
+ int prev_r, prev_c;
+ int stream_num = 1, cell_num = 0;
+ int contrib_cell;
+ STREAM *SA;
- stream_attributes = (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
- G_message("Finding inits...");
- SA=stream_attributes;
+ stream_num = 1;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c])
- if (ram_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
- if(stream_num>number_of_streams)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
-
- SA[stream_num].stream_num = stream_num;
- SA[stream_num].init_r = r;
- SA[stream_num++].init_c = c;
- }
-
- for(i=1;i<stream_num;++i) {
+ Rast_get_window(&window);
- r=SA[i].init_r;
- c=SA[i].init_c;
- SA[i].order=streams[r][c];
- SA[i].number_of_cells=0;
- do {
-
- SA[i].number_of_cells++;
- d=abs(dirs[r][c]);
- if(NOT_IN_REGION(d) || d==0)
- break;
- r=NR(d);
- c=NC(d);
- } while (streams[r][c]==SA[i].order);
-
- SA[i].number_of_cells+=2; /* add two extra points for init+ and outlet+ */
- }
+ stream_attributes =
+ (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
+ G_message("Finding inits...");
+ SA = stream_attributes;
- for(i=1;i<number_of_streams;++i) {
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c])
+ if (ram_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
+ if (stream_num > number_of_streams)
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
- SA[i].points = (unsigned long int *)
- G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
- SA[i].elevation = (float*)
- G_malloc((SA[i].number_of_cells) * sizeof(float));
- SA[i].distance = (double*)
- G_malloc((SA[i].number_of_cells) * sizeof(double));
-
- r=SA[i].init_r;
- c=SA[i].init_c;
- contrib_cell=ram_find_contributing_cell(r,c, dirs,elevation);
- prev_r=NR(contrib_cell);
- prev_c=NC(contrib_cell);
-
- /* add one point contributing to init to calculate parameters */
- /* what to do if there is no contributing points? */
- SA[i].points[0] = (contrib_cell==0) ? -1 :
- INDEX(prev_r,prev_c);
- SA[i].elevation[0]=(contrib_cell==0) ? -99999 :
- elevation[prev_r][prev_c];
- d=(contrib_cell==0) ? dirs[r][c] : dirs[prev_r][prev_c];
- SA[i].distance[0]=(contrib_cell==0) ? get_distance(r,c,d) :
- get_distance(prev_r,prev_c,d);
-
- SA[i].points[1] = INDEX(r,c);
- SA[i].elevation[1]=elevation[r][c];
- d=abs(dirs[r][c]);
- SA[i].distance[1]=get_distance(r,c,d);
+ SA[stream_num].stream_num = stream_num;
+ SA[stream_num].init_r = r;
+ SA[stream_num++].init_c = c;
+ }
- cell_num=2;
- do {
- d=abs(dirs[r][c]);
+ for (i = 1; i < stream_num; ++i) {
- if(NOT_IN_REGION(d) || d==0) {
+ r = SA[i].init_r;
+ c = SA[i].init_c;
+ SA[i].order = streams[r][c];
+ SA[i].number_of_cells = 0;
+ do {
+
+ SA[i].number_of_cells++;
+ d = abs(dirs[r][c]);
+ if (NOT_IN_REGION(d) || d == 0)
+ break;
+ r = NR(d);
+ c = NC(d);
+ } while (streams[r][c] == SA[i].order);
+
+ SA[i].number_of_cells += 2; /* add two extra points for init+ and outlet+ */
+ }
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ SA[i].points = (unsigned long int *)
+ G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+ SA[i].elevation = (float *)
+ G_malloc((SA[i].number_of_cells) * sizeof(float));
+ SA[i].distance = (double *)
+ G_malloc((SA[i].number_of_cells) * sizeof(double));
+
+ r = SA[i].init_r;
+ c = SA[i].init_c;
+ contrib_cell = ram_find_contributing_cell(r, c, dirs, elevation);
+ prev_r = NR(contrib_cell);
+ prev_c = NC(contrib_cell);
+
+ /* add one point contributing to init to calculate parameters */
+ /* what to do if there is no contributing points? */
+ SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
+ SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
+ elevation[prev_r][prev_c];
+ d = (contrib_cell == 0) ? dirs[r][c] : dirs[prev_r][prev_c];
+ SA[i].distance[0] = (contrib_cell == 0) ? get_distance(r, c, d) :
+ get_distance(prev_r, prev_c, d);
+
+ SA[i].points[1] = INDEX(r, c);
+ SA[i].elevation[1] = elevation[r][c];
+ d = abs(dirs[r][c]);
+ SA[i].distance[1] = get_distance(r, c, d);
+
+ cell_num = 2;
+ do {
+ d = abs(dirs[r][c]);
+
+ if (NOT_IN_REGION(d) || d == 0) {
SA[i].points[cell_num] = -1;
- SA[i].distance[cell_num] = SA[i].distance[cell_num-1];
- SA[i].elevation[cell_num] =
- 2*SA[i].elevation[cell_num-1]-SA[i].elevation[cell_num-2];
+ SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+ SA[i].elevation[cell_num] =
+ 2 * SA[i].elevation[cell_num - 1] -
+ SA[i].elevation[cell_num - 2];
break;
- }
- r=NR(d);
- c=NC(d);
- SA[i].points[cell_num] = INDEX(r,c);
- SA[i].elevation[cell_num] = elevation[r][c];
- next_d=(abs(dirs[r][c])==0) ? d : abs(dirs[r][c]);
- SA[i].distance[cell_num] = get_distance(r,c,next_d);
- cell_num++;
- if(cell_num>SA[i].number_of_cells)
- G_fatal_error(_("To much points in stream line..."));
- } while (streams[r][c]==SA[i].order);
-
- if(SA[i].elevation[0]==-99999)
- SA[i].elevation[0]=2*SA[i].elevation[1]-SA[i].elevation[2];
- }
+ }
+ r = NR(d);
+ c = NC(d);
+ SA[i].points[cell_num] = INDEX(r, c);
+ SA[i].elevation[cell_num] = elevation[r][c];
+ next_d = (abs(dirs[r][c]) == 0) ? d : abs(dirs[r][c]);
+ SA[i].distance[cell_num] = get_distance(r, c, next_d);
+ cell_num++;
+ if (cell_num > SA[i].number_of_cells)
+ G_fatal_error(_("To much points in stream line..."));
+ } while (streams[r][c] == SA[i].order);
- return 0;
+ if (SA[i].elevation[0] == -99999)
+ SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
+ }
+
+ return 0;
}
-int ram_find_contributing_cell(int r, int c, CELL** dirs, FCELL** elevation)
+int ram_find_contributing_cell(int r, int c, CELL **dirs, FCELL **elevation)
{
- int i,j=0;
- int next_r, next_c;
- float elev_min=9999;
+ int i, j = 0;
+ int next_r, next_c;
+ float elev_min = 9999;
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- next_r=NR(i);
- next_c=NC(i);
- if(dirs[next_r][next_c]==DIAG(i) && elevation[next_r][next_c]<elev_min) {
- elev_min=elevation[next_r][next_c];
- j=i;
- }
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ next_r = NR(i);
+ next_c = NC(i);
+ if (dirs[next_r][next_c] == DIAG(i) &&
+ elevation[next_r][next_c] < elev_min) {
+ elev_min = elevation[next_r][next_c];
+ j = i;
}
+ }
- return j;
+ return j;
}
-int seg_trib_nums(int r, int c, SEGMENT* streams, SEGMENT* dirs)
+int seg_trib_nums(int r, int c, SEGMENT * streams, SEGMENT * dirs)
{ /* calculate number of tributuaries */
- int trib_num = 0;
- int i, j;
- int next_r, next_c;
- int streams_cell, streams_next_cell, dirs_next_cell;
+ int trib_num = 0;
+ int i, j;
+ int next_r, next_c;
+ int streams_cell, streams_next_cell, dirs_next_cell;
- segment_get(streams,&streams_cell,r,c);
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ segment_get(streams, &streams_cell, r, c);
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
- segment_get(streams,&streams_next_cell,next_r,next_c);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
- if (streams_next_cell > 0 && dirs_next_cell == j)
- trib_num++;
- }
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+ segment_get(streams, &streams_next_cell, next_r, next_c);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
+ if (streams_next_cell > 0 && dirs_next_cell == j)
+ trib_num++;
+ }
- if (trib_num > 1)
+ if (trib_num > 1)
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
- segment_get(streams,&streams_next_cell,next_r,next_c);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
- if (streams_next_cell == streams_cell &&
- dirs_next_cell == j)
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+ segment_get(streams, &streams_next_cell, next_r, next_c);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
+ if (streams_next_cell == streams_cell && dirs_next_cell == j)
trib_num--;
}
-
+
if (trib_num > 5)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
if (trib_num > 3)
G_warning(_("Stream network may be too dense..."));
return trib_num;
} /* end trib_num */
-int seg_number_of_streams(SEGMENT* streams, SEGMENT* dirs)
+int seg_number_of_streams(SEGMENT *streams, SEGMENT *dirs)
{
- int r, c;
- int stream_num=0;
- int streams_cell;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- if (streams_cell>0)
- if (seg_trib_nums(r, c, streams, dirs) != 1)
- stream_num++;
- }
- G_message("%d",stream_num);
- return stream_num;
+ int r, c;
+ int stream_num = 0;
+ int streams_cell;
+
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+ if (streams_cell > 0)
+ if (seg_trib_nums(r, c, streams, dirs) != 1)
+ stream_num++;
+ }
+ G_message("%d", stream_num);
+ return stream_num;
}
-int seg_build_streamlines(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation, int number_of_streams)
+int seg_build_streamlines(SEGMENT *streams, SEGMENT *dirs,
+ SEGMENT *elevation, int number_of_streams)
{
- int r, c, i;
- int d, next_d;
- int next_r, next_c;
- int prev_r, prev_c;
- int streams_cell, dirs_cell, streams_next_cell, dirs_next_cell;
- float elevation_cell, elevation_prev_cell;
- int stream_num=1, cell_num=0;
- int contrib_cell;
- STREAM* SA;
- int tmp=0;
- stream_num = 1;
+ int r, c, i;
+ int d, next_d;
+ int prev_r, prev_c;
+ int streams_cell, dirs_cell;
+ float elevation_prev_cell;
+ int stream_num = 1, cell_num = 0;
+ int contrib_cell;
+ STREAM *SA;
+ stream_num = 1;
- stream_attributes = (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
- G_message("Finding inits...");
- SA=stream_attributes;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- if (streams_cell)
- if (seg_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
- if(stream_num>number_of_streams)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
-
- SA[stream_num].stream_num = stream_num;
- SA[stream_num].init_r = r;
- SA[stream_num++].init_c = c;
- }
+ stream_attributes =
+ (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
+ G_message("Finding inits...");
+ SA = stream_attributes;
+
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+ if (streams_cell)
+ if (seg_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
+ if (stream_num > number_of_streams)
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
+
+ SA[stream_num].stream_num = stream_num;
+ SA[stream_num].init_r = r;
+ SA[stream_num++].init_c = c;
}
-
- for(i=1;i<stream_num;++i) {
-
- r=SA[i].init_r;
- c=SA[i].init_c;
- segment_get(streams,&(SA[i].order),r,c);
- //segment_get(streams,&streams_cell,r,c);
- SA[i].number_of_cells=0;
- do {
-
- SA[i].number_of_cells++;
- segment_get(dirs,&dirs_cell,r,c);
- d=abs(dirs_cell);
- if(NOT_IN_REGION(d) || d==0)
- break;
- r=NR(d);
- c=NC(d);
- segment_get(streams,&streams_cell,r,c);
- } while (streams_cell==SA[i].order);
-
- SA[i].number_of_cells+=2; /* add two extra points for init+ and outlet+ */
}
- for(i=1;i<number_of_streams;++i) {
+ for (i = 1; i < stream_num; ++i) {
- SA[i].points = (unsigned long int *)
- G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
- SA[i].elevation = (float*)
- G_malloc((SA[i].number_of_cells) * sizeof(float));
- SA[i].distance = (double*)
- G_malloc((SA[i].number_of_cells) * sizeof(double));
-
- r=SA[i].init_r;
- c=SA[i].init_c;
- contrib_cell=seg_find_contributing_cell(r,c, streams, dirs,elevation);
- prev_r=NR(contrib_cell);
- prev_c=NC(contrib_cell);
-
- /* add one point contributing to init to calculate parameters */
- /* what to do if there is no contributing points? */
- SA[i].points[0] = (contrib_cell==0) ? -1 :
- INDEX(prev_r,prev_c);
-
- segment_get(elevation,&elevation_prev_cell,prev_r,prev_c);
- SA[i].elevation[0]=(contrib_cell==0) ? -99999 :
- elevation_prev_cell;
-
- if(contrib_cell==0)
- segment_get(dirs,&d,r,c);
- else
- segment_get(dirs,&d,prev_r,prev_c);
- SA[i].distance[0]=(contrib_cell==0) ? get_distance(r,c,d) :
- get_distance(prev_r,prev_c,d);
-
- SA[i].points[1] = INDEX(r,c);
- segment_get(elevation,&(SA[i].elevation[1]),r,c);
- segment_get(dirs,&d,r,c);
- SA[i].distance[1]=get_distance(r,c,d);
+ r = SA[i].init_r;
+ c = SA[i].init_c;
+ segment_get(streams, &(SA[i].order), r, c);
+ //segment_get(streams,&streams_cell,r,c);
+ SA[i].number_of_cells = 0;
+ do {
- cell_num=2;
- do {
- segment_get(dirs,&dirs_cell,r,c);
- d=abs(dirs_cell);
- if(NOT_IN_REGION(d) || d==0) {
- SA[i].points[cell_num] = -1;
- SA[i].distance[cell_num] = SA[i].distance[cell_num-1];
- SA[i].elevation[cell_num] =
- 2*SA[i].elevation[cell_num-1]-SA[i].elevation[cell_num-2];
- break;
- }
- r=NR(d);
- c=NC(d);
- SA[i].points[cell_num] = INDEX(r,c);
- segment_get(elevation,&(SA[i].elevation[cell_num]),r,c);
- segment_get(dirs,&next_d,r,c);
- next_d=(abs(next_d)==0) ? d : abs(next_d);
- SA[i].distance[cell_num] = get_distance(r,c,next_d);
- cell_num++;
- segment_get(streams,&streams_cell,r,c);
- if(cell_num>SA[i].number_of_cells)
- G_fatal_error(_("To much points in stream line..."));
- } while (streams_cell==SA[i].order);
-
- if(SA[i].elevation[0]==-99999)
- SA[i].elevation[0]=2*SA[i].elevation[1]-SA[i].elevation[2];
- }
- return 0;
+ SA[i].number_of_cells++;
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+ if (NOT_IN_REGION(d) || d == 0)
+ break;
+ r = NR(d);
+ c = NC(d);
+ segment_get(streams, &streams_cell, r, c);
+ } while (streams_cell == SA[i].order);
+
+ SA[i].number_of_cells += 2; /* add two extra points for init+ and outlet+ */
+ }
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ SA[i].points = (unsigned long int *)
+ G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+ SA[i].elevation = (float *)
+ G_malloc((SA[i].number_of_cells) * sizeof(float));
+ SA[i].distance = (double *)
+ G_malloc((SA[i].number_of_cells) * sizeof(double));
+
+ r = SA[i].init_r;
+ c = SA[i].init_c;
+ contrib_cell =
+ seg_find_contributing_cell(r, c, dirs, elevation);
+ prev_r = NR(contrib_cell);
+ prev_c = NC(contrib_cell);
+
+ /* add one point contributing to init to calculate parameters */
+ /* what to do if there is no contributing points? */
+ SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
+
+ segment_get(elevation, &elevation_prev_cell, prev_r, prev_c);
+ SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
+ elevation_prev_cell;
+
+ if (contrib_cell == 0)
+ segment_get(dirs, &d, r, c);
+ else
+ segment_get(dirs, &d, prev_r, prev_c);
+ SA[i].distance[0] = (contrib_cell == 0) ? get_distance(r, c, d) :
+ get_distance(prev_r, prev_c, d);
+
+ SA[i].points[1] = INDEX(r, c);
+ segment_get(elevation, &(SA[i].elevation[1]), r, c);
+ segment_get(dirs, &d, r, c);
+ SA[i].distance[1] = get_distance(r, c, d);
+
+ cell_num = 2;
+ do {
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+ if (NOT_IN_REGION(d) || d == 0) {
+ SA[i].points[cell_num] = -1;
+ SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+ SA[i].elevation[cell_num] =
+ 2 * SA[i].elevation[cell_num - 1] -
+ SA[i].elevation[cell_num - 2];
+ break;
+ }
+ r = NR(d);
+ c = NC(d);
+ SA[i].points[cell_num] = INDEX(r, c);
+ segment_get(elevation, &(SA[i].elevation[cell_num]), r, c);
+ segment_get(dirs, &next_d, r, c);
+ next_d = (abs(next_d) == 0) ? d : abs(next_d);
+ SA[i].distance[cell_num] = get_distance(r, c, next_d);
+ cell_num++;
+ segment_get(streams, &streams_cell, r, c);
+ if (cell_num > SA[i].number_of_cells)
+ G_fatal_error(_("To much points in stream line..."));
+ } while (streams_cell == SA[i].order);
+
+ if (SA[i].elevation[0] == -99999)
+ SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
+ }
+ return 0;
}
-
-int seg_find_contributing_cell(int r, int c, SEGMENT* dirs, SEGMENT* elevation)
+
+int seg_find_contributing_cell(int r, int c, SEGMENT *dirs,
+ SEGMENT *elevation)
{
- int i,j=0;
- int next_r, next_c;
- float elev_min=9999;
- int dirs_next_cell;
- float elevation_next_cell;
+ int i, j = 0;
+ int next_r, next_c;
+ float elev_min = 9999;
+ int dirs_next_cell;
+ float elevation_next_cell;
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- next_r=NR(i);
- next_c=NC(i);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
- segment_get(elevation,&elevation_next_cell,next_r,next_c);
- if(dirs_next_cell==DIAG(i) && elevation_next_cell<elev_min) {
- elev_min=elevation_next_cell;
- j=i;
- }
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ next_r = NR(i);
+ next_c = NC(i);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
+ segment_get(elevation, &elevation_next_cell, next_r, next_c);
+ if (dirs_next_cell == DIAG(i) && elevation_next_cell < elev_min) {
+ elev_min = elevation_next_cell;
+ j = i;
}
- return j;
+ }
+ return j;
}
-int free_attributes (int number_of_streams)
+int free_attributes(int number_of_streams)
{
- int i;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- G_free(SA[i].points);
- G_free(SA[i].elevation);
- G_free(SA[i].distance);
- }
- G_free(stream_attributes);
- return 0;
+ int i;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ G_free(SA[i].points);
+ G_free(SA[i].elevation);
+ G_free(SA[i].distance);
+ }
+ G_free(stream_attributes);
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.channel/stream_write.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.channel/stream_write.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.channel/stream_write.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,453 +1,505 @@
#include "local_proto.h"
-int ram_set_null_output(DCELL** output) {
+int ram_set_null_output(DCELL **output)
+{
- int r,c;
- for (r=0;r<nrows;++r)
- Rast_set_d_null_value(output[r],ncols);
- return 0;
+ int r;
+
+ for (r = 0; r < nrows; ++r)
+ Rast_set_d_null_value(output[r], ncols);
+ return 0;
}
-int seg_set_null_output(SEGMENT* output) {
+int seg_set_null_output(SEGMENT *output)
+{
- int r,c;
- double output_cell;
- for (r=0;r<nrows;++r)
- for (c=0;c<ncols;++c) {
- Rast_set_d_null_value(&output_cell,1);
- segment_put(output,&output_cell,r,c);
- }
- return 0;
+ int r, c;
+ double output_cell;
+
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ Rast_set_d_null_value(&output_cell, 1);
+ segment_put(output, &output_cell, r, c);
+ }
+ return 0;
}
-int ram_calculate_identifiers(CELL** identifier,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- identifier[r][c] = SA[i].stream_num;
- }
+int ram_calculate_identifiers(CELL **identifier, int number_of_streams,
+ int downstream)
+{
+ int r, c;
+ int i, j;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ identifier[r][c] = SA[i].stream_num;
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_identifiers(SEGMENT* identifier,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- segment_put(identifier,&(SA[i].stream_num),r,c);
- }
+int seg_calculate_identifiers(SEGMENT *identifier, int number_of_streams,
+ int downstream)
+{
+ int r, c;
+ int i, j;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ segment_put(identifier, &(SA[i].stream_num), r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_calculate_distance(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_distance(DCELL **output, int number_of_streams,
+ int downstream)
{
- int r,c;
- double cum_length;
- int i,j,k;
- STREAM* SA;
-
- SA=stream_attributes;
+ int r, c;
+ double cum_length;
+ int i, j;
+ STREAM *SA;
- for(i=1;i<number_of_streams;++i) {
- cum_length=0;
- if(!downstream)
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c]=cum_length;
- }
- else
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c]=cum_length;
- }
- }
- return 0;
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ cum_length = 0;
+ if (!downstream)
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = cum_length;
+ }
+ else
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = cum_length;
+ }
+ }
+ return 0;
}
-int seg_calculate_distance(SEGMENT* output,int number_of_streams, int downstream)
+
+int seg_calculate_distance(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- double cum_length;
- int i,j,k;
- STREAM* SA;
-
- SA=stream_attributes;
+ int r, c;
+ double cum_length;
+ int i, j;
+ STREAM *SA;
- for(i=1;i<number_of_streams;++i) {
- cum_length=0;
- if(!downstream)
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- segment_put(output,&cum_length,r,c);
- }
- else
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- segment_put(output,&cum_length,r,c);
- }
- }
- return 0;
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ cum_length = 0;
+ if (!downstream)
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ segment_put(output, &cum_length, r, c);
+ }
+ else
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ segment_put(output, &cum_length, r, c);
+ }
+ }
+ return 0;
}
-int ram_calculate_cell(DCELL** output,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j,k;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
-
- k=SA[i].number_of_cells-2;
- for(j=1;j<SA[i].number_of_cells-1;++j,--k) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = downstream ? k : j;
- }
+int ram_calculate_cell(DCELL **output, int number_of_streams, int downstream)
+{
+ int r, c;
+ int i, j, k;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ k = SA[i].number_of_cells - 2;
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j, --k) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = downstream ? k : j;
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_cell(SEGMENT* output,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j,k;
- double output_cell;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
-
- k=SA[i].number_of_cells-2;
- for(j=1;j<SA[i].number_of_cells-1;++j,--k) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = downstream ? k : j;
- segment_put(output,&output_cell,r,c);
- }
+int seg_calculate_cell(SEGMENT *output, int number_of_streams,
+ int downstream)
+{
+ int r, c;
+ int i, j, k;
+ double output_cell;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ k = SA[i].number_of_cells - 2;
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j, --k) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = downstream ? k : j;
+ segment_put(output, &output_cell, r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_calculate_difference(DCELL** output,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j;
- double result;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
+int ram_calculate_difference(DCELL **output, int number_of_streams,
+ int downstream)
+{
+ int r, c;
+ int i, j;
+ double result;
+ STREAM *SA;
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- result= downstream ?
- SA[i].elevation[j-1]-SA[i].elevation[j] :
- SA[i].elevation[j]-SA[i].elevation[j+1];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = result;
- }
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ result = downstream ?
+ SA[i].elevation[j - 1] - SA[i].elevation[j] :
+ SA[i].elevation[j] - SA[i].elevation[j + 1];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = result;
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_difference(SEGMENT* output,int number_of_streams, int downstream)
-{
- int r,c;
- int i,j;
- double output_cell;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
+int seg_calculate_difference(SEGMENT *output, int number_of_streams,
+ int downstream)
+{
+ int r, c;
+ int i, j;
+ double output_cell;
+ STREAM *SA;
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- output_cell= downstream ?
- SA[i].elevation[j-1]-SA[i].elevation[j] :
- SA[i].elevation[j]-SA[i].elevation[j+1];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- segment_put(output,&output_cell,r,c);
- }
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ output_cell = downstream ?
+ SA[i].elevation[j - 1] - SA[i].elevation[j] :
+ SA[i].elevation[j] - SA[i].elevation[j + 1];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ segment_put(output, &output_cell, r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_calculate_drop(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_drop(DCELL **output, int number_of_streams, int downstream)
{
- int r,c;
- int i,j;
- double init;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- if(!downstream) {
- init = SA[i].elevation[1];
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = init-SA[i].elevation[j];
- }
- } else {
- init = SA[i].elevation[SA[i].number_of_cells-2];
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = SA[i].elevation[j]-init;
- }
- }
+ int r, c;
+ int i, j;
+ double init;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ if (!downstream) {
+ init = SA[i].elevation[1];
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = init - SA[i].elevation[j];
+ }
}
- return 0;
+ else {
+ init = SA[i].elevation[SA[i].number_of_cells - 2];
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = SA[i].elevation[j] - init;
+ }
+ }
+ }
+ return 0;
}
-int seg_calculate_drop(SEGMENT* output,int number_of_streams, int downstream)
+
+int seg_calculate_drop(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double init;
- double output_cell;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- if(!downstream) {
- init = SA[i].elevation[1];
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = init-SA[i].elevation[j];
- segment_put(output,&output_cell,r,c);
- }
- } else {
- init = SA[i].elevation[SA[i].number_of_cells-2];
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = SA[i].elevation[j]-init;
- segment_put(output,&output_cell,r,c);
- }
- }
+ int r, c;
+ int i, j;
+ double init;
+ double output_cell;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ if (!downstream) {
+ init = SA[i].elevation[1];
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = init - SA[i].elevation[j];
+ segment_put(output, &output_cell, r, c);
+ }
}
- return 0;
+ else {
+ init = SA[i].elevation[SA[i].number_of_cells - 2];
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = SA[i].elevation[j] - init;
+ segment_put(output, &output_cell, r, c);
+ }
+ }
+ }
+ return 0;
}
-int ram_calculate_gradient(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_gradient(DCELL **output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double init;
- double result;
- double cum_length;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- cum_length=0;
- if(!downstream) {
- init = SA[i].elevation[0];
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = (init-SA[i].elevation[j])/cum_length;
- }
- } else {
- init = SA[i].elevation[SA[i].number_of_cells-1];
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = (SA[i].elevation[j]-init)/cum_length;
- }
- }
+ int r, c;
+ int i, j;
+ double init;
+ double cum_length;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ cum_length = 0;
+ if (!downstream) {
+ init = SA[i].elevation[0];
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = (init - SA[i].elevation[j]) / cum_length;
+ }
}
- return 0;
+ else {
+ init = SA[i].elevation[SA[i].number_of_cells - 1];
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = (SA[i].elevation[j] - init) / cum_length;
+ }
+ }
+ }
+ return 0;
}
-int seg_calculate_gradient(SEGMENT* output,int number_of_streams, int downstream)
+int seg_calculate_gradient(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double init;
- double output_cell;
- double cum_length;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- cum_length=0;
- if(!downstream) {
- init = SA[i].elevation[1];
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = (init-SA[i].elevation[j])/cum_length;
- segment_put(output,&output_cell,r,c);
-
- }
- } else {
- init = SA[i].elevation[SA[i].number_of_cells-2];
- for(j=SA[i].number_of_cells-2;j>0;--j) {
- cum_length+=SA[i].distance[j];
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = (SA[i].elevation[j]-init)/cum_length;
- segment_put(output,&output_cell,r,c);
- }
- }
+ int r, c;
+ int i, j;
+ double init;
+ double output_cell;
+ double cum_length;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ cum_length = 0;
+ if (!downstream) {
+ init = SA[i].elevation[1];
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = (init - SA[i].elevation[j]) / cum_length;
+ segment_put(output, &output_cell, r, c);
+
+ }
}
- return 0;
+ else {
+ init = SA[i].elevation[SA[i].number_of_cells - 2];
+ for (j = SA[i].number_of_cells - 2; j > 0; --j) {
+ cum_length += SA[i].distance[j];
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = (SA[i].elevation[j] - init) / cum_length;
+ segment_put(output, &output_cell, r, c);
+ }
+ }
+ }
+ return 0;
}
-int ram_calculate_local_gradient(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_local_gradient(DCELL **output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double elev_diff;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- elev_diff=(SA[i].elevation[j]-SA[i].elevation[j+1])<0 ? 0 :
- (SA[i].elevation[j]-SA[i].elevation[j+1]);
- output[r][c] = elev_diff/SA[i].distance[j];
- }
+ int r, c;
+ int i, j;
+ double elev_diff;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ elev_diff =
+ (SA[i].elevation[j] - SA[i].elevation[j + 1]) <
+ 0 ? 0 : (SA[i].elevation[j] - SA[i].elevation[j + 1]);
+ output[r][c] = elev_diff / SA[i].distance[j];
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_local_gradient(SEGMENT* output,int number_of_streams, int downstream)
+int seg_calculate_local_gradient(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double elev_diff;
- double output_cell;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- elev_diff=(SA[i].elevation[j]-SA[i].elevation[j+1])<0 ? 0 :
- (SA[i].elevation[j]-SA[i].elevation[j+1]);
- output_cell = elev_diff/SA[i].distance[j];
- segment_put(output,&output_cell,r,c);
- }
+ int r, c;
+ int i, j;
+ double elev_diff;
+ double output_cell;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ elev_diff =
+ (SA[i].elevation[j] - SA[i].elevation[j + 1]) <
+ 0 ? 0 : (SA[i].elevation[j] - SA[i].elevation[j + 1]);
+ output_cell = elev_diff / SA[i].distance[j];
+ segment_put(output, &output_cell, r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_calculate_local_distance(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_local_distance(DCELL **output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output[r][c] = SA[i].distance[j];
- }
+ int r, c;
+ int i, j;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output[r][c] = SA[i].distance[j];
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_local_distance(SEGMENT* output,int number_of_streams, int downstream)
+int seg_calculate_local_distance(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double output_cell;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- output_cell = SA[i].distance[j];
- segment_put(output,&output_cell,r,c);
- }
+ int r, c;
+ int i, j;
+ double output_cell;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ output_cell = SA[i].distance[j];
+ segment_put(output, &output_cell, r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_calculate_curvature(DCELL** output,int number_of_streams, int downstream)
+int ram_calculate_curvature(DCELL **output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- STREAM* SA;
- double result, first_derivative, second_derivative;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- first_derivative=(SA[i].elevation[j-1]-SA[i].elevation[j+1])/
- (SA[i].distance[j-1]+SA[i].distance[j]);
- second_derivative=((SA[i].elevation[j-1]-SA[i].elevation[j])-
- (SA[i].elevation[j]-SA[i].elevation[j+1]))/
- (SA[i].distance[j-1]+SA[i].distance[j]);
- output[r][c] =
- first_derivative/pow((1+second_derivative*second_derivative),1.5);
- }
+ int r, c;
+ int i, j;
+ STREAM *SA;
+ double first_derivative, second_derivative;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ first_derivative =
+ (SA[i].elevation[j - 1] -
+ SA[i].elevation[j + 1]) / (SA[i].distance[j - 1] +
+ SA[i].distance[j]);
+ second_derivative =
+ ((SA[i].elevation[j - 1] - SA[i].elevation[j]) -
+ (SA[i].elevation[j] -
+ SA[i].elevation[j + 1])) / (SA[i].distance[j - 1] +
+ SA[i].distance[j]);
+ output[r][c] =
+ first_derivative /
+ pow((1 + second_derivative * second_derivative), 1.5);
}
- return 0;
+ }
+ return 0;
}
-int seg_calculate_curvature(SEGMENT* output,int number_of_streams, int downstream)
+int seg_calculate_curvature(SEGMENT *output, int number_of_streams,
+ int downstream)
{
- int r,c;
- int i,j;
- double output_cell;
- STREAM* SA;
- double result, first_derivative, second_derivative;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- first_derivative=(SA[i].elevation[j-1]-SA[i].elevation[j+1])/
- (SA[i].distance[j-1]+SA[i].distance[j]);
- second_derivative=((SA[i].elevation[j-1]-SA[i].elevation[j])-
- (SA[i].elevation[j]-SA[i].elevation[j+1]))/
- (SA[i].distance[j-1]+SA[i].distance[j]);
- output_cell =
- first_derivative/pow((1+second_derivative*second_derivative),1.5);
- segment_put(output,&output_cell,r,c);
- }
+ int r, c;
+ int i, j;
+ double output_cell;
+ STREAM *SA;
+ double first_derivative, second_derivative;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ first_derivative =
+ (SA[i].elevation[j - 1] -
+ SA[i].elevation[j + 1]) / (SA[i].distance[j - 1] +
+ SA[i].distance[j]);
+ second_derivative =
+ ((SA[i].elevation[j - 1] - SA[i].elevation[j]) -
+ (SA[i].elevation[j] -
+ SA[i].elevation[j + 1])) / (SA[i].distance[j - 1] +
+ SA[i].distance[j]);
+ output_cell =
+ first_derivative /
+ pow((1 + second_derivative * second_derivative), 1.5);
+ segment_put(output, &output_cell, r, c);
}
- return 0;
+ }
+ return 0;
}
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/distance_calc.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/distance_calc.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/distance_calc.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -22,30 +22,32 @@
head = -1;
}
fifo_count--;
-
+
return fifo_points[++head];
}
-int ram_calculate_downstream (CELL** dirs, FCELL** distance, FCELL** elevation, OUTLET outlet, int outs) {
-
- int r, c, i, j;
- int next_r,next_c;
- POINT n_cell;
- float cur_dist = 0;
- float tmp_dist = 0;
- float target_elev; /* eleavation at stream or outlet */
- float easting, northing;
- float cell_easting, cell_northing;
- struct Cell_head window;
-
- Rast_get_window(&window);
+int ram_calculate_downstream(CELL ** dirs, FCELL ** distance,
+ FCELL ** elevation, OUTLET outlet, int outs)
+{
- tail = 0;
- head = -1;
- r = outlet.r;
- c = outlet.c;
+ int r, c, i, j;
+ int next_r, next_c;
+ POINT n_cell;
+ float cur_dist = 0;
+ float tmp_dist = 0;
+ float target_elev; /* eleavation at stream or outlet */
+ float easting, northing;
+ float cell_easting, cell_northing;
+ struct Cell_head window;
+ Rast_get_window(&window);
+
+ tail = 0;
+ head = -1;
+ r = outlet.r;
+ c = outlet.c;
+
if (elevation) {
target_elev = elevation[r][c];
elevation[r][c] = 0.;
@@ -57,45 +59,55 @@
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue; /* border */
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
- if (dirs[NR(i)][NC(i)] == j) { /* countributing cell, reset distance and elevation */
-
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+ if (dirs[NR(i)][NC(i)] == j) { /* countributing cell, reset distance and elevation */
+
if (outs) { /* outlet mode */
- if (distance[NR(i)][NC(i)] == 0)
+ if (distance[NR(i)][NC(i)] == 0)
continue; /* continue loop, point is not added to the queue! */
- else {
- cell_northing = window.north - (next_r +.5) * window.ns_res;
- cell_easting = window.west + (next_c +.5) * window.ew_res;
- cur_dist = tmp_dist +
- G_distance(easting,northing,cell_easting,cell_northing);
+ else {
+ cell_northing =
+ window.north - (next_r + .5) * window.ns_res;
+ cell_easting =
+ window.west + (next_c + .5) * window.ew_res;
+ cur_dist =
+ tmp_dist + G_distance(easting, northing,
+ cell_easting,
+ cell_northing);
distance[NR(i)][NC(i)] = cur_dist;
- }
+ }
- } else { /* stream mode */
-
- if (distance[next_r][next_c] == 0) {
- cur_dist = 0;
- if (elevation)
- target_elev = elevation[next_r][next_c];
- } else {
- cell_northing = window.north - (next_r +.5) * window.ns_res;
- cell_easting = window.west + (next_c +.5) * window.ew_res;
- cur_dist = tmp_dist +
- G_distance(easting,northing,cell_easting,cell_northing);
+ }
+ else { /* stream mode */
+
+ if (distance[next_r][next_c] == 0) {
+ cur_dist = 0;
+ if (elevation)
+ target_elev = elevation[next_r][next_c];
+ }
+ else {
+ cell_northing =
+ window.north - (next_r + .5) * window.ns_res;
+ cell_easting =
+ window.west + (next_c + .5) * window.ew_res;
+ cur_dist =
+ tmp_dist + G_distance(easting, northing,
+ cell_easting,
+ cell_northing);
distance[NR(i)][NC(i)] = cur_dist;
}
} /* end stream mode */
if (elevation) {
- elevation[next_r][next_c] =
- elevation[next_r][next_c] - target_elev;
- n_cell.target_elev = target_elev;
+ elevation[next_r][next_c] =
+ elevation[next_r][next_c] - target_elev;
+ n_cell.target_elev = target_elev;
}
n_cell.r = next_r;
@@ -112,34 +124,36 @@
target_elev = n_cell.target_elev;
} /* end while */
- return 0;
+ return 0;
}
-int seg_calculate_downstream (SEGMENT* dirs, SEGMENT* distance, SEGMENT* elevation, OUTLET outlet, int outs) {
-
- int r, c, i, j;
- int next_r, next_c;
- POINT n_cell;
- float cur_dist = 0;
- float tmp_dist = 0;
- float target_elev; /* eleavation at stream or outlet */
- float easting, northing;
- float cell_easting, cell_northing;
- CELL dirs_cell;
- FCELL distance_cell, elevation_cell;
- FCELL zero_cell=0;
- struct Cell_head window;
-
- Rast_get_window(&window);
+int seg_calculate_downstream(SEGMENT *dirs, SEGMENT * distance,
+ SEGMENT *elevation, OUTLET outlet, int outs)
+{
- tail = 0;
- head = -1;
- r = outlet.r;
- c = outlet.c;
+ int r, c, i, j;
+ int next_r, next_c;
+ POINT n_cell;
+ float cur_dist = 0;
+ float tmp_dist = 0;
+ float target_elev; /* eleavation at stream or outlet */
+ float easting, northing;
+ float cell_easting, cell_northing;
+ CELL dirs_cell;
+ FCELL distance_cell, elevation_cell;
+ FCELL zero_cell = 0;
+ struct Cell_head window;
+ Rast_get_window(&window);
+
+ tail = 0;
+ head = -1;
+ r = outlet.r;
+ c = outlet.c;
+
if (elevation) {
- segment_get(elevation,&target_elev,r,c);
- segment_put(elevation,&zero_cell,r,c);
+ segment_get(elevation, &target_elev, r, c);
+ segment_put(elevation, &zero_cell, r, c);
}
while (tail != head) {
@@ -148,49 +162,60 @@
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue; /* border */
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- segment_get(dirs,&dirs_cell,next_r,next_c);
- if (dirs_cell == j) { /* countributing cell, reset distance and elevation */
-
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+ if (dirs_cell == j) { /* countributing cell, reset distance and elevation */
+
if (outs) { /* outlet mode */
- segment_get(distance,&distance_cell,next_r,next_c);
- if (distance_cell == 0)
+ segment_get(distance, &distance_cell, next_r, next_c);
+ if (distance_cell == 0)
continue; /* continue loop, point is not added to the queue! */
- else {
- cell_northing = window.north - (next_r +.5) * window.ns_res;
- cell_easting = window.west + (next_c +.5) * window.ew_res;
- cur_dist = tmp_dist +
- G_distance(easting,northing,cell_easting,cell_northing);
- segment_put(distance,&cur_dist,next_r,next_c);
-
- }
+ else {
+ cell_northing =
+ window.north - (next_r + .5) * window.ns_res;
+ cell_easting =
+ window.west + (next_c + .5) * window.ew_res;
+ cur_dist =
+ tmp_dist + G_distance(easting, northing,
+ cell_easting,
+ cell_northing);
+ segment_put(distance, &cur_dist, next_r, next_c);
- } else { /* stream mode */
- segment_get(distance,&distance_cell,next_r,next_c);
- if (distance_cell == 0) {
- cur_dist = 0;
- if (elevation)
- segment_get(elevation,&target_elev,next_r,next_c);
- } else {
- cell_northing = window.north - (next_r +.5) * window.ns_res;
- cell_easting = window.west + (next_c +.5) * window.ew_res;
- cur_dist = tmp_dist +
- G_distance(easting,northing,cell_easting,cell_northing);
- segment_put(distance,&cur_dist,next_r,next_c);
}
+
+ }
+ else { /* stream mode */
+ segment_get(distance, &distance_cell, next_r, next_c);
+ if (distance_cell == 0) {
+ cur_dist = 0;
+ if (elevation)
+ segment_get(elevation, &target_elev, next_r,
+ next_c);
+ }
+ else {
+ cell_northing =
+ window.north - (next_r + .5) * window.ns_res;
+ cell_easting =
+ window.west + (next_c + .5) * window.ew_res;
+ cur_dist =
+ tmp_dist + G_distance(easting, northing,
+ cell_easting,
+ cell_northing);
+ segment_put(distance, &cur_dist, next_r, next_c);
+ }
} /* end stream mode */
if (elevation) {
- segment_get(elevation,&elevation_cell,next_r,next_c);
- elevation_cell -= target_elev;
- segment_put(elevation,&elevation_cell,next_r,next_c);
- n_cell.target_elev = target_elev;
+ segment_get(elevation, &elevation_cell, next_r, next_c);
+ elevation_cell -= target_elev;
+ segment_put(elevation, &elevation_cell, next_r, next_c);
+ n_cell.target_elev = target_elev;
}
n_cell.r = next_r;
@@ -206,45 +231,45 @@
tmp_dist = n_cell.cur_dist;
target_elev = n_cell.target_elev;
- } /* end while */
- return 0;
+ } /* end while */
+ return 0;
}
-int ram_fill_basins(OUTLET outlet, FCELL** distance, CELL** dirs)
+int ram_fill_basins(OUTLET outlet, FCELL ** distance, CELL ** dirs)
{
-/* fill empty spaces with zeros but leave -1 as a markers of NULL */
- int r, c, i, j;
- int next_r, next_c;
- float stop, val;
- POINT n_cell;
+ /* fill empty spaces with zeros but leave -1 as a markers of NULL */
+ int r, c, i, j;
+ int next_r, next_c;
+ float stop, val;
+ POINT n_cell;
- tail = 0;
- head = -1;
- r = outlet.r;
- c = outlet.c;
- val = 1;
- stop = 0;
+ tail = 0;
+ head = -1;
+ r = outlet.r;
+ c = outlet.c;
+ val = 1;
+ stop = 0;
- distance[r][c] = stop;
-
+ distance[r][c] = stop;
+
while (tail != head) {
for (i = 1; i < 9; ++i) {
if (NOT_IN_REGION(i))
continue; /* out of border */
-
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
+
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
if (dirs[next_r][next_c] == j) { /* countributing cell */
distance[next_r][next_c] =
- (distance[next_r][next_c] == stop) ? stop : val;
+ (distance[next_r][next_c] == stop) ? stop : val;
n_cell.r = next_r;
n_cell.c = next_c;
fifo_insert(n_cell);
}
-
+
} /* end for i... */
n_cell = fifo_return_del();
@@ -255,46 +280,46 @@
return 0;
}
-int seg_fill_basins(OUTLET outlet, SEGMENT* distance, SEGMENT* dirs)
+int seg_fill_basins(OUTLET outlet, SEGMENT * distance, SEGMENT * dirs)
{
-/* fill empty spaces with zeros but leave -1 as a markers of NULL */
- int r, c, i, j;
- int next_r, next_c;
- float stop, val;
- POINT n_cell;
- CELL dirs_cell;
- FCELL distance_cell;
+ /* fill empty spaces with zeros but leave -1 as a markers of NULL */
+ int r, c, i, j;
+ int next_r, next_c;
+ float stop, val;
+ POINT n_cell;
+ CELL dirs_cell;
+ FCELL distance_cell;
- tail = 0;
- head = -1;
- r = outlet.r;
- c = outlet.c;
- val = 1;
- stop = 0;
+ tail = 0;
+ head = -1;
+ r = outlet.r;
+ c = outlet.c;
+ val = 1;
+ stop = 0;
- segment_put(distance,&stop,r,c);
-
+ segment_put(distance, &stop, r, c);
+
while (tail != head) {
for (i = 1; i < 9; ++i) {
if (NOT_IN_REGION(i))
continue; /* out of border */
-
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- segment_get(dirs,&dirs_cell,next_r,next_c);
-
- if (dirs_cell == j) { /* countributing cell */
-
- segment_get(distance,&distance_cell,next_r,next_c);
- distance_cell= (distance_cell == stop) ? stop : val;
- segment_put(distance,&distance_cell,next_r,next_c);
+
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+
+ if (dirs_cell == j) { /* countributing cell */
+
+ segment_get(distance, &distance_cell, next_r, next_c);
+ distance_cell = (distance_cell == stop) ? stop : val;
+ segment_put(distance, &distance_cell, next_r, next_c);
n_cell.r = next_r;
n_cell.c = next_c;
fifo_insert(n_cell);
-
+
}
} /* end for i... */
@@ -306,91 +331,94 @@
return 0;
}
-int ram_calculate_upstream(FCELL** distance, CELL** dirs, FCELL** elevation, FCELL** tmp_elevation, int near)
+int ram_calculate_upstream(FCELL ** distance, CELL ** dirs,
+ FCELL ** elevation, FCELL ** tmp_elevation,
+ int near)
{
- int r, c;
- int next_r, next_c;
- float easting, northing;
- float cell_easting, cell_northing;
- int i, j, k, d;
- int done;
- int counter;
- int n_inits = 0;
- float cur_dist;
- POINT *d_inits;
- float tmp_dist = 0;
- float target_elev = 0;
- size_t elevation_data_size;
- struct Cell_head window;
-
- Rast_get_window(&window);
-
- if(elevation) {
- elevation_data_size=Rast_cell_size(FCELL_TYPE);
- for (r = 0; r < nrows; ++r)
- memcpy(tmp_elevation[r],elevation[r],ncols*elevation_data_size);
- }
-
- for (r = 0; r < nrows; ++r)
+ int r, c;
+ int next_r, next_c;
+ float easting, northing;
+ float cell_easting, cell_northing;
+ int i, j, k, d;
+ int done;
+ int counter;
+ int n_inits = 0;
+ float cur_dist;
+ POINT *d_inits;
+ float tmp_dist = 0;
+ float target_elev = 0;
+ size_t elevation_data_size;
+ struct Cell_head window;
+
+ Rast_get_window(&window);
+
+ if (elevation) {
+ elevation_data_size = Rast_cell_size(FCELL_TYPE);
+ for (r = 0; r < nrows; ++r)
+ memcpy(tmp_elevation[r], elevation[r],
+ ncols * elevation_data_size);
+ }
+
+ for (r = 0; r < nrows; ++r)
for (c = 0; c < ncols; ++c) {
-
+
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue; /* out of border */
j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
- if (dirs[next_r][next_c] == j && distance[r][c] != 0) { /* is contributing cell */
- distance[r][c] = -1;
- break;
- }
+ next_r = NR(i);
+ next_c = NC(i);
+ if (dirs[next_r][next_c] == j && distance[r][c] != 0) { /* is contributing cell */
+ distance[r][c] = -1;
+ break;
+ }
}
- if (distance[r][c] == 1 && dirs[r][c] > 0)
- n_inits++;
- else if (dirs[r][c] > 0)
- distance[r][c] = -1;
+ if (distance[r][c] == 1 && dirs[r][c] > 0)
+ n_inits++;
+ else if (dirs[r][c] > 0)
+ distance[r][c] = -1;
}
- d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
+ d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
k = 0;
- for (r = 0; r < nrows; ++r)
+ for (r = 0; r < nrows; ++r)
for (c = 0; c < ncols; ++c) {
if (distance[r][c] == 1) {
-
- distance[r][c] = 0;
- if (elevation)
- elevation[r][c] = 0;
- d=dirs[r][c];
-
- if (dirs[NR(d)][NC(d)]<0)
+ distance[r][c] = 0;
+ if (elevation)
+ elevation[r][c] = 0;
+
+ d = dirs[r][c];
+
+ if (dirs[NR(d)][NC(d)] < 0)
continue;
-
+
d_inits[k].r = r;
d_inits[k].c = c;
d_inits[k].cur_dist = 0;
-
- if (elevation)
- d_inits[k].target_elev = tmp_elevation[r][c];
+ if (elevation)
+ d_inits[k].target_elev = tmp_elevation[r][c];
+
k++;
}
}
counter = n_inits = k;
- //return 0; // do usunięcia
- G_message("Calculate upstream parameters...");
+ //return 0; // do usunięcia
+ G_message("Calculate upstream parameters...");
while (n_inits > 0) {
k = 0;
- G_percent((counter-n_inits),counter,10);
+ G_percent((counter - n_inits), counter, 10);
for (i = 0; i < n_inits; ++i) {
r = d_inits[i].r;
c = d_inits[i].c;
- d=dirs[r][c];
+ d = dirs[r][c];
next_r = NR(d);
next_c = NC(d);
tmp_dist = d_inits[i].cur_dist;
@@ -398,27 +426,27 @@
if (elevation)
target_elev = d_inits[i].target_elev;
- easting = window.west + (c + 0.5) * window.ew_res;
- northing = window.north - (r + 0.5) * window.ns_res;
- cell_easting = window.west + (next_c + 0.5) * window.ew_res;
- cell_northing = window.north - (next_r + 0.5) * window.ns_res;
+ easting = window.west + (c + 0.5) * window.ew_res;
+ northing = window.north - (r + 0.5) * window.ns_res;
+ cell_easting = window.west + (next_c + 0.5) * window.ew_res;
+ cell_northing = window.north - (next_r + 0.5) * window.ns_res;
- cur_dist = tmp_dist +
- G_distance(easting, northing, cell_easting, cell_northing);
-
+ cur_dist = tmp_dist +
+ G_distance(easting, northing, cell_easting, cell_northing);
+
if (near)
done = (distance[next_r][next_c] > cur_dist ||
- distance[next_r][next_c] <=0) ? 1 : 0;
+ distance[next_r][next_c] <= 0) ? 1 : 0;
else
done = (distance[next_r][next_c] < cur_dist ||
- distance[next_r][next_c] <=0) ? 1 : 0;
+ distance[next_r][next_c] <= 0) ? 1 : 0;
if (done) {
distance[next_r][next_c] = cur_dist;
- if (elevation) {
- elevation[next_r][next_c] =
- target_elev - tmp_elevation[next_r][next_c];
- }
+ if (elevation) {
+ elevation[next_r][next_c] =
+ target_elev - tmp_elevation[next_r][next_c];
+ }
if (dirs[NR(d)][NC(d)] < 1)
continue;
@@ -426,122 +454,126 @@
d_inits[k].c = next_c;
d_inits[k].cur_dist = cur_dist;
- if (elevation)
- d_inits[k].target_elev = target_elev;
+ if (elevation)
+ d_inits[k].target_elev = target_elev;
k++;
} /* end of if done */
}
n_inits = k;
}
- G_percent((counter-n_inits),counter,10);
+ G_percent((counter - n_inits), counter, 10);
return 0;
}
-int seg_calculate_upstream(SEGMENT* distance, SEGMENT* dirs, SEGMENT* elevation, SEGMENT* tmp_elevation, int near)
+int seg_calculate_upstream(SEGMENT * distance, SEGMENT * dirs,
+ SEGMENT * elevation, SEGMENT * tmp_elevation,
+ int near)
{
- int r, c;
- int next_r, next_c;
- float easting, northing;
- float cell_easting, cell_northing;
- int i, j, k, d, d_next;
- FCELL minus_one_cell = -1;
- FCELL zero_cell = 0;
- int done;
- int counter;
- int n_inits = 0;
- float cur_dist;
- POINT *d_inits;
- float tmp_dist = 0;
- float target_elev = 0;
- CELL dirs_cell;
- FCELL distance_cell, elevation_cell, tmp_elevation_cell;
- size_t elevation_data_size;
- struct Cell_head window;
+ int r, c;
+ int next_r, next_c;
+ float easting, northing;
+ float cell_easting, cell_northing;
+ int i, j, k, d, d_next;
+ FCELL minus_one_cell = -1;
+ FCELL zero_cell = 0;
+ int done;
+ int counter;
+ int n_inits = 0;
+ float cur_dist;
+ POINT *d_inits;
+ float tmp_dist = 0;
+ float target_elev = 0;
+ CELL dirs_cell;
+ FCELL distance_cell, elevation_cell, tmp_elevation_cell;
+ size_t elevation_data_size;
+ struct Cell_head window;
- Rast_get_window(&window);
-
- if(elevation) {
- elevation_data_size=Rast_cell_size(FCELL_TYPE);
- for (r = 0; r < nrows; ++r)
- for(c = 0; c<ncols; ++c) {
- segment_get(elevation,&elevation_cell,r,c);
- segment_put(tmp_elevation,&elevation_cell,r,c);
- }
- }
-
- for (r = 0; r < nrows; ++r)
+ Rast_get_window(&window);
+
+ if (elevation) {
+ elevation_data_size = Rast_cell_size(FCELL_TYPE);
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(elevation, &elevation_cell, r, c);
+ segment_put(tmp_elevation, &elevation_cell, r, c);
+ }
+ }
+
+ for (r = 0; r < nrows; ++r)
for (c = 0; c < ncols; ++c) {
- segment_get(distance,&distance_cell,r,c);
+ segment_get(distance, &distance_cell, r, c);
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
+ if (NOT_IN_REGION(i))
continue; /* out of border */
j = DIAG(i);
next_r = NR(i);
next_c = NC(i);
-
- segment_get(dirs,&dirs_cell,next_r,next_c);
-
- if (dirs_cell == j && distance_cell != 0) { /* is contributing cell */
- segment_put(distance,&minus_one_cell,r,c);
- break;
- }
- } /* end for i */
-
- segment_get(distance,&distance_cell,r,c);
- segment_get(dirs,&dirs_cell,r,c);
- if(distance_cell== 1)
- if(distance_cell == 1 && dirs_cell >0)
- n_inits++;
- else if (dirs_cell > 0)
- segment_put(distance,&minus_one_cell,r,c);
-
+
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+
+ if (dirs_cell == j && distance_cell != 0) { /* is contributing cell */
+ segment_put(distance, &minus_one_cell, r, c);
+ break;
}
+ } /* end for i */
- d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
+ segment_get(distance, &distance_cell, r, c);
+ segment_get(dirs, &dirs_cell, r, c);
+ if (distance_cell == 1) {
+ if (distance_cell == 1 && dirs_cell > 0)
+ n_inits++;
+ else if (dirs_cell > 0)
+ segment_put(distance, &minus_one_cell, r, c);
+ }
+ }
+
+ d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
+
k = 0;
- for (r = 0; r < nrows; ++r)
+ for (r = 0; r < nrows; ++r)
for (c = 0; c < ncols; ++c) {
-
- segment_get(distance,&distance_cell,r,c);
+
+ segment_get(distance, &distance_cell, r, c);
if (distance_cell == 1) {
-
- segment_put(distance,&zero_cell,r,c);
- if (elevation)
- segment_put(elevation,&zero_cell,r,c);
- segment_get(dirs,&d,r,c);
- segment_get(dirs,&d_next,NR(d),NR(d));
-
- if (d_next<0)
- continue;
+ segment_put(distance, &zero_cell, r, c);
+ if (elevation)
+ segment_put(elevation, &zero_cell, r, c);
+ segment_get(dirs, &d, r, c);
+ segment_get(dirs, &d_next, NR(d), NR(d));
+
+ if (d_next < 0)
+ continue;
+
d_inits[k].r = r;
d_inits[k].c = c;
d_inits[k].cur_dist = 0;
- if (elevation)
- segment_get(tmp_elevation,&(d_inits[k].target_elev),r,c);
+ if (elevation)
+ segment_get(tmp_elevation, &(d_inits[k].target_elev), r,
+ c);
k++;
}
}
counter = n_inits = k;
- G_message("Calculate upstream parameters...");
+ G_message("Calculate upstream parameters...");
while (n_inits > 0) {
k = 0;
- G_percent((counter-n_inits),counter,10);
-
+ G_percent((counter - n_inits), counter, 10);
+
for (i = 0; i < n_inits; ++i) {
r = d_inits[i].r;
c = d_inits[i].c;
-
- segment_get(dirs,&d,r,c);
+
+ segment_get(dirs, &d, r, c);
next_r = NR(d);
next_c = NC(d);
tmp_dist = d_inits[i].cur_dist;
@@ -549,32 +581,34 @@
if (elevation)
target_elev = d_inits[i].target_elev;
- easting = window.west + (c + 0.5) * window.ew_res;
- northing = window.north - (r + 0.5) * window.ns_res;
- cell_easting = window.west + (next_c + 0.5) * window.ew_res;
- cell_northing = window.north - (next_r + 0.5) * window.ns_res;
+ easting = window.west + (c + 0.5) * window.ew_res;
+ northing = window.north - (r + 0.5) * window.ns_res;
+ cell_easting = window.west + (next_c + 0.5) * window.ew_res;
+ cell_northing = window.north - (next_r + 0.5) * window.ns_res;
- cur_dist = tmp_dist +
- G_distance(easting, northing, cell_easting, cell_northing);
+ cur_dist = tmp_dist +
+ G_distance(easting, northing, cell_easting, cell_northing);
- segment_get(distance,&distance_cell,next_r,next_c);
-
+ segment_get(distance, &distance_cell, next_r, next_c);
+
if (near)
done = (distance_cell > cur_dist ||
- distance_cell <=0) ? 1 : 0;
+ distance_cell <= 0) ? 1 : 0;
else
done = (distance_cell < cur_dist ||
- distance_cell <=0) ? 1 : 0;
+ distance_cell <= 0) ? 1 : 0;
if (done) {
- segment_put(distance,&cur_dist,next_r,next_c);
- if (elevation) {
- segment_get(tmp_elevation,&tmp_elevation_cell,next_r,next_c);
- tmp_elevation_cell = target_elev-tmp_elevation_cell;
- segment_put(elevation,&tmp_elevation_cell,next_r,next_c);
- }
-
- segment_get(dirs,&dirs_cell,NR(d),NC(d));
+ segment_put(distance, &cur_dist, next_r, next_c);
+ if (elevation) {
+ segment_get(tmp_elevation, &tmp_elevation_cell, next_r,
+ next_c);
+ tmp_elevation_cell = target_elev - tmp_elevation_cell;
+ segment_put(elevation, &tmp_elevation_cell, next_r,
+ next_c);
+ }
+
+ segment_get(dirs, &dirs_cell, NR(d), NC(d));
if (dirs_cell < 1)
continue;
@@ -582,13 +616,13 @@
d_inits[k].c = next_c;
d_inits[k].cur_dist = cur_dist;
- if (elevation)
- d_inits[k].target_elev = target_elev;
+ if (elevation)
+ d_inits[k].target_elev = target_elev;
k++;
} /* end of if done */
}
n_inits = k;
}
- G_percent((counter-n_inits),counter,10);
+ G_percent((counter - n_inits), counter, 10);
return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/distance_init.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/distance_init.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/distance_init.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,10 +1,10 @@
#include "local_proto.h"
-int ram_find_outlets(CELL** streams, int number_of_streams, CELL** dirs, int subs, int outs)
+int ram_find_outlets(CELL ** streams, int number_of_streams, CELL ** dirs,
+ int subs, int outs)
{
- int d; /* d: direction */
+ int d; /* d: direction */
int r, c;
- int i,j, cat;
int next_stream = -1, cur_stream;
int out_max = ncols + nrows;
int outlets_num;
@@ -12,182 +12,197 @@
G_message(_("Finding nodes..."));
outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
- outlets_num = 0;
+ outlets_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c]>0) {
- if (outlets_num > (out_max - 1)) {
- if (outlets_num > 4 * (out_max - 1))
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c] > 0) {
+ if (outlets_num > (out_max - 1)) {
+ if (outlets_num > 4 * (out_max - 1))
G_fatal_error
- ("Stream and direction maps probably do not match");
- out_max *= 4;
- outlets = (OUTLET *) G_realloc(outlets, (out_max) * sizeof(OUTLET));
- }
-
+ ("Stream and direction maps probably do not match");
+ out_max *= 4;
+ outlets =
+ (OUTLET *) G_realloc(outlets,
+ (out_max) * sizeof(OUTLET));
+ }
+
d = abs(dirs[r][c]); /* r.watershed */
- if (NOT_IN_REGION(d)) {
- next_stream = -1;
- }
- else {
- next_stream = streams[NR(d)][NC(d)];
- if (next_stream < 1)
+ if (NOT_IN_REGION(d)) {
+ next_stream = -1;
+ }
+ else {
+ next_stream = streams[NR(d)][NC(d)];
+ if (next_stream < 1)
next_stream = -1;
- }
+ }
- if (d == 0)
- next_stream = -1;
-
+ if (d == 0)
+ next_stream = -1;
+
cur_stream = streams[r][c];
if (subs && outs) { /* in stream mode subs is ignored */
- if (cur_stream != next_stream) { /* is outlet or node! */
- outlets[outlets_num].r = r;
- outlets[outlets_num++].c = c;
- }
+ if (cur_stream != next_stream) { /* is outlet or node! */
+ outlets[outlets_num].r = r;
+ outlets[outlets_num++].c = c;
+ }
}
else {
- if (next_stream < 0) { /* is outlet! */
- outlets[outlets_num].r = r;
- outlets[outlets_num++].c = c;
- }
+ if (next_stream < 0) { /* is outlet! */
+ outlets[outlets_num].r = r;
+ outlets[outlets_num++].c = c;
+ }
} /* end lasts */
- } /* end if streams */
+ } /* end if streams */
return outlets_num;
}
-int seg_find_outlets(SEGMENT* streams, int number_of_streams, SEGMENT* dirs, int subs, int outs)
+int seg_find_outlets(SEGMENT * streams, int number_of_streams, SEGMENT * dirs,
+ int subs, int outs)
{
- int d; /* d: direction */
+ int d; /* d: direction */
int r, c;
- int i,j, cat;
- int next_stream = -1, cur_stream;
+ int next_stream = -1;
int out_max = ncols + nrows;
int outlets_num;
- CELL streams_cell, streams_next_cell;
+ CELL streams_cell;
CELL dirs_cell;
G_message(_("Finding nodes..."));
outlets = (OUTLET *) G_malloc((out_max) * sizeof(OUTLET));
- outlets_num = 0;
+ outlets_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
-
- if (streams_cell>0) {
- if (outlets_num > (out_max - 1)) {
- if (outlets_num > 4 * (out_max - 1))
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+
+ if (streams_cell > 0) {
+ if (outlets_num > (out_max - 1)) {
+ if (outlets_num > 4 * (out_max - 1))
G_fatal_error
- ("Stream and direction maps probably do not match");
- out_max *= 4;
- outlets = (OUTLET *) G_realloc(outlets, (out_max) * sizeof(OUTLET));
- }
-
- segment_get(dirs,&dirs_cell,r,c);
+ ("Stream and direction maps probably do not match");
+ out_max *= 4;
+ outlets =
+ (OUTLET *) G_realloc(outlets,
+ (out_max) * sizeof(OUTLET));
+ }
+
+ segment_get(dirs, &dirs_cell, r, c);
d = abs(dirs_cell); /* r.watershed */
- if (NOT_IN_REGION(d)) {
- next_stream = -1;
- }
- else {
- segment_get(streams,&next_stream,NR(d),NC(d));
- if (next_stream < 1)
+ if (NOT_IN_REGION(d)) {
+ next_stream = -1;
+ }
+ else {
+ segment_get(streams, &next_stream, NR(d), NC(d));
+ if (next_stream < 1)
next_stream = -1;
- }
+ }
- if (d == 0)
- next_stream = -1;
-
+ if (d == 0)
+ next_stream = -1;
+
if (subs && outs) { /* in stream mode subs is ignored */
- if (streams_cell != next_stream) { /* is outlet or node! */
- outlets[outlets_num].r = r;
- outlets[outlets_num++].c = c;
- }
+ if (streams_cell != next_stream) { /* is outlet or node! */
+ outlets[outlets_num].r = r;
+ outlets[outlets_num++].c = c;
+ }
}
else {
- if (next_stream < 0) { /* is outlet! */
- outlets[outlets_num].r = r;
- outlets[outlets_num++].c = c;
- }
+ if (next_stream < 0) { /* is outlet! */
+ outlets[outlets_num].r = r;
+ outlets[outlets_num++].c = c;
+ }
} /* end lasts */
- } /* end if streams */
- } /* end for c */
+ } /* end if streams */
+ } /* end for c */
return outlets_num;
}
-int ram_init_distance(CELL** streams, FCELL** distance, int outlets_num, int outs) {
- int r, c, i;
- size_t data_size;
- data_size = Rast_cell_size(FCELL_TYPE);
+int ram_init_distance(CELL ** streams, FCELL ** distance, int outlets_num,
+ int outs)
+{
+ int r, c, i;
+ size_t data_size;
- if (!outs) { /* stream mode */
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- distance[r][c] = (streams[r][c]) ? 0 : -1;
- }
- else { /* outlets mode */
- for (r=0;r<nrows;++r)
- for (c = 0; c < ncols; ++c)
- distance[r][c]=-1;
-
- for (i = 0; i < outlets_num; ++i)
- distance[outlets[i].r][outlets[i].c] = 0;
+ data_size = Rast_cell_size(FCELL_TYPE);
+
+ if (!outs) { /* stream mode */
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ distance[r][c] = (streams[r][c]) ? 0 : -1;
+ }
+ else { /* outlets mode */
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ distance[r][c] = -1;
+
+ for (i = 0; i < outlets_num; ++i)
+ distance[outlets[i].r][outlets[i].c] = 0;
+ }
+
+ return 0;
}
-
- return 0;
+
+int seg_init_distance(SEGMENT * streams, SEGMENT * distance, int outlets_num,
+ int outs)
+{
+ int r, c, i;
+ CELL streams_cell;
+ FCELL distance_cell;
+ FCELL minus_one_cell = -1;
+ FCELL zero_cell = 0;
+
+ if (!outs) { /* stream mode */
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+ distance_cell = (streams_cell) ? 0 : -1;
+ segment_put(distance, &distance_cell, r, c);
+ }
+ }
+ else { /* outlets mode */
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ segment_put(distance, &minus_one_cell, r, c);
+
+ for (i = 0; i < outlets_num; ++i)
+ segment_put(distance, &zero_cell, outlets[i].r, outlets[i].c);
+ }
+ return 0;
}
-int seg_init_distance(SEGMENT* streams, SEGMENT* distance, int outlets_num, int outs) {
- int r, c, i;
- CELL streams_cell;
- FCELL distance_cell;
- FCELL minus_one_cell=-1;
- FCELL zero_cell=0;
+int ram_prep_null_elevation(FCELL ** distance, FCELL ** elevation)
+{
- if (!outs) { /* stream mode */
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- distance_cell=(streams_cell) ? 0 : -1;
- segment_put(distance,&distance_cell,r,c);
- }
- }
- else { /* outlets mode */
- for (r=0;r<nrows;++r)
- for (c = 0; c < ncols; ++c)
- segment_put(distance,&minus_one_cell,r,c);
+ int r, c;
- for (i = 0; i < outlets_num; ++i)
- segment_put(distance,&zero_cell,outlets[i].r,outlets[i].c);
- }
- return 0;
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (distance[r][c] == -1) {
+ elevation[r][c] = -1;
+ }
+
+ return 0;
}
-int ram_prep_null_elevation (FCELL** distance, FCELL** elevation) {
- int r,c;
- for (r=0;r<nrows;++r)
- for (c=0;c<ncols;++c)
- if(distance[r][c]==-1) {
- elevation[r][c]=-1;
- }
-}
+int seg_prep_null_elevation(SEGMENT * distance, SEGMENT * elevation)
+{
+ int r, c;
+ FCELL distance_cell;
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(distance, &distance_cell, r, c);
+ if (distance_cell == -1)
+ segment_put(elevation, &distance_cell, r, c);
+ }
-int seg_prep_null_elevation (SEGMENT* distance, SEGMENT* elevation) {
- int r,c;
- FCELL distance_cell;
-
- for (r=0;r<nrows;++r)
- for (c=0;c<ncols;++c) {
- segment_get(distance,&distance_cell,r,c);
- if(distance_cell==-1)
- segment_put(elevation,&distance_cell,r,c);
- }
-}
-
+ return 0;
+}
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,523 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- /*
- * set all cells in the map to value
- */
- int r;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
- for (r=0;r<map->nrows;++r)
- memset((map->map)[r],value,map->ncols*map->data_size);
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
-
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
Rast_put_row(output_fd, (map->map)[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
@@ -7,20 +10,21 @@
#include <grass/raster.h>
#include <grass/segment.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(r,c) (r)*ncols+(c)
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4));
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
+#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -28,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -39,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/local_proto.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/local_proto.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -2,21 +2,21 @@
#include "local_vars.h"
/* inits */
-int ram_find_outlets(CELL** streams, int number_of_streams, CELL** dirs, int subs, int outs);
-int ram_init_distance(CELL** streams, FCELL** distance, int outlets_num, int outs);
-int ram_prep_null_elevation (FCELL** distance, FCELL** elevation);
+int ram_find_outlets(CELL **streams, int number_of_streams, CELL **dirs, int subs, int outs);
+int ram_init_distance(CELL **streams, FCELL **distance, int outlets_num, int outs);
+int ram_prep_null_elevation(FCELL **distance, FCELL **elevation);
-int seg_find_outlets(SEGMENT* streams, int number_of_streams, SEGMENT* dirs, int subs, int outs);
-int seg_prep_null_elevation (SEGMENT* distance, SEGMENT* elevation);
-int seg_init_distance(SEGMENT* streams, SEGMENT* distance, int outlets_num, int outs);
+int seg_find_outlets(SEGMENT *streams, int number_of_streams, SEGMENT *dirs, int subs, int outs);
+int seg_prep_null_elevation(SEGMENT *distance, SEGMENT *elevation);
+int seg_init_distance(SEGMENT *streams, SEGMENT *distance, int outlets_num, int outs);
/* calculate */
-int ram_calculate_downstream (CELL** dirs, FCELL** distance, FCELL** elevation, OUTLET outlet, int outs);
-int ram_fill_basins(OUTLET outlet, FCELL** distance, CELL** dirs);
-int ram_calculate_upstream(FCELL** distance, CELL** dirs, FCELL** elevation, FCELL** tmp_elevation, int near);
+int ram_calculate_downstream(CELL **dirs, FCELL **distance, FCELL **elevation, OUTLET outlet, int outs);
+int ram_fill_basins(OUTLET outlet, FCELL **distance, CELL **dirs);
+int ram_calculate_upstream(FCELL **distance, CELL **dirs, FCELL **elevation, FCELL **tmp_elevation, int near);
-int seg_calculate_downstream (SEGMENT* dirs, SEGMENT* distance, SEGMENT* elevation, OUTLET outlet, int outs);
-int seg_fill_basins(OUTLET outlet, SEGMENT* distance, SEGMENT* dirs);
-int seg_calculate_upstream(SEGMENT* distance, SEGMENT* dirs, SEGMENT* elevation, SEGMENT* tmp_elevation, int near);
+int seg_calculate_downstream (SEGMENT *dirs, SEGMENT *distance, SEGMENT *elevation, OUTLET outlet, int outs);
+int seg_fill_basins(OUTLET outlet, SEGMENT *distance, SEGMENT *dirs);
+int seg_calculate_upstream(SEGMENT *distance, SEGMENT *dirs, SEGMENT *elevation, SEGMENT *tmp_elevation, int near);
Modified: grass-addons/grass7/raster/r.stream/r.stream.distance/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.distance/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.distance/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,4 @@
+
/****************************************************************************
*
* MODULE: r.stream.distance
@@ -24,278 +25,292 @@
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 process_cat(char** cat_list);
+int process_cat(char **cat_list);
int main(int argc, char *argv[])
{
- struct GModule *module;
- struct Option *in_dir_opt,
- *in_stm_opt,
- *in_stm_cats_opt,
- *in_elev_opt,
- *in_method_opt,
- *opt_swapsize,
- *out_dist_opt,
- *out_diff_opt;
- struct Flag *flag_outs,
- *flag_sub,
- *flag_near,
- *flag_segmentation;
+ struct GModule *module;
+ struct Option *in_dir_opt,
+ *in_stm_opt,
+ *in_elev_opt,
+ *in_method_opt, *opt_swapsize, *out_dist_opt, *out_diff_opt;
+ struct Flag *flag_outs, *flag_sub, *flag_near, *flag_segmentation;
char *method_name[] = { "UPSTREAM", "DOWNSTREAM" };
int method;
int number_of_segs;
int outlets_num;
int number_of_streams;
- int outs, subs, near, segmentation; /*flags */
+ int outs, subs, near, segmentation; /*flags */
int j;
-
+
G_gisinit(argv[0]);
- module = G_define_module();
- module->description =
+ module = G_define_module();
+ module->description =
_("Calculate distance to and alavation above streams \
and outlets according user' input. It can work in stream mode where target are streams and outlets mode \
where targets are outlets");
- G_add_keyword("watercourse distance");
- G_add_keyword("relative elevation");
-
- in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_stm_opt->key = "stream";
- in_stm_opt->description = "Name of streams (outlets) mask input map";
-
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_dir_opt->key = "dirs";
- in_dir_opt->description = "Name of flow direction input map";
+ G_add_keyword("watercourse distance");
+ G_add_keyword("relative elevation");
- in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_elev_opt->key = "elevation";
- in_elev_opt->required = NO;
- in_elev_opt->description = "Name of elevation map";
+ in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_stm_opt->key = "stream";
+ in_stm_opt->description = "Name of streams (outlets) mask input map";
- in_method_opt = G_define_option();
- in_method_opt->key = "method";
- in_method_opt->description = "Calculation method";
- in_method_opt->type = TYPE_STRING;
- in_method_opt->required = YES;
- in_method_opt->options = "upstream,downstream";
- in_method_opt->answer = "downstream";
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_dir_opt->key = "dirs";
+ in_dir_opt->description = "Name of flow direction input map";
- out_dist_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_dist_opt->key = "distance";
- out_dist_opt->required=NO;
- out_dist_opt->description = "Output distance/accumulation map";
+ in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_elev_opt->key = "elevation";
+ in_elev_opt->required = NO;
+ in_elev_opt->description = "Name of elevation map";
- out_diff_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_diff_opt->key = "difference";
- out_diff_opt->required=NO;
- out_diff_opt->description = "Output elevation difference map";
-
- flag_outs = G_define_flag();
- flag_outs->key = 'o';
- flag_outs->description =
+ in_method_opt = G_define_option();
+ in_method_opt->key = "method";
+ in_method_opt->description = "Calculation method";
+ in_method_opt->type = TYPE_STRING;
+ in_method_opt->required = YES;
+ in_method_opt->options = "upstream,downstream";
+ in_method_opt->answer = "downstream";
+
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
+
+ out_dist_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_dist_opt->key = "distance";
+ out_dist_opt->required = NO;
+ out_dist_opt->description = "Output distance/accumulation map";
+
+ out_diff_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_diff_opt->key = "difference";
+ out_diff_opt->required = NO;
+ out_diff_opt->description = "Output elevation difference map";
+
+ flag_outs = G_define_flag();
+ flag_outs->key = 'o';
+ flag_outs->description =
_("Calculate parameters for outlets (outlet mode) instead of (default) streams");
- flag_sub = G_define_flag();
- flag_sub->key = 's';
- flag_sub->description =
+ flag_sub = G_define_flag();
+ flag_sub->key = 's';
+ flag_sub->description =
_("Calculate parameters for subbasins (ignored in stream mode)");
- flag_near = G_define_flag();
- flag_near->key = 'n';
- flag_near->description =
+ flag_near = G_define_flag();
+ flag_near->key = 'n';
+ flag_near->description =
_("Calculate nearest local maximum (ignored in downstream calculation)");
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
-
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
-
+
if (!out_diff_opt->answer && !out_dist_opt->answer)
G_fatal_error(_("You must select at least one output maps: distance and (or) elevation"));
if (out_diff_opt->answer && !in_elev_opt->answer)
G_fatal_error(_("If you select elevation output, elevation is required"));
-
- if(out_dist_opt->answer)
- if (G_legal_filename(out_dist_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal distance name"), out_dist_opt->answer);
-
- if(out_diff_opt->answer)
- if (G_legal_filename(out_diff_opt->answer) < 0)
- G_fatal_error(_("<%s> is an illegal elevation difference name"), out_diff_opt->answer);
-
- if (!strcmp(in_method_opt->answer, "upstream"))
+
+ if (out_dist_opt->answer)
+ if (G_legal_filename(out_dist_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal distance name"),
+ out_dist_opt->answer);
+
+ if (out_diff_opt->answer)
+ if (G_legal_filename(out_diff_opt->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal elevation difference name"),
+ out_diff_opt->answer);
+
+ if (!strcmp(in_method_opt->answer, "upstream"))
method = UPSTREAM;
else if (!strcmp(in_method_opt->answer, "downstream"))
method = DOWNSTREAM;
-
- outs = (flag_outs->answer != 0);
- subs = (flag_sub->answer != 0);
- near = (flag_near->answer != 0);
- segmentation = (flag_segmentation->answer != 0);
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
- G_begin_distance_calculations();
+ outs = (flag_outs->answer != 0);
+ subs = (flag_sub->answer != 0);
+ near = (flag_near->answer != 0);
+ segmentation = (flag_segmentation->answer != 0);
- fifo_max=4* nrows*ncols;
- fifo_points=(POINT *)G_malloc((fifo_max+1)*sizeof(POINT));
-
-if(!segmentation) {
- G_message(_("ALL IN RAM CALCULATION - METHOD: %s"),method_name[method]);
- MAP map_dirs, map_streams, map_distance, map_elevation, map_tmp_elevation;
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+ G_begin_distance_calculations();
+
+ fifo_max = 4 * nrows * ncols;
+ fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+
+ if (!segmentation) {
+ G_message(_("ALL IN RAM CALCULATION - METHOD: %s"),
+ method_name[method]);
+ MAP map_dirs, map_streams, map_distance, map_elevation,
+ map_tmp_elevation;
CELL **streams, **dirs;
- FCELL **distance;
- FCELL** elevation=NULL;
- FCELL** tmp_elevation=NULL;
+ FCELL **distance;
+ FCELL **elevation = NULL;
+ FCELL **tmp_elevation = NULL;
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_distance,FCELL_TYPE);
-
- streams=(CELL**)map_streams.map;
- dirs=(CELL**)map_dirs.map;
- distance=(FCELL**)map_distance.map;
- number_of_streams = (int)map_streams.max+1;
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_distance, FCELL_TYPE);
- outlets_num=ram_find_outlets(streams, number_of_streams, dirs,subs,outs);
- ram_init_distance(streams, distance,outlets_num, outs);
+ streams = (CELL **) map_streams.map;
+ dirs = (CELL **) map_dirs.map;
+ distance = (FCELL **) map_distance.map;
+ number_of_streams = (int)map_streams.max + 1;
+
+ outlets_num =
+ ram_find_outlets(streams, number_of_streams, dirs, subs, outs);
+ ram_init_distance(streams, distance, outlets_num, outs);
ram_release_map(&map_streams);
- if(in_elev_opt->answer) {
- ram_create_map(&map_elevation,FCELL_TYPE);
- ram_read_map(&map_elevation,in_elev_opt->answer,0,-1);
- elevation=(FCELL**)map_elevation.map;
- } /* map elevation will be replaced by elevation difference map */
-
-
- if(method == DOWNSTREAM) {
- G_message(_("Calculate downstream parameters..."));
- for(j=0; j<outlets_num;++j) {
- G_percent(j,outlets_num,1);
- ram_calculate_downstream(dirs, distance, elevation, outlets[j],outs);
- }
- G_percent(j,outlets_num,1);
-
- } else if(method == UPSTREAM) {
+ if (in_elev_opt->answer) {
+ ram_create_map(&map_elevation, FCELL_TYPE);
+ ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+ elevation = (FCELL **) map_elevation.map;
+ } /* map elevation will be replaced by elevation difference map */
- if(out_diff_opt->answer) {
- ram_create_map(&map_tmp_elevation,FCELL_TYPE);
- tmp_elevation=(FCELL**)map_tmp_elevation.map;
- }
- for(j=0; j<outlets_num;++j)
- ram_fill_basins(outlets[j],distance,dirs);
- ram_calculate_upstream(distance,dirs,elevation,tmp_elevation, near);
+ if (method == DOWNSTREAM) {
+ G_message(_("Calculate downstream parameters..."));
+ for (j = 0; j < outlets_num; ++j) {
+ G_percent(j, outlets_num, 1);
+ ram_calculate_downstream(dirs, distance, elevation,
+ outlets[j], outs);
+ }
+ G_percent(j, outlets_num, 1);
- } else {
- G_fatal_error(_("Unrecognised method of processing"));
- } /* end methods */
+ }
+ else if (method == UPSTREAM) {
- if(out_diff_opt->answer) {
- ram_prep_null_elevation(distance,elevation);
- ram_write_map(&map_elevation, out_diff_opt->answer, FCELL_TYPE, 1, -1);
- }
+ if (out_diff_opt->answer) {
+ ram_create_map(&map_tmp_elevation, FCELL_TYPE);
+ tmp_elevation = (FCELL **) map_tmp_elevation.map;
+ }
- if(out_dist_opt->answer)
- ram_write_map(&map_distance, out_dist_opt->answer, FCELL_TYPE, 1, -1);
+ for (j = 0; j < outlets_num; ++j)
+ ram_fill_basins(outlets[j], distance, dirs);
+ ram_calculate_upstream(distance, dirs, elevation, tmp_elevation,
+ near);
+ }
+ else {
+ G_fatal_error(_("Unrecognised method of processing"));
+ } /* end methods */
+
+ if (out_diff_opt->answer) {
+ ram_prep_null_elevation(distance, elevation);
+ ram_write_map(&map_elevation, out_diff_opt->answer, FCELL_TYPE, 1,
+ -1);
+ }
+
+ if (out_dist_opt->answer)
+ ram_write_map(&map_distance, out_dist_opt->answer, FCELL_TYPE, 1,
+ -1);
+
ram_release_map(&map_dirs);
ram_release_map(&map_distance);
- if(in_elev_opt->answer)
- ram_release_map(&map_elevation);
- if(in_elev_opt->answer && method==UPSTREAM)
- ram_release_map(&map_tmp_elevation);
-}
+ if (in_elev_opt->answer)
+ ram_release_map(&map_elevation);
+ if (in_elev_opt->answer && method == UPSTREAM)
+ ram_release_map(&map_tmp_elevation);
+ }
-if(segmentation) {
- G_message(_("MEMORY SWAP CALCULATION - METHOD: %s MAY TAKE SOME TIME"),method_name[method]);
- SEG map_dirs, map_streams, map_distance, map_elevation, map_tmp_elevation;
+ if (segmentation) {
+ G_message(_("MEMORY SWAP CALCULATION - METHOD: %s MAY TAKE SOME TIME"),
+ method_name[method]);
+ SEG map_dirs, map_streams, map_distance, map_elevation,
+ map_tmp_elevation;
SEGMENT *streams, *dirs, *distance;
- SEGMENT *elevation=NULL;
- SEGMENT *tmp_elevation=NULL;
-
+ SEGMENT *elevation = NULL;
+ SEGMENT *tmp_elevation = NULL;
+
number_of_segs = (int)atof(opt_swapsize->answer);
- if (method==DOWNSTREAM)
- number_of_segs < 32 ? (int)(32/0.18) : number_of_segs/0.18;
- else /* two elevation maps */
- number_of_segs < 32 ? (int)(32/0.24) : number_of_segs/0.24;
-
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs,CELL_TYPE);
- seg_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_distance,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
-
- streams=&map_streams.seg;
- dirs=&map_dirs.seg;
- distance=&map_distance.seg;
- number_of_streams = (int)map_streams.max+1;
-
- outlets_num = seg_find_outlets(streams,number_of_streams,dirs,subs,outs);
- seg_init_distance(streams,distance,outlets_num,outs);
+ if (method == DOWNSTREAM)
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
+ else /* two elevation maps */
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.24) : number_of_segs / 0.24;
+
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_distance, SROWS, SCOLS, number_of_segs,
+ FCELL_TYPE);
+
+ streams = &map_streams.seg;
+ dirs = &map_dirs.seg;
+ distance = &map_distance.seg;
+ number_of_streams = (int)map_streams.max + 1;
+
+ outlets_num =
+ seg_find_outlets(streams, number_of_streams, dirs, subs, outs);
+ seg_init_distance(streams, distance, outlets_num, outs);
seg_release_map(&map_streams);
- if(in_elev_opt->answer) {
- seg_create_map(&map_elevation,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
- seg_read_map(&map_elevation,in_elev_opt->answer,0,-1);
- elevation = &map_elevation.seg;
- } /* map elevation will be replaced by elevation difference map */
-
- if(method == DOWNSTREAM) {
- G_message(_("Calculate downstream parameters..."));
- for(j=0; j<outlets_num;++j) {
- G_percent(j,outlets_num,1);
- seg_calculate_downstream(dirs, distance, elevation, outlets[j],outs);
- }
- G_percent(j,outlets_num,1);
-
- } else if(method == UPSTREAM) {
+ if (in_elev_opt->answer) {
+ seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
+ FCELL_TYPE);
+ seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+ elevation = &map_elevation.seg;
+ } /* map elevation will be replaced by elevation difference map */
- if(out_diff_opt->answer) {
- seg_create_map(&map_tmp_elevation,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
- tmp_elevation=&map_tmp_elevation.seg;
- }
+ if (method == DOWNSTREAM) {
+ G_message(_("Calculate downstream parameters..."));
+ for (j = 0; j < outlets_num; ++j) {
+ G_percent(j, outlets_num, 1);
+ seg_calculate_downstream(dirs, distance, elevation,
+ outlets[j], outs);
+ }
+ G_percent(j, outlets_num, 1);
- for(j=0; j<outlets_num;++j)
- seg_fill_basins(outlets[j],distance,dirs);
- seg_calculate_upstream(distance,dirs,elevation,tmp_elevation, near);
+ }
+ else if (method == UPSTREAM) {
- } else {
- G_fatal_error(_("Unrecognised method of processing"));
- } /* end methods */
+ if (out_diff_opt->answer) {
+ seg_create_map(&map_tmp_elevation, SROWS, SCOLS,
+ number_of_segs, FCELL_TYPE);
+ tmp_elevation = &map_tmp_elevation.seg;
+ }
- if(out_dist_opt->answer)
- seg_write_map(&map_distance, out_dist_opt->answer, FCELL_TYPE, 1, -1);
+ for (j = 0; j < outlets_num; ++j)
+ seg_fill_basins(outlets[j], distance, dirs);
+ seg_calculate_upstream(distance, dirs, elevation, tmp_elevation,
+ near);
- if(out_diff_opt->answer) {
- seg_prep_null_elevation(distance,elevation);
- seg_write_map(&map_elevation, out_diff_opt->answer, FCELL_TYPE, 1, -1);
- }
+ }
+ else {
+ G_fatal_error(_("Unrecognised method of processing"));
+ } /* end methods */
+ if (out_dist_opt->answer)
+ seg_write_map(&map_distance, out_dist_opt->answer, FCELL_TYPE, 1,
+ -1);
+
+ if (out_diff_opt->answer) {
+ seg_prep_null_elevation(distance, elevation);
+ seg_write_map(&map_elevation, out_diff_opt->answer, FCELL_TYPE, 1,
+ -1);
+ }
+
seg_release_map(&map_dirs);
seg_release_map(&map_distance);
- if(in_elev_opt->answer)
- seg_release_map(&map_elevation);
- if(in_elev_opt->answer && method==UPSTREAM)
- seg_release_map(&map_tmp_elevation);
-}
+ if (in_elev_opt->answer)
+ seg_release_map(&map_elevation);
+ if (in_elev_opt->answer && method == UPSTREAM)
+ seg_release_map(&map_tmp_elevation);
+ }
- G_free(fifo_points);
- exit(EXIT_SUCCESS);
+ G_free(fifo_points);
+ exit(EXIT_SUCCESS);
}
-
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,523 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- /*
- * set all cells in the map to value
- */
- int r;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
- for (r=0;r<map->nrows;++r)
- memset((map->map)[r],value,map->ncols*map->data_size);
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
-
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
Rast_put_row(output_fd, (map->map)[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
@@ -7,20 +10,21 @@
#include <grass/raster.h>
#include <grass/segment.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(r,c) (r)*ncols+(c)
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4));
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
+#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -28,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -39,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,4 @@
+
/****************************************************************************
*
* MODULE: r.stream.order
@@ -27,249 +28,260 @@
int main(int argc, char *argv[])
{
- IO input[] = {
- {"streams",YES,"output of r.stream.extract or r.watershed"},
- {"dirs",YES,"output of r.stream.extract or r.watershed"},
- {"elevation",NO,"any type digital elevation model"},
- {"accum",NO,"any type acc map created by r.watershed or used in r.stream.extract"}
- };
-
- /* add new basic rdering here and in local_vars.h declaration
- * it will be added to output without addational programing.
- * Ordering functions are to be added in stream_order.c file.
- * Addational operations may shall be done in common part sections
- * derivative orders (like Scheideggers/Shreve) shall be added only
- * to table definition as a formula and to description file. */
- IO output[] = {
- {"strahler",NO,"Strahler's stream order"},
- {"horton",NO,"Original Hortons's stream order"},
- {"shreve",NO,"Shereve's stream magnitude"},
- {"hack",NO,"Hack's streams or Gravelius stream hierarchy"},
- {"topo",NO,"Topological dimension of streams"}
- };
- struct GModule *module; /* GRASS module for parsing arguments */
-
- struct Option *opt_input[input_size];
- struct Option *opt_output[orders_size];
- struct Option *opt_swapsize;
- struct Option *opt_vector;
- struct Flag *flag_zerofill, * flag_accum, *flag_segmentation;
-
- int output_num;
- int num_seg; /* number of segments */
- int segmentation, zerofill;
- int i; /* iteration vars */
- int number_of_segs;
- int number_of_streams;
- char *in_streams=NULL, *in_dirs=NULL, *in_elev=NULL, *in_accum=NULL;
- char *out_vector=NULL;
-
- G_gisinit(argv[0]);
+ IO input[] = {
+ {"streams", YES, "output of r.stream.extract or r.watershed"},
+ {"dirs", YES, "output of r.stream.extract or r.watershed"},
+ {"elevation", NO, "any type digital elevation model"},
+ {"accum", NO,
+ "any type acc map created by r.watershed or used in r.stream.extract"}
+ };
- module = G_define_module();
- module->description =
+ /* add new basic rdering here and in local_vars.h declaration
+ * it will be added to output without addational programing.
+ * Ordering functions are to be added in stream_order.c file.
+ * Addational operations may shall be done in common part sections
+ * derivative orders (like Scheideggers/Shreve) shall be added only
+ * to table definition as a formula and to description file. */
+ IO output[] = {
+ {"strahler", NO, "Strahler's stream order"},
+ {"horton", NO, "Original Hortons's stream order"},
+ {"shreve", NO, "Shereve's stream magnitude"},
+ {"hack", NO, "Hack's streams or Gravelius stream hierarchy"},
+ {"topo", NO, "Topological dimension of streams"}
+ };
+ struct GModule *module; /* GRASS module for parsing arguments */
+
+ struct Option *opt_input[input_size];
+ struct Option *opt_output[orders_size];
+ struct Option *opt_swapsize;
+ struct Option *opt_vector;
+ struct Flag *flag_zerofill, *flag_accum, *flag_segmentation;
+
+ int output_num = 0;
+ int num_seg; /* number of segments */
+ int segmentation, zerofill;
+ int i; /* iteration vars */
+ int number_of_segs;
+ int number_of_streams;
+ char *in_streams = NULL, *in_dirs = NULL, *in_elev = NULL, *in_accum =
+ NULL;
+ char *out_vector = NULL;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->description =
_("Calculate Strahler's and more streams hierarchy. Basic module for topological analysis of drainage network");
- G_add_keyword("Strahler Stream Order");
- G_add_keyword("Hack Streams");
- G_add_keyword("Stream network topology");
- G_add_keyword("Stream network geometry");
- G_add_keyword("Nework vectorisation");
+ G_add_keyword("Strahler Stream Order");
+ G_add_keyword("Hack Streams");
+ G_add_keyword("Stream network topology");
+ G_add_keyword("Stream network geometry");
+ G_add_keyword("Nework vectorisation");
- for (i=0;i<input_size;++i) {
+ for (i = 0; i < input_size; ++i) {
opt_input[i] = G_define_standard_option(G_OPT_R_INPUT);
- opt_input[i]->key = input[i].name;
- opt_input[i]->required = input[i].required;
- opt_input[i]->description = _(input[i].description);
- }
-
- opt_vector = G_define_standard_option(G_OPT_V_OUTPUT);
- opt_vector->key="vector";
- opt_vector->required = NO;
- opt_vector->description =_("OUTPUT vector file to write stream atributes");
- opt_vector->guisection=_("Output");
+ opt_input[i]->key = input[i].name;
+ opt_input[i]->required = input[i].required;
+ opt_input[i]->description = _(input[i].description);
+ }
- for (i=0;i<orders_size;++i) {
+ opt_vector = G_define_standard_option(G_OPT_V_OUTPUT);
+ opt_vector->key = "vector";
+ opt_vector->required = NO;
+ opt_vector->description =
+ _("OUTPUT vector file to write stream atributes");
+ opt_vector->guisection = _("Output");
+
+ for (i = 0; i < orders_size; ++i) {
opt_output[i] = G_define_standard_option(G_OPT_R_OUTPUT);
- opt_output[i]->key = output[i].name;
- opt_output[i]->required = NO;
- opt_output[i]->description = _(output[i].description);
- opt_output[i]->guisection = _("Output");
- }
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
+ opt_output[i]->key = output[i].name;
+ opt_output[i]->required = NO;
+ opt_output[i]->description = _(output[i].description);
+ opt_output[i]->guisection = _("Output");
+ }
- flag_zerofill = G_define_flag();
- flag_zerofill->key = 'z';
- flag_zerofill->description = _("Create zero-valued background instead of NULL");
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
-
- flag_accum = G_define_flag();
- flag_accum->key = 'a';
- flag_accum->description = _("Use flow accumulation to trace horton and hack models");
-
+ flag_zerofill = G_define_flag();
+ flag_zerofill->key = 'z';
+ flag_zerofill->description =
+ _("Create zero-valued background instead of NULL");
+
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+
+ flag_accum = G_define_flag();
+ flag_accum->key = 'a';
+ flag_accum->description =
+ _("Use flow accumulation to trace horton and hack models");
+
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
-
- /* check output names */
- zerofill=(flag_zerofill->answer != 0);
- segmentation=(flag_segmentation->answer != 0);
- use_accum = (flag_accum->answer != 0);
- use_vector = (opt_vector->answer!=NULL);
-
- number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32/0.12) : number_of_segs/0.12;
- if(use_vector)
- if(!opt_input[o_elev]->answer || !opt_input[o_accum]->answer)
- G_fatal_error(_("To calculate vector file both accum and elev are required"));
- if(use_accum)
- if(!opt_input[o_accum]->answer)
- G_fatal_error(_("with -a (use accumulation) accum map is required"));
-
- for (i=0;i<orders_size;++i) {
- if(!opt_output[i]->answer)
- continue;
- if (G_legal_filename(opt_output[i]->answer) < 0)
- G_fatal_error(_("<%s> is an illegal file name"), opt_output[i]->answer);
+ /* check output names */
+ zerofill = (flag_zerofill->answer != 0);
+ segmentation = (flag_segmentation->answer != 0);
+ use_accum = (flag_accum->answer != 0);
+ use_vector = (opt_vector->answer != NULL);
+
+ number_of_segs = (int)atof(opt_swapsize->answer);
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
+
+ if (use_vector)
+ if (!opt_input[o_elev]->answer || !opt_input[o_accum]->answer)
+ G_fatal_error(_("To calculate vector file both accum and elev are required"));
+ if (use_accum)
+ if (!opt_input[o_accum]->answer)
+ G_fatal_error(_("with -a (use accumulation) accum map is required"));
+
+ for (i = 0; i < orders_size; ++i) {
+ if (!opt_output[i]->answer)
+ continue;
+ if (G_legal_filename(opt_output[i]->answer) < 0)
+ G_fatal_error(_("<%s> is an illegal file name"),
+ opt_output[i]->answer);
output_num++;
- } /* end for */
- if (!output_num && !opt_vector->answer)
+ } /* end for */
+ if (!output_num && !opt_vector->answer)
G_fatal_error(_("You must select one or more output orders maps or insert the table name"));
-
+
/* start */
- in_streams=opt_input[o_streams]->answer;
- in_dirs=opt_input[o_dirs]->answer;
- in_elev=opt_input[o_elev]->answer;
- in_accum=opt_input[o_accum]->answer;
- out_vector=opt_vector->answer;
-
- output_map_names=(char**)G_malloc(orders_size*sizeof(char*));
- for(i=0;i<orders_size;++i)
- output_map_names[i] = opt_output[i]->answer;
-
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
-
- /* ALL IN RAM VERSION */
- if (!segmentation) {
+ in_streams = opt_input[o_streams]->answer;
+ in_dirs = opt_input[o_dirs]->answer;
+ in_elev = opt_input[o_elev]->answer;
+ in_accum = opt_input[o_accum]->answer;
+ out_vector = opt_vector->answer;
+
+ output_map_names = (char **)G_malloc(orders_size * sizeof(char *));
+ for (i = 0; i < orders_size; ++i)
+ output_map_names[i] = opt_output[i]->answer;
+
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+
+ /* ALL IN RAM VERSION */
+ if (!segmentation) {
G_message("ALL IN RAM CALCULATION");
MAP map_streams, map_dirs;
CELL **streams, **dirs;
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_streams,1,CELL_TYPE);
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dirs,1,CELL_TYPE);
- stream_init((int)map_streams.min,(int)map_streams.max);
- number_of_streams=(int)(map_streams.max+1);
- streams=(CELL**)map_streams.map;
- dirs=(CELL**)map_dirs.map;
-
- ram_stream_topology(streams,dirs,number_of_streams);
-
- if(out_vector || output_map_names[o_horton] ||
- output_map_names[o_hack] || output_map_names[o_topo])
- ram_stream_geometry(streams,dirs);
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_streams, 1, CELL_TYPE);
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dirs, 1, CELL_TYPE);
+ stream_init((int)map_streams.min, (int)map_streams.max);
+ number_of_streams = (int)(map_streams.max + 1);
+ streams = (CELL **) map_streams.map;
+ dirs = (CELL **) map_dirs.map;
-/* common part */
- if(use_vector) {
- stream_sample_map(in_elev, number_of_streams, 0);
- stream_sample_map(in_elev, number_of_streams, 1);
- }
- if(use_accum || use_vector)
- stream_sample_map(in_accum, number_of_streams, 2);
+ ram_stream_topology(streams, dirs, number_of_streams);
- if( output_map_names[o_strahler] || output_map_names[o_horton] || out_vector)
- strahler(all_orders[o_strahler]);
+ if (out_vector || output_map_names[o_horton] ||
+ output_map_names[o_hack] || output_map_names[o_topo])
+ ram_stream_geometry(streams, dirs);
- if(output_map_names[o_horton] || out_vector)
- horton(all_orders[o_strahler],all_orders[o_horton], number_of_streams);
+ /* common part */
+ if (use_vector) {
+ stream_sample_map(in_elev, number_of_streams, 0);
+ stream_sample_map(in_elev, number_of_streams, 1);
+ }
+ if (use_accum || use_vector)
+ stream_sample_map(in_accum, number_of_streams, 2);
- if(output_map_names[o_shreve] || out_vector)
- shreve(all_orders[o_shreve]);
+ if (output_map_names[o_strahler] || output_map_names[o_horton] ||
+ out_vector)
+ strahler(all_orders[o_strahler]);
- if(output_map_names[o_hack] || output_map_names[o_topo] || out_vector)
- hack(all_orders[o_hack],all_orders[o_topo],number_of_streams);
-/* end of common part */
+ if (output_map_names[o_horton] || out_vector)
+ horton(all_orders[o_strahler], all_orders[o_horton],
+ number_of_streams);
- if(out_vector)
- ram_create_vector(streams, dirs, out_vector, number_of_streams);
-
+ if (output_map_names[o_shreve] || out_vector)
+ shreve(all_orders[o_shreve]);
+
+ if (output_map_names[o_hack] || output_map_names[o_topo] ||
+ out_vector)
+ hack(all_orders[o_hack], all_orders[o_topo], number_of_streams);
+ /* end of common part */
+
+ if (out_vector)
+ ram_create_vector(streams, dirs, out_vector, number_of_streams);
+
ram_close_raster_order(streams, number_of_streams, zerofill);
ram_release_map(&map_streams);
ram_release_map(&map_dirs);
/* end ram section */
-}
+ }
- /* SEGMENTATION VERSION */
-if (segmentation) {
- G_message("MEMORY SWAP CALCULATION: MAY TAKE SOME TIME!");
-
+ /* SEGMENTATION VERSION */
+ if (segmentation) {
+ G_message("MEMORY SWAP CALCULATION: MAY TAKE SOME TIME!");
+
SEG map_streams, map_dirs;
SEGMENT *streams, *dirs;
-
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_streams,1,CELL_TYPE);
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_dirs,in_dirs,1,CELL_TYPE);
- stream_init((int)map_streams.min,(int)map_streams.max);
- number_of_streams=(int)(map_streams.max+1);
- streams=&map_streams.seg;
- dirs=&map_dirs.seg;
- seg_stream_topology(streams,dirs, number_of_streams);
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_streams, in_streams, 1, CELL_TYPE);
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dirs, 1, CELL_TYPE);
+ stream_init((int)map_streams.min, (int)map_streams.max);
+ number_of_streams = (int)(map_streams.max + 1);
+ streams = &map_streams.seg;
+ dirs = &map_dirs.seg;
- if(out_vector || output_map_names[o_horton] ||
- output_map_names[o_hack] || output_map_names[o_topo])
- seg_stream_geometry(streams,dirs);
+ seg_stream_topology(streams, dirs, number_of_streams);
-/* common part */
- if(use_vector) {
- stream_sample_map(in_elev, number_of_streams, 0);
- stream_sample_map(in_elev, number_of_streams, 1);
- }
- if(use_accum || use_vector)
- stream_sample_map(in_accum, number_of_streams, 2);
+ if (out_vector || output_map_names[o_horton] ||
+ output_map_names[o_hack] || output_map_names[o_topo])
+ seg_stream_geometry(streams, dirs);
- if( output_map_names[o_strahler] || output_map_names[o_horton] || out_vector)
- strahler(all_orders[o_strahler]);
+ /* common part */
+ if (use_vector) {
+ stream_sample_map(in_elev, number_of_streams, 0);
+ stream_sample_map(in_elev, number_of_streams, 1);
+ }
+ if (use_accum || use_vector)
+ stream_sample_map(in_accum, number_of_streams, 2);
- if(output_map_names[o_horton] || out_vector)
- horton(all_orders[o_strahler],all_orders[o_horton], number_of_streams);
+ if (output_map_names[o_strahler] || output_map_names[o_horton] ||
+ out_vector)
+ strahler(all_orders[o_strahler]);
- if(output_map_names[o_shreve] || out_vector)
- shreve(all_orders[o_shreve]);
+ if (output_map_names[o_horton] || out_vector)
+ horton(all_orders[o_strahler], all_orders[o_horton],
+ number_of_streams);
- if(output_map_names[o_hack] || output_map_names[o_topo] || out_vector)
- hack(all_orders[o_hack],all_orders[o_topo],number_of_streams);
-/* end of common part */
+ if (output_map_names[o_shreve] || out_vector)
+ shreve(all_orders[o_shreve]);
- if(out_vector)
- seg_create_vector(streams,dirs, out_vector, number_of_streams);
-
+ if (output_map_names[o_hack] || output_map_names[o_topo] ||
+ out_vector)
+ hack(all_orders[o_hack], all_orders[o_topo], number_of_streams);
+ /* end of common part */
+
+ if (out_vector)
+ seg_create_vector(streams, dirs, out_vector, number_of_streams);
+
seg_close_raster_order(streams, number_of_streams, zerofill);
seg_release_map(&map_streams);
seg_release_map(&map_dirs);
/* end segmentation section */
-}
+ }
-/* free */
-G_free(stream_attributes);
-G_free(init_streams);
-G_free(outlet_streams);
-G_free(init_cells);
+ /* free */
+ G_free(stream_attributes);
+ G_free(init_streams);
+ G_free(outlet_streams);
+ G_free(init_cells);
exit(EXIT_SUCCESS);
}
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/stream_init.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/stream_init.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/stream_init.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,15 +1,17 @@
#include "local_proto.h"
-int stream_init(int min_index_of_stream, int max_index_of_stream) {
- int i,j;
- int number_of_streams=max_index_of_stream;
+int stream_init(int min_index_of_stream, int max_index_of_stream)
+{
+ int i, j;
+ int number_of_streams = max_index_of_stream;
- if(max_index_of_stream==0)
+ if (max_index_of_stream == 0)
G_fatal_error(_("Empty stream input map: check if a stream map"));
- if(min_index_of_stream<0)
+ if (min_index_of_stream < 0)
G_fatal_error(_("Stream map has negative values: check if a stream map"));
- stream_attributes = (STREAM *) G_malloc((number_of_streams + 1) * sizeof(STREAM));
- for (i = 0; i < max_index_of_stream+1; ++i) {
+ stream_attributes =
+ (STREAM *) G_malloc((number_of_streams + 1) * sizeof(STREAM));
+ for (i = 0; i < max_index_of_stream + 1; ++i) {
stream_attributes[i].next_stream = -1;
stream_attributes[i].stream = -1;
stream_attributes[i].trib_num = -1;
@@ -18,9 +20,9 @@
stream_attributes[i].trib[2] = 0;
stream_attributes[i].trib[3] = 0;
stream_attributes[i].trib[4] = 0;
- stream_attributes[i].cells_num=0;
- stream_attributes[i].init=0;
- stream_attributes[i].outlet=0;
+ stream_attributes[i].cells_num = 0;
+ stream_attributes[i].init = 0;
+ stream_attributes[i].outlet = 0;
stream_attributes[i].length = 0.;
stream_attributes[i].accum_length = 0.;
stream_attributes[i].distance = 0.;
@@ -29,94 +31,104 @@
stream_attributes[i].init_elev = -10000;
stream_attributes[i].outlet_elev = -10000;
}
-
- all_orders=(int**)G_malloc(orders_size*sizeof(int*));
- for(i=0;i<orders_size;++i)
- all_orders[i]=(int*)G_malloc((number_of_streams+1)*sizeof(int));
- for(i=0;i<orders_size;++i)
- for(j=0;j<number_of_streams+1;++j)
- all_orders[i][j]=-1;
+ all_orders = (int **)G_malloc(orders_size * sizeof(int *));
+ for (i = 0; i < orders_size; ++i)
+ all_orders[i] =
+ (int *)G_malloc((number_of_streams + 1) * sizeof(int));
+ for (i = 0; i < orders_size; ++i)
+ for (j = 0; j < number_of_streams + 1; ++j)
+ all_orders[i][j] = -1;
+
return 0;
}
-int compare_inits (const void *a, const void *b) {
- return (*(STREAM **)a)->init - (*(STREAM **)b)->init;
+int compare_inits(const void *a, const void *b)
+{
+ return (*(STREAM **) a)->init - (*(STREAM **) b)->init;
}
-int compare_outlets (const void *a, const void *b) {
- return (*(STREAM **)a)->outlet - (*(STREAM **)b)->outlet;
+int compare_outlets(const void *a, const void *b)
+{
+ return (*(STREAM **) a)->outlet - (*(STREAM **) b)->outlet;
}
-int stream_sample_map(char* input_map_name, int number_of_streams, int what) {
-
- /* if what == 2 sample outputs only for accum map */
- /* if what == 1 sample outputs only for elev map */
- /* if what == 0 sample inits only for elev map) */
-
- char* mapset;
- int input_map_fd;
- int i;
- int r, c, cur_row=-1;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer;
- void* input_ptr;
-
- STREAM** pointers_to_stream;
- pointers_to_stream=(STREAM**)G_malloc(sizeof(STREAM*)*(number_of_streams));
- for (i=0;i<(number_of_streams);++i)
- *(pointers_to_stream+i)=stream_attributes+i;
-
- if(what)
- qsort(pointers_to_stream, (number_of_streams), sizeof(STREAM*), compare_outlets);
- else
- qsort(pointers_to_stream, (number_of_streams), sizeof(STREAM*), compare_inits);
+int stream_sample_map(char *input_map_name, int number_of_streams, int what)
+{
+ /* if what == 2 sample outputs only for accum map */
+ /* if what == 1 sample outputs only for elev map */
+ /* if what == 0 sample inits only for elev map) */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ char *mapset;
+ int input_map_fd;
+ int i;
+ int r, c, cur_row = -1;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer;
+ void *input_ptr;
+
+ STREAM **pointers_to_stream;
+
+ pointers_to_stream =
+ (STREAM **) G_malloc(sizeof(STREAM *) * (number_of_streams));
+ for (i = 0; i < (number_of_streams); ++i)
+ *(pointers_to_stream + i) = stream_attributes + i;
+
+ if (what)
+ qsort(pointers_to_stream, (number_of_streams), sizeof(STREAM *),
+ compare_outlets);
+ else
+ qsort(pointers_to_stream, (number_of_streams), sizeof(STREAM *),
+ compare_inits);
+
+
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
- input_map_fd = Rast_open_old(input_map_name,mapset);
- input_data_type = Rast_map_type(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
- input_buffer=Rast_allocate_buf(input_data_type);
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (i=0;i<(number_of_streams);++i) {
- if (pointers_to_stream[i]->stream ==-1)
- continue;
-
- if(what) {
- r = (int)pointers_to_stream[i]->outlet / ncols;
- c = (int)pointers_to_stream[i]->outlet % ncols;
- } else {
- r = (int)pointers_to_stream[i]->init / ncols;
- c = (int)pointers_to_stream[i]->init % ncols;
- }
-
- if(r>cur_row) {
- Rast_get_row(input_map_fd,input_buffer,r,input_data_type);
- input_ptr=input_buffer;
- }
- switch (what) {
- case 0: /* inits for elev */
- pointers_to_stream[i]->init_elev=
- Rast_get_d_value(input_ptr+c*input_data_size, input_data_type);
- break;
- case 1: /* outlets for elev */
- pointers_to_stream[i]->outlet_elev=
- Rast_get_d_value(input_ptr+c*input_data_size, input_data_type);
- break;
- case 2: /* outlets for accum */
- pointers_to_stream[i]->accum=
- Rast_get_d_value(input_ptr+c*input_data_size, input_data_type);
- break;
- }
+ for (i = 0; i < (number_of_streams); ++i) {
+ if (pointers_to_stream[i]->stream == -1)
+ continue;
+
+ if (what) {
+ r = (int)pointers_to_stream[i]->outlet / ncols;
+ c = (int)pointers_to_stream[i]->outlet % ncols;
}
-G_free(input_buffer);
-G_free(pointers_to_stream);
-return 0;
+ else {
+ r = (int)pointers_to_stream[i]->init / ncols;
+ c = (int)pointers_to_stream[i]->init % ncols;
+ }
+
+ if (r > cur_row) {
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_ptr = input_buffer;
+ }
+ switch (what) {
+ case 0: /* inits for elev */
+ pointers_to_stream[i]->init_elev =
+ Rast_get_d_value(input_ptr + c * input_data_size,
+ input_data_type);
+ break;
+ case 1: /* outlets for elev */
+ pointers_to_stream[i]->outlet_elev =
+ Rast_get_d_value(input_ptr + c * input_data_size,
+ input_data_type);
+ break;
+ case 2: /* outlets for accum */
+ pointers_to_stream[i]->accum =
+ Rast_get_d_value(input_ptr + c * input_data_size,
+ input_data_type);
+ break;
+ }
+ }
+ G_free(input_buffer);
+ G_free(pointers_to_stream);
+ return 0;
}
-
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/stream_order.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/stream_order.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/stream_order.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -2,16 +2,16 @@
All algorithms used in analysis ar not recursive. For Strahler order and Shreve magnitude starts from initial channel and proceed downstream. Algortitms try to assgin order for branch and if it is imposible start from next initial channel, till all branches are ordered.
For Hortor and Hack ordering it proceed upstream and uses stack data structure to determine unordered branch.
Algorithm of Hack main stram according idea of Markus Metz.
-*/
+ */
#include "local_proto.h"
-int strahler(int* strahler)
+int strahler(int *strahler)
{
- int i, j, done = 1;
- int cur_stream, next_stream;
- int max_strahler = 0, max_strahler_num;
- STREAM* SA=stream_attributes; /* for better code readability */
+ int i, j, done = 1;
+ int cur_stream, next_stream;
+ int max_strahler = 0, max_strahler_num;
+ STREAM *SA = stream_attributes; /* for better code readability */
G_message(_("Calculating Strahler's stream order ..."));
@@ -37,7 +37,7 @@
break; /* strahler is not determined, break for loop */
}
else if (strahler[SA[cur_stream].trib[i]] > max_strahler) {
- max_strahler = strahler[SA[cur_stream].trib[i]];
+ max_strahler = strahler[SA[cur_stream].trib[i]];
max_strahler_num = 1;
}
else if (strahler[SA[cur_stream].trib[i]] == max_strahler) {
@@ -45,11 +45,11 @@
}
} /* end determining strahler */
- if (done == 1) {
- strahler[cur_stream] = (max_strahler_num >1) ?
- ++max_strahler : max_strahler;
- cur_stream = next_stream; /* if next_stream<0 we in outlet stream */
- }
+ if (done == 1) {
+ strahler[cur_stream] = (max_strahler_num > 1) ?
+ ++max_strahler : max_strahler;
+ cur_stream = next_stream; /* if next_stream<0 we in outlet stream */
+ }
}
} while (done && next_stream > 0);
@@ -57,13 +57,13 @@
return 0;
} /* end strahler */
-int shreve(int* shreve)
+int shreve(int *shreve)
{
int i, j, done = 1;
int cur_stream, next_stream;
int max_shreve = 0;
- STREAM* SA=stream_attributes; /* for better code readability */
+ STREAM *SA = stream_attributes; /* for better code readability */
G_message(_("Calculating Shreve's stream magnitude, Scheidegger's consistent integer and Drwal's streams hierarchy (old style) ..."));
@@ -106,16 +106,17 @@
return 0;
} /* end shreeve */
-int horton(const int* strahler, int* horton, int number_of_streams) {
+int horton(const int *strahler, int *horton, int number_of_streams)
+{
- int *stack;
- int stack_max=number_of_streams;
- int top, i, j;
- int cur_stream, cur_horton;
- int max_strahler;
- double max_accum, accum;
- int up_stream = 0;
- STREAM* SA=stream_attributes; /* for better code readability */
+ int *stack;
+ int stack_max = number_of_streams;
+ int top, i, j;
+ int cur_stream, cur_horton;
+ int max_strahler;
+ double max_accum, accum;
+ int up_stream = 0;
+ STREAM *SA = stream_attributes; /* for better code readability */
G_message(_("Calculating Hortons's stream order ..."));
stack = (int *)G_malloc(stack_max * sizeof(int));
@@ -144,21 +145,28 @@
if (horton[SA[cur_stream].trib[i]] < 0) {
if (strahler[SA[cur_stream].trib[i]] > max_strahler) {
- max_strahler =strahler[SA[cur_stream].trib[i]];
- max_accum = (use_accum) ? SA[SA[cur_stream].trib[i]].accum :
- SA[SA[cur_stream].trib[i]].accum_length;
+ max_strahler = strahler[SA[cur_stream].trib[i]];
+ max_accum =
+ (use_accum) ? SA[SA[cur_stream].trib[i]].
+ accum : SA[SA[cur_stream].trib[i]].
+ accum_length;
up_stream = SA[cur_stream].trib[i];
}
- else if (strahler[SA[cur_stream].trib[i]] == max_strahler) {
+ else if (strahler[SA[cur_stream].trib[i]] ==
+ max_strahler) {
- accum = (use_accum) ? SA[SA[cur_stream].trib[i]].accum :
- SA[SA[cur_stream].trib[i]].accum_length;
-
- if (accum > max_accum) {
- max_accum = (use_accum) ? SA[SA[cur_stream].trib[i]].accum :
- SA[SA[cur_stream].trib[i]].accum_length;
+ accum =
+ (use_accum) ? SA[SA[cur_stream].trib[i]].
+ accum : SA[SA[cur_stream].trib[i]].
+ accum_length;
+ if (accum > max_accum) {
+ max_accum =
+ (use_accum) ? SA[SA[cur_stream].trib[i]].
+ accum : SA[SA[cur_stream].trib[i]].
+ accum_length;
+
up_stream = SA[cur_stream].trib[i];
}
}
@@ -187,18 +195,17 @@
return 0;
}
-int hack(int* hack, int* topo_dim, int number_of_streams) /* also calculate topological dimension */
-{
+int hack(int *hack, int *topo_dim, int number_of_streams)
+{ /* also calculate topological dimension */
int *stack;
int top, i, j;
int cur_stream, cur_hack;
double accum, max_accum;
int up_stream = 0;
- int stack_max=number_of_streams;
- double cur_distance=0;
- int cur_topo_dim=0;
- STREAM* SA=stream_attributes; /* for better code readability */
+ int stack_max = number_of_streams;
+ double cur_distance = 0;
+ STREAM *SA = stream_attributes; /* for better code readability */
G_message(_("Calculating Hack's main streams and topological dimension..."));
stack = (int *)G_malloc(stack_max * sizeof(int));
@@ -210,9 +217,9 @@
stack[0] = 0;
stack[1] = cur_stream;
top = 1;
-
- topo_dim[cur_stream]=top;
- cur_distance=SA[cur_stream].distance=SA[cur_stream].length;
+
+ topo_dim[cur_stream] = top;
+ cur_distance = SA[cur_stream].distance = SA[cur_stream].length;
do {
max_accum = 0;
@@ -227,14 +234,17 @@
for (i = 0; i < SA[cur_stream].trib_num; ++i) { /* determining upstream */
if (hack[SA[cur_stream].trib[i]] < 0) {
-
- accum = (use_accum) ? SA[SA[cur_stream].trib[i]].accum :
- SA[SA[cur_stream].trib[i]].accum_length;
-
+
+ accum =
+ (use_accum) ? SA[SA[cur_stream].trib[i]].
+ accum : SA[SA[cur_stream].trib[i]].accum_length;
+
if (accum > max_accum) {
- max_accum = (use_accum) ? SA[SA[cur_stream].trib[i]].accum :
- SA[SA[cur_stream].trib[i]].accum_length;
- up_stream = SA[cur_stream].trib[i];
+ max_accum =
+ (use_accum) ? SA[SA[cur_stream].trib[i]].
+ accum : SA[SA[cur_stream].trib[i]].
+ accum_length;
+ up_stream = SA[cur_stream].trib[i];
}
}
} /* end determining up_stream */
@@ -243,17 +253,18 @@
if (hack[cur_stream] < 0) {
hack[cur_stream] = cur_hack;
- }
+ }
else {
cur_hack = hack[cur_stream];
++cur_hack;
}
- cur_distance=SA[cur_stream].distance;
- cur_stream = up_stream;
- stack[++top] = cur_stream;
- SA[cur_stream].distance=cur_distance+SA[cur_stream].length;
- topo_dim[cur_stream]=top;
+ cur_distance = SA[cur_stream].distance;
+ cur_stream = up_stream;
+ stack[++top] = cur_stream;
+ SA[cur_stream].distance =
+ cur_distance + SA[cur_stream].length;
+ topo_dim[cur_stream] = top;
}
else { /* all asigned, go downstream */
cur_stream = stack[--top];
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/stream_raster_close.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/stream_raster_close.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/stream_raster_close.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,128 +1,130 @@
#include "local_proto.h"
-int ram_close_raster_order(CELL** streams, int number_of_streams, int zerofill) {
-
- G_message("Closing maps...");
- int* output_fd;
- int r, c, i, j;
- CELL *output_buffer, *streams_buffer;
- void* row;
- struct History history;
- size_t data_size;
-
- output_fd=(int*)G_malloc(orders_size*sizeof(int));
- for(i=0;i<orders_size;++i) {
- if(output_map_names[i]==NULL)
- continue;
- output_fd[i]=Rast_open_c_new(output_map_names[i]);
- }
+int ram_close_raster_order(CELL **streams, int number_of_streams,
+ int zerofill)
+{
- /* this is not very elegant but use for compatibility with seg version */
-
- data_size=Rast_cell_size(CELL_TYPE);
- output_buffer=Rast_allocate_c_buf();
+ G_message("Closing maps...");
+ int *output_fd;
+ int r, c, i;
+ CELL *output_buffer, *streams_buffer;
+ struct History history;
+ size_t data_size;
- for(r=0;r<nrows;++r) {
- streams_buffer=streams[r];
+ output_fd = (int *)G_malloc(orders_size * sizeof(int));
+ for (i = 0; i < orders_size; ++i) {
+ if (output_map_names[i] == NULL)
+ continue;
+ output_fd[i] = Rast_open_c_new(output_map_names[i]);
+ }
- for(i=0;i<orders_size;++i) {
-
- if(output_map_names[i]==NULL)
+ /* this is not very elegant but use for compatibility with seg version */
+
+ data_size = Rast_cell_size(CELL_TYPE);
+ output_buffer = Rast_allocate_c_buf();
+
+ for (r = 0; r < nrows; ++r) {
+ streams_buffer = streams[r];
+
+ for (i = 0; i < orders_size; ++i) {
+
+ if (output_map_names[i] == NULL)
continue;
- if(zerofill)
- memset(output_buffer,0,ncols*data_size);
- else
- Rast_set_c_null_value(output_buffer,ncols);
+ if (zerofill)
+ memset(output_buffer, 0, ncols * data_size);
+ else
+ Rast_set_c_null_value(output_buffer, ncols);
- for(c=0;c<ncols;++c)
- if(streams_buffer[c])
- output_buffer[c]=all_orders[i][streams_buffer[c]];
-
- Rast_put_c_row(output_fd[i], output_buffer);
- } /* end i */
- }
+ for (c = 0; c < ncols; ++c)
+ if (streams_buffer[c])
+ output_buffer[c] = all_orders[i][streams_buffer[c]];
- G_free(output_buffer);
- for(i=0;i<orders_size;++i)
+ Rast_put_c_row(output_fd[i], output_buffer);
+ } /* end i */
+ }
+
+ G_free(output_buffer);
+ for (i = 0; i < orders_size; ++i)
G_free(all_orders[i]);
- G_free(all_orders);
+ G_free(all_orders);
-
- for(i=0;i<orders_size;++i) {
- if(output_map_names[i]==NULL)
- continue;
+
+ for (i = 0; i < orders_size; ++i) {
+ if (output_map_names[i] == NULL)
+ continue;
Rast_close(output_fd[i]);
- Rast_short_history(output_map_names[i], "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_names[i], &history);
- G_message(_("%s Done"), output_map_names[i]);
- }
-
- G_free(output_fd);
- return 0;
- }
+ Rast_short_history(output_map_names[i], "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_names[i], &history);
+ G_message(_("%s Done"), output_map_names[i]);
+ }
+ G_free(output_fd);
+ return 0;
+}
-int seg_close_raster_order(SEGMENT* streams, int number_of_streams, int zerofill) {
- int* output_fd;
- int r, c, i, j;
- CELL *output_buffer, *streams_buffer;
- void* row;
- struct History history;
- size_t data_size;
-
- output_fd=(int*)G_malloc(orders_size*sizeof(int));
- for(i=0;i<orders_size;++i) {
- if(output_map_names[i]==NULL)
- continue;
- output_fd[i]=Rast_open_c_new(output_map_names[i]);
- }
-
- data_size=Rast_cell_size(CELL_TYPE);
- output_buffer=Rast_allocate_c_buf();
- streams_buffer=Rast_allocate_c_buf();
- segment_flush(streams);
-
- for(r=0;r<nrows;++r) {
- if(0>segment_get_row(streams,streams_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_names[i]);
-
- for(i=0;i<orders_size;++i) {
+int seg_close_raster_order(SEGMENT *streams, int number_of_streams,
+ int zerofill)
+{
- if(output_map_names[i]==NULL)
- continue;
+ int *output_fd;
+ int r, c, i;
+ CELL *output_buffer, *streams_buffer;
+ struct History history;
+ size_t data_size;
- if(zerofill)
- memset(output_buffer,0,ncols*data_size);
- else
- Rast_set_c_null_value(output_buffer,ncols);
+ output_fd = (int *)G_malloc(orders_size * sizeof(int));
+ for (i = 0; i < orders_size; ++i) {
+ if (output_map_names[i] == NULL)
+ continue;
+ output_fd[i] = Rast_open_c_new(output_map_names[i]);
+ }
- for(c=0;c<ncols;++c)
- if(!Rast_is_c_null_value(&streams_buffer[c]))
- output_buffer[c]=all_orders[i][streams_buffer[c]];
-
- Rast_put_c_row(output_fd[i], output_buffer);
- } /* end i */
- }
+ data_size = Rast_cell_size(CELL_TYPE);
+ output_buffer = Rast_allocate_c_buf();
+ streams_buffer = Rast_allocate_c_buf();
+ segment_flush(streams);
- G_free(output_buffer);
- G_free(streams_buffer);
- for(i=0;i<orders_size;++i)
+ for (r = 0; r < nrows; ++r) {
+ if (0 > segment_get_row(streams, streams_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_names[i]);
+
+ for (i = 0; i < orders_size; ++i) {
+
+ if (output_map_names[i] == NULL)
+ continue;
+
+ if (zerofill)
+ memset(output_buffer, 0, ncols * data_size);
+ else
+ Rast_set_c_null_value(output_buffer, ncols);
+
+ for (c = 0; c < ncols; ++c)
+ if (!Rast_is_c_null_value(&streams_buffer[c]))
+ output_buffer[c] = all_orders[i][streams_buffer[c]];
+
+ Rast_put_c_row(output_fd[i], output_buffer);
+ } /* end i */
+ }
+
+ G_free(output_buffer);
+ G_free(streams_buffer);
+ for (i = 0; i < orders_size; ++i)
G_free(all_orders[i]);
- G_free(all_orders);
+ G_free(all_orders);
- for(i=0;i<orders_size;++i) {
- if(output_map_names[i]==NULL)
- continue;
+ for (i = 0; i < orders_size; ++i) {
+ if (output_map_names[i] == NULL)
+ continue;
Rast_close(output_fd[i]);
- Rast_short_history(output_map_names[i], "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_names[i], &history);
- G_message(_("%s Done"), output_map_names[i]);
- }
-
- G_free(output_fd);
- return 0;
+ Rast_short_history(output_map_names[i], "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_names[i], &history);
+ G_message(_("%s Done"), output_map_names[i]);
+ }
+
+ G_free(output_fd);
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/stream_topology.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/stream_topology.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,15 +1,17 @@
#include "local_proto.h"
-int ram_number_of_tribs (int r, int c, CELL** streams, CELL** dirs) {
-
- int trib = 0;
- int i, j;
-
- for (i = 1; i < 9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- j = DIAG(i);
- if (streams[NR(i)][NC(i)] && dirs[NR(i)][NC(i)] == j)
- trib++;
+
+int ram_number_of_tribs(int r, int c, CELL **streams, CELL **dirs)
+{
+
+ int trib = 0;
+ int i, j;
+
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ j = DIAG(i);
+ if (streams[NR(i)][NC(i)] && dirs[NR(i)][NC(i)] == j)
+ trib++;
}
if (trib > 5)
@@ -18,41 +20,42 @@
if (trib > 3)
G_warning(_("Stream network may be too dense..."));
- return trib;
+ return trib;
}
-int ram_stream_topology(CELL** streams, CELL** dirs, int number_of_streams) {
+int ram_stream_topology(CELL **streams, CELL **dirs, int number_of_streams)
+{
- int d, i, j; /* d: direction, i: iteration */
- int r, c;
- int next_r, next_c;
- int trib_num, trib = 0;
- int next_stream = -1, cur_stream;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- init_num = 0, outlet_num = 0;
-
- G_message(_("Finding nodes..."));
+ int d, i, j; /* d: direction, i: iteration */
+ int r, c;
+ int next_r, next_c;
+ int trib_num, trib = 0;
+ int next_stream = -1, cur_stream;
+ STREAM *SA = stream_attributes; /* for better code readability */
- outlet_streams = (unsigned int *)G_malloc((number_of_streams) *
- sizeof(unsigned int));
- init_streams = (unsigned int*)G_malloc((number_of_streams) *
- sizeof(unsigned int));
- init_cells = (unsigned
- long int *)G_malloc((number_of_streams) * sizeof(unsigned
- long int));
- // free at the end
+ init_num = 0, outlet_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c]) {
- trib_num=ram_number_of_tribs (r,c,streams,dirs);
+ G_message(_("Finding nodes..."));
+
+ outlet_streams = (unsigned int *)G_malloc((number_of_streams) *
+ sizeof(unsigned int));
+ init_streams = (unsigned int *)G_malloc((number_of_streams) *
+ sizeof(unsigned int));
+ init_cells = (unsigned
+ long int *)G_malloc((number_of_streams) * sizeof(unsigned
+ long int));
+ /* free at the end */
+
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c]) {
+ trib_num = ram_number_of_tribs(r, c, streams, dirs);
trib = 0;
d = abs(dirs[r][c]); /* r.watershed! */
- if(d<1 || NOT_IN_REGION(d) || !streams[NR(d)][NC(d)])
- next_stream = -1;
- else
- next_stream = streams[NR(d)][NC(d)];
+ if (d < 1 || NOT_IN_REGION(d) || !streams[NR(d)][NC(d)])
+ next_stream = -1;
+ else
+ next_stream = streams[NR(d)][NC(d)];
cur_stream = streams[r][c];
@@ -62,350 +65,367 @@
G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
- SA[cur_stream].stream = cur_stream;
- SA[cur_stream].next_stream = next_stream;
+ SA[cur_stream].stream = cur_stream;
+ SA[cur_stream].next_stream = next_stream;
- if (next_stream < 0) /* is outlet stream */
+ if (next_stream < 0) /* is outlet stream */
outlet_streams[outlet_num++] = cur_stream;
}
-
+
if (trib_num == 0) { /* is init */
- if (init_num > (number_of_streams - 1))
+ if (init_num > (number_of_streams - 1))
G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
- SA[cur_stream].trib_num = 0;
- init_cells[init_num]=r*ncols+c;
- init_streams[init_num++] = cur_stream; /* collecting inits */
+ SA[cur_stream].trib_num = 0;
+ init_cells[init_num] = r * ncols + c;
+ init_streams[init_num++] = cur_stream; /* collecting inits */
}
if (trib_num > 1) { /* adding tributuaries */
- SA[cur_stream].trib_num = trib_num;
-
- for (i = 1; i < 9; ++i) {
- if (trib > 4)
- G_fatal_error(_("Error finding nodes. \
+ SA[cur_stream].trib_num = 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..."));
- if(NOT_IN_REGION(i))
- continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
- if (streams[next_r][next_c] && dirs[next_r][next_c] == j)
- SA[cur_stream].trib[trib++] = streams[next_r][next_c];
+ if (NOT_IN_REGION(i))
+ continue;
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+ if (streams[next_r][next_c] &&
+ dirs[next_r][next_c] == j)
+ SA[cur_stream].trib[trib++] =
+ streams[next_r][next_c];
} /* end for i... */
}
- } /* end if streams */
+ } /* end if streams */
return 0;
}
-int ram_stream_geometry(CELL** streams, CELL** dirs) {
+int ram_stream_geometry(CELL **streams, CELL **dirs)
+{
- int i, s, d; /* s - streams index; d - direction */
- int done = 1;
- int r, c;
- int next_r, next_c;
- int prev_r, prev_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_length=0.;
- STREAM* SA=stream_attributes; /* for better code readability */
- struct Cell_head window;
-
- G_get_window(&window);
-
- G_message(_("Finding longest streams..."));
- G_begin_distance_calculations();
-
- for (s=0; s<init_num; ++s) { /* main loop on springs */
- r = (int)init_cells[s]/ncols;
- c = (int)init_cells[s]%ncols;
- cur_stream=streams[r][c];
- cur_length=0;
- done=1;
-
- SA[cur_stream].init=init_cells[s]; /* stored as index */
-
- 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=abs(dirs[r][c]);
- next_r=NR(d);
- next_c=NC(d);
+ int i, s, d; /* s - streams index; d - direction */
+ int done = 1;
+ int r, c;
+ int next_r, next_c;
+ int prev_r, prev_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_length = 0.;
+ STREAM *SA = stream_attributes; /* for better code readability */
+ struct Cell_head window;
- if (d<1 || NOT_IN_REGION(d) || !streams[next_r][next_c]) {
- cur_length=(window.ns_res+window.ew_res)/2;
- SA[cur_stream].accum_length += cur_length;
- SA[cur_stream].length += cur_length;
- SA[cur_stream].stright =
- G_distance(cur_easting, cur_northing, init_easting, init_northing);
- SA[cur_stream].outlet=(r*ncols+c); /* add outlet to sorting */
- break;
- }
+ G_get_window(&window);
- 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);
+ G_message(_("Finding longest streams..."));
+ G_begin_distance_calculations();
+
+ for (s = 0; s < init_num; ++s) { /* main loop on springs */
+ r = (int)init_cells[s] / ncols;
+ c = (int)init_cells[s] % ncols;
+ cur_stream = streams[r][c];
+ cur_length = 0;
+ done = 1;
+
+ SA[cur_stream].init = init_cells[s]; /* stored as index */
+
+ 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 = abs(dirs[r][c]);
+ next_r = NR(d);
+ next_c = NC(d);
+
+ if (d < 1 || NOT_IN_REGION(d) || !streams[next_r][next_c]) {
+ cur_length = (window.ns_res + window.ew_res) / 2;
SA[cur_stream].accum_length += cur_length;
SA[cur_stream].length += cur_length;
- prev_r=r;
- prev_c=c;
- r=next_r;
- c=next_c;
-
- if (streams[next_r][next_c] != cur_stream) {
SA[cur_stream].stright =
- G_distance(next_easting, next_northing, init_easting, init_northing);
+ G_distance(cur_easting, cur_northing, init_easting,
+ init_northing);
+ SA[cur_stream].outlet = (r * ncols + c); /* add outlet to sorting */
+ 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);
+ SA[cur_stream].accum_length += cur_length;
+ SA[cur_stream].length += cur_length;
+ prev_r = r;
+ prev_c = c;
+ r = next_r;
+ c = next_c;
+
+ if (streams[next_r][next_c] != cur_stream) {
+ SA[cur_stream].stright =
+ G_distance(next_easting, next_northing, init_easting,
+ init_northing);
init_northing = cur_northing;
init_easting = cur_easting;
- SA[cur_stream].outlet=(prev_r*ncols+prev_c);
- cur_stream=streams[next_r][next_c];
+ SA[cur_stream].outlet = (prev_r * ncols + prev_c);
+ cur_stream = streams[next_r][next_c];
- cur_accum_length=0;
- SA[cur_stream].init=(r*ncols+c);
+ cur_accum_length = 0;
+ SA[cur_stream].init = (r * ncols + c);
- for (i = 0; i < SA[cur_stream].trib_num; ++i) {
- if (SA[SA[cur_stream].trib[i]].accum_length == 0) {
- done=0;
- cur_accum_length=0;
- break; /* do not pass accum*/
- }
- if(SA[SA[cur_stream].trib[i]].accum_length>cur_accum_length)
- cur_accum_length=SA[SA[cur_stream].trib[i]].accum_length;
- } /* end for i */
- SA[cur_stream].accum_length=cur_accum_length;
- }/* end if */
- } /* end while */
- } /* end for s*/
-return 0;
+ for (i = 0; i < SA[cur_stream].trib_num; ++i) {
+ if (SA[SA[cur_stream].trib[i]].accum_length == 0) {
+ done = 0;
+ cur_accum_length = 0;
+ break; /* do not pass accum */
+ }
+ if (SA[SA[cur_stream].trib[i]].accum_length >
+ cur_accum_length)
+ cur_accum_length =
+ SA[SA[cur_stream].trib[i]].accum_length;
+ } /* end for i */
+ SA[cur_stream].accum_length = cur_accum_length;
+ } /* end if */
+ } /* end while */
+ } /* end for s */
+ return 0;
}
-int seg_number_of_tribs (int r, int c, SEGMENT* streams, SEGMENT* dirs) {
-
- int trib = 0;
- int i, j;
- int streams_cell=0;
- int dirs_cell=0;
-
+int seg_number_of_tribs(int r, int c, SEGMENT *streams, SEGMENT *dirs)
+{
+
+ int trib = 0;
+ int i, j;
+ int streams_cell = 0;
+ int dirs_cell = 0;
+
for (i = 1; i < 9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
-
+ if (NOT_IN_REGION(i))
+ continue;
+
j = DIAG(i);
- segment_get(streams,&streams_cell,NR(i),NC(i));
- segment_get(dirs,&dirs_cell,NR(i),NC(i));
-
- if (streams_cell && dirs_cell == j)
- trib++;
+ segment_get(streams, &streams_cell, NR(i), NC(i));
+ segment_get(dirs, &dirs_cell, NR(i), NC(i));
+
+ if (streams_cell && dirs_cell == j)
+ trib++;
}
-
+
if (trib > 5)
G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
if (trib > 3)
G_warning(_("Stream network may be too dense..."));
- return trib;
+ return trib;
}
-int seg_stream_topology(SEGMENT* streams, SEGMENT* dirs, int number_of_streams) {
+int seg_stream_topology(SEGMENT *streams, SEGMENT *dirs,
+ int number_of_streams)
+{
- int d, i, j; /* d: direction, i: iteration */
- int r, c;
- int next_r, next_c;
- int trib_num, trib = 0;
- int next_stream = -1, cur_stream;
- int streams_cell, dirs_cell;
- int next_streams_cell, trib_dirs_cell, trib_stream_cell;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- init_num = 0, outlet_num = 0;
-
- G_message(_("Finding nodes..."));
+ int d, i, j; /* d: direction, i: iteration */
+ int r, c;
+ int next_r, next_c;
+ int trib_num, trib = 0;
+ int next_stream = -1, cur_stream;
+ int streams_cell, dirs_cell;
+ int next_streams_cell, trib_dirs_cell, trib_stream_cell;
+ STREAM *SA = stream_attributes; /* for better code readability */
- outlet_streams = (unsigned int *)G_malloc((number_of_streams) *
- sizeof(unsigned int));
- init_streams = (unsigned int*)G_malloc((number_of_streams) *
- sizeof(unsigned int));
- init_cells = (unsigned
- long int *)G_malloc((number_of_streams) * sizeof(unsigned
- long int));
+ init_num = 0, outlet_num = 0;
- for (r = 0; r < nrows; ++r) {
- G_percent(r, nrows, 2);
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- segment_get(dirs,&dirs_cell,r,c);
-
- if (streams_cell) {
- trib_num=seg_number_of_tribs (r,c,streams,dirs);
+ G_message(_("Finding nodes..."));
+
+ outlet_streams = (unsigned int *)G_malloc((number_of_streams) *
+ sizeof(unsigned int));
+ init_streams = (unsigned int *)G_malloc((number_of_streams) *
+ sizeof(unsigned int));
+ init_cells = (unsigned
+ long int *)G_malloc((number_of_streams) * sizeof(unsigned
+ long int));
+
+ for (r = 0; r < nrows; ++r) {
+ G_percent(r, nrows, 2);
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+ segment_get(dirs, &dirs_cell, r, c);
+
+ if (streams_cell) {
+ trib_num = seg_number_of_tribs(r, c, streams, dirs);
trib = 0;
d = abs(dirs_cell); /* r.watershed! */
- if(NOT_IN_REGION(d))
- next_stream=-1;
- else
- segment_get(streams,&next_streams_cell,NR(d),NC(d));
-
- if(d<1 || NOT_IN_REGION(d) || !next_streams_cell)
- next_stream = -1;
- else
- segment_get(streams,&next_stream,NR(d),NC(d));
-
+ if (NOT_IN_REGION(d))
+ next_stream = -1;
+ else
+ segment_get(streams, &next_streams_cell, NR(d), NC(d));
+
+ if (d < 1 || NOT_IN_REGION(d) || !next_streams_cell)
+ next_stream = -1;
+ else
+ segment_get(streams, &next_stream, NR(d), NC(d));
+
cur_stream = streams_cell;
-
+
if (cur_stream != next_stream) { /* junction: building topology */
if (outlet_num > (number_of_streams - 1))
G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
- SA[cur_stream].stream = cur_stream;
- SA[cur_stream].next_stream = next_stream;
+ SA[cur_stream].stream = cur_stream;
+ SA[cur_stream].next_stream = next_stream;
- if (next_stream < 0) /* is outlet stream */
+ if (next_stream < 0) /* is outlet stream */
outlet_streams[outlet_num++] = cur_stream;
}
-
+
if (trib_num == 0) { /* is init */
- if (init_num > (number_of_streams - 1))
+ if (init_num > (number_of_streams - 1))
G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
- SA[cur_stream].trib_num = 0;
- init_cells[init_num]=r*ncols+c;
- init_streams[init_num++] = cur_stream; /* collecting inits */
+ SA[cur_stream].trib_num = 0;
+ init_cells[init_num] = r * ncols + c;
+ init_streams[init_num++] = cur_stream; /* collecting inits */
}
if (trib_num > 1) { /* adding tributuaries */
- SA[cur_stream].trib_num = trib_num;
-
- for (i = 1; i < 9; ++i) {
+ SA[cur_stream].trib_num = trib_num;
- if (trib > 4)
- G_fatal_error(_("Error finding nodes. \
+ for (i = 1; i < 9; ++i) {
+
+ if (trib > 4)
+ G_fatal_error(_("Error finding nodes. \
Stream and direction maps probably do not match..."));
- if(NOT_IN_REGION(i))
- continue;
- j = DIAG(i);
- next_r = NR(i);
- next_c = NC(i);
- segment_get(streams,&trib_stream_cell,next_r,next_c);
- segment_get(dirs,&trib_dirs_cell,next_r,next_c);
-
- if (trib_stream_cell &&
- trib_dirs_cell == j)
- SA[cur_stream].trib[trib++] = trib_stream_cell;
+ if (NOT_IN_REGION(i))
+ continue;
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+ segment_get(streams, &trib_stream_cell, next_r,
+ next_c);
+ segment_get(dirs, &trib_dirs_cell, next_r, next_c);
+
+ if (trib_stream_cell && trib_dirs_cell == j)
+ SA[cur_stream].trib[trib++] = trib_stream_cell;
} /* end for i... */
}
- } /* end if streams */
- } } /* end r, c */
+ } /* end if streams */
+ }
+ } /* end r, c */
G_percent(r, nrows, 2);
return 0;
}
-int seg_stream_geometry(SEGMENT* streams, SEGMENT* dirs) {
+int seg_stream_geometry(SEGMENT *streams, SEGMENT *dirs)
+{
- int i, s, d; /* s - streams index; d - direction */
- int done = 1;
- int r, c;
- int next_r, next_c;
- int prev_r, prev_c;
- int attrib_cells_num=0;
- int cur_stream, next_stream, dirs_cell;
- float cur_northing, cur_easting;
- float next_northing, next_easting;
- float init_northing, init_easting;
- double cur_length=0.;
- double cur_accum_length=0.;
- STREAM* SA=stream_attributes; /* for better code readability */
- struct Cell_head window;
-
- G_get_window(&window);
-
- G_message(_("Finding longest streams..."));
- G_begin_distance_calculations();
-
- for (s=0; s<init_num; ++s) { /* main loop on springs */
+ int i, s, d; /* s - streams index; d - direction */
+ int done = 1;
+ int r, c;
+ int next_r, next_c;
+ int prev_r, prev_c;
+ int cur_stream, next_stream, dirs_cell;
+ float cur_northing, cur_easting;
+ float next_northing, next_easting;
+ float init_northing, init_easting;
+ double cur_length = 0.;
+ double cur_accum_length = 0.;
+ STREAM *SA = stream_attributes; /* for better code readability */
+ struct Cell_head window;
+
+ G_get_window(&window);
+
+ G_message(_("Finding longest streams..."));
+ G_begin_distance_calculations();
+
+ for (s = 0; s < init_num; ++s) { /* main loop on springs */
G_percent(s, init_num, 2);
- r = (int)init_cells[s]/ncols;
- c = (int)init_cells[s]%ncols;
- segment_get(streams,&cur_stream,r,c);
- cur_length=0;
- done=1;
-
- SA[cur_stream].init=init_cells[s]; /* stored as index */
-
- 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;
-
- segment_get(dirs,&dirs_cell,r,c);
- d=abs(dirs_cell);
- next_r=NR(d);
- next_c=NC(d);
- if(NOT_IN_REGION(d))
- Rast_set_c_null_value(&next_stream,1);
- else
- segment_get(streams,&next_stream,next_r,next_c);
-
- if (d<1|| NOT_IN_REGION(d) || !next_stream) {
- cur_length=(window.ns_res+window.ew_res)/2;
- SA[cur_stream].accum_length += cur_length;
- SA[cur_stream].length += cur_length;
- SA[cur_stream].stright =
- G_distance(cur_easting, cur_northing, init_easting, init_northing);
- SA[cur_stream].outlet=(r*ncols+c); /* add outlet to sorting */
- break;
- }
+ r = (int)init_cells[s] / ncols;
+ c = (int)init_cells[s] % ncols;
+ segment_get(streams, &cur_stream, 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);
+ SA[cur_stream].init = init_cells[s]; /* stored as index */
+
+ 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;
+
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+ next_r = NR(d);
+ next_c = NC(d);
+ if (NOT_IN_REGION(d))
+ Rast_set_c_null_value(&next_stream, 1);
+ else
+ segment_get(streams, &next_stream, next_r, next_c);
+
+ if (d < 1 || NOT_IN_REGION(d) || !next_stream) {
+ cur_length = (window.ns_res + window.ew_res) / 2;
SA[cur_stream].accum_length += cur_length;
SA[cur_stream].length += cur_length;
- prev_r=r;
- prev_c=c;
- r=next_r;
- c=next_c;
-
- if (next_stream != cur_stream) {
SA[cur_stream].stright =
- G_distance(next_easting, next_northing, init_easting, init_northing);
+ G_distance(cur_easting, cur_northing, init_easting,
+ init_northing);
+ SA[cur_stream].outlet = (r * ncols + c); /* add outlet to sorting */
+ 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);
+ SA[cur_stream].accum_length += cur_length;
+ SA[cur_stream].length += cur_length;
+ prev_r = r;
+ prev_c = c;
+ r = next_r;
+ c = next_c;
+
+ if (next_stream != cur_stream) {
+ SA[cur_stream].stright =
+ G_distance(next_easting, next_northing, init_easting,
+ init_northing);
init_northing = cur_northing;
init_easting = cur_easting;
- SA[cur_stream].outlet=(prev_r*ncols+prev_c);
- cur_stream=next_stream;
- cur_accum_length=0;
- SA[cur_stream].init=(r*ncols+c);
-
- for (i = 0; i < SA[cur_stream].trib_num; ++i) {
- if (SA[SA[cur_stream].trib[i]].accum_length == 0) {
- done=0;
- cur_accum_length=0;
- break; /* do not pass accum*/
- }
- if(SA[SA[cur_stream].trib[i]].accum_length>cur_accum_length)
- cur_accum_length=SA[SA[cur_stream].trib[i]].accum_length;
- } /* end for i */
- SA[cur_stream].accum_length=cur_accum_length;
- }/* end if */
- } /* end while */
- } /* end for s*/
-G_percent(s, init_num, 2);
-return 0;
+ SA[cur_stream].outlet = (prev_r * ncols + prev_c);
+ cur_stream = next_stream;
+ cur_accum_length = 0;
+ SA[cur_stream].init = (r * ncols + c);
+
+ for (i = 0; i < SA[cur_stream].trib_num; ++i) {
+ if (SA[SA[cur_stream].trib[i]].accum_length == 0) {
+ done = 0;
+ cur_accum_length = 0;
+ break; /* do not pass accum */
+ }
+ if (SA[SA[cur_stream].trib[i]].accum_length >
+ cur_accum_length)
+ cur_accum_length =
+ SA[SA[cur_stream].trib[i]].accum_length;
+ } /* end for i */
+ SA[cur_stream].accum_length = cur_accum_length;
+ } /* end if */
+ } /* end while */
+ } /* end for s */
+ G_percent(s, init_num, 2);
+ return 0;
}
-
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.order/stream_vector.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.order/stream_vector.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.order/stream_vector.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,377 +1,360 @@
#include "local_proto.h"
-int ram_create_vector(CELL** streams, CELL** dirs, char* out_vector, int number_of_streams) {
+int ram_create_vector(CELL ** streams, CELL ** dirs, char *out_vector,
+ int number_of_streams)
+{
- int i,j,d;
- int r,c;
- int next_r, next_c;
- int index_cat=0;
- int add_outlet=0;
- int cur_stream;
- float northing, easting;
- struct Cell_head window;
- struct line_pnts *Segments;
- struct line_cats *Cats;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- G_get_window(&window);
- Segments = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
- Vect_open_new(&Out, out_vector, 0);
-
+ int i, d;
+ int r, c;
+ int next_r, next_c;
+ int add_outlet = 0;
+ int cur_stream;
+ float northing, easting;
+ struct Cell_head window;
+ struct line_pnts *Segments;
+ struct line_cats *Cats;
+ STREAM *SA = stream_attributes; /* for better code readability */
+
+ G_get_window(&window);
+ Segments = Vect_new_line_struct();
+ Cats = Vect_new_cats_struct();
+ Vect_open_new(&Out, out_vector, 0);
+
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ for (i = 0; i < number_of_streams; ++i) {
+
+ if (SA[i].stream == -1)
+ continue; /* empty category */
+
+ add_outlet = 0;
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+
+ cur_stream = SA[i].stream;
+ Vect_cat_set(Cats, 1, cur_stream);
+ easting = window.west + (c + .5) * window.ew_res;
+ northing = window.north - (r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
+ Vect_write_line(&Out, GV_POINT, Segments, Cats);
Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
-
- for (i=0;i<number_of_streams;++i) {
+ Vect_append_point(Segments, easting, northing, 0);
- if(SA[i].stream==-1)
- continue; /* empty category */
+ while (streams[r][c] == cur_stream) {
- add_outlet=0;
- r = (int)SA[i].init / ncols;
- c = (int)SA[i].init % ncols;
+ d = abs(dirs[r][c]);
+ next_r = NR(d);
+ next_c = NC(d);
- cur_stream=SA[i].stream;
- Vect_cat_set(Cats,1,cur_stream);
- easting = window.west + (c + .5) * window.ew_res;
- northing = window.north - (r + .5) * window.ns_res;
- Vect_append_point(Segments, easting,northing, 0);
- Vect_write_line(&Out, GV_POINT, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_append_point(Segments, easting,northing, 0);
-
- while (streams[r][c]==cur_stream) {
-
- d=abs(dirs[r][c]);
- next_r=NR(d);
- next_c=NC(d);
-
- easting = window.west + (next_c + .5) * window.ew_res;
- northing = window.north - (next_r + .5) * window.ns_res;
- Vect_append_point(Segments, easting, northing, 0);
+ easting = window.west + (next_c + .5) * window.ew_res;
+ northing = window.north - (next_r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
- if (d<1 || NOT_IN_REGION(d) || !streams[next_r][next_c]) {
- add_outlet=1;
- break;
- }
- r=next_r;
- c=next_c;
- } /* end while */
-
- Vect_cat_set(Cats, 1, cur_stream);
- Vect_write_line(&Out, GV_LINE, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
+ if (d < 1 || NOT_IN_REGION(d) || !streams[next_r][next_c]) {
+ add_outlet = 1;
+ break;
+ }
+ r = next_r;
+ c = next_c;
+ } /* end while */
- if(add_outlet) {
- Vect_cat_set(Cats, 1, 0);
- Vect_reset_line(Segments);
- Vect_append_point(Segments, easting, northing, 0);
- Vect_write_line(&Out, GV_POINT, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
- }
+ Vect_cat_set(Cats, 1, cur_stream);
+ Vect_write_line(&Out, GV_LINE, Segments, Cats);
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ if (add_outlet) {
+ Vect_cat_set(Cats, 1, 0);
+ Vect_reset_line(Segments);
+ Vect_append_point(Segments, easting, northing, 0);
+ Vect_write_line(&Out, GV_POINT, Segments, Cats);
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
}
+ }
-/* build vector after adding table */
- if(0<stream_add_table(number_of_streams))
- G_warning(_("Cannot add table to vector %s"),out_vector);
-Vect_hist_command(&Out);
-Vect_build(&Out);
-Vect_close(&Out);
+ /* build vector after adding table */
+ if (0 < stream_add_table(number_of_streams))
+ G_warning(_("Cannot add table to vector %s"), out_vector);
+ Vect_hist_command(&Out);
+ Vect_build(&Out);
+ Vect_close(&Out);
-return 0;
-}
+ return 0;
+}
-int seg_create_vector(SEGMENT* streams, SEGMENT* dirs, char* out_vector, int number_of_streams) {
+int seg_create_vector(SEGMENT * streams, SEGMENT * dirs, char *out_vector,
+ int number_of_streams)
+{
- int i,j,d;
- int r,c;
- int next_r, next_c;
- int index_cat=0;
- int add_outlet;
- int streams_cell, dirs_cell;
- int cur_stream, next_stream;
- float northing, easting;
- struct Cell_head window;
- struct line_pnts *Segments;
- struct line_cats *Cats;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- G_get_window(&window);
- Segments = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
- Vect_open_new(&Out, out_vector, 0);
-
+ int i, d;
+ int r, c;
+ int next_r, next_c;
+ int add_outlet;
+ int streams_cell, dirs_cell;
+ int cur_stream, next_stream;
+ float northing, easting;
+ struct Cell_head window;
+ struct line_pnts *Segments;
+ struct line_cats *Cats;
+ STREAM *SA = stream_attributes; /* for better code readability */
+
+ G_get_window(&window);
+ Segments = Vect_new_line_struct();
+ Cats = Vect_new_cats_struct();
+ Vect_open_new(&Out, out_vector, 0);
+
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ for (i = 0; i < number_of_streams; ++i) {
+ if (SA[i].stream == -1)
+ continue;
+
+ add_outlet = 0;
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+
+ cur_stream = SA[i].stream;
+ Vect_cat_set(Cats, 1, cur_stream);
+ easting = window.west + (c + .5) * window.ew_res;
+ northing = window.north - (r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
+ Vect_write_line(&Out, GV_POINT, Segments, Cats);
Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
-
- for (i=0;i<number_of_streams;++i) {
- if(SA[i].stream==-1)
- continue;
+ Vect_append_point(Segments, easting, northing, 0);
- add_outlet=0;
- r = (int)SA[i].init / ncols;
- c = (int)SA[i].init % ncols;
-
- cur_stream=SA[i].stream;
- Vect_cat_set(Cats,1,cur_stream);
- easting = window.west + (c + .5) * window.ew_res;
- northing = window.north - (r + .5) * window.ns_res;
- Vect_append_point(Segments, easting,northing, 0);
- Vect_write_line(&Out, GV_POINT, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_append_point(Segments, easting,northing, 0);
-
- segment_get(streams,&streams_cell,r,c);
- while (streams_cell==cur_stream) {
-
- segment_get(dirs,&dirs_cell,r,c);
- d=abs(dirs_cell);
- next_r=NR(d);
- next_c=NC(d);
-
- easting = window.west + (next_c + .5) * window.ew_res;
- northing = window.north - (next_r + .5) * window.ns_res;
- Vect_append_point(Segments, easting, northing, 0);
+ segment_get(streams, &streams_cell, r, c);
+ while (streams_cell == cur_stream) {
- if(NOT_IN_REGION(d))
- Rast_set_c_null_value(&next_stream,1);
- else
- segment_get(streams,&next_stream,next_r,next_c);
-
- if (d<1 || NOT_IN_REGION(d) ||!next_stream) {
- add_outlet=1;
- break;
- }
- r=next_r;
- c=next_c;
- segment_get(streams,&streams_cell,r,c);
- } /* end while */
-
- Vect_cat_set(Cats, 1, cur_stream);
- Vect_write_line(&Out, GV_LINE, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
-
- if(add_outlet) {
- Vect_cat_set(Cats, 1, 0);
- Vect_reset_line(Segments);
- Vect_append_point(Segments, easting, northing, 0);
- Vect_write_line(&Out, GV_POINT, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
- }
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+ next_r = NR(d);
+ next_c = NC(d);
+
+ easting = window.west + (next_c + .5) * window.ew_res;
+ northing = window.north - (next_r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
+
+ if (NOT_IN_REGION(d))
+ Rast_set_c_null_value(&next_stream, 1);
+ else
+ segment_get(streams, &next_stream, next_r, next_c);
+
+ if (d < 1 || NOT_IN_REGION(d) || !next_stream) {
+ add_outlet = 1;
+ break;
+ }
+ r = next_r;
+ c = next_c;
+ segment_get(streams, &streams_cell, r, c);
+ } /* end while */
+
+ Vect_cat_set(Cats, 1, cur_stream);
+ Vect_write_line(&Out, GV_LINE, Segments, Cats);
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ if (add_outlet) {
+ Vect_cat_set(Cats, 1, 0);
+ Vect_reset_line(Segments);
+ Vect_append_point(Segments, easting, northing, 0);
+ Vect_write_line(&Out, GV_POINT, Segments, Cats);
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
}
+ }
-/* build vector after adding table */
- if(0<stream_add_table(number_of_streams))
-G_warning(_("Cannot add table to vector %s"),out_vector);
-Vect_hist_command(&Out);
-Vect_build(&Out);
-Vect_close(&Out);
-return 0;
+ /* build vector after adding table */
+ if (0 < stream_add_table(number_of_streams))
+ G_warning(_("Cannot add table to vector %s"), out_vector);
+ Vect_hist_command(&Out);
+ Vect_build(&Out);
+ Vect_close(&Out);
+ return 0;
}
-int stream_add_table (int number_of_streams) {
-
- int i,j;
- int max_trib=0;
- struct field_info *Fi;
- STREAM* SA=stream_attributes; /* for better code readability */
- dbDriver *driver;
- dbHandle handle;
- dbString table_name, db_sql, val_string;
- char *cat_col_name="cat";
- char buf[1000];
- char ins_prev_streams[50]; /* insert */
- /* rest of 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_orders="strahler integer, horton integer, shreve integer, hack integer, topo_dim integer";
- char* tab_scheidegger="scheidegger integer";
- char* tab_drwal_old="drwal_old integer";
- char* tab_length="length double precision";
- char* tab_stright="stright double precision";
- char* tab_sinusoid="sinosoid double precision";
- char* tab_cumlength="cum_length double precision";
- char* tab_accum="flow_accum double precision";
- char* tab_distance="out_dist double precision";
- char* tab_elev_init="source_elev double precision";
- char* tab_elev_outlet="outlet_elev double precision";
- char* tab_drop="elev_drop double precision";
- char* tab_out_drop="out_drop double precision";
- char* tab_gradient="gradient double precision";
- /* addational atrributes */
- int scheidegger, drwal_old=-1;
- double sinusoid=1, elev_drop, out_drop=0, gradient=-1;
- double flow_accum;
- char insert_orders[60]; /* must have to be increased if new orders are added */
+int stream_add_table(int number_of_streams)
+{
- db_init_string(&db_sql);
- db_init_string(&val_string);
- db_init_string(&table_name);
- db_init_handle(&handle);
-
- Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
- driver = db_start_driver_open_database(Fi->driver, Fi->database);
+ int i;
+ int max_trib = 0;
+ struct field_info *Fi;
+ STREAM *SA = stream_attributes; /* for better code readability */
+ dbDriver *driver;
+ dbHandle handle;
+ dbString table_name, db_sql, val_string;
+ char *cat_col_name = "cat";
+ char buf[1000];
+ char ins_prev_streams[50]; /* insert */
- /* create table */
- for (i=0;i<number_of_streams;++i)
- if (SA[i].trib_num>max_trib)
- max_trib=SA[i].trib_num;
+ /* rest of 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_orders =
+ "strahler integer, horton integer, shreve integer, hack integer, topo_dim integer";
+ char *tab_scheidegger = "scheidegger integer";
+ char *tab_drwal_old = "drwal_old integer";
+ char *tab_length = "length double precision";
+ char *tab_stright = "stright double precision";
+ char *tab_sinusoid = "sinosoid double precision";
+ char *tab_cumlength = "cum_length double precision";
+ char *tab_accum = "flow_accum double precision";
+ char *tab_distance = "out_dist double precision";
+ char *tab_elev_init = "source_elev double precision";
+ char *tab_elev_outlet = "outlet_elev double precision";
+ char *tab_drop = "elev_drop double precision";
+ char *tab_out_drop = "out_drop double precision";
+ char *tab_gradient = "gradient double precision";
- switch (max_trib) {
- case 2:
- tab_prev_streams="prev_str01 integer, prev_str02 integer";
+ /* addational atrributes */
+ int scheidegger, drwal_old = -1;
+ double sinusoid = 1, elev_drop, out_drop = 0, gradient = -1;
+ char insert_orders[60]; /* must have to be increased if new orders are added */
+
+ db_init_string(&db_sql);
+ db_init_string(&val_string);
+ db_init_string(&table_name);
+ db_init_handle(&handle);
+
+ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
+ driver = db_start_driver_open_database(Fi->driver, Fi->database);
+
+ /* create table */
+ for (i = 0; i < number_of_streams; ++i)
+ if (SA[i].trib_num > max_trib)
+ max_trib = SA[i].trib_num;
+
+ 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, \
+ sprintf(buf, "create table %s (%s, %s, %s, %s, %s, \
%s, %s, %s, %s, %s, \
%s, %s, %s, %s, %s, \
- %s, %s, %s)",
- Fi->table,
- tab_cat_col_name, /* 1 */
- tab_stream,
- tab_next_stream,
- tab_prev_streams,
- tab_orders, /* 5 */
- tab_scheidegger,
- tab_drwal_old,
- tab_length,
- tab_stright,
- tab_sinusoid, /* 10 */
- tab_cumlength,
- tab_accum,
- tab_distance,
- tab_elev_init,
- tab_elev_outlet, /* 15 */
- tab_drop,
- tab_out_drop,
- tab_gradient /* 18 */
- );
-
- db_set_string(&db_sql, buf);
+ %s, %s, %s)", Fi->table, tab_cat_col_name, /* 1 */
+ tab_stream, tab_next_stream, tab_prev_streams, tab_orders, /* 5 */
+ tab_scheidegger, tab_drwal_old, tab_length, tab_stright, tab_sinusoid, /* 10 */
+ tab_cumlength, tab_accum, tab_distance, tab_elev_init, tab_elev_outlet, /* 15 */
+ tab_drop, tab_out_drop, tab_gradient /* 18 */
+ );
- if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
+ db_set_string(&db_sql, buf);
+
+ if (db_execute_immediate(driver, &db_sql) != DB_OK) {
db_close_database(driver);
db_shutdown_driver(driver);
G_warning("Cannot create table %s", db_get_string(&db_sql));
return -1;
- }
+ }
- if (db_create_index2(driver,Fi->table,cat_col_name) !=DB_OK)
- G_warning(_("cannot create index on table"), Fi->table);
-
- if (db_grant_on_table(driver,Fi->table,
- DB_PRIV_SELECT, DB_GROUP|DB_PUBLIC) !=DB_OK) {
+ if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
+ G_warning(_("cannot create index on table %s"), Fi->table);
+
+ if (db_grant_on_table(driver, Fi->table,
+ DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK) {
G_warning(_("cannot grant privileges on table %s"), Fi->table);
return -1;
- }
- db_begin_transaction(driver);
-
- for (i=0;i<number_of_streams;++i) {
-
- if(SA[i].stream<0)
- continue;
-
+ }
+ db_begin_transaction(driver);
+
+ for (i = 0; i < number_of_streams; ++i) {
+
+ if (SA[i].stream < 0)
+ continue;
+
/* calc addational parameters */
-
- scheidegger=(all_orders[o_shreve][i])*2;
- if(all_orders[o_shreve][i]>0)
- drwal_old = (int)(log(all_orders[o_shreve][i])/log(2))+1;
-
- sinusoid=-1;
- if(SA[i].stright>0)
- sinusoid = SA[i].length/SA[i].stright;
+ scheidegger = (all_orders[o_shreve][i]) * 2;
- out_drop=0;
- if(SA[i].next_stream>0)
- out_drop = SA[i].outlet_elev - SA[SA[i].next_stream].init_elev;
+ if (all_orders[o_shreve][i] > 0)
+ drwal_old = (int)(log(all_orders[o_shreve][i]) / log(2)) + 1;
- elev_drop = (SA[i].init_elev-SA[i].outlet_elev)+out_drop;
- if(elev_drop<0)
- elev_drop=0;
-
- gradient = -1;
- if(SA[i].length>0)
- gradient = elev_drop / SA[i].length;
-
+ sinusoid = -1;
+ if (SA[i].stright > 0)
+ sinusoid = SA[i].length / SA[i].stright;
+
+ out_drop = 0;
+ if (SA[i].next_stream > 0)
+ out_drop = SA[i].outlet_elev - SA[SA[i].next_stream].init_elev;
+
+ elev_drop = (SA[i].init_elev - SA[i].outlet_elev) + out_drop;
+ if (elev_drop < 0)
+ elev_drop = 0;
+
+ gradient = -1;
+ if (SA[i].length > 0)
+ gradient = elev_drop / SA[i].length;
+
switch (max_trib) {
- case 2:
- sprintf(ins_prev_streams,"%d, %d",SA[i].trib[0],SA[i].trib[1]);
- break;
- case 3:
- sprintf(ins_prev_streams,"%d ,%d, %d",SA[i].trib[0],SA[i].trib[1],SA[i].trib[2]);
- break;
- case 4:
- sprintf(ins_prev_streams,"%d, %d, %d, %d",SA[i].trib[0],SA[i].trib[1],SA[i].trib[2],SA[i].trib[3]);
- break;
- case 5:
- sprintf(ins_prev_streams,"%d, %d, %d, %d, %d",SA[i].trib[0],SA[i].trib[1],SA[i].trib[2],SA[i].trib[3],SA[i].trib[4]);
- break;
- default:
- G_fatal_error("Error with number of tributuaries");
- break;
+ case 2:
+ sprintf(ins_prev_streams, "%d, %d", SA[i].trib[0], SA[i].trib[1]);
+ break;
+ case 3:
+ sprintf(ins_prev_streams, "%d ,%d, %d", SA[i].trib[0],
+ SA[i].trib[1], SA[i].trib[2]);
+ break;
+ case 4:
+ sprintf(ins_prev_streams, "%d, %d, %d, %d", SA[i].trib[0],
+ SA[i].trib[1], SA[i].trib[2], SA[i].trib[3]);
+ break;
+ case 5:
+ sprintf(ins_prev_streams, "%d, %d, %d, %d, %d", SA[i].trib[0],
+ SA[i].trib[1], SA[i].trib[2], SA[i].trib[3],
+ SA[i].trib[4]);
+ break;
+ default:
+ G_fatal_error("Error with number of tributuaries");
+ break;
}
-
+
sprintf(insert_orders, "%d, %d, %d, %d, %d",
- all_orders[0][i],all_orders[1][i],
- all_orders[2][i],all_orders[3][i],all_orders[4][i]);
-
- sprintf(buf,"insert into %s values( %d, %d, %d, %s, %s, \
+ all_orders[0][i], all_orders[1][i],
+ all_orders[2][i], all_orders[3][i], all_orders[4][i]);
+
+ sprintf(buf, "insert into %s values( %d, %d, %d, %s, %s, \
%d, %d, %f, %f, %f, \
%f, %f, %f, %f, %f, \
- %f, %f, %f)",
- Fi->table,
- i, /* 1 */
- SA[i].stream,
- SA[i].next_stream,
- ins_prev_streams, /* buffer created before */
- insert_orders, /* 5 */ /* buffer created before */
- scheidegger,
- drwal_old,
- SA[i].length,
- SA[i].stright,
- sinusoid, /* 10 */
- SA[i].accum_length,
- fabs(SA[i].accum),
- SA[i].distance,
- SA[i].init_elev,
- SA[i].outlet_elev, /* 15 */
- elev_drop,
- out_drop,
- gradient /* 18 */
- );
+ %f, %f, %f)", Fi->table, i, /* 1 */
+ SA[i].stream, SA[i].next_stream, ins_prev_streams, /* buffer created before */
+ insert_orders, /* 5 *//* buffer created before */
+ scheidegger, drwal_old, SA[i].length, SA[i].stright, sinusoid, /* 10 */
+ SA[i].accum_length, fabs(SA[i].accum), SA[i].distance, SA[i].init_elev, SA[i].outlet_elev, /* 15 */
+ elev_drop, out_drop, gradient /* 18 */
+ );
- db_set_string(&db_sql,buf);
+ db_set_string(&db_sql, buf);
- if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
- db_close_database(driver);
- db_shutdown_driver(driver);
- G_warning(_("Cannot inset new row: %s"), db_get_string(&db_sql));
- return -1;
- }
- } /* end for */
-
- db_commit_transaction(driver);
- db_close_database_shutdown_driver(driver);
- Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
- cat_col_name, Fi->database, Fi->driver);
- return 0;
+ if (db_execute_immediate(driver, &db_sql) != DB_OK) {
+ db_close_database(driver);
+ db_shutdown_driver(driver);
+ G_warning(_("Cannot inset new row: %s"), db_get_string(&db_sql));
+ return -1;
+ }
+ } /* end for */
+
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
+ cat_col_name, Fi->database, Fi->driver);
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,516 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- return 0;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
+
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
- Rast_put_row(output_fd, map->map[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
+ Rast_put_row(output_fd, (map->map)[r], output_data_type);
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,28 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/segment.h>
-#include <grass/vector.h>
-#include <grass/dbmi.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(x,y) (x)*ncols+(y)
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -30,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -41,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/local_proto.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/local_proto.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,3 +1,30 @@
#include "io.h"
#include "local_vars.h"
+int free_attributes(int number_of_streams);
+int convert_border_dir(int r, int c, int dir);
+
+int create_sectors(STREAM *cur_stream, int seg_length, int seg_skip,
+ double seg_treshold);
+int calc_tangents(STREAM *cur_stream, int seg_length, int seg_skip,
+ int number_streams);
+
+int create_sector_vector(char *out_vector, int number_of_streams, int radians);
+int create_segment_vector(char *out_vector, int number_of_streams,
+ int radians);
+
+
+int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
+ int number_of_streams);
+int ram_fill_streams(CELL **unique_streams, int number_of_streams);
+int ram_find_contributing_cell(int r, int c, CELL **dirs, FCELL **elevation);
+int ram_identify_next_stream(CELL **streams, int number_of_streams);
+int ram_number_of_streams(CELL **streams, CELL **dirs, int *ordered);
+
+int seg_build_streamlines(SEGMENT *streams, SEGMENT *dirs,
+ SEGMENT *elevation, int number_of_streams);
+int seg_fill_streams(SEGMENT *unique_streams, int number_of_streams);
+int seg_find_contributing_cell(int r, int c, SEGMENT *dirs,
+ SEGMENT *elevation);
+int seg_identify_next_stream(SEGMENT *streams, int number_of_streams);
+int seg_number_of_streams(SEGMENT *streams, SEGMENT *dirs, int *ordered);
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -24,272 +24,280 @@
#include <grass/glocale.h>
#include "local_proto.h"
- 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 nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int main(int argc, char *argv[])
{
- struct GModule *module; /* GRASS module for parsing arguments */
- struct Option *in_dir_opt, /* options */
- *in_stm_opt,
- *in_elev_opt,
- *out_segment_opt,
- *out_sector_opt,
- *opt_length,
- *opt_skip,
- *opt_threshold,
- *opt_swapsize;
-
- struct Flag *flag_radians,
- *flag_segmentation; /* segmentation library */
-
- int i,j;
- int seg_length, seg_skip;
- int radians, segmentation; /* flags */
- double seg_treshold;
- int number_of_streams, ordered;
-
- /* initialize GIS environment */
- G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
+ struct GModule *module; /* GRASS module for parsing arguments */
+ struct Option *in_dir_opt, /* options */
+ *in_stm_opt,
+ *in_elev_opt,
+ *out_segment_opt,
+ *out_sector_opt,
+ *opt_length, *opt_skip, *opt_threshold, *opt_swapsize;
- /* initialize module */
- module = G_define_module();
- module->description =
+ struct Flag *flag_radians, *flag_segmentation; /* segmentation library */
+
+ int i;
+ int seg_length, seg_skip;
+ int radians, segmentation; /* flags */
+ double seg_treshold;
+ int number_of_streams, ordered;
+
+ /* initialize GIS environment */
+ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
+
+ /* initialize module */
+ module = G_define_module();
+ module->description =
_("Divide network into near strigh-line segments and calculate its order");
- G_add_keyword("Stream divide");
- G_add_keyword("Stream direction");
- G_add_keyword("Stream gradient");
+ G_add_keyword("Stream divide");
+ G_add_keyword("Stream direction");
+ G_add_keyword("Stream gradient");
- in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_stm_opt->key = "streams";
- in_stm_opt->description = "Name of streams mask input map";
+ in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_stm_opt->key = "streams";
+ in_stm_opt->description = "Name of streams mask input map";
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_dir_opt->key = "dirs";
- in_dir_opt->description = "Name of flow direction input map";
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_dir_opt->key = "dirs";
+ in_dir_opt->description = "Name of flow direction input map";
- in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_elev_opt->key = "elevation";
- in_elev_opt->description = "Name of elevation map";
-
- out_segment_opt = G_define_standard_option(G_OPT_V_OUTPUT);
- out_segment_opt->key="segments";
- out_segment_opt->description =_("OUTPUT vector file to write segment atributes");
-
- out_sector_opt = G_define_standard_option(G_OPT_V_OUTPUT);
- out_sector_opt->key="sectors";
- out_sector_opt->description =_("OUTPUT vector file to write segment atributes");
-
- opt_length = G_define_option();
- opt_length->key = "length";
- opt_length->label = _("Search length to calculate direction");
- opt_length->description = _("Must be > 0");
- opt_length->answer = "15";
- opt_length->type = TYPE_INTEGER;
- opt_length->guisection=_("Optional");
-
- opt_skip = G_define_option();
- opt_skip->key = "skip";
- opt_skip->label = _("Skip segments shorter than");
- opt_skip->description = _("Must be >= 0");
- opt_skip->answer = "5";
- opt_skip->type = TYPE_INTEGER;
- opt_skip->guisection=_("Optional");
-
- opt_threshold = G_define_option();
- opt_threshold->key = "treshold";
- opt_threshold->label = _("Max angle (degrees) beetwen stream segments");
- opt_threshold->description = _("Must be > 0");
- opt_threshold->answer = "160";
- opt_threshold->type = TYPE_DOUBLE;
- opt_threshold->guisection=_("Optional");
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
-
- flag_radians = G_define_flag();
- flag_radians->key = 'r';
- flag_radians->description = _("Output angles in radians (default: degrees)");
-
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
+ in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_elev_opt->key = "elevation";
+ in_elev_opt->description = "Name of elevation map";
-
+ out_segment_opt = G_define_standard_option(G_OPT_V_OUTPUT);
+ out_segment_opt->key = "segments";
+ out_segment_opt->description =
+ _("OUTPUT vector file to write segment atributes");
+
+ out_sector_opt = G_define_standard_option(G_OPT_V_OUTPUT);
+ out_sector_opt->key = "sectors";
+ out_sector_opt->description =
+ _("OUTPUT vector file to write segment atributes");
+
+ opt_length = G_define_option();
+ opt_length->key = "length";
+ opt_length->label = _("Search length to calculate direction");
+ opt_length->description = _("Must be > 0");
+ opt_length->answer = "15";
+ opt_length->type = TYPE_INTEGER;
+ opt_length->guisection = _("Optional");
+
+ opt_skip = G_define_option();
+ opt_skip->key = "skip";
+ opt_skip->label = _("Skip segments shorter than");
+ opt_skip->description = _("Must be >= 0");
+ opt_skip->answer = "5";
+ opt_skip->type = TYPE_INTEGER;
+ opt_skip->guisection = _("Optional");
+
+ opt_threshold = G_define_option();
+ opt_threshold->key = "treshold";
+ opt_threshold->label = _("Max angle (degrees) beetwen stream segments");
+ opt_threshold->description = _("Must be > 0");
+ opt_threshold->answer = "160";
+ opt_threshold->type = TYPE_DOUBLE;
+ opt_threshold->guisection = _("Optional");
+
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
+
+ flag_radians = G_define_flag();
+ flag_radians->key = 'r';
+ flag_radians->description =
+ _("Output angles in radians (default: degrees)");
+
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+
+
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
- seg_length = atoi(opt_length->answer);
- seg_treshold = atof(opt_threshold->answer);
- seg_skip = atoi(opt_skip->answer);
- radians = (flag_radians->answer != 0);
- segmentation = (flag_segmentation->answer != 0);
-
- if (seg_length <= 0)
+ seg_length = atoi(opt_length->answer);
+ seg_treshold = atof(opt_threshold->answer);
+ seg_skip = atoi(opt_skip->answer);
+ radians = (flag_radians->answer != 0);
+ segmentation = (flag_segmentation->answer != 0);
+
+ if (seg_length <= 0)
G_fatal_error("Search's length must be > 0");
if (seg_treshold < 0 || seg_treshold > 180)
G_fatal_error("Treshold must be between 0 and 180");
- if (seg_skip < 0)
+ if (seg_skip < 0)
G_fatal_error("Segment's length must be >= 0");
-
- seg_treshold=DEG2RAD(seg_treshold);
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
- Rast_get_window(&window);
- G_begin_distance_calculations();
+ seg_treshold = DEG2RAD(seg_treshold);
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+ Rast_get_window(&window);
+ G_begin_distance_calculations();
-
-if(!segmentation) {
- MAP map_dirs, map_streams, map_elevation, map_output, map_unique_streams;
- CELL **streams, **dirs, **unique_streams=NULL;
+
+
+ if (!segmentation) {
+ MAP map_dirs, map_streams, map_elevation, map_unique_streams;
+ CELL **streams, **dirs, **unique_streams = NULL;
FCELL **elevation;
-
+
G_message(_("ALL IN RAM CALCULATION"));
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_elevation,FCELL_TYPE);
- ram_read_map(&map_elevation,in_elev_opt->answer,0,-1);
-
- streams=(CELL**)map_streams.map;
- dirs=(CELL**)map_dirs.map;
- elevation=(FCELL**)map_elevation.map;
-
- number_of_streams=ram_number_of_streams(streams,dirs,&ordered)+1;
- ram_build_streamlines(streams,dirs,elevation,number_of_streams);
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_elevation, FCELL_TYPE);
+ ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
- if(ordered) {
- ram_create_map(&map_unique_streams,CELL_TYPE);
- unique_streams=(CELL**)map_unique_streams.map;
- ram_fill_streams(unique_streams,number_of_streams);
- ram_identify_next_stream(unique_streams,number_of_streams);
- ram_release_map(&map_unique_streams);
- } else
- ram_identify_next_stream(streams,number_of_streams);
-
+ streams = (CELL **) map_streams.map;
+ dirs = (CELL **) map_dirs.map;
+ elevation = (FCELL **) map_elevation.map;
+
+ number_of_streams =
+ ram_number_of_streams(streams, dirs, &ordered) + 1;
+ ram_build_streamlines(streams, dirs, elevation, number_of_streams);
+
+ if (ordered) {
+ ram_create_map(&map_unique_streams, CELL_TYPE);
+ unique_streams = (CELL **) map_unique_streams.map;
+ ram_fill_streams(unique_streams, number_of_streams);
+ ram_identify_next_stream(unique_streams, number_of_streams);
+ ram_release_map(&map_unique_streams);
+ }
+ else
+ ram_identify_next_stream(streams, number_of_streams);
+
ram_release_map(&map_streams);
ram_release_map(&map_dirs);
ram_release_map(&map_elevation);
-}
+ }
-if(segmentation) {
- SEG map_dirs, map_streams, map_elevation, map_output, map_unique_streams;
- SEGMENT *streams, *dirs, *unique_streams=NULL;
+ if (segmentation) {
+ SEG map_dirs, map_streams, map_elevation, map_unique_streams;
+ SEGMENT *streams, *dirs, *unique_streams = NULL;
SEGMENT *elevation;
int number_of_segs;
-
+
G_message(_("MEMORY SWAP CALCULATION - MAY TAKE SOME TIME"));
number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32/0.18) : number_of_segs/0.18;
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs,CELL_TYPE);
- seg_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_elevation,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
- seg_read_map(&map_elevation,in_elev_opt->answer,0,-1);
-
- streams=&map_streams.seg;
- dirs=&map_dirs.seg;
- elevation=&map_elevation.seg;
-
- number_of_streams=seg_number_of_streams(streams,dirs,&ordered)+1;
- seg_build_streamlines(streams,dirs,elevation,number_of_streams);
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
+ FCELL_TYPE);
+ seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
- if(ordered) {
- seg_create_map(&map_unique_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- unique_streams=&map_unique_streams.seg;
- seg_fill_streams(unique_streams,number_of_streams);
- seg_identify_next_stream(unique_streams,number_of_streams);
- seg_release_map(&map_unique_streams);
- } else
- seg_identify_next_stream(streams,number_of_streams);
-
+ streams = &map_streams.seg;
+ dirs = &map_dirs.seg;
+ elevation = &map_elevation.seg;
+
+ number_of_streams =
+ seg_number_of_streams(streams, dirs, &ordered) + 1;
+ seg_build_streamlines(streams, dirs, elevation, number_of_streams);
+
+ if (ordered) {
+ seg_create_map(&map_unique_streams, SROWS, SCOLS, number_of_segs,
+ CELL_TYPE);
+ unique_streams = &map_unique_streams.seg;
+ seg_fill_streams(unique_streams, number_of_streams);
+ seg_identify_next_stream(unique_streams, number_of_streams);
+ seg_release_map(&map_unique_streams);
+ }
+ else
+ seg_identify_next_stream(streams, number_of_streams);
+
seg_release_map(&map_streams);
seg_release_map(&map_dirs);
seg_release_map(&map_elevation);
-}
+ }
-
- for(i=1;i<number_of_streams;++i)
- G_message("%d %d %d",stream_attributes[i].stream,
- stream_attributes[i].next_stream,
- stream_attributes[i].last_cell_dir);
+ for (i = 1; i < number_of_streams; ++i)
+ G_message("%d %d %d", stream_attributes[i].stream,
+ stream_attributes[i].next_stream,
+ stream_attributes[i].last_cell_dir);
-/*
- for(i=1;i<number_of_streams;++i)
- printf("STREAM %d NEXT_STREAM %d POINT %d \n",
- stream_attributes[i].stream,
- stream_attributes[i].next_stream,
- stream_attributes[i].outlet);
-*/
- G_message("Creating sectors and calculating attributes...");
-
- for(i=1;i<number_of_streams;++i) {
- create_sectors(&stream_attributes[i],seg_length, seg_skip,seg_treshold);
- calc_tangents(&stream_attributes[i],seg_length,seg_skip,number_of_streams);
- }
+ /*
+ for(i=1;i<number_of_streams;++i)
+ printf("STREAM %d NEXT_STREAM %d POINT %d \n",
+ stream_attributes[i].stream,
+ stream_attributes[i].next_stream,
+ stream_attributes[i].outlet);
+ */
+ G_message("Creating sectors and calculating attributes...");
+ for (i = 1; i < number_of_streams; ++i) {
+ create_sectors(&stream_attributes[i], seg_length, seg_skip,
+ seg_treshold);
+ calc_tangents(&stream_attributes[i], seg_length, seg_skip,
+ number_of_streams);
+ }
-/*
- for(j=1;j<number_of_streams;++j)
- G_message("STREAM %d ncells %d len %f str %f sin %f",
- stream_attributes[j].stream,
- stream_attributes[j].number_of_cells,
- stream_attributes[j].length,
- stream_attributes[j].stright,
- stream_attributes[j].length/stream_attributes[j].stright);
- G_message("%d",j);
+ /*
- for(j=1;j<number_of_streams;++j)
- printf("STREAM %d max %f min %f drop %f\n",
- stream_attributes[j].stream,
- stream_attributes[j].elevation[1],
- stream_attributes[j].elevation[stream_attributes[j].number_of_cells-1],
- stream_attributes[j].drop);
-
- for(j=1;j<number_of_streams;++j)
- for (i = 0; i < stream_attributes[j].number_of_sectors; ++i)
- printf("%d cat %d |BRAEK %d | dir %.2f | len %.2f | drop %.3f \n" ,j,
- stream_attributes[j].sector_cats[i],
- stream_attributes[j].sector_breakpoints[i],
- stream_attributes[j].sector_directions[i],
- stream_attributes[j].sector_lengths[i],
- stream_attributes[j].sector_drops[i]);
-
- for(j=1;j<number_of_streams;++j) {
- printf(" %d %d \n" ,j,stream_attributes[j].number_of_cells);
- for (i = 0; i <= stream_attributes[j].number_of_cells; ++i)
- printf("%d %f \n" ,i,stream_attributes[j].elevation[i]);
- }
-*/
-
- create_segment_vector(out_segment_opt->answer,number_of_streams,radians);
- create_sector_vector(out_sector_opt->answer,number_of_streams,radians);
-
- free_attributes(number_of_streams);
- G_message("Done");
- exit(EXIT_SUCCESS);
+ for(j=1;j<number_of_streams;++j)
+ G_message("STREAM %d ncells %d len %f str %f sin %f",
+ stream_attributes[j].stream,
+ stream_attributes[j].number_of_cells,
+ stream_attributes[j].length,
+ stream_attributes[j].stright,
+ stream_attributes[j].length/stream_attributes[j].stright);
+ G_message("%d",j);
+
+
+ for(j=1;j<number_of_streams;++j)
+ printf("STREAM %d max %f min %f drop %f\n",
+ stream_attributes[j].stream,
+ stream_attributes[j].elevation[1],
+ stream_attributes[j].elevation[stream_attributes[j].number_of_cells-1],
+ stream_attributes[j].drop);
+
+ for(j=1;j<number_of_streams;++j)
+ for (i = 0; i < stream_attributes[j].number_of_sectors; ++i)
+ printf("%d cat %d |BRAEK %d | dir %.2f | len %.2f | drop %.3f \n" ,j,
+ stream_attributes[j].sector_cats[i],
+ stream_attributes[j].sector_breakpoints[i],
+ stream_attributes[j].sector_directions[i],
+ stream_attributes[j].sector_lengths[i],
+ stream_attributes[j].sector_drops[i]);
+
+ for(j=1;j<number_of_streams;++j) {
+ printf(" %d %d \n" ,j,stream_attributes[j].number_of_cells);
+ for (i = 0; i <= stream_attributes[j].number_of_cells; ++i)
+ printf("%d %f \n" ,i,stream_attributes[j].elevation[i]);
+ }
+ */
+
+ create_segment_vector(out_segment_opt->answer, number_of_streams,
+ radians);
+ create_sector_vector(out_sector_opt->answer, number_of_streams, radians);
+
+ free_attributes(number_of_streams);
+ G_message("Done");
+ exit(EXIT_SUCCESS);
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/stream_segment.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/stream_segment.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/stream_segment.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,267 +1,283 @@
#include "local_proto.h"
-static int sector_cat=0;
+static int sector_cat = 0;
-float calc_dir(int rp,int cp,int rn,int cn) {
- return
- (cp-cn) == 0 ?
- (rp-rn) > 0 ? 0 : PI :
- (cp-cn) < 0 ?
- PI/2+atan((rp-rn)/(float)(cp-cn)) :
- 3*PI/2+atan((rp-rn)/(float)(cp-cn));
+float calc_dir(int rp, int cp, int rn, int cn)
+{
+ return
+ (cp - cn) == 0 ?
+ (rp - rn) > 0 ? 0 : PI :
+ (cp - cn) < 0 ?
+ PI / 2 + atan((rp - rn) / (float)(cp - cn)) :
+ 3 * PI / 2 + atan((rp - rn) / (float)(cp - cn));
}
-float calc_length(double* distance, int start, int stop)
-{
- float cum_length=0;
- int i;
+float calc_length(double *distance, int start, int stop)
+{
+ float cum_length = 0;
+ int i;
- for(i=start;i<stop;++i)
- cum_length+=distance[i];
- return cum_length;
+ for (i = start; i < stop; ++i)
+ cum_length += distance[i];
+ return cum_length;
}
-double calc_drop (float* elevation, int start, int stop)
+double calc_drop(float *elevation, int start, int stop)
{
- float result;
- result = elevation[start]-elevation[stop];
- return result<0 ? 0 : result;
+ float result;
+
+ result = elevation[start] - elevation[stop];
+ return result < 0 ? 0 : result;
}
-double calc_stright (int rp,int cp,int rn,int cn)
+double calc_stright(int rp, int cp, int rn, int cn)
{
- double northing, easting, next_northing, next_easting;
- northing = window.north - (rp +.5) * window.ns_res;
- easting = window.west + (cp +.5) * window.ew_res;
- next_northing = window.north - (rn +.5) * window.ns_res;
- next_easting = window.west + (cn +.5) * window.ew_res;
- return G_distance(easting,northing,next_easting,next_northing);
+ double northing, easting, next_northing, next_easting;
+
+ northing = window.north - (rp + .5) * window.ns_res;
+ easting = window.west + (cp + .5) * window.ew_res;
+ next_northing = window.north - (rn + .5) * window.ns_res;
+ next_easting = window.west + (cn + .5) * window.ew_res;
+ return G_distance(easting, northing, next_easting, next_northing);
}
-int create_sectors(STREAM* cur_stream, int seg_length, int seg_skip, double seg_treshold)
+int create_sectors(STREAM *cur_stream, int seg_length, int seg_skip,
+ double seg_treshold)
{
- DIRCELLS* streamline;
- unsigned long int* P; /* alias for points */
-
- int i, prev_i = 0;
- int number_of_cells;
- int cell_down, cell_up;
- int r,c,r_up,c_up,r_down,c_down;
- int seg_length_short=seg_length/3;
- float dir_down, dir_up, dir_diff, dir_diff_short;
-
- float local_minimum=PI;
- int number_of_sectors=0, cells_in_segment=1;
- int category_num=0;
- int local_minimum_point=0;
- int sector_index=0;
- int in_loop=0;
- int num_of_points=0, num_of_breakpoints=0;
-
- number_of_cells=cur_stream->number_of_cells-1;
- P=cur_stream->points;
-
- streamline=(DIRCELLS *) G_malloc((number_of_cells+1) * sizeof(DIRCELLS));
+ DIRCELLS *streamline;
+ unsigned long int *P; /* alias for points */
- /* init cells */
- for(i=0;i<number_of_cells+1;++i) {
- streamline[i].long_dir_diff=0;
- streamline[i].short_dir_diff=0;
- streamline[i].long_break=0;
- streamline[i].decision=0;
- }
-
- /* upstream: to init, downstream: to outlet */
- for(i=seg_skip; i<number_of_cells-seg_skip; ++i) {
- cell_up = i<seg_length ? i : seg_length;
- cell_down = i>number_of_cells-1-seg_length ?
- number_of_cells-1-i : seg_length;
-
- r=(int)P[i]/ncols;
- c=(int)P[i]%ncols;
- r_up=(int)P[i-cell_up]/ncols;
- c_up=(int)P[i-cell_up]%ncols;
- r_down=(int)P[i+cell_down]/ncols;
- c_down=(int)P[i+cell_down]%ncols;
+ int i, prev_i = 0;
+ int number_of_cells;
+ int cell_down, cell_up;
+ int r, c, r_up, c_up, r_down, c_down;
+ int seg_length_short = seg_length / 3;
+ float dir_down, dir_up, dir_diff;
- dir_down=calc_dir(r,c,r_down,c_down);
- dir_up=calc_dir(r,c,r_up,c_up);
- dir_diff=fabs(dir_up-dir_down);
- streamline[i].long_dir_diff= dir_diff > PI ? PI*2 - dir_diff : dir_diff;
- streamline[i].long_break = (streamline[i].long_dir_diff<seg_treshold) ? 1 : 0;
+ float local_minimum = PI;
+ int number_of_sectors = 0;
+ int local_minimum_point = 0;
+ int sector_index = 0;
+ int in_loop = 0;
+ int num_of_points = 0, num_of_breakpoints = 0;
- cell_up = i<seg_length_short ? i : seg_length_short;
- cell_down = i>number_of_cells-1-seg_length_short ?
- number_of_cells-1-i : seg_length_short;
+ number_of_cells = cur_stream->number_of_cells - 1;
+ P = cur_stream->points;
- r=(int)P[i]/ncols;
- c=(int)P[i]%ncols;
- r_up=(int)P[i-cell_up]/ncols;
- c_up=(int)P[i-cell_up]%ncols;
- r_down=(int)P[i+cell_down]/ncols;
- c_down=(int)P[i+cell_down]%ncols;
+ streamline =
+ (DIRCELLS *) G_malloc((number_of_cells + 1) * sizeof(DIRCELLS));
- dir_down=calc_dir(r,c,r_down,c_down);
- dir_up=calc_dir(r,c,r_up,c_up);
- dir_diff=fabs(dir_up-dir_down);
- streamline[i].short_dir_diff= dir_diff > PI ? PI*2 - dir_diff : dir_diff;
- }
-
- /* look for breakpoints */
- for (i = 0; i < number_of_cells; ++i) {
+ /* init cells */
+ for (i = 0; i < number_of_cells + 1; ++i) {
+ streamline[i].long_dir_diff = 0;
+ streamline[i].short_dir_diff = 0;
+ streamline[i].long_break = 0;
+ streamline[i].decision = 0;
+ }
- if (streamline[i].long_break) {
- num_of_breakpoints = 0;
- if (local_minimum > streamline[i].short_dir_diff) {
- local_minimum = streamline[i].short_dir_diff;
- local_minimum_point = i;
- in_loop = 1;
- } /* end local minimum */
+ /* upstream: to init, downstream: to outlet */
+ for (i = seg_skip; i < number_of_cells - seg_skip; ++i) {
+ cell_up = i < seg_length ? i : seg_length;
+ cell_down = i > number_of_cells - 1 - seg_length ?
+ number_of_cells - 1 - i : seg_length;
- } else if (!streamline[i].long_break && in_loop) {
- num_of_breakpoints++;
- if (num_of_breakpoints == (seg_length / 5)) {
- streamline[local_minimum_point].decision = 1;
- local_minimum = PI;
- in_loop = 0;
- }
+ r = (int)P[i] / ncols;
+ c = (int)P[i] % ncols;
+ r_up = (int)P[i - cell_up] / ncols;
+ c_up = (int)P[i - cell_up] % ncols;
+ r_down = (int)P[i + cell_down] / ncols;
+ c_down = (int)P[i + cell_down] % ncols;
+
+ dir_down = calc_dir(r, c, r_down, c_down);
+ dir_up = calc_dir(r, c, r_up, c_up);
+ dir_diff = fabs(dir_up - dir_down);
+ streamline[i].long_dir_diff =
+ dir_diff > PI ? PI * 2 - dir_diff : dir_diff;
+ streamline[i].long_break =
+ (streamline[i].long_dir_diff < seg_treshold) ? 1 : 0;
+
+ cell_up = i < seg_length_short ? i : seg_length_short;
+ cell_down = i > number_of_cells - 1 - seg_length_short ?
+ number_of_cells - 1 - i : seg_length_short;
+
+ r = (int)P[i] / ncols;
+ c = (int)P[i] % ncols;
+ r_up = (int)P[i - cell_up] / ncols;
+ c_up = (int)P[i - cell_up] % ncols;
+ r_down = (int)P[i + cell_down] / ncols;
+ c_down = (int)P[i + cell_down] % ncols;
+
+ dir_down = calc_dir(r, c, r_down, c_down);
+ dir_up = calc_dir(r, c, r_up, c_up);
+ dir_diff = fabs(dir_up - dir_down);
+ streamline[i].short_dir_diff =
+ dir_diff > PI ? PI * 2 - dir_diff : dir_diff;
+ }
+
+ /* look for breakpoints */
+ for (i = 0; i < number_of_cells; ++i) {
+
+ if (streamline[i].long_break) {
+ num_of_breakpoints = 0;
+ if (local_minimum > streamline[i].short_dir_diff) {
+ local_minimum = streamline[i].short_dir_diff;
+ local_minimum_point = i;
+ in_loop = 1;
+ } /* end local minimum */
+
+ }
+ else if (!streamline[i].long_break && in_loop) {
+ num_of_breakpoints++;
+ if (num_of_breakpoints == (seg_length / 5)) {
+ streamline[local_minimum_point].decision = 1;
+ local_minimum = PI;
+ in_loop = 0;
}
- }
+ }
+ }
- /* cleaning breakpoints*/
- for (i=0,num_of_points=0;i<number_of_cells;++i,++num_of_points) {
-
- if (streamline[i].decision) {
- //printf(" BEFORE %d %d\n",i,num_of_points);
- if (i<seg_skip || (i > seg_skip && num_of_points < seg_skip)) {
- streamline[i].decision = 0;
- i = local_minimum_point;
- } else {
- local_minimum_point = i;
- }
- num_of_points = 0;
+ /* cleaning breakpoints */
+ for (i = 0, num_of_points = 0; i < number_of_cells; ++i, ++num_of_points) {
+
+ if (streamline[i].decision) {
+ //printf(" BEFORE %d %d\n",i,num_of_points);
+ if (i < seg_skip || (i > seg_skip && num_of_points < seg_skip)) {
+ streamline[i].decision = 0;
+ i = local_minimum_point;
}
- }
-
- /* number of segment in streamline */
- for (i = 0; i < number_of_cells+1; ++i)
- if (streamline[i].decision == 1 || i == (number_of_cells - 1))
- number_of_sectors++;
+ else {
+ local_minimum_point = i;
+ }
+ num_of_points = 0;
+ }
+ }
+ /* number of segment in streamline */
+ for (i = 0; i < number_of_cells + 1; ++i)
+ if (streamline[i].decision == 1 || i == (number_of_cells - 1))
+ number_of_sectors++;
- cur_stream->number_of_sectors=number_of_sectors;
- cur_stream->sector_breakpoints=(int *)G_malloc(number_of_sectors * sizeof(int));
- cur_stream->sector_cats=(int *)G_malloc(number_of_sectors * sizeof(int));
- cur_stream->sector_directions=(float *)G_malloc(number_of_sectors * sizeof(float));
- cur_stream->sector_strights=(float *)G_malloc(number_of_sectors * sizeof(float));
- cur_stream->sector_lengths=(double *)G_malloc(number_of_sectors * sizeof(double));
- cur_stream->sector_drops=(float *)G_malloc(number_of_sectors * sizeof(float));
- /* add attributies */
- for (i = 0, prev_i=0; i < number_of_cells+1; ++i) {
- if (streamline[i].decision == 1 || i == (number_of_cells-1)) {
+ cur_stream->number_of_sectors = number_of_sectors;
+ cur_stream->sector_breakpoints =
+ (int *)G_malloc(number_of_sectors * sizeof(int));
+ cur_stream->sector_cats =
+ (int *)G_malloc(number_of_sectors * sizeof(int));
+ cur_stream->sector_directions =
+ (float *)G_malloc(number_of_sectors * sizeof(float));
+ cur_stream->sector_strights =
+ (float *)G_malloc(number_of_sectors * sizeof(float));
+ cur_stream->sector_lengths =
+ (double *)G_malloc(number_of_sectors * sizeof(double));
+ cur_stream->sector_drops =
+ (float *)G_malloc(number_of_sectors * sizeof(float));
- r=(int)P[i]/ncols;
- c=(int)P[i]%ncols;
- r_up=(int)P[prev_i]/ncols;
- c_up=(int)P[prev_i]%ncols;
-
- cur_stream->sector_breakpoints[sector_index] = i;
+ /* add attributies */
+ for (i = 0, prev_i = 0; i < number_of_cells + 1; ++i) {
+ if (streamline[i].decision == 1 || i == (number_of_cells - 1)) {
- cur_stream->sector_directions[sector_index] =
- calc_dir(r_up,c_up,r,c);
+ r = (int)P[i] / ncols;
+ c = (int)P[i] % ncols;
+ r_up = (int)P[prev_i] / ncols;
+ c_up = (int)P[prev_i] % ncols;
- cur_stream->sector_lengths[sector_index] =
- calc_length(cur_stream->distance,prev_i,i);
-
- cur_stream->sector_strights[sector_index] =
- calc_stright(r_up,c_up,r,c);
-
- cur_stream->sector_drops[sector_index] =
- calc_drop(cur_stream->elevation,prev_i,i);
+ cur_stream->sector_breakpoints[sector_index] = i;
- cur_stream->sector_cats[sector_index] = ++sector_cat;
- sector_index++;
- if (i < (number_of_cells - 1))
+ cur_stream->sector_directions[sector_index] =
+ calc_dir(r_up, c_up, r, c);
+
+ cur_stream->sector_lengths[sector_index] =
+ calc_length(cur_stream->distance, prev_i, i);
+
+ cur_stream->sector_strights[sector_index] =
+ calc_stright(r_up, c_up, r, c);
+
+ cur_stream->sector_drops[sector_index] =
+ calc_drop(cur_stream->elevation, prev_i, i);
+
+ cur_stream->sector_cats[sector_index] = ++sector_cat;
+ sector_index++;
+ if (i < (number_of_cells - 1))
prev_i = i;
- }
}
+ }
-/*
- for (i = 0; i < number_of_cells; ++i)
- printf("%d | %f %f |break %d | Dec %d \n" ,i,
- streamline[i].long_dir_diff,
- streamline[i].short_dir_diff,
- streamline[i].long_break,
- streamline[i].decision);
+ /*
+ for (i = 0; i < number_of_cells; ++i)
+ printf("%d | %f %f |break %d | Dec %d \n" ,i,
+ streamline[i].long_dir_diff,
+ streamline[i].short_dir_diff,
+ streamline[i].long_break,
+ streamline[i].decision);
-*/
- G_free(streamline);
- return 0;
+ */
+ G_free(streamline);
+ return 0;
}
-int calc_tangents(STREAM* cur_stream, int seg_length, int seg_skip, int number_streams)
+int calc_tangents(STREAM *cur_stream, int seg_length, int seg_skip,
+ int number_streams)
{
- int i;
- int cell_up, cell_down;
- int r,c,r_up,c_up,r_down,c_down;
- STREAM* SA=stream_attributes;
- unsigned long int* P=cur_stream->points;
- int next_stream=cur_stream->next_stream;
- int outlet=cur_stream->outlet;
- int last_cell=cur_stream->number_of_cells-1;
- int reached_end=1;
-
- /*before calc tangents add rest of streamline attributes */
- r_up=(int)P[1]/ncols;
- c_up=(int)P[1]%ncols;
- r_down=(int)P[last_cell]/ncols;
- c_down=(int)P[last_cell]%ncols;
+ int i;
+ int cell_up, cell_down;
+ int r, c, r_up, c_up, r_down, c_down;
+ STREAM *SA = stream_attributes;
+ unsigned long int *P = cur_stream->points;
+ int next_stream = cur_stream->next_stream;
+ int outlet = cur_stream->outlet;
+ int last_cell = cur_stream->number_of_cells - 1;
+ int reached_end = 1;
- cur_stream->direction=calc_dir(r_up,c_up,r_down,c_down);
- cur_stream->length=calc_length(cur_stream->distance,1,last_cell);
- cur_stream->stright=calc_stright(r_up,c_up,r_down,c_down);
- cur_stream->drop=calc_drop(cur_stream->elevation,1,last_cell);
+ /*before calc tangents add rest of streamline attributes */
+ r_up = (int)P[1] / ncols;
+ c_up = (int)P[1] % ncols;
+ r_down = (int)P[last_cell] / ncols;
+ c_down = (int)P[last_cell] % ncols;
- if(next_stream<1) {
- cur_stream->tangent=-1;
- cur_stream->continuation=-1;
+ cur_stream->direction = calc_dir(r_up, c_up, r_down, c_down);
+ cur_stream->length = calc_length(cur_stream->distance, 1, last_cell);
+ cur_stream->stright = calc_stright(r_up, c_up, r_down, c_down);
+ cur_stream->drop = calc_drop(cur_stream->elevation, 1, last_cell);
+
+ if (next_stream < 1) {
+ cur_stream->tangent = -1;
+ cur_stream->continuation = -1;
return 0;
- }
-
- /* find location of outlet in next stream */
- for(i=1;i<SA[next_stream].number_of_cells;++i) {
- if(SA[next_stream].points[i]==outlet) {
- reached_end=0;
- break;
- }
- }
-
- /* outlet not lies on the next stream */
- if(reached_end) {
+ }
+
+ /* find location of outlet in next stream */
+ for (i = 1; i < SA[next_stream].number_of_cells; ++i) {
+ if (SA[next_stream].points[i] == outlet) {
+ reached_end = 0;
+ break;
+ }
+ }
+
+ /* outlet not lies on the next stream */
+ if (reached_end) {
G_warning(_("Network topology error: cannot identify stream join for stream %d"),
- cur_stream->stream);
- cur_stream->tangent=-1;
- cur_stream->continuation=-1;
+ cur_stream->stream);
+ cur_stream->tangent = -1;
+ cur_stream->continuation = -1;
return 0;
- }
+ }
- cell_up = i <= seg_length ? i-1 : seg_length;
- cell_down = i >= (SA[next_stream].number_of_cells-seg_length) ?
- SA[next_stream].number_of_cells-seg_length-1 : seg_length;
-
- r=(int)SA[next_stream].points[i]/ncols;
- c=(int)SA[next_stream].points[i]%ncols;
- r_up=(int)SA[next_stream].points[i-cell_up]/ncols;
- c_up=(int)SA[next_stream].points[i-cell_up]%ncols;
- r_down=(int)SA[next_stream].points[i+cell_down]/ncols;
- c_down=(int)SA[next_stream].points[i+cell_down]%ncols;
-
- cur_stream->continuation=calc_dir(r,c,r_down,c_down);
- cur_stream->tangent = i==1 ? -1 :
- i < seg_skip ? cur_stream->continuation : calc_dir(r_up,c_up,r_down,c_down);
+ cell_up = i <= seg_length ? i - 1 : seg_length;
+ cell_down = i >= (SA[next_stream].number_of_cells - seg_length) ?
+ SA[next_stream].number_of_cells - seg_length - 1 : seg_length;
- return 0;
+ r = (int)SA[next_stream].points[i] / ncols;
+ c = (int)SA[next_stream].points[i] % ncols;
+ r_up = (int)SA[next_stream].points[i - cell_up] / ncols;
+ c_up = (int)SA[next_stream].points[i - cell_up] % ncols;
+ r_down = (int)SA[next_stream].points[i + cell_down] / ncols;
+ c_down = (int)SA[next_stream].points[i + cell_down] % ncols;
+
+ cur_stream->continuation = calc_dir(r, c, r_down, c_down);
+ cur_stream->tangent = i == 1 ? -1 :
+ i < seg_skip ? cur_stream->continuation : calc_dir(r_up, c_up, r_down,
+ c_down);
+
+ return 0;
}
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/stream_topology.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/stream_topology.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/stream_topology.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,53 +1,53 @@
#include "local_proto.h"
double get_distance(int r, int c, int d)
{
- double northing, easting, next_northing, next_easting;
- int next_r, next_c;
- next_r=NR(d);
- next_c=NC(d);
- northing = window.north - (r +.5) * window.ns_res;
- easting = window.west + (c +.5) * window.ew_res;
- next_northing = window.north - (next_r +.5) * window.ns_res;
- next_easting = window.west + (next_c +.5) * window.ew_res;
- return G_distance(easting,northing,next_easting,next_northing);
+ double northing, easting, next_northing, next_easting;
+ int next_r, next_c;
+
+ next_r = NR(d);
+ next_c = NC(d);
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (next_r + .5) * window.ns_res;
+ next_easting = window.west + (next_c + .5) * window.ew_res;
+ return G_distance(easting, northing, next_easting, next_northing);
}
-int ram_trib_nums(int r, int c, CELL** streams, CELL** dirs)
+int ram_trib_nums(int r, int c, CELL ** streams, CELL ** dirs)
{ /* calculate number of tributuaries */
- int trib_num = 0;
- int i, j;
- int next_r, next_c;
+ int trib_num = 0;
+ int i, j;
+ int next_r, next_c;
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
- if (streams[next_r][next_c] > 0 && dirs[next_r][next_c] == j)
- trib_num++;
- }
+ if (streams[next_r][next_c] > 0 && dirs[next_r][next_c] == j)
+ trib_num++;
+ }
- if (trib_num > 1)
+ if (trib_num > 1)
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
if (streams[next_r][next_c] == streams[r][c] &&
- dirs[next_r][next_c] == j)
+ dirs[next_r][next_c] == j)
trib_num--;
}
-
+
if (trib_num > 5)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
if (trib_num > 3)
G_warning(_("Stream network may be too dense..."));
@@ -55,50 +55,48 @@
} /* end trib_num */
-int seg_trib_nums(int r, int c, SEGMENT* streams, SEGMENT* dirs)
+int seg_trib_nums(int r, int c, SEGMENT *streams, SEGMENT *dirs)
{ /* calculate number of tributuaries */
- int trib_num = 0;
- int i, j;
- int next_r, next_c;
- int streams_cell, streams_next_cell, dirs_next_cell;
+ int trib_num = 0;
+ int i, j;
+ int next_r, next_c;
+ int streams_cell, streams_next_cell, dirs_next_cell;
- segment_get(streams,&streams_cell,r,c);
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ segment_get(streams, &streams_cell, r, c);
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
- segment_get(streams,&streams_next_cell,next_r,next_c);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
+ segment_get(streams, &streams_next_cell, next_r, next_c);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
- if (streams_next_cell > 0 && dirs_next_cell == j)
- trib_num++;
- }
+ if (streams_next_cell > 0 && dirs_next_cell == j)
+ trib_num++;
+ }
- if (trib_num > 1)
+ if (trib_num > 1)
for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue;
+ if (NOT_IN_REGION(i))
+ continue;
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- segment_get(streams,&streams_next_cell,next_r,next_c);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
-
- if (streams_next_cell == streams_cell &&
- dirs_next_cell == j)
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
+ segment_get(streams, &streams_next_cell, next_r, next_c);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
+
+ if (streams_next_cell == streams_cell && dirs_next_cell == j)
trib_num--;
}
-
+
if (trib_num > 5)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
if (trib_num > 3)
G_warning(_("Stream network may be too dense..."));
@@ -106,467 +104,467 @@
} /* end trib_num */
-int ram_number_of_streams(CELL** streams, CELL** dirs, int* ordered)
+int ram_number_of_streams(CELL **streams, CELL **dirs, int *ordered)
{
- int r, c;
- int stream_num=0;
- int min=100000000, max=-99999999;
- int one=0, two=0;
-
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c]>0)
+ int r, c;
+ int stream_num = 0;
+ int one = 0, two = 0;
+
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c] > 0)
if (ram_trib_nums(r, c, streams, dirs) != 1) {
- stream_num++;
- if(streams[r][c]==1)
+ stream_num++;
+ if (streams[r][c] == 1)
one++;
- if(streams[r][c]==2)
+ if (streams[r][c] == 2)
two++;
}
- *ordered = (one>1 || two>1) ? 1 : 0;
- /* if there is more than 1 stream with identifier 1 or 2 network is ordered */
-
- return stream_num;
+ *ordered = (one > 1 || two > 1) ? 1 : 0;
+ /* if there is more than 1 stream with identifier 1 or 2 network is ordered */
+
+ return stream_num;
}
-int seg_number_of_streams(SEGMENT* streams, SEGMENT* dirs, int* ordered)
+int seg_number_of_streams(SEGMENT *streams, SEGMENT *dirs, int *ordered)
{
- int r, c;
- int stream_num=0;
- int min=100000000, max=-99999999;
- int one=0, two=0;
- int streams_cell;
-
- for (r = 0; r < nrows; ++r)
+ int r, c;
+ int stream_num = 0;
+ int one = 0, two = 0;
+ int streams_cell;
+
+ for (r = 0; r < nrows; ++r)
for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- if (streams_cell>0)
+ segment_get(streams, &streams_cell, r, c);
+ if (streams_cell > 0)
if (seg_trib_nums(r, c, streams, dirs) != 1) {
- stream_num++;
- if(streams_cell==1)
+ stream_num++;
+ if (streams_cell == 1)
one++;
- if(streams_cell==2)
+ if (streams_cell == 2)
two++;
}
}
- *ordered = (one>1 || two>1) ? 1 : 0;
- /* if there is more than 1 stream with identifier 1 or 2 network is ordered */
-
- return stream_num;
+ *ordered = (one > 1 || two > 1) ? 1 : 0;
+ /* if there is more than 1 stream with identifier 1 or 2 network is ordered */
+
+ return stream_num;
}
-int ram_build_streamlines(CELL** streams, CELL** dirs, FCELL** elevation, int number_of_streams)
+int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
+ int number_of_streams)
{
- int r, c, i;
- int d, next_d;
- int next_r, next_c;
- int prev_r, prev_c;
- int stream_num=1, cell_num=0;
- int contrib_cell;
- STREAM* SA;
- int tmp=0;
- stream_num = 1;
- int border_dir;
+ int r, c, i;
+ int d, next_d;
+ int prev_r, prev_c;
+ int stream_num = 1, cell_num = 0;
+ int contrib_cell;
+ STREAM *SA;
+ int border_dir;
- stream_attributes = (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
- G_message("Finding inits...");
- SA=stream_attributes;
+ stream_attributes =
+ (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
+ G_message("Finding inits...");
+ SA = stream_attributes;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c])
- if (ram_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
- if(stream_num>number_of_streams)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
-
- SA[stream_num].stream = stream_num;
- SA[stream_num].init = INDEX(r,c);
- stream_num++;
- }
-
- for(i=1;i<stream_num;++i) {
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c])
+ if (ram_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
+ if (stream_num > number_of_streams)
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
-
- r=(int)SA[i].init/ncols;
- c=(int)SA[i].init%ncols;
- SA[i].order=streams[r][c];
- SA[i].number_of_cells=0;
- do {
-
- SA[i].number_of_cells++;
- d=abs(dirs[r][c]);
- if(NOT_IN_REGION(d) || d==0)
- break;
- r=NR(d);
- c=NC(d);
- } while (streams[r][c]==SA[i].order);
-
- SA[i].number_of_cells+=2; /* add two extra points for init+ and outlet+ */
- }
+ SA[stream_num].stream = stream_num;
+ SA[stream_num].init = INDEX(r, c);
+ stream_num++;
+ }
- for(i=1;i<number_of_streams;++i) {
+ for (i = 1; i < stream_num; ++i) {
- SA[i].points = (unsigned long int *)
- G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
- SA[i].elevation = (float*)
- G_malloc((SA[i].number_of_cells) * sizeof(float));
- SA[i].distance = (double*)
- G_malloc((SA[i].number_of_cells) * sizeof(double));
-
- r=(int)SA[i].init/ncols;
- c=(int)SA[i].init%ncols;
- contrib_cell=ram_find_contributing_cell(r,c, dirs,elevation);
- prev_r=NR(contrib_cell);
- prev_c=NC(contrib_cell);
-
- /* add one point contributing to init to calculate parameters */
- /* what to do if there is no contributing points? */
- SA[i].points[0] = (contrib_cell==0) ? -1 :
- INDEX(prev_r,prev_c);
- SA[i].elevation[0]=(contrib_cell==0) ? -99999 :
- elevation[prev_r][prev_c];
- d=(contrib_cell==0) ? dirs[r][c] : dirs[prev_r][prev_c];
- SA[i].distance[0]=(contrib_cell==0) ? get_distance(r,c,d) :
- get_distance(prev_r,prev_c,d);
-
- SA[i].points[1] = INDEX(r,c);
- SA[i].elevation[1]=elevation[r][c];
- d=abs(dirs[r][c]);
- SA[i].distance[1]=get_distance(r,c,d);
- cell_num=2;
- do {
- d=abs(dirs[r][c]);
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+ SA[i].order = streams[r][c];
+ SA[i].number_of_cells = 0;
+ do {
- if(NOT_IN_REGION(d) || d==0) {
- SA[i].points[cell_num]=-1;
- SA[i].distance[cell_num] = SA[i].distance[cell_num-1];
- SA[i].elevation[cell_num] =
- 2*SA[i].elevation[cell_num-1]-SA[i].elevation[cell_num-2];
- border_dir=convert_border_dir(r,c, dirs[r][c]);
- SA[i].last_cell_dir=border_dir;
+ SA[i].number_of_cells++;
+ d = abs(dirs[r][c]);
+ if (NOT_IN_REGION(d) || d == 0)
break;
- }
- r=NR(d);
- c=NC(d);
- SA[i].last_cell_dir=dirs[r][c];
- SA[i].points[cell_num] = INDEX(r,c);
- SA[i].elevation[cell_num] = elevation[r][c];
- next_d=(abs(dirs[r][c])==0) ? d : abs(dirs[r][c]);
- SA[i].distance[cell_num] = get_distance(r,c,next_d);
- cell_num++;
- if(cell_num>SA[i].number_of_cells)
- G_fatal_error(_("To much points in stream line..."));
- } while (streams[r][c]==SA[i].order);
-
- if(SA[i].elevation[0]==-99999)
- SA[i].elevation[0]=2*SA[i].elevation[1]-SA[i].elevation[2];
- }
- return 0;
+ r = NR(d);
+ c = NC(d);
+ } while (streams[r][c] == SA[i].order);
+
+ SA[i].number_of_cells += 2; /* add two extra points for init+ and outlet+ */
+ }
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ SA[i].points = (unsigned long int *)
+ G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+ SA[i].elevation = (float *)
+ G_malloc((SA[i].number_of_cells) * sizeof(float));
+ SA[i].distance = (double *)
+ G_malloc((SA[i].number_of_cells) * sizeof(double));
+
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+ contrib_cell = ram_find_contributing_cell(r, c, dirs, elevation);
+ prev_r = NR(contrib_cell);
+ prev_c = NC(contrib_cell);
+
+ /* add one point contributing to init to calculate parameters */
+ /* what to do if there is no contributing points? */
+ SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
+ SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
+ elevation[prev_r][prev_c];
+ d = (contrib_cell == 0) ? dirs[r][c] : dirs[prev_r][prev_c];
+ SA[i].distance[0] = (contrib_cell == 0) ? get_distance(r, c, d) :
+ get_distance(prev_r, prev_c, d);
+
+ SA[i].points[1] = INDEX(r, c);
+ SA[i].elevation[1] = elevation[r][c];
+ d = abs(dirs[r][c]);
+ SA[i].distance[1] = get_distance(r, c, d);
+
+ cell_num = 2;
+ do {
+ d = abs(dirs[r][c]);
+
+ if (NOT_IN_REGION(d) || d == 0) {
+ SA[i].points[cell_num] = -1;
+ SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+ SA[i].elevation[cell_num] =
+ 2 * SA[i].elevation[cell_num - 1] -
+ SA[i].elevation[cell_num - 2];
+ border_dir = convert_border_dir(r, c, dirs[r][c]);
+ SA[i].last_cell_dir = border_dir;
+ break;
+ }
+ r = NR(d);
+ c = NC(d);
+ SA[i].last_cell_dir = dirs[r][c];
+ SA[i].points[cell_num] = INDEX(r, c);
+ SA[i].elevation[cell_num] = elevation[r][c];
+ next_d = (abs(dirs[r][c]) == 0) ? d : abs(dirs[r][c]);
+ SA[i].distance[cell_num] = get_distance(r, c, next_d);
+ cell_num++;
+ if (cell_num > SA[i].number_of_cells)
+ G_fatal_error(_("To much points in stream line..."));
+ } while (streams[r][c] == SA[i].order);
+
+ if (SA[i].elevation[0] == -99999)
+ SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
+ }
+ return 0;
}
-int seg_build_streamlines(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation, int number_of_streams)
+int seg_build_streamlines(SEGMENT *streams, SEGMENT *dirs,
+ SEGMENT *elevation, int number_of_streams)
{
- int r, c, i;
- int d, next_d;
- int next_r, next_c;
- int prev_r, prev_c;
- int stream_num=1, cell_num=0;
- int contrib_cell;
- STREAM* SA;
- int tmp=0;
- stream_num = 1;
- int border_dir;
- int streams_cell, dirs_cell;
- int streams_next_cell, dirs_next_cell;
- int streams_prev_cell, dirs_prev_cell;
- float elevation_cell, elevation_next_cell, elevation_prev_cell;
+ int r, c, i;
+ int d, next_d;
+ int prev_r, prev_c;
+ int stream_num = 1, cell_num = 0;
+ int contrib_cell;
+ STREAM *SA;
+ int border_dir;
+ int streams_cell, dirs_cell;
+ int dirs_prev_cell;
+ float elevation_cell, elevation_prev_cell;
- stream_attributes = (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
- G_message("Finding inits...");
- SA=stream_attributes;
+ stream_attributes =
+ (STREAM *) G_malloc(number_of_streams * sizeof(STREAM));
+ G_message("Finding inits...");
+ SA = stream_attributes;
-/* finding inits */
- for (r = 0; r < nrows; ++r)
-for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
+ /* finding inits */
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
- if (streams_cell)
- if (seg_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
- if(stream_num>number_of_streams)
- G_fatal_error(_
- ("Error finding inits. Stream and direction maps probably do not match..."));
+ if (streams_cell)
+ if (seg_trib_nums(r, c, streams, dirs) != 1) { /* adding inits */
+ if (stream_num > number_of_streams)
+ G_fatal_error(_("Error finding inits. Stream and direction maps probably do not match..."));
- SA[stream_num].stream = stream_num;
- SA[stream_num].init = INDEX(r,c);
- stream_num++;
+ SA[stream_num].stream = stream_num;
+ SA[stream_num].init = INDEX(r, c);
+ stream_num++;
+ }
}
-}
-
-/* buildning streamline */
- for(i=1;i<stream_num;++i) {
- r=(int)SA[i].init/ncols;
- c=(int)SA[i].init%ncols;
- segment_get(streams,&streams_cell,r,c);
- SA[i].order=streams_cell;
- SA[i].number_of_cells=0;
+ /* buildning streamline */
+ for (i = 1; i < stream_num; ++i) {
- do {
-
- SA[i].number_of_cells++;
- segment_get(dirs,&dirs_cell,r,c);
-
- d=abs(dirs_cell);
- if(NOT_IN_REGION(d) || d==0)
- break;
- r=NR(d);
- c=NC(d);
- segment_get(streams,&streams_cell,r,c);
- } while (streams_cell==SA[i].order);
-
- SA[i].number_of_cells+=2; /* add two extra points for point before init and after outlet */
- }
-
- for(i=1;i<number_of_streams;++i) {
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+ segment_get(streams, &streams_cell, r, c);
+ SA[i].order = streams_cell;
+ SA[i].number_of_cells = 0;
- SA[i].points = (unsigned long int *)
- G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
- SA[i].elevation = (float*)
- G_malloc((SA[i].number_of_cells) * sizeof(float));
- SA[i].distance = (double*)
- G_malloc((SA[i].number_of_cells) * sizeof(double));
-
- r=(int)SA[i].init/ncols;
- c=(int)SA[i].init%ncols;
- contrib_cell=seg_find_contributing_cell(r,c, dirs,elevation);
- prev_r=NR(contrib_cell);
- prev_c=NC(contrib_cell);
-
- /* add one point contributing to init to calculate parameters */
- /* what to do if there is no contributing points? */
-
- segment_get(dirs,&dirs_cell,r,c);
- segment_get(dirs,&dirs_prev_cell,prev_r,prev_c);
- segment_get(elevation,&elevation_prev_cell,prev_r,prev_c);
- segment_get(elevation,&elevation_cell,r,c);
-
- SA[i].points[0] = (contrib_cell==0) ? -1 :
- INDEX(prev_r,prev_c);
- SA[i].elevation[0]=(contrib_cell==0) ? -99999 :
- elevation_prev_cell;
- d=(contrib_cell==0) ? dirs_cell : dirs_prev_cell;
- SA[i].distance[0]=(contrib_cell==0) ? get_distance(r,c,d) :
- get_distance(prev_r,prev_c,d);
-
- SA[i].points[1] = INDEX(r,c);
- SA[i].elevation[1]=elevation_cell;
- d=abs(dirs_cell);
- SA[i].distance[1]=get_distance(r,c,d);
+ do {
- cell_num=2;
- do {
- segment_get(dirs,&dirs_cell,r,c);
- d=abs(dirs_cell);
+ SA[i].number_of_cells++;
+ segment_get(dirs, &dirs_cell, r, c);
- if(NOT_IN_REGION(d) || d==0) {
- SA[i].points[cell_num]=-1;
- SA[i].distance[cell_num] = SA[i].distance[cell_num-1];
- SA[i].elevation[cell_num] =
- 2*SA[i].elevation[cell_num-1]-SA[i].elevation[cell_num-2];
- border_dir=convert_border_dir(r,c, dirs_cell);
- SA[i].last_cell_dir=border_dir;
+ d = abs(dirs_cell);
+ if (NOT_IN_REGION(d) || d == 0)
break;
- }
- r=NR(d);
- c=NC(d);
- segment_get(dirs,&dirs_cell,r,c);
- SA[i].last_cell_dir=dirs_cell;
- SA[i].points[cell_num] = INDEX(r,c);
- segment_get(elevation,&SA[i].elevation[cell_num],r,c);
- next_d=(abs(dirs_cell)==0) ? d : abs(dirs_cell);
- SA[i].distance[cell_num] = get_distance(r,c,next_d);
- cell_num++;
- if(cell_num>SA[i].number_of_cells)
- G_fatal_error(_("To much points in stream line..."));
- segment_get(streams,&streams_cell,r,c);
- } while (streams_cell==SA[i].order);
-
- if(SA[i].elevation[0]==-99999)
- SA[i].elevation[0]=2*SA[i].elevation[1]-SA[i].elevation[2];
- }
- return 0;
+ r = NR(d);
+ c = NC(d);
+ segment_get(streams, &streams_cell, r, c);
+ } while (streams_cell == SA[i].order);
+
+ SA[i].number_of_cells += 2; /* add two extra points for point before init and after outlet */
+ }
+
+ for (i = 1; i < number_of_streams; ++i) {
+
+ SA[i].points = (unsigned long int *)
+ G_malloc((SA[i].number_of_cells) * sizeof(unsigned long int));
+ SA[i].elevation = (float *)
+ G_malloc((SA[i].number_of_cells) * sizeof(float));
+ SA[i].distance = (double *)
+ G_malloc((SA[i].number_of_cells) * sizeof(double));
+
+ r = (int)SA[i].init / ncols;
+ c = (int)SA[i].init % ncols;
+ contrib_cell = seg_find_contributing_cell(r, c, dirs, elevation);
+ prev_r = NR(contrib_cell);
+ prev_c = NC(contrib_cell);
+
+ /* add one point contributing to init to calculate parameters */
+ /* what to do if there is no contributing points? */
+
+ segment_get(dirs, &dirs_cell, r, c);
+ segment_get(dirs, &dirs_prev_cell, prev_r, prev_c);
+ segment_get(elevation, &elevation_prev_cell, prev_r, prev_c);
+ segment_get(elevation, &elevation_cell, r, c);
+
+ SA[i].points[0] = (contrib_cell == 0) ? -1 : INDEX(prev_r, prev_c);
+ SA[i].elevation[0] = (contrib_cell == 0) ? -99999 :
+ elevation_prev_cell;
+ d = (contrib_cell == 0) ? dirs_cell : dirs_prev_cell;
+ SA[i].distance[0] = (contrib_cell == 0) ? get_distance(r, c, d) :
+ get_distance(prev_r, prev_c, d);
+
+ SA[i].points[1] = INDEX(r, c);
+ SA[i].elevation[1] = elevation_cell;
+ d = abs(dirs_cell);
+ SA[i].distance[1] = get_distance(r, c, d);
+
+ cell_num = 2;
+ do {
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+
+ if (NOT_IN_REGION(d) || d == 0) {
+ SA[i].points[cell_num] = -1;
+ SA[i].distance[cell_num] = SA[i].distance[cell_num - 1];
+ SA[i].elevation[cell_num] =
+ 2 * SA[i].elevation[cell_num - 1] -
+ SA[i].elevation[cell_num - 2];
+ border_dir = convert_border_dir(r, c, dirs_cell);
+ SA[i].last_cell_dir = border_dir;
+ break;
+ }
+ r = NR(d);
+ c = NC(d);
+ segment_get(dirs, &dirs_cell, r, c);
+ SA[i].last_cell_dir = dirs_cell;
+ SA[i].points[cell_num] = INDEX(r, c);
+ segment_get(elevation, &SA[i].elevation[cell_num], r, c);
+ next_d = (abs(dirs_cell) == 0) ? d : abs(dirs_cell);
+ SA[i].distance[cell_num] = get_distance(r, c, next_d);
+ cell_num++;
+ if (cell_num > SA[i].number_of_cells)
+ G_fatal_error(_("To much points in stream line..."));
+ segment_get(streams, &streams_cell, r, c);
+ } while (streams_cell == SA[i].order);
+
+ if (SA[i].elevation[0] == -99999)
+ SA[i].elevation[0] = 2 * SA[i].elevation[1] - SA[i].elevation[2];
+ }
+ return 0;
}
-int ram_find_contributing_cell(int r, int c, CELL** dirs, FCELL** elevation)
+int ram_find_contributing_cell(int r, int c, CELL **dirs, FCELL **elevation)
{
- int i,j=0;
- int next_r, next_c;
- float elev_min=9999;
+ int i, j = 0;
+ int next_r, next_c;
+ float elev_min = 9999;
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- next_r=NR(i);
- next_c=NC(i);
- if(dirs[next_r][next_c]==DIAG(i) && elevation[next_r][next_c]<elev_min) {
- elev_min=elevation[next_r][next_c];
- j=i;
- }
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ next_r = NR(i);
+ next_c = NC(i);
+ if (dirs[next_r][next_c] == DIAG(i) &&
+ elevation[next_r][next_c] < elev_min) {
+ elev_min = elevation[next_r][next_c];
+ j = i;
}
+ }
- return j;
+ return j;
}
-int seg_find_contributing_cell(int r, int c, SEGMENT* dirs, SEGMENT* elevation)
+int seg_find_contributing_cell(int r, int c, SEGMENT *dirs,
+ SEGMENT *elevation)
{
- int i,j=0;
- int next_r, next_c;
- float elev_min=9999;
- int dirs_next_cell;
- float elevation_next_cell;
+ int i, j = 0;
+ int next_r, next_c;
+ float elev_min = 9999;
+ int dirs_next_cell;
+ float elevation_next_cell;
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- next_r=NR(i);
- next_c=NC(i);
- segment_get(elevation,&elevation_next_cell,next_r,next_c);
- segment_get(dirs,&dirs_next_cell,next_r,next_c);
-
- if(dirs_next_cell==DIAG(i) && elevation_next_cell<elev_min) {
- elev_min=elevation_next_cell;
- j=i;
- }
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ next_r = NR(i);
+ next_c = NC(i);
+ segment_get(elevation, &elevation_next_cell, next_r, next_c);
+ segment_get(dirs, &dirs_next_cell, next_r, next_c);
+
+ if (dirs_next_cell == DIAG(i) && elevation_next_cell < elev_min) {
+ elev_min = elevation_next_cell;
+ j = i;
}
- return j;
+ }
+ return j;
}
-int ram_fill_streams(CELL** unique_streams,int number_of_streams)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- unique_streams[r][c] = SA[i].stream;
- }
+int ram_fill_streams(CELL **unique_streams, int number_of_streams)
+{
+ int r, c;
+ int i, j;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ unique_streams[r][c] = SA[i].stream;
}
- return 0;
+ }
+ return 0;
}
-int seg_fill_streams(SEGMENT* unique_streams,int number_of_streams)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- for(j=1;j<SA[i].number_of_cells-1;++j) {
- r=(int)SA[i].points[j]/ncols;
- c=(int)SA[i].points[j]%ncols;
- segment_put(unique_streams,&SA[i].stream,r,c);
- }
+int seg_fill_streams(SEGMENT *unique_streams, int number_of_streams)
+{
+ int r, c;
+ int i, j;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ for (j = 1; j < SA[i].number_of_cells - 1; ++j) {
+ r = (int)SA[i].points[j] / ncols;
+ c = (int)SA[i].points[j] % ncols;
+ segment_put(unique_streams, &SA[i].stream, r, c);
}
- return 0;
+ }
+ return 0;
}
-int ram_identify_next_stream(CELL** streams,int number_of_streams)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- if(SA[i].points[SA[i].number_of_cells-1]==-1) {
- SA[i].next_stream=-1;
- SA[i].outlet=-1;
- }
- else {
- r=(int)SA[i].points[SA[i].number_of_cells-1]/ncols;
- c=(int)SA[i].points[SA[i].number_of_cells-1]%ncols;
- SA[i].next_stream=streams[r][c];
- SA[i].outlet=SA[i].points[SA[i].number_of_cells-1];
- }
- }
- return 0;
+int ram_identify_next_stream(CELL **streams, int number_of_streams)
+{
+ int r, c;
+ int i;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ if (SA[i].points[SA[i].number_of_cells - 1] == -1) {
+ SA[i].next_stream = -1;
+ SA[i].outlet = -1;
+ }
+ else {
+ r = (int)SA[i].points[SA[i].number_of_cells - 1] / ncols;
+ c = (int)SA[i].points[SA[i].number_of_cells - 1] % ncols;
+ SA[i].next_stream = streams[r][c];
+ SA[i].outlet = SA[i].points[SA[i].number_of_cells - 1];
+ }
+ }
+ return 0;
}
-int seg_identify_next_stream(SEGMENT* streams,int number_of_streams)
-{
- int r,c;
- int i,j;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- if(SA[i].points[SA[i].number_of_cells-1]==-1) {
- SA[i].next_stream=-1;
- SA[i].outlet=-1;
- }
- else {
- r=(int)SA[i].points[SA[i].number_of_cells-1]/ncols;
- c=(int)SA[i].points[SA[i].number_of_cells-1]%ncols;
- segment_get(streams,&SA[i].next_stream,r,c);
- SA[i].outlet=SA[i].points[SA[i].number_of_cells-1];
- }
- }
- return 0;
+int seg_identify_next_stream(SEGMENT *streams, int number_of_streams)
+{
+ int r, c;
+ int i;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ if (SA[i].points[SA[i].number_of_cells - 1] == -1) {
+ SA[i].next_stream = -1;
+ SA[i].outlet = -1;
+ }
+ else {
+ r = (int)SA[i].points[SA[i].number_of_cells - 1] / ncols;
+ c = (int)SA[i].points[SA[i].number_of_cells - 1] % ncols;
+ segment_get(streams, &SA[i].next_stream, r, c);
+ SA[i].outlet = SA[i].points[SA[i].number_of_cells - 1];
+ }
+ }
+ return 0;
}
-int free_attributes (int number_of_streams)
+int free_attributes(int number_of_streams)
{
- int i;
- STREAM* SA;
- SA=stream_attributes;
-
- for(i=1;i<number_of_streams;++i) {
- G_free(SA[i].points);
- G_free(SA[i].elevation);
- G_free(SA[i].distance);
- G_free(SA[i].sector_breakpoints);
- G_free(SA[i].sector_cats);
- G_free(SA[i].sector_directions);
- G_free(SA[i].sector_lengths);
- G_free(SA[i].sector_drops);
- }
- G_free(stream_attributes);
- return 0;
+ int i;
+ STREAM *SA;
+
+ SA = stream_attributes;
+
+ for (i = 1; i < number_of_streams; ++i) {
+ G_free(SA[i].points);
+ G_free(SA[i].elevation);
+ G_free(SA[i].distance);
+ G_free(SA[i].sector_breakpoints);
+ G_free(SA[i].sector_cats);
+ G_free(SA[i].sector_directions);
+ G_free(SA[i].sector_lengths);
+ G_free(SA[i].sector_drops);
+ }
+ G_free(stream_attributes);
+ return 0;
}
int convert_border_dir(int r, int c, int dir)
{
-/* this function must be added to other modules */
-/* this is added to fix r.stream.extract issue with broder cell direction */
- if (dir)
+ /* this function must be added to other modules */
+ /* this is added to fix r.stream.extract issue with broder cell direction */
+ if (dir)
return dir;
-
- if(r==0 && c==0)
+
+ if (r == 0 && c == 0)
return -3;
- else if (r==0 && c==ncols-1)
+ else if (r == 0 && c == ncols - 1)
return -1;
- else if (r==nrows-1 && c==ncols-1)
+ else if (r == nrows - 1 && c == ncols - 1)
return -7;
- else if (r==nrows-1 && c==0)
+ else if (r == nrows - 1 && c == 0)
return -5;
- else if(r==0)
+ else if (r == 0)
return -2;
- else if(r==nrows-1)
+ else if (r == nrows - 1)
return -6;
- else if(c==0)
+ else if (c == 0)
return -4;
- else if(c==ncols-1)
+ else if (c == ncols - 1)
return -8;
- else
+ else
return 0;
-}
+}
Modified: grass-addons/grass7/raster/r.stream/r.stream.segment/stream_vector.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.segment/stream_vector.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.segment/stream_vector.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,73 +1,76 @@
#include "local_proto.h"
-int create_sector_vector(char* out_vector, int number_of_streams, int radians)
+
+int create_sector_vector(char *out_vector, int number_of_streams, int radians)
{
- int i,j,k;
- int r,c,d;
- int start, stop;
- float northing, easting;
- struct Map_info Out;
- struct line_pnts *Segments;
- struct line_cats *Cats;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- struct field_info *Fi;
- dbString table_name, db_sql, val_string;
- dbDriver *driver;
- dbHandle handle;
- char *cat_col_name = "cat";
- char buf[1000];
+ int i, j, k;
+ int r, c, d;
+ int start, stop;
+ float northing, easting;
+ struct Map_info Out;
+ struct line_pnts *Segments;
+ struct line_cats *Cats;
+ STREAM *SA = stream_attributes; /* for better code readability */
- int sector_category, segment, sector, order;
- double direction, azimuth, length, stright, sinusoid;
- double elev_min, elev_max, drop, gradient;
-
- Segments = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
- Vect_open_new(&Out, out_vector, 0);
-
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
-
- for (i=1;i<number_of_streams;++i) {
- stop=1;
- for(j=0;j<SA[i].number_of_sectors;++j) {
- start=stop;
- stop= (j==SA[i].number_of_sectors-1) ?
- SA[i].sector_breakpoints[j]+1 : SA[i].sector_breakpoints[j]+1;
- Vect_cat_set(Cats, 1, SA[i].sector_cats[j]);
- for(k=start;k<=stop;++k) {
- if(SA[i].points[k]==-1) {
- d=abs(SA[i].last_cell_dir);
- r=NR(d);
- c=NC(d);
- } else {
- r = (int)SA[i].points[k] / ncols;
- c = (int)SA[i].points[k] % ncols;
- }
- easting = window.west + (c + .5) * window.ew_res;
- northing = window.north - (r + .5) * window.ns_res;
- Vect_append_point(Segments, easting,northing, 0);
- }
- Vect_write_line(&Out, GV_LINE, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
- }
+ struct field_info *Fi;
+ dbString table_name, db_sql, val_string;
+ dbDriver *driver;
+ dbHandle handle;
+ char *cat_col_name = "cat";
+ char buf[1000];
+
+ int sector_category, segment, sector, order;
+ double direction, azimuth, length, stright, sinusoid;
+ double elev_min, elev_max, drop, gradient;
+
+ Segments = Vect_new_line_struct();
+ Cats = Vect_new_cats_struct();
+ Vect_open_new(&Out, out_vector, 0);
+
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ for (i = 1; i < number_of_streams; ++i) {
+ stop = 1;
+ for (j = 0; j < SA[i].number_of_sectors; ++j) {
+ start = stop;
+ stop = (j == SA[i].number_of_sectors - 1) ?
+ SA[i].sector_breakpoints[j] +
+ 1 : SA[i].sector_breakpoints[j] + 1;
+ Vect_cat_set(Cats, 1, SA[i].sector_cats[j]);
+ for (k = start; k <= stop; ++k) {
+ if (SA[i].points[k] == -1) {
+ d = abs(SA[i].last_cell_dir);
+ r = NR(d);
+ c = NC(d);
}
+ else {
+ r = (int)SA[i].points[k] / ncols;
+ c = (int)SA[i].points[k] % ncols;
+ }
+ easting = window.west + (c + .5) * window.ew_res;
+ northing = window.north - (r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
+ }
+ Vect_write_line(&Out, GV_LINE, Segments, Cats);
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+ }
+ }
- /* add attributes */
- db_init_string(&db_sql);
- db_init_string(&val_string);
- db_init_string(&table_name);
- db_init_handle(&handle);
+ /* add attributes */
+ db_init_string(&db_sql);
+ db_init_string(&val_string);
+ db_init_string(&table_name);
+ db_init_handle(&handle);
- Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
- driver = db_start_driver_open_database(Fi->driver, Fi->database);
+ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
+ driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (driver == NULL) {
G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
}
-/* create table */
- sprintf(buf, "create table %s (%s integer, \
+ /* create table */
+ sprintf(buf, "create table %s (%s integer, \
segment integer, \
sector integer, \
s_order integer, \
@@ -79,12 +82,11 @@
elev_min double precision, \
elev_max double precision, \
s_drop double precision, \
- gradient double precision)",
- Fi->table, cat_col_name);
+ gradient double precision)", Fi->table, cat_col_name);
- //printf("%s \n",buf);
+ //printf("%s \n",buf);
- db_set_string(&db_sql, buf);
+ db_set_string(&db_sql, buf);
if (db_execute_immediate(driver, &db_sql) != DB_OK) {
db_close_database(driver);
@@ -99,143 +101,133 @@
DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK)
G_fatal_error("cannot grant privileges on table %s", Fi->table);
- db_begin_transaction(driver);
+ db_begin_transaction(driver);
- for(i=1;i<number_of_streams;++i) {
- stop=1;
-for(j=0;j<SA[i].number_of_sectors;++j) {
- start=stop;
- stop=SA[i].sector_breakpoints[j];
+ for (i = 1; i < number_of_streams; ++i) {
+ stop = 1;
+ for (j = 0; j < SA[i].number_of_sectors; ++j) {
+ start = stop;
+ stop = SA[i].sector_breakpoints[j];
- /* calculate and add parameters */
- sector_category=SA[i].sector_cats[j];
- segment=SA[i].stream;
- sector=j+1;
- order=SA[i].order;
- direction=SA[i].sector_directions[j];
- azimuth=direction<=PI ? direction : direction-PI;
- length=SA[i].sector_lengths[j];
- stright=SA[i].sector_strights[j];
- sinusoid=length/stright;
- elev_max=SA[i].elevation[start];
- elev_min=SA[i].elevation[stop];
- drop=elev_max-elev_min;
- gradient=drop/length;
-
- if(!radians) {
- direction=RAD2DEG(direction);
- azimuth=RAD2DEG(azimuth);
- }
-
- sprintf(buf,"insert into %s values( %d, %d, %d, %d, \
+ /* calculate and add parameters */
+ sector_category = SA[i].sector_cats[j];
+ segment = SA[i].stream;
+ sector = j + 1;
+ order = SA[i].order;
+ direction = SA[i].sector_directions[j];
+ azimuth = direction <= PI ? direction : direction - PI;
+ length = SA[i].sector_lengths[j];
+ stright = SA[i].sector_strights[j];
+ sinusoid = length / stright;
+ elev_max = SA[i].elevation[start];
+ elev_min = SA[i].elevation[stop];
+ drop = elev_max - elev_min;
+ gradient = drop / length;
+
+ if (!radians) {
+ direction = RAD2DEG(direction);
+ azimuth = RAD2DEG(azimuth);
+ }
+
+ sprintf(buf, "insert into %s values( %d, %d, %d, %d, \
%f, %f, %f, %f, %f, \
- %f, %f, %f, %f)",
- Fi->table,
- sector_category,
- segment,
- sector,
- order, /*4*/
- direction,
- azimuth,
- length,
- stright,
- sinusoid, /*9*/
- elev_max,
- elev_min,
- drop,
- gradient); /*13*/
-
- //printf("%s \n",buf); }}
+ %f, %f, %f, %f)", Fi->table, sector_category, segment, sector, order, /*4 */
+ direction, azimuth, length, stright, sinusoid, /*9 */
+ elev_max, elev_min, drop, gradient); /*13 */
+ //printf("%s \n",buf); }}
- 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_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);
- Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
- cat_col_name, Fi->database, Fi->driver);
+ }
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
+ cat_col_name, Fi->database, Fi->driver);
-Vect_hist_command(&Out);
-Vect_build(&Out);
-Vect_close(&Out);
-return 0;
+ Vect_hist_command(&Out);
+ Vect_build(&Out);
+ Vect_close(&Out);
+ return 0;
}
-int create_segment_vector(char* out_vector, int number_of_streams, int radians)
+int create_segment_vector(char *out_vector, int number_of_streams,
+ int radians)
{
- int i,j,k;
- int r,c,d;
- int start, stop;
- float northing, easting;
- struct Map_info Out;
- struct line_pnts *Segments;
- struct line_cats *Cats;
- STREAM* SA=stream_attributes; /* for better code readability */
-
- struct field_info *Fi;
- dbString table_name, db_sql, val_string;
- dbDriver *driver;
- dbHandle handle;
- char *cat_col_name = "cat";
- char buf[1000];
-
- /* variables to store table attributes*/
- int last;
- int segment, next_segment, order, next_order;
- double direction, azimuth, length, stright, sinusoid;
- double elev_min, elev_max, drop, gradient;
- double out_direction, out_azimuth, out_length, out_drop, out_gradient;
- double tangent_dir, tangent_azimuth, next_direction, next_azimuth;
-
- Segments = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
- Vect_open_new(&Out, out_vector, 0);
-
+ int i, k;
+ int r, c, d;
+ float northing, easting;
+ struct Map_info Out;
+ struct line_pnts *Segments;
+ struct line_cats *Cats;
+ STREAM *SA = stream_attributes; /* for better code readability */
+
+ struct field_info *Fi;
+ dbString table_name, db_sql, val_string;
+ dbDriver *driver;
+ dbHandle handle;
+ char *cat_col_name = "cat";
+ char buf[1000];
+
+ /* variables to store table attributes */
+ int last;
+ int segment, next_segment, order, next_order;
+ double direction, azimuth, length, stright, sinusoid;
+ double elev_min, elev_max, drop, gradient;
+ double out_direction, out_azimuth, out_length, out_drop, out_gradient;
+ double tangent_dir, tangent_azimuth, next_direction, next_azimuth;
+
+ Segments = Vect_new_line_struct();
+ Cats = Vect_new_cats_struct();
+ Vect_open_new(&Out, out_vector, 0);
+
+ Vect_reset_line(Segments);
+ Vect_reset_cats(Cats);
+
+ for (i = 1; i < number_of_streams; ++i) {
+ Vect_cat_set(Cats, 1, SA[i].stream);
+ for (k = 1; k < SA[i].number_of_cells; ++k) {
+ if (SA[i].points[k] == -1) {
+ d = abs(SA[i].last_cell_dir);
+ r = NR(d);
+ c = NC(d);
+ }
+ else {
+ r = (int)SA[i].points[k] / ncols;
+ c = (int)SA[i].points[k] % ncols;
+ }
+ easting = window.west + (c + .5) * window.ew_res;
+ northing = window.north - (r + .5) * window.ns_res;
+ Vect_append_point(Segments, easting, northing, 0);
+ }
+ Vect_write_line(&Out, GV_LINE, Segments, Cats);
Vect_reset_line(Segments);
Vect_reset_cats(Cats);
-
- for (i=1;i<number_of_streams;++i) {
- Vect_cat_set(Cats, 1, SA[i].stream);
- for(k=1;k<SA[i].number_of_cells;++k) {
- if(SA[i].points[k]==-1) {
- d=abs(SA[i].last_cell_dir);
- r=NR(d);
- c=NC(d);
- } else {
- r = (int)SA[i].points[k] / ncols;
- c = (int)SA[i].points[k] % ncols;
- }
- easting = window.west + (c + .5) * window.ew_res;
- northing = window.north - (r + .5) * window.ns_res;
- Vect_append_point(Segments, easting,northing, 0);
- }
- Vect_write_line(&Out, GV_LINE, Segments, Cats);
- Vect_reset_line(Segments);
- Vect_reset_cats(Cats);
- }
+ }
- /* add attributes */
- db_init_string(&db_sql);
- db_init_string(&val_string);
- db_init_string(&table_name);
- db_init_handle(&handle);
+ /* add attributes */
+ db_init_string(&db_sql);
+ db_init_string(&val_string);
+ db_init_string(&table_name);
+ db_init_handle(&handle);
- Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
- driver = db_start_driver_open_database(Fi->driver, Fi->database);
+ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
+ driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (driver == NULL) {
G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
}
-
- /* create table */
- sprintf(buf, "create table %s (%s integer, \
+
+ /* create table */
+ sprintf(buf, "create table %s (%s integer, \
segment integer, \
next_segment integer, \
s_order integer, \
@@ -257,13 +249,12 @@
tangent_dir double precision, \
tangent_azimuth double precision, \
next_direction double precision, \
- next_azimuth double precision)",
- Fi->table, cat_col_name);
+ next_azimuth double precision)", Fi->table, cat_col_name);
- //printf("%s \n",buf);
+ //printf("%s \n",buf);
- db_set_string(&db_sql, buf);
-
+ db_set_string(&db_sql, buf);
+
if (db_execute_immediate(driver, &db_sql) != DB_OK) {
db_close_database(driver);
db_shutdown_driver(driver);
@@ -277,94 +268,77 @@
DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK)
G_fatal_error("cannot grant privileges on table %s", Fi->table);
- db_begin_transaction(driver);
-
-for(i=1;i<number_of_streams;++i) {
- /* calculate and add parameters */
- segment=SA[i].stream;
- next_segment=SA[i].next_stream;
- order=SA[i].order;
- next_order=next_segment==-1 ? -1: SA[next_segment].order;
- direction=SA[i].direction;
- azimuth=direction<=PI ? direction : direction-PI;
- length=SA[i].length;
- stright=SA[i].stright;
- sinusoid=length/stright;
- elev_max=SA[i].elevation[1];
- elev_min=SA[i].elevation[SA[i].number_of_cells-1];
- drop=SA[i].drop;
- gradient=drop/length;
- last=SA[i].number_of_sectors-1;
- out_direction=SA[i].sector_directions[last];
- out_azimuth=out_direction<=PI ? out_direction : out_direction-PI;
- out_length=SA[i].sector_lengths[last];
- out_drop=SA[i].sector_drops[last];
- out_gradient=out_drop/out_length;
- tangent_dir=SA[i].tangent;
- tangent_azimuth=tangent_dir<=PI ? tangent_dir: tangent_dir-PI;
- next_direction=SA[i].continuation;
- next_azimuth=next_direction<=PI ? next_direction: next_direction-PI;
-
- if(!radians) {
- direction=RAD2DEG(direction);
- azimuth=RAD2DEG(azimuth);
- out_direction=RAD2DEG(out_direction);
- out_azimuth=RAD2DEG(out_azimuth);
- tangent_dir=RAD2DEG(tangent_dir);
- tangent_azimuth=RAD2DEG(tangent_azimuth);
- next_direction=RAD2DEG(next_direction);
- next_azimuth=RAD2DEG(next_azimuth);
- }
+ db_begin_transaction(driver);
- sprintf(buf,"insert into %s values( %d, %d, %d, %d, %d, \
+ for (i = 1; i < number_of_streams; ++i) {
+ /* calculate and add parameters */
+ segment = SA[i].stream;
+ next_segment = SA[i].next_stream;
+ order = SA[i].order;
+ next_order = next_segment == -1 ? -1 : SA[next_segment].order;
+ direction = SA[i].direction;
+ azimuth = direction <= PI ? direction : direction - PI;
+ length = SA[i].length;
+ stright = SA[i].stright;
+ sinusoid = length / stright;
+ elev_max = SA[i].elevation[1];
+ elev_min = SA[i].elevation[SA[i].number_of_cells - 1];
+ drop = SA[i].drop;
+ gradient = drop / length;
+ last = SA[i].number_of_sectors - 1;
+ out_direction = SA[i].sector_directions[last];
+ out_azimuth =
+ out_direction <= PI ? out_direction : out_direction - PI;
+ out_length = SA[i].sector_lengths[last];
+ out_drop = SA[i].sector_drops[last];
+ out_gradient = out_drop / out_length;
+ tangent_dir = SA[i].tangent;
+ tangent_azimuth = tangent_dir <= PI ? tangent_dir : tangent_dir - PI;
+ next_direction = SA[i].continuation;
+ next_azimuth =
+ next_direction <= PI ? next_direction : next_direction - PI;
+
+ if (!radians) {
+ direction = RAD2DEG(direction);
+ azimuth = RAD2DEG(azimuth);
+ out_direction = RAD2DEG(out_direction);
+ out_azimuth = RAD2DEG(out_azimuth);
+ tangent_dir = RAD2DEG(tangent_dir);
+ tangent_azimuth = RAD2DEG(tangent_azimuth);
+ next_direction = RAD2DEG(next_direction);
+ next_azimuth = RAD2DEG(next_azimuth);
+ }
+
+ sprintf(buf, "insert into %s values( %d, %d, %d, %d, %d, \
%f, %f, %f, %f, %f, \
%f, %f, %f, %f, \
%f, %f, %f, %f, %f, \
- %f, %f, %f, %f)",
- Fi->table,
- i,
- segment,
- next_segment,
- order,
- next_order, /*5*/
- direction,
- azimuth,
- length,
- stright,
- sinusoid, /*10*/
- elev_max,
- elev_min,
- drop,
- gradient, /*14*/
- out_direction,
- out_azimuth,
- out_length,
- out_drop,
- out_gradient, /*19*/
- tangent_dir,
- tangent_azimuth,
- next_direction,
- next_azimuth); /*23*/
-
- //printf("%s \n",buf);
+ %f, %f, %f, %f)", Fi->table, i, segment, next_segment, order, next_order, /*5 */
+ direction, azimuth, length, stright, sinusoid, /*10 */
+ elev_max, elev_min, drop, gradient, /*14 */
+ out_direction, out_azimuth, out_length, out_drop, out_gradient, /*19 */
+ tangent_dir, tangent_azimuth, next_direction, next_azimuth); /*23 */
+ /* printf("%s \n",buf); */
- 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_set_string(&db_sql, buf);
-}
- db_commit_transaction(driver);
- db_close_database_shutdown_driver(driver);
- Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
- cat_col_name, Fi->database, Fi->driver);
+ 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));
+ }
- Vect_hist_command(&Out);
- Vect_build(&Out);
- Vect_close(&Out);
- return 0;
+ }
+ db_commit_transaction(driver);
+ db_close_database_shutdown_driver(driver);
+ Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
+ cat_col_name, Fi->database, Fi->driver);
+
+ Vect_hist_command(&Out);
+ Vect_build(&Out);
+ Vect_close(&Out);
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.slope/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.slope/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.slope/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -34,8 +34,8 @@
int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int nrows, ncols;
-CELL** dir_rows;
-DCELL** elev_rows;
+CELL **dir_rows;
+DCELL **elev_rows;
struct Cell_head window;
DCELL calculate_difference(int r, int c);
@@ -45,344 +45,359 @@
int main(int argc, char *argv[])
{
- struct GModule *module;
- struct Option *in_dir_opt, /* options */
- *in_elev_opt,
- *out_differnce_opt,
- *out_gradient_opt,
- *out_max_curv_opt,
- *out_min_curv_opt;
- struct Cell_head cellhd;
- struct History history;
-
- int r,c, d, i, cur_row;
- int elev_map_type, elev_data_size;
- int gradient;
+ struct GModule *module;
+ struct Option *in_dir_opt, /* options */
+ *in_elev_opt,
+ *out_differnce_opt,
+ *out_gradient_opt, *out_max_curv_opt, *out_min_curv_opt;
+ struct Cell_head cellhd;
+ struct History history;
- int in_dir_fd, in_elev_fd;
- int out_difference_fd, out_gradient_fd, out_max_curv_fd, out_min_curv_fd;
- double cellsize;
- char* mapset;
- void* tmp_buffer;
- DCELL* tmp_elev_buf;
- CELL* tmp_dir_buf;
- DCELL *out_difference_buf, *out_gradient_buf, *out_max_curv_buf, *out_min_curv_buf;
-
- G_gisinit(argv[0]);
- module = G_define_module();
- module->description =
- _("Calculate local parameters for slope subsystem");
-
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_dir_opt->key = "dir";
- in_dir_opt->description = "Name of flow direction input map";
+ int r, c, d, i, cur_row;
+ int elev_map_type, elev_data_size;
+ int gradient;
- in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_elev_opt->key = "elevation";
- in_elev_opt->description = "Name of elevation map";
-
- out_differnce_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_differnce_opt->key = "difference";
- out_differnce_opt->required = NO;
- out_differnce_opt->description = "Output local downstream elevation difference";
-
- out_gradient_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_gradient_opt->key = "gradient";
- out_gradient_opt->required = NO;
- out_gradient_opt->description = "Output local downstream gradient";
-
- out_max_curv_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_max_curv_opt->key = "maxcurv";
- out_max_curv_opt->required = NO;
- out_max_curv_opt->description = "Output local downstream maximium cuvature";
-
- out_min_curv_opt = G_define_standard_option(G_OPT_R_OUTPUT);
- out_min_curv_opt->key = "mincurv";
- out_min_curv_opt->required = NO;
- out_min_curv_opt->description = "Output local downstream minimum cuvature";
-
- if (G_parser(argc, argv)) /* parser */
+ int in_dir_fd, in_elev_fd;
+ int out_difference_fd, out_gradient_fd, out_max_curv_fd, out_min_curv_fd;
+ double cellsize;
+ char *mapset;
+ void *tmp_buffer;
+ DCELL *tmp_elev_buf;
+ CELL *tmp_dir_buf;
+ DCELL *out_difference_buf, *out_gradient_buf, *out_max_curv_buf,
+ *out_min_curv_buf;
+
+ G_gisinit(argv[0]);
+ module = G_define_module();
+ module->description = _("Calculate local parameters for slope subsystem");
+
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_dir_opt->key = "dir";
+ in_dir_opt->description = "Name of flow direction input map";
+
+ in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_elev_opt->key = "elevation";
+ in_elev_opt->description = "Name of elevation map";
+
+ out_differnce_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_differnce_opt->key = "difference";
+ out_differnce_opt->required = NO;
+ out_differnce_opt->description =
+ "Output local downstream elevation difference";
+
+ out_gradient_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_gradient_opt->key = "gradient";
+ out_gradient_opt->required = NO;
+ out_gradient_opt->description = "Output local downstream gradient";
+
+ out_max_curv_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_max_curv_opt->key = "maxcurv";
+ out_max_curv_opt->required = NO;
+ out_max_curv_opt->description =
+ "Output local downstream maximium cuvature";
+
+ out_min_curv_opt = G_define_standard_option(G_OPT_R_OUTPUT);
+ out_min_curv_opt->key = "mincurv";
+ out_min_curv_opt->required = NO;
+ out_min_curv_opt->description =
+ "Output local downstream minimum cuvature";
+
+ if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
-
- /* open map */
- mapset = (char*)G_find_raster2(in_dir_opt->answer, "");
- if (mapset == NULL)
+
+ /* open map */
+ mapset = (char *)G_find_raster2(in_dir_opt->answer, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), in_dir_opt->answer);
-
- G_get_window(&window);
- Rast_get_cellhd(in_dir_opt->answer, mapset, &cellhd);
- if (window.ew_res != cellhd.ew_res ||
- window.ns_res != cellhd.ns_res)
+
+ G_get_window(&window);
+ Rast_get_cellhd(in_dir_opt->answer, mapset, &cellhd);
+ if (window.ew_res != cellhd.ew_res || window.ns_res != cellhd.ns_res)
G_fatal_error(_("Region resolution and map %s resolution differs. \
- Run g.region rast=%s to set proper region resolution"),
- in_dir_opt->answer, in_dir_opt->answer);
-
- if (Rast_map_type(in_dir_opt->answer,mapset) != CELL_TYPE)
- G_fatal_error(_("<%s> is not of type CELL"),in_dir_opt->answer);
-
- in_dir_fd = Rast_open_old(in_dir_opt->answer, mapset);
-
- mapset = (char*)G_find_raster2(in_elev_opt->answer, "");
- if (mapset == NULL)
+ Run g.region rast=%s to set proper region resolution"), in_dir_opt->answer, in_dir_opt->answer);
+
+ if (Rast_map_type(in_dir_opt->answer, mapset) != CELL_TYPE)
+ G_fatal_error(_("<%s> is not of type CELL"), in_dir_opt->answer);
+
+ in_dir_fd = Rast_open_old(in_dir_opt->answer, mapset);
+
+ mapset = (char *)G_find_raster2(in_elev_opt->answer, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), in_elev_opt->answer);
- elev_map_type=Rast_map_type(in_elev_opt->answer,mapset);
- elev_data_size = Rast_cell_size(elev_map_type);
- in_elev_fd = Rast_open_old(in_elev_opt->answer, mapset);
-
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
+ elev_map_type = Rast_map_type(in_elev_opt->answer, mapset);
+ elev_data_size = Rast_cell_size(elev_map_type);
+ in_elev_fd = Rast_open_old(in_elev_opt->answer, mapset);
- G_begin_distance_calculations();
-
- if(out_differnce_opt->answer) {
- out_difference_fd=Rast_open_new(out_differnce_opt->answer,DCELL_TYPE);
- out_difference_buf=Rast_allocate_d_buf();
- }
-
- if(out_gradient_opt->answer) {
- out_gradient_fd=Rast_open_new(out_gradient_opt->answer,DCELL_TYPE);
- out_gradient_buf=Rast_allocate_d_buf();
- }
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
- if(out_max_curv_opt->answer) {
- out_max_curv_fd=Rast_open_new(out_max_curv_opt->answer,DCELL_TYPE);
- out_max_curv_buf=Rast_allocate_d_buf();
- }
+ G_begin_distance_calculations();
- if(out_min_curv_opt->answer) {
- out_min_curv_fd=Rast_open_new(out_min_curv_opt->answer,DCELL_TYPE);
- out_min_curv_buf=Rast_allocate_d_buf();
- }
+ if (out_differnce_opt->answer) {
+ out_difference_fd =
+ Rast_open_new(out_differnce_opt->answer, DCELL_TYPE);
+ out_difference_buf = Rast_allocate_d_buf();
+ }
- dir_rows=(CELL**)G_malloc(3*sizeof(CELL*));
- elev_rows=(DCELL**)G_malloc(3*sizeof(DCELL*));
-
- /* init shift buffer */
- tmp_buffer=Rast_allocate_buf(elev_map_type);
-
- for(i=0;i<3;++i) {
- dir_rows[i]=Rast_allocate_c_buf();
- elev_rows[i]=Rast_allocate_d_buf();
- Rast_get_row(in_dir_fd,dir_rows[i],i,CELL_TYPE);
- Rast_get_row(in_elev_fd,tmp_buffer,i,elev_map_type);
- for(c=0;c<ncols;++c)
- elev_rows[i][c]=Rast_get_d_value(tmp_buffer+c*elev_data_size,elev_map_type);
- }
+ if (out_gradient_opt->answer) {
+ out_gradient_fd = Rast_open_new(out_gradient_opt->answer, DCELL_TYPE);
+ out_gradient_buf = Rast_allocate_d_buf();
+ }
-for (r=0;r<nrows;++r) { /*main loop */
-
+ if (out_max_curv_opt->answer) {
+ out_max_curv_fd = Rast_open_new(out_max_curv_opt->answer, DCELL_TYPE);
+ out_max_curv_buf = Rast_allocate_d_buf();
+ }
+
+ if (out_min_curv_opt->answer) {
+ out_min_curv_fd = Rast_open_new(out_min_curv_opt->answer, DCELL_TYPE);
+ out_min_curv_buf = Rast_allocate_d_buf();
+ }
+
+ dir_rows = (CELL **) G_malloc(3 * sizeof(CELL *));
+ elev_rows = (DCELL **) G_malloc(3 * sizeof(DCELL *));
+
+ /* init shift buffer */
+ tmp_buffer = Rast_allocate_buf(elev_map_type);
+
+ for (i = 0; i < 3; ++i) {
+ dir_rows[i] = Rast_allocate_c_buf();
+ elev_rows[i] = Rast_allocate_d_buf();
+ Rast_get_row(in_dir_fd, dir_rows[i], i, CELL_TYPE);
+ Rast_get_row(in_elev_fd, tmp_buffer, i, elev_map_type);
+ for (c = 0; c < ncols; ++c)
+ elev_rows[i][c] =
+ Rast_get_d_value(tmp_buffer + c * elev_data_size,
+ elev_map_type);
+ }
+
+ for (r = 0; r < nrows; ++r) { /*main loop */
+
G_percent(r, nrows, 2);
- if (r==0)
- cur_row=0;
- else if (r==(nrows-1))
- cur_row=2;
- else
- cur_row=1;
-
- for(c=0;c<ncols;++c) {
- if(out_differnce_opt->answer)
- out_difference_buf[c]=calculate_difference(cur_row,c);
- if(out_gradient_opt->answer)
- out_gradient_buf[c]=calculate_gradient(cur_row,c);
- if(out_max_curv_opt->answer)
- out_max_curv_buf[c]=calculate_max_curvature(cur_row,c);
- if(out_min_curv_opt->answer)
- out_min_curv_buf[c]=calculate_min_curvature(cur_row,c);
- }
+ if (r == 0)
+ cur_row = 0;
+ else if (r == (nrows - 1))
+ cur_row = 2;
+ else
+ cur_row = 1;
- if(out_differnce_opt->answer)
- Rast_put_row(out_difference_fd, out_difference_buf, DCELL_TYPE);
- if(out_gradient_opt->answer)
- Rast_put_row(out_gradient_fd, out_gradient_buf, DCELL_TYPE);
- if(out_max_curv_opt->answer)
- Rast_put_row(out_max_curv_fd, out_max_curv_buf, DCELL_TYPE);
- if(out_min_curv_opt->answer)
- Rast_put_row(out_min_curv_fd, out_min_curv_buf, DCELL_TYPE);
+ for (c = 0; c < ncols; ++c) {
+ if (out_differnce_opt->answer)
+ out_difference_buf[c] = calculate_difference(cur_row, c);
+ if (out_gradient_opt->answer)
+ out_gradient_buf[c] = calculate_gradient(cur_row, c);
+ if (out_max_curv_opt->answer)
+ out_max_curv_buf[c] = calculate_max_curvature(cur_row, c);
+ if (out_min_curv_opt->answer)
+ out_min_curv_buf[c] = calculate_min_curvature(cur_row, c);
+ }
+ if (out_differnce_opt->answer)
+ Rast_put_row(out_difference_fd, out_difference_buf, DCELL_TYPE);
+ if (out_gradient_opt->answer)
+ Rast_put_row(out_gradient_fd, out_gradient_buf, DCELL_TYPE);
+ if (out_max_curv_opt->answer)
+ Rast_put_row(out_max_curv_fd, out_max_curv_buf, DCELL_TYPE);
+ if (out_min_curv_opt->answer)
+ Rast_put_row(out_min_curv_fd, out_min_curv_buf, DCELL_TYPE);
+
/* shift buffer */
-
- if(r!=0 && r<nrows-2) {
- tmp_elev_buf=elev_rows[0];
- tmp_dir_buf=dir_rows[0];
+ if (r != 0 && r < nrows - 2) {
- for (i = 1; i < 3; ++i) {
+ tmp_elev_buf = elev_rows[0];
+ tmp_dir_buf = dir_rows[0];
+
+ for (i = 1; i < 3; ++i) {
dir_rows[i - 1] = dir_rows[i];
elev_rows[i - 1] = elev_rows[i];
- }
+ }
- dir_rows[2]=tmp_dir_buf;
- elev_rows[2]=tmp_elev_buf;
- Rast_get_row(in_dir_fd,dir_rows[2],r+2,CELL_TYPE);
- Rast_get_row(in_elev_fd,tmp_buffer,r+2,elev_map_type);
+ dir_rows[2] = tmp_dir_buf;
+ elev_rows[2] = tmp_elev_buf;
+ Rast_get_row(in_dir_fd, dir_rows[2], r + 2, CELL_TYPE);
+ Rast_get_row(in_elev_fd, tmp_buffer, r + 2, elev_map_type);
- for(c=0;c<ncols;++c)
- elev_rows[2][c]=Rast_get_d_value(tmp_buffer+c*elev_data_size,elev_map_type);
+ for (c = 0; c < ncols; ++c)
+ elev_rows[2][c] =
+ Rast_get_d_value(tmp_buffer + c * elev_data_size,
+ elev_map_type);
}
-}
-G_percent(r, nrows, 2);
+ }
+ G_percent(r, nrows, 2);
- if(out_differnce_opt->answer) {
- G_free(out_difference_buf);
- Rast_close(out_difference_fd);
- Rast_short_history(out_differnce_opt->answer, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(out_differnce_opt->answer, &history);
- }
-
- if(out_gradient_opt->answer) {
- G_free(out_gradient_buf);
- Rast_close(out_gradient_fd);
- Rast_short_history(out_gradient_opt->answer, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(out_gradient_opt->answer, &history);
- }
+ if (out_differnce_opt->answer) {
+ G_free(out_difference_buf);
+ Rast_close(out_difference_fd);
+ Rast_short_history(out_differnce_opt->answer, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(out_differnce_opt->answer, &history);
+ }
- if(out_max_curv_opt->answer) {
- G_free(out_max_curv_buf);
- Rast_close(out_max_curv_fd);
- Rast_short_history(out_max_curv_opt->answer, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(out_max_curv_opt->answer, &history);
- }
+ if (out_gradient_opt->answer) {
+ G_free(out_gradient_buf);
+ Rast_close(out_gradient_fd);
+ Rast_short_history(out_gradient_opt->answer, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(out_gradient_opt->answer, &history);
+ }
- if(out_min_curv_opt->answer) {
- G_free(out_min_curv_buf);
- Rast_close(out_min_curv_fd);
- Rast_short_history(out_min_curv_opt->answer, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(out_min_curv_opt->answer, &history);
- }
+ if (out_max_curv_opt->answer) {
+ G_free(out_max_curv_buf);
+ Rast_close(out_max_curv_fd);
+ Rast_short_history(out_max_curv_opt->answer, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(out_max_curv_opt->answer, &history);
+ }
-exit(EXIT_SUCCESS);
+ if (out_min_curv_opt->answer) {
+ G_free(out_min_curv_buf);
+ Rast_close(out_min_curv_fd);
+ Rast_short_history(out_min_curv_opt->answer, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(out_min_curv_opt->answer, &history);
+ }
+
+ exit(EXIT_SUCCESS);
}
DCELL calculate_difference(int r, int c)
{
- int d;
- d=dir_rows[r][c];
+ int d;
- if(NOT_IN_REGION(d))
+ d = dir_rows[r][c];
+
+ if (NOT_IN_REGION(d))
return 0.;
- return elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
+ return elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
}
DCELL calculate_gradient(int r, int c)
{
-
- int d;
- double easting, northing, next_easting, next_northing;
- double distance;
- d=dir_rows[r][c];
-
- if(NOT_IN_REGION(d))
+
+ int d;
+ double easting, northing, next_easting, next_northing;
+ double distance;
+
+ d = dir_rows[r][c];
+
+ if (NOT_IN_REGION(d))
return 0.;
-
- northing = window.north - (r + .5) * window.ns_res;
- easting = window.west + (c + .5) * window.ew_res;
- next_northing = window.north - (NR(d) +.5) * window.ns_res;
- next_easting = window.west + (NC(d) +.5) * window.ew_res;
- distance=G_distance(easting,northing,next_easting,next_northing);
-
- return (elev_rows[r][c] - elev_rows[NR(d)][NC(d)])/distance;
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (NR(d) + .5) * window.ns_res;
+ next_easting = window.west + (NC(d) + .5) * window.ew_res;
+ distance = G_distance(easting, northing, next_easting, next_northing);
+ return (elev_rows[r][c] - elev_rows[NR(d)][NC(d)]) / distance;
+
+
}
+
DCELL calculate_max_curvature(int r, int c)
{
-
- int i, j=0, d;
- double easting, northing, next_easting, next_northing;
- double elev_max=-1000;
- double diff_up,diff_down, diff_elev, first_derivative, second_derivative;
- double distance_up, distance_down, distance;
- d=dir_rows[r][c];
-
- if(NOT_IN_REGION(d))
+
+ int i, j = 0, d;
+ double easting, northing, next_easting, next_northing;
+ double elev_max = -1000;
+ double diff_up, diff_down, diff_elev, first_derivative, second_derivative;
+ double distance_up, distance_down, distance;
+
+ d = dir_rows[r][c];
+
+ if (NOT_IN_REGION(d))
return 0.;
-
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- if(dir_rows[NR(i)][NC(i)]==DIAG(i) && elev_rows[NR(i)][NC(i)]>elev_max) {
- elev_max=elev_rows[NR(i)][NC(i)];
- j=i;
- }
- }
- if(elev_max==-1000)
- elev_max=elev_rows[r][c];
- diff_up=elev_max-elev_rows[r][c];
- diff_down=elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
- diff_elev = elev_max-elev_rows[NR(d)][NC(d)];
- if(diff_elev<0)
- diff_elev=0;
-
- northing = window.north - (r + .5) * window.ns_res;
- easting = window.west + (c + .5) * window.ew_res;
- next_northing = window.north - (NR(j) +.5) * window.ns_res;
- next_easting = window.west + (NC(j) +.5) * window.ew_res;
- distance_up=G_distance(easting,northing,next_easting,next_northing);
-
- northing = window.north - (r + .5) * window.ns_res;
- easting = window.west + (c + .5) * window.ew_res;
- next_northing = window.north - (NR(d) +.5) * window.ns_res;
- next_easting = window.west + (NC(d) +.5) * window.ew_res;
- distance_down=G_distance(easting,northing,next_easting,next_northing);
- distance = distance_up+distance_down;
- first_derivative=diff_elev/distance;
- second_derivative=(diff_up-diff_down)/distance;
-
- return second_derivative/pow((1+first_derivative*first_derivative),1.5);
-
+
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ if (dir_rows[NR(i)][NC(i)] == DIAG(i) &&
+ elev_rows[NR(i)][NC(i)] > elev_max) {
+ elev_max = elev_rows[NR(i)][NC(i)];
+ j = i;
+ }
+ }
+ if (elev_max == -1000)
+ elev_max = elev_rows[r][c];
+ diff_up = elev_max - elev_rows[r][c];
+ diff_down = elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
+ diff_elev = elev_max - elev_rows[NR(d)][NC(d)];
+ if (diff_elev < 0)
+ diff_elev = 0;
+
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (NR(j) + .5) * window.ns_res;
+ next_easting = window.west + (NC(j) + .5) * window.ew_res;
+ distance_up = G_distance(easting, northing, next_easting, next_northing);
+
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (NR(d) + .5) * window.ns_res;
+ next_easting = window.west + (NC(d) + .5) * window.ew_res;
+ distance_down =
+ G_distance(easting, northing, next_easting, next_northing);
+ distance = distance_up + distance_down;
+ first_derivative = diff_elev / distance;
+ second_derivative = (diff_up - diff_down) / distance;
+
+ return second_derivative / pow((1 + first_derivative * first_derivative),
+ 1.5);
+
}
DCELL calculate_min_curvature(int r, int c)
{
- int i, j=0, d;
- int next_r, next_c;
- double easting, northing, next_easting, next_northing;
- double elev_min=9999;
- double diff_up,diff_down,diff_elev,first_derivative, second_derivative;
- double distance_up, distance_down,distance;
- d=dir_rows[r][c];
-
- if(NOT_IN_REGION(d))
+ int i, j = 0, d;
+ int next_r, next_c;
+ double easting, northing, next_easting, next_northing;
+ double elev_min = 9999;
+ double diff_up, diff_down, diff_elev, first_derivative, second_derivative;
+ double distance_up, distance_down, distance;
+
+ d = dir_rows[r][c];
+
+ if (NOT_IN_REGION(d))
return 0.;
-
- for(i=1; i<9; ++i) {
- if(NOT_IN_REGION(i))
- continue;
- if(dir_rows[NR(i)][NC(i)]==DIAG(i) && elev_rows[NR(i)][NC(i)]<elev_min) {
- elev_min=elev_rows[NR(i)][NC(i)];
- j=i;
- }
- }
-
- if(elev_min==9999)
- elev_min=elev_rows[r][c];
- diff_up=elev_min-elev_rows[r][c];
- diff_down=elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
- diff_elev = elev_min-elev_rows[NR(d)][NC(d)];
-
- northing = window.north - (r + .5) * window.ns_res;
- easting = window.west + (c + .5) * window.ew_res;
- next_northing = window.north - (NR(j) +.5) * window.ns_res;
- next_easting = window.west + (NC(j) +.5) * window.ew_res;
- distance_up=G_distance(easting,northing,next_easting,next_northing);
-
- northing = window.north - (r + .5) * window.ns_res;
- easting = window.west + (c + .5) * window.ew_res;
- next_northing = window.north - (NR(d) +.5) * window.ns_res;
- next_easting = window.west + (NC(d) +.5) * window.ew_res;
- distance_down=G_distance(easting,northing,next_easting,next_northing);
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue;
+ if (dir_rows[NR(i)][NC(i)] == DIAG(i) &&
+ elev_rows[NR(i)][NC(i)] < elev_min) {
+ elev_min = elev_rows[NR(i)][NC(i)];
+ j = i;
+ }
+ }
- distance = distance_up+distance_down;
- first_derivative=diff_elev/distance;
- second_derivative=(diff_up-diff_down)/distance;
- return second_derivative/pow((1+first_derivative*first_derivative),1.5);
+ if (elev_min == 9999)
+ elev_min = elev_rows[r][c];
+ diff_up = elev_min - elev_rows[r][c];
+ diff_down = elev_rows[r][c] - elev_rows[NR(d)][NC(d)];
+ diff_elev = elev_min - elev_rows[NR(d)][NC(d)];
+
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (NR(j) + .5) * window.ns_res;
+ next_easting = window.west + (NC(j) + .5) * window.ew_res;
+ distance_up = G_distance(easting, northing, next_easting, next_northing);
+
+ northing = window.north - (r + .5) * window.ns_res;
+ easting = window.west + (c + .5) * window.ew_res;
+ next_northing = window.north - (NR(d) + .5) * window.ns_res;
+ next_easting = window.west + (NC(d) + .5) * window.ew_res;
+ distance_down =
+ G_distance(easting, northing, next_easting, next_northing);
+
+ distance = distance_up + distance_down;
+ first_derivative = diff_elev / distance;
+ second_derivative = (diff_up - diff_down) / distance;
+ return second_derivative / pow((1 + first_derivative * first_derivative),
+ 1.5);
+
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.snap/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.snap/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.snap/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -11,7 +11,7 @@
* map type to be created must be CELL, FCELL, DCELL;
* */
- int r, c;
+ int r;
if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
@@ -233,7 +233,7 @@
return 0;
}
-int ram_release_map(MAP * map)
+int ram_release_map(MAP *map)
{
/*
* free memory allocated for map, set pointer to null;
@@ -467,6 +467,8 @@
for (r = 0; r < seg->nrows; ++r)
for (c = 0; c < seg->ncols; ++c)
segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
}
int seg_write_map(SEG * seg, char *output_map_name,
Modified: grass-addons/grass7/raster/r.stream/r.stream.snap/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.snap/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.snap/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,42 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
+#include <grass/glocale.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/segment.h>
-#include <grass/glocale.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(r,c) (r)*ncols+(c)
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4));
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
+#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
-typedef struct
-{
- void **map; /* matrix of data */
- double min, max; /* data range : may requre casting */
- int nrows, ncols;
- char *map_name; /* map name, unused */
- RASTER_MAP_TYPE data_type; /* type of data */
- size_t data_size; /* type of data */
+typedef struct {
+ void **map; /* matrix of data */
+ double min, max; /* data range : may requre casting */
+ int nrows, ncols;
+ char *map_name; /* map name, unused */
+ RASTER_MAP_TYPE data_type; /* type of data */
+ size_t data_size; /* type of data */
} MAP;
-typedef struct
-{
- SEGMENT seg; /* segmented data store */
- int fd; /* segment temporary file name descriptor */
- char *filename; /* segment temporary file name */
- char *map_name; /* map name converted to segment */
- char *mapset;
- int nrows, ncols; /* store nrows and rcols */
- RASTER_MAP_TYPE data_type; /* data type of the map */
- size_t data_size; /* size of cell returned by sizeof */
- double min, max; /* data range */
+typedef struct {
+ SEGMENT seg; /* segmented data store */
+ int fd; /* segment temporary file name descriptor */
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
+ int nrows, ncols; /* store nrows and rcols */
+ RASTER_MAP_TYPE data_type; /* data type of the map */
+ size_t data_size; /* size of cell returned by sizeof */
+ double min, max; /* data range */
} SEG;
@@ -45,11 +47,13 @@
int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
int ram_reset_map(MAP *, int);
int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
int ram_destory_map(MAP *);
/* memory swap functions */
int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
-int seg_reset_map(SEG *, int);
+int seg_reset_map (SEG *, int);
int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
int seg_release_map(SEG *);
+
Modified: grass-addons/grass7/raster/r.stream/r.stream.snap/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.snap/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.snap/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -28,16 +28,13 @@
struct GModule *module;
struct Option *in_points_opt,
- *in_points_cat_opt,
*out_points_opt,
*in_stream_opt,
*in_accum_opt,
*opt_accum_treshold, *opt_distance_treshold, *opt_swapsize;
-
- int snap_to_streams;
int i;
- SEG map_dirs, map_streams, map_accum;
+ SEG map_streams, map_accum;
SEGMENT *streams = NULL, *accum = NULL;
int number_of_segs;
int number_of_points;
@@ -92,7 +89,7 @@
exit(EXIT_FAILURE);
number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.12) : number_of_segs / 0.12;
radius = atoi(opt_distance_treshold->answer);
accum_treshold = atof(opt_accum_treshold->answer);
Modified: grass-addons/grass7/raster/r.stream/r.stream.snap/points_io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.snap/points_io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.snap/points_io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -9,7 +9,6 @@
int num_point = 0;
int total_points = 0;
int type, i, j, cat;
- int r, c;
struct line_pnts *sites;
struct line_cats *cats;
double absaccum;
Modified: grass-addons/grass7/raster/r.stream/r.stream.snap/snap.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.snap/snap.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.snap/snap.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,7 +1,7 @@
#include "local_proto.h"
-#define SQRT(x) ((x) * (x))
-static int num;
+#define SQRT(x) ((x) * (x)) /* ??? */
+
int create_distance_mask(int radius)
{
@@ -22,13 +22,12 @@
return 0;
}
-int snap_point(OUTLET * point, int radius, SEGMENT * streams, SEGMENT * accum,
+int snap_point(OUTLET *point, int radius, SEGMENT *streams, SEGMENT *accum,
double accum_treshold)
{
int i, j, di = -1, dj = -1;
int status = 3;
- int number_of_cells = radius * radius;
int teststream = 0;
float cur_distance = radius;
float distance = 0;
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/io.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/io.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/io.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,523 +1,564 @@
#include "io.h"
/* all in ram functions section */
-int ram_create_map(MAP* map, RASTER_MAP_TYPE data_type) {
-
- /*
- * allocates 0 filled nrows*ncols map of type void;
- * map parameters are stored in structure;
- * map: map to be created;
- * map type to be created must be CELL, FCELL, DCELL;
- * */
-
- int r, c;
-
- if(data_type < 0 || data_type > 2)
+int ram_create_map(MAP * map, RASTER_MAP_TYPE data_type)
+{
+
+ /*
+ * allocates 0 filled nrows*ncols map of type void;
+ * map parameters are stored in structure;
+ * map: map to be created;
+ * map type to be created must be CELL, FCELL, DCELL;
+ * */
+
+ int r;
+
+ if (data_type < 0 || data_type > 2)
G_fatal_error(_("ram_creat: Cannot create map of unrecognised type"));
-
- map->data_type=data_type;
- map->map_name=NULL;
- map->nrows = Rast_window_rows();
- map->ncols = Rast_window_cols();
- map->data_size=Rast_cell_size(data_type);
-/* preparing internal map */
- switch (map->data_type) {
- case CELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(CELL *));
- break;
+ map->data_type = data_type;
+ map->map_name = NULL;
+ map->nrows = Rast_window_rows();
+ map->ncols = Rast_window_cols();
+ map->data_size = Rast_cell_size(data_type);
- case FCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(FCELL *));
- break;
-
- case DCELL_TYPE:
- map->map = G_calloc(map->nrows,sizeof(DCELL *));
- break;
- }
+ /* preparing internal map */
+ switch (map->data_type) {
+ case CELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(CELL *));
+ break;
- for (r = 0; r < map->nrows; ++r)
- (map->map)[r] = G_calloc(map->ncols,map->data_size);
-
- return 0;
+ case FCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(FCELL *));
+ break;
+
+ case DCELL_TYPE:
+ map->map = G_calloc(map->nrows, sizeof(DCELL *));
+ break;
+ }
+
+ for (r = 0; r < map->nrows; ++r)
+ (map->map)[r] = G_calloc(map->ncols, map->data_size);
+
+ return 0;
}
-int ram_read_map(MAP* map, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-/*
- * Funciton read external map and put it in MAP structure (created with create_map)
- * map: map to be read can be of any data type, read map is converted to target map if neccesary.
- * input_map_name: name of the map to be read;
- * map pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
+int ram_read_map(MAP * map, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+ /*
+ * Funciton read external map and put it in MAP structure (created with create_map)
+ * map: map to be read can be of any data type, read map is converted to target map if neccesary.
+ * input_map_name: name of the map to be read;
+ * map pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
- int r, c;
- char *mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- int input_map_fd;
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* input_pointer;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ int input_map_fd;
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *input_pointer;
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
- if (mapset == NULL)
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input_map_name);
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res ||
- this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- /* checking if input map is of required type */
- if(check_data_type != map->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
+ /* checking if input map is of required type */
+ if (check_data_type != map->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
- input_map_fd = Rast_open_old(input_map_name, mapset);
- input_data_size = Rast_cell_size(input_data_type);
-
-{/* reading range */
+ input_map_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- map->min=(double)min;
- map->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(map->min), &(map->max));
- }
-}
-/* end opening and checking */
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ map->min = (double)min;
+ map->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(map->min),
+ &(map->max));
+ }
+ }
+ /* end opening and checking */
- input_buffer=Rast_allocate_buf(input_data_type);
-
- /* start reading */
- G_message(_("Reading map <%s>"),input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
+ /* start reading */
+ G_message(_("Reading map <%s>"), input_map_name);
- Rast_get_row(input_map_fd, input_buffer, r,input_data_type);
- input_pointer=input_buffer;
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
- for (c = 0; c < map->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type))
- switch (map->data_type) {
- case CELL_TYPE:
- ((CELL**)map->map)[r][c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL**)map->map)[r][c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL**)map->map)[r][c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("ram_open:Wrong internal data type"));
- break;
- }
- } /*end for r */
+ Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
- G_free(input_buffer);
- G_percent(r, map->nrows, 2);
- Rast_close(input_map_fd);
- return 0;
+ for (c = 0; c < map->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type))
+ switch (map->data_type) {
+ case CELL_TYPE:
+ ((CELL **) map->map)[r][c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL **) map->map)[r][c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL **) map->map)[r][c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("ram_open:Wrong internal data type"));
+ break;
+ }
+ } /*end for r */
+
+ G_free(input_buffer);
+ G_percent(r, map->nrows, 2);
+ Rast_close(input_map_fd);
+ return 0;
} /* end create floating point map */
-int ram_reset_map(MAP* map, int value) {
- /*
- * set all cells in the map to value
- */
- int r;
+int ram_reset_map(MAP * map, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r;
- for (r=0;r<map->nrows;++r)
- memset((map->map)[r],value,map->ncols*map->data_size);
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ memset((map->map)[r], value, map->ncols * map->data_size);
+ return 0;
}
-int ram_write_map(MAP* map, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
-
- int r, c;
- int output_fd = 0;
- struct History history;
- void* row;
+int ram_write_map(MAP * map, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write map to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
- /* check for output format */
- if(output_data_type == -1)
+ int r, c;
+ int output_fd = 0;
+ struct History history;
+ void *row;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = map->data_type;
- if(output_data_type != map->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd = Rast_open_new(output_map_name, output_data_type);
-
- /* writing */
- for (r = 0; r < map->nrows; ++r) {
- G_percent(r, map->nrows, 2);
-
- if(convert_to_null) {
- row = map->map[r];
- switch (map->data_type) {
- case CELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((CELL*)row)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(map->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((FCELL*)row)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(map->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < map->ncols; ++c)
- if (((DCELL*)row)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(map->data_size), 1);
- break;
- default:
- G_debug(1,"ram_null:Cannot convert to null at: %d %d",r,c);
- }
- }
-
+ if (output_data_type != map->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+
+ /* writing */
+ for (r = 0; r < map->nrows; ++r) {
+ G_percent(r, map->nrows, 2);
+
+ if (convert_to_null) {
+ row = map->map[r];
+ switch (map->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((CELL *) row)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (map->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((FCELL *) row)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (map->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < map->ncols; ++c)
+ if (((DCELL *) row)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (map->data_size), 1);
+ break;
+ default:
+ G_debug(1, "ram_null:Cannot convert to null at: %d %d", r, c);
+ }
+ }
+
Rast_put_row(output_fd, (map->map)[r], output_data_type);
- }
- G_percent(r, map->nrows, 2);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("<%s> Done"), output_map_name);
- return 0;
+ }
+ G_percent(r, map->nrows, 2);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("<%s> Done"), output_map_name);
+ return 0;
}
-int ram_release_map (MAP* map) {
- /*
- * free memory allocated for map, set pointer to null;
- */
- int r;
+int ram_release_map(MAP *map)
+{
+ /*
+ * free memory allocated for map, set pointer to null;
+ */
+ int r;
- for (r = 0; r < map->nrows; ++r)
- G_free((map->map)[r]);
- G_free(map->map);
- map=NULL;
- return 0;
+ for (r = 0; r < map->nrows; ++r)
+ G_free((map->map)[r]);
+ G_free(map->map);
+ map = NULL;
+ return 0;
}
/* memory swap functions section */
-int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs, RASTER_MAP_TYPE data_type) {
- /* create segment and returns pointer to it;
- * seg must be declared first;
- * parameters are stored in structure;
- * seg: segment to be created;
- * srows, scols segment size
- * number of segs max number of segs stored in memory
- * data_type to be created must be CELL, FCELL, DCELL;
- */
+int seg_create_map(SEG * seg, int srows, int scols, int number_of_segs,
+ RASTER_MAP_TYPE data_type)
+{
+ /* create segment and returns pointer to it;
+ * seg must be declared first;
+ * parameters are stored in structure;
+ * seg: segment to be created;
+ * srows, scols segment size
+ * number of segs max number of segs stored in memory
+ * data_type to be created must be CELL, FCELL, DCELL;
+ */
- char* filename;
- int fd;
- int local_number_of_segs;
+ char *filename;
+ int fd;
+ int local_number_of_segs;
- seg->fd=-1;
- seg->filename = NULL;
- seg->map_name = NULL;
- seg->mapset = NULL;
- seg->data_type = data_type;
- seg->nrows = Rast_window_rows();
- seg->ncols = Rast_window_cols();
+ seg->fd = -1;
+ seg->filename = NULL;
+ seg->map_name = NULL;
+ seg->mapset = NULL;
+ seg->data_type = data_type;
+ seg->nrows = Rast_window_rows();
+ seg->ncols = Rast_window_cols();
- local_number_of_segs=(seg->nrows/srows+1)*(seg->ncols/scols+1);
- number_of_segs=(number_of_segs>local_number_of_segs) ?
- local_number_of_segs : number_of_segs;
-
- G_debug(3,"seg_creat:number of segments %d",number_of_segs);
-
- switch (seg->data_type) {
- case CELL_TYPE:
- seg->data_size=sizeof(CELL);
- break;
- case FCELL_TYPE:
- seg->data_size=sizeof(FCELL);
- break;
- case DCELL_TYPE:
- seg->data_size=sizeof(DCELL);
- break;
- default:
- G_fatal_error(_("seg_create: unrecognisabe data type"));
- }
-
- filename=G_tempfile();
- fd=creat(filename,0666);
-
- if(0 > segment_format(fd,seg->nrows,seg->ncols,srows,scols,seg->data_size)) {
- close(fd);
- unlink(filename);
- G_fatal_error(_("seg_create: cannot format segment"));
- }
-
+ local_number_of_segs =
+ (seg->nrows / srows + 1) * (seg->ncols / scols + 1);
+ number_of_segs =
+ (number_of_segs >
+ local_number_of_segs) ? local_number_of_segs : number_of_segs;
+
+ G_debug(3, "seg_creat:number of segments %d", number_of_segs);
+
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ seg->data_size = sizeof(CELL);
+ break;
+ case FCELL_TYPE:
+ seg->data_size = sizeof(FCELL);
+ break;
+ case DCELL_TYPE:
+ seg->data_size = sizeof(DCELL);
+ break;
+ default:
+ G_fatal_error(_("seg_create: unrecognisabe data type"));
+ }
+
+ filename = G_tempfile();
+ fd = creat(filename, 0666);
+
+ if (0 >
+ segment_format(fd, seg->nrows, seg->ncols, srows, scols,
+ seg->data_size)) {
close(fd);
- if(0 > (fd = open(filename,2))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot re-open file"));
- }
-
- if(0>(fd = segment_init(&(seg->seg),fd,number_of_segs))) {
- unlink(filename);
- G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
- }
-
- seg->filename = G_store(filename);
- seg->fd = fd;
- return 0;
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot format segment"));
+ }
+
+ close(fd);
+ if (0 > (fd = open(filename, 2))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot re-open file"));
+ }
+
+ if (0 > (fd = segment_init(&(seg->seg), fd, number_of_segs))) {
+ unlink(filename);
+ G_fatal_error(_("seg_create: cannot init segment file or out of memory"));
+ }
+
+ seg->filename = G_store(filename);
+ seg->fd = fd;
+ return 0;
}
-int seg_read_map(SEG* seg, char* input_map_name, int check_res, RASTER_MAP_TYPE check_data_type) {
-
-/*
- * Funciton read external map and put it in SEG structure (created with seg_create_map)
- * map to be read can be of any data type, read map is converted if neccesary.
- * input_map_name: name of the map to be read;
- * seg: pointer to map stucture (created with create_map);
- * check_res: [1]: check res correspondence between region and map [0 no check];
- * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
- */
-
- int input_fd;
- int r,c;
- char* mapset;
- struct Cell_head cellhd, this_window;
- char* maptypes[]= { "CELL", "FCELL", "DCELL" };
- RASTER_MAP_TYPE input_data_type;
- size_t input_data_size;
- void* input_buffer=NULL;
- void* target_buffer=NULL;
- void* input_pointer=NULL;
-
- /* checking if map exist */
- mapset = (char*)G_find_raster2(input_map_name, "");
+int seg_read_map(SEG * seg, char *input_map_name, int check_res,
+ RASTER_MAP_TYPE check_data_type)
+{
+
+ /*
+ * Funciton read external map and put it in SEG structure (created with seg_create_map)
+ * map to be read can be of any data type, read map is converted if neccesary.
+ * input_map_name: name of the map to be read;
+ * seg: pointer to map stucture (created with create_map);
+ * check_res: [1]: check res correspondence between region and map [0 no check];
+ * check_data_type [CELL, FCELL, DCELL] check if reading map is of particular type, [-1] no check;
+ */
+
+ int input_fd;
+ int r, c;
+ char *mapset;
+ struct Cell_head cellhd, this_window;
+ char *maptypes[] = { "CELL", "FCELL", "DCELL" };
+ RASTER_MAP_TYPE input_data_type;
+ size_t input_data_size;
+ void *input_buffer = NULL;
+ void *target_buffer = NULL;
+ void *input_pointer = NULL;
+
+ /* checking if map exist */
+ mapset = (char *)G_find_raster2(input_map_name, "");
if (mapset == NULL)
- G_fatal_error(_("seg_read:Raster map <%s> not found"), input_map_name);
- seg->mapset=mapset;
-
- /* checking if region and input are the same */
- G_get_window(&this_window);
- Rast_get_cellhd(input_map_name, mapset, &cellhd);
-
- /* check resolution equal anyinteger check; equal 0 no check*/
- if(check_res)
- if (this_window.ew_res != cellhd.ew_res || this_window.ns_res != cellhd.ns_res)
- G_fatal_error(_("Region resolution and map %s resolution differs. \
+ G_fatal_error(_("seg_read:Raster map <%s> not found"),
+ input_map_name);
+ seg->mapset = mapset;
+
+ /* checking if region and input are the same */
+ G_get_window(&this_window);
+ Rast_get_cellhd(input_map_name, mapset, &cellhd);
+
+ /* check resolution equal anyinteger check; equal 0 no check */
+ if (check_res)
+ if (this_window.ew_res != cellhd.ew_res ||
+ this_window.ns_res != cellhd.ns_res)
+ G_fatal_error(_("Region resolution and map %s resolution differs. \
Run g.region rast=%s to set proper region resolution"),
- input_map_name, input_map_name);
+ input_map_name, input_map_name);
- if(check_data_type != seg->data_type)
- G_debug(1,"ram_open:required map type and internal map type differs: conversion forced!");
- input_data_type = Rast_map_type(input_map_name,mapset);
- if(check_data_type !=-1)
- if (input_data_type != check_data_type)
- G_fatal_error(_("<%s> is not of type %s"),
- input_map_name, maptypes[check_data_type]);
-
- input_fd = Rast_open_old(input_map_name,mapset);
- input_data_size = Rast_cell_size(input_data_type);
+ if (check_data_type != seg->data_type)
+ G_debug(1,
+ "ram_open:required map type and internal map type differs: conversion forced!");
+ input_data_type = Rast_map_type(input_map_name, mapset);
+ if (check_data_type != -1)
+ if (input_data_type != check_data_type)
+ G_fatal_error(_("<%s> is not of type %s"),
+ input_map_name, maptypes[check_data_type]);
-{/* reading range */
+ input_fd = Rast_open_old(input_map_name, mapset);
+ input_data_size = Rast_cell_size(input_data_type);
+
+ { /* reading range */
struct Range map_range;
struct FPRange map_fp_range;
int min, max;
- if(input_data_type==CELL_TYPE) {
- Rast_init_range(&map_range);
- Rast_read_range(input_map_name,mapset,&map_range);
- Rast_get_range_min_max(&map_range, &min, &max);
- seg->min=(double)min;
- seg->max=(double)max;
- }
- else {
- Rast_init_fp_range(&map_fp_range);
- Rast_read_fp_range(input_map_name,mapset,&map_fp_range);
- Rast_get_fp_range_min_max(&map_fp_range, &(seg->min), &(seg->max));
+ if (input_data_type == CELL_TYPE) {
+ Rast_init_range(&map_range);
+ Rast_read_range(input_map_name, mapset, &map_range);
+ Rast_get_range_min_max(&map_range, &min, &max);
+ seg->min = (double)min;
+ seg->max = (double)max;
+ }
+ else {
+ Rast_init_fp_range(&map_fp_range);
+ Rast_read_fp_range(input_map_name, mapset, &map_fp_range);
+ Rast_get_fp_range_min_max(&map_fp_range, &(seg->min),
+ &(seg->max));
+ }
+ }
+
+ /* end opening and checking */
+
+ G_message(_("Reading map <%s>"), input_map_name);
+ input_buffer = Rast_allocate_buf(input_data_type);
+
+ target_buffer = Rast_allocate_buf(seg->data_type);
+
+ for (r = 0; r < seg->nrows; ++r) {
+ G_percent(r, seg->nrows, 2);
+ Rast_get_row(input_fd, input_buffer, r, input_data_type);
+ input_pointer = input_buffer;
+ memset(target_buffer, 0, seg->ncols * seg->data_size);
+
+ for (c = 0; c < seg->ncols; ++c)
+ if (!Rast_is_null_value
+ (input_pointer + c * input_data_size, input_data_type)) {
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ ((CELL *) target_buffer)[c] =
+ Rast_get_c_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case FCELL_TYPE:
+ ((FCELL *) target_buffer)[c] =
+ Rast_get_f_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ case DCELL_TYPE:
+ ((DCELL *) target_buffer)[c] =
+ Rast_get_d_value(input_pointer + c * input_data_size,
+ input_data_type);
+ break;
+ default:
+ G_fatal_error(_("Wrong internal data type"));
+ break;
}
-}
+ }
- /* end opening and checking */
-
- G_message(_("Reading map <%s>"),input_map_name);
- input_buffer=Rast_allocate_buf(input_data_type);
-
- target_buffer=Rast_allocate_buf(seg->data_type);
+ if (0 > segment_put_row(&(seg->seg), target_buffer, r)) {
+ G_free(input_buffer);
+ G_free(target_buffer);
+ Rast_close(input_fd);
+ G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
+ r, input_map_name);
+ }
+ } /* end for row */
- for (r=0; r<seg->nrows; ++r) {
- G_percent(r, seg->nrows, 2);
- Rast_get_row(input_fd,input_buffer,r,input_data_type);
- input_pointer=input_buffer;
- memset(target_buffer,0,seg->ncols*seg->data_size);
+ G_percent(r, seg->nrows, 2);
+ Rast_close(input_fd);
+ G_free(input_buffer);
+ G_free(target_buffer);
- for (c = 0; c < seg->ncols; ++c)
- if(!Rast_is_null_value(input_pointer+c*input_data_size,input_data_type)) {
- switch (seg->data_type) {
- case CELL_TYPE:
- ((CELL*)target_buffer)[c] =
- Rast_get_c_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case FCELL_TYPE:
- ((FCELL*)target_buffer)[c] =
- Rast_get_f_value(input_pointer+c*input_data_size, input_data_type);
- break;
- case DCELL_TYPE:
- ((DCELL*)target_buffer)[c] =
- Rast_get_d_value(input_pointer+c*input_data_size, input_data_type);
- break;
- default:
- G_fatal_error(_("Wrong internal data type"));
- break;
- }
- }
+ seg->map_name = G_store(input_map_name);
+ seg->mapset = G_store(mapset);
- if(0>segment_put_row(&(seg->seg),target_buffer,r)) {
- G_free(input_buffer);
- G_free(target_buffer);
- Rast_close(input_fd);
- G_fatal_error(_("seg_read: Cannot segment put row %d for map %s"),
- r,input_map_name);
- }
- } /* end for row */
-
- G_percent(r, seg->nrows, 2);
- Rast_close(input_fd);
- G_free(input_buffer);
- G_free(target_buffer);
-
- seg->map_name=G_store(input_map_name);
- seg->mapset=G_store(mapset);
-
- return 0;
+ return 0;
}
-int seg_reset_map (SEG* seg, int value) {
- /*
- * set all cells in the map to value
- */
-int r,c;
- for (r=0;r<seg->nrows;++r)
- for (c=0;c<seg->ncols;++c)
-segment_put(&(seg->seg),&value,r,c);
- }
+int seg_reset_map(SEG * seg, int value)
+{
+ /*
+ * set all cells in the map to value
+ */
+ int r, c;
-int seg_write_map(SEG* seg, char* output_map_name, RASTER_MAP_TYPE output_data_type, int convert_to_null, double value) {
- /*
- * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
- * if output_data_type = -1 than internal map type is used for output;
- * if output map != -1 and types differ data_type, conversion is forced
- * convert to null: check if convert to null a particular value in dataset;
- */
- int output_fd;
- int r, c;
- void* output_buffer;
- void* row;
- struct History history;
-
- /* check for output format */
- if(output_data_type == -1)
+ for (r = 0; r < seg->nrows; ++r)
+ for (c = 0; c < seg->ncols; ++c)
+ segment_put(&(seg->seg), &value, r, c);
+
+ return 0;
+}
+
+int seg_write_map(SEG * seg, char *output_map_name,
+ RASTER_MAP_TYPE output_data_type, int convert_to_null,
+ double value)
+{
+ /*
+ * write seg to disk with output_map_name and output_data_type [CELL, FCELL, DCELL];
+ * if output_data_type = -1 than internal map type is used for output;
+ * if output map != -1 and types differ data_type, conversion is forced
+ * convert to null: check if convert to null a particular value in dataset;
+ */
+ int output_fd;
+ int r, c;
+ void *output_buffer;
+ void *row;
+ struct History history;
+
+ /* check for output format */
+ if (output_data_type == -1)
output_data_type = seg->data_type;
- if(output_data_type != seg->data_type)
- G_debug(1,"ram_write:required map type and internal map type differs: conversion forced!");
-
- G_message(_("Writing map <%s>"),output_map_name);
- output_fd=Rast_open_new(output_map_name,output_data_type);
- output_buffer=Rast_allocate_buf(output_data_type);
- segment_flush(&(seg->seg));
-
- /* writing */
- for(r=0;r<seg->nrows;++r) {
+ if (output_data_type != seg->data_type)
+ G_debug(1,
+ "ram_write:required map type and internal map type differs: conversion forced!");
+ G_message(_("Writing map <%s>"), output_map_name);
+ output_fd = Rast_open_new(output_map_name, output_data_type);
+ output_buffer = Rast_allocate_buf(output_data_type);
+ segment_flush(&(seg->seg));
+
+ /* writing */
+ for (r = 0; r < seg->nrows; ++r) {
+
G_percent(r, seg->nrows, 2);
- if(0>segment_get_row(&(seg->seg),output_buffer,r))
- G_warning(_("seg_write: Cannot segment read row %d for map %s"),
- r,output_map_name);
+ if (0 > segment_get_row(&(seg->seg), output_buffer, r))
+ G_warning(_("seg_write: Cannot segment read row %d for map %s"),
+ r, output_map_name);
- if(convert_to_null) {
+ if (convert_to_null) {
- row = output_buffer;
- switch (seg->data_type) {
- case CELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((CELL*)output_buffer)[c] == (CELL)value)
- Rast_set_c_null_value(row+c*(seg->data_size), 1);
- break;
- case FCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((FCELL*)output_buffer)[c] == (FCELL)value)
- Rast_set_f_null_value(row+c*(seg->data_size), 1);
- break;
- case DCELL_TYPE:
- for (c = 0; c < seg->ncols; ++c)
- if (((DCELL*)output_buffer)[c] == (DCELL)value)
- Rast_set_d_null_value(row+c*(seg->data_size), 1);
- break;
- default:
- G_warning(_("ram_null:Cannot convert to null at: %d %d"),r,c);
- }
- }
+ row = output_buffer;
+ switch (seg->data_type) {
+ case CELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((CELL *) output_buffer)[c] == (CELL) value)
+ Rast_set_c_null_value(row + c * (seg->data_size), 1);
+ break;
+ case FCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((FCELL *) output_buffer)[c] == (FCELL) value)
+ Rast_set_f_null_value(row + c * (seg->data_size), 1);
+ break;
+ case DCELL_TYPE:
+ for (c = 0; c < seg->ncols; ++c)
+ if (((DCELL *) output_buffer)[c] == (DCELL) value)
+ Rast_set_d_null_value(row + c * (seg->data_size), 1);
+ break;
+ default:
+ G_warning(_("ram_null:Cannot convert to null at: %d %d"), r,
+ c);
+ }
+ }
Rast_put_row(output_fd, output_buffer, output_data_type);
- }
+ }
- G_percent(r, seg->nrows, 2);
- G_free(output_buffer);
- Rast_close(output_fd);
- Rast_short_history(output_map_name, "raster", &history);
- Rast_command_history(&history);
- Rast_write_history(output_map_name, &history);
- G_message(_("%s Done"), output_map_name);
-
- return 0;
+ G_percent(r, seg->nrows, 2);
+ G_free(output_buffer);
+ Rast_close(output_fd);
+ Rast_short_history(output_map_name, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(output_map_name, &history);
+ G_message(_("%s Done"), output_map_name);
+
+ return 0;
}
-int seg_release_map(SEG* seg) {
-/*
- * release segment close files, set pointers to null;
- */
- segment_release(&(seg->seg));
- close(seg->fd);
- unlink(seg->filename);
+int seg_release_map(SEG * seg)
+{
+ /*
+ * release segment close files, set pointers to null;
+ */
+ segment_release(&(seg->seg));
+ close(seg->fd);
+ unlink(seg->filename);
- if(seg->map_name)
+ if (seg->map_name)
G_free(seg->map_name);
- if(seg->mapset)
+ if (seg->mapset)
G_free(seg->mapset);
-return 0;
+ return 0;
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/io.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/io.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/io.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <grass/glocale.h>
@@ -7,20 +10,21 @@
#include <grass/raster.h>
#include <grass/segment.h>
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > (nrows-1) || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-1))
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define INDEX(r,c) (r)*ncols+(c)
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4));
+#define NOT_IN_REGION(x) (r + nextr[(x)] < 0 || r + nextr[(x)] > (nrows - 1) || \
+ c + nextc[(x)] < 0 || c + nextc[(x)] > (ncols - 1))
+#define NR(x) (r + nextr[(x)])
+#define NC(x) (c + nextc[(x)])
+#define INDEX(r,c) ((r) * ncols + (c))
+#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
#define SROWS 256
#define SCOLS 256
typedef struct {
- void** map; /* matrix of data */
+ void **map; /* matrix of data */
double min, max; /* data range : may requre casting */
int nrows, ncols;
- char* map_name; /* map name, unused */
+ char *map_name; /* map name, unused */
RASTER_MAP_TYPE data_type; /* type of data */
size_t data_size; /* type of data */
} MAP;
@@ -28,9 +32,9 @@
typedef struct {
SEGMENT seg; /* segmented data store */
int fd; /* segment temporary file name descriptor */
- char* filename; /* segment temporary file name */
- char* map_name; /* map name converted to segment */
- char* mapset;
+ char *filename; /* segment temporary file name */
+ char *map_name; /* map name converted to segment */
+ char *mapset;
int nrows, ncols; /* store nrows and rcols */
RASTER_MAP_TYPE data_type; /* data type of the map */
size_t data_size; /* size of cell returned by sizeof */
@@ -39,16 +43,17 @@
/* all in ram functions */
-int ram_create_map(MAP*, RASTER_MAP_TYPE);
-int ram_read_map (MAP* , char*, int, RASTER_MAP_TYPE);
-int ram_reset_map (MAP*, int);
-int ram_write_map (MAP*, char*, RASTER_MAP_TYPE, int, double);
-int ram_destory_map(MAP*);
+int ram_create_map(MAP *, RASTER_MAP_TYPE);
+int ram_read_map(MAP *, char *, int, RASTER_MAP_TYPE);
+int ram_reset_map(MAP *, int);
+int ram_write_map(MAP *, char *, RASTER_MAP_TYPE, int, double);
+int ram_release_map(MAP *);
+int ram_destory_map(MAP *);
/* memory swap functions */
-int seg_create_map(SEG*, int, int, int, RASTER_MAP_TYPE);
-int seg_read_map (SEG*, char*, int, RASTER_MAP_TYPE);
-int seg_reset_map (SEG*, int);
-int seg_write_map (SEG*, char*, RASTER_MAP_TYPE, int, double);
-int seg_release_map(SEG*);
+int seg_create_map(SEG *, int, int, int, RASTER_MAP_TYPE);
+int seg_read_map(SEG *, char *, int, RASTER_MAP_TYPE);
+int seg_reset_map (SEG *, int);
+int seg_write_map(SEG *, char *, RASTER_MAP_TYPE, int, double);
+int seg_release_map(SEG *);
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/local_proto.h 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/local_proto.h 2011-04-28 12:12:21 UTC (rev 46127)
@@ -5,15 +5,15 @@
int fifo_insert(POINT point);
POINT fifo_return_del(void);
/* ram version */
-int ram_init_streams(CELL** streams, CELL** dirs, FCELL** elevation);
-int ram_calculate_streams(CELL** streams, CELL** dirs, FCELL** elevation);
-double ram_calculate_basins_area(CELL** dirs, int r, int c);
-int ram_calculate_basins(CELL** dirs);
+int ram_init_streams(CELL **streams, CELL **dirs, FCELL **elevation);
+int ram_calculate_streams(CELL **streams, CELL **dirs, FCELL **elevation);
+double ram_calculate_basins_area(CELL **dirs, int r, int c);
+int ram_calculate_basins(CELL **dirs);
/* seg version */
-int seg_init_streams(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation);
-int seg_calculate_streams(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation);
-double seg_calculate_basins_area(SEGMENT* dirs, int r, int c);
-int seg_calculate_basins(SEGMENT* dirs);
+int seg_init_streams(SEGMENT *streams, SEGMENT *dirs, SEGMENT *elevation);
+int seg_calculate_streams(SEGMENT *streams, SEGMENT *dirs, SEGMENT *elevation);
+double seg_calculate_basins_area(SEGMENT *dirs, int r, int c);
+int seg_calculate_basins(SEGMENT *dirs);
/* stats calculate */
double stats_linear_reg(int max_order, double* statistic);
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/main.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/main.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/main.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -24,164 +24,164 @@
#include <grass/glocale.h>
#include "local_proto.h"
- 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 nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int main(int argc, char *argv[])
{
- struct GModule *module;
- struct Option *in_dir_opt, /* options */
- *in_stm_opt,
- *in_elev_opt,
- *opt_swapsize,
- *opt_output;
-
- struct Flag *flag_segmentation,
- *flag_catchment_total,
- *flag_orders_summary;
-
- char* filename;
- int number_of_segs;
- int outlets_num;
- int order_max;
- int segmentation, catchment_total, orders_summary; /*flags */
+ struct GModule *module;
+ struct Option *in_dir_opt, /* options */
+ *in_stm_opt, *in_elev_opt, *opt_swapsize, *opt_output;
+ struct Flag *flag_segmentation,
+ *flag_catchment_total, *flag_orders_summary;
+
+ char *filename;
+ int number_of_segs;
+ int order_max;
+ int segmentation, catchment_total, orders_summary; /*flags */
+
/* initialize GIS environment */
- G_gisinit(argv[0]);
+ G_gisinit(argv[0]);
/* initialize module */
- module = G_define_module();
- module->description =
+ module = G_define_module();
+ module->description =
_("Calculate Horton's statistics for Strahler and Horton ordered networks created with r.stream.");
- G_add_keyword("Horton's statistics");
- G_add_keyword("Bifuracation ratio");
- G_add_keyword("Drainege density");
- G_add_keyword("Catchment statistics");
+ G_add_keyword("Horton's statistics");
+ G_add_keyword("Bifuracation ratio");
+ G_add_keyword("Drainege density");
+ G_add_keyword("Catchment statistics");
- in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_stm_opt->key = "streams";
- in_stm_opt->description = "Name of streams mask input map";
+ in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_stm_opt->key = "streams";
+ in_stm_opt->description = "Name of streams mask input map";
- in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_dir_opt->key = "dirs";
- in_dir_opt->description = "Name of flow direction input map";
+ in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_dir_opt->key = "dirs";
+ in_dir_opt->description = "Name of flow direction input map";
- in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
- in_elev_opt->key = "elevation";
- in_elev_opt->description = "Name of elevation map";
-
- opt_swapsize = G_define_option();
- opt_swapsize->key="memory";
- opt_swapsize->type = TYPE_INTEGER;
- opt_swapsize->answer = "300";
- opt_swapsize->description =_("Max memory used in memory swap mode (MB)");
- opt_swapsize->guisection=_("Optional");
-
- opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
- opt_output->required = NO;
- opt_output->description =
+ in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
+ in_elev_opt->key = "elevation";
+ in_elev_opt->description = "Name of elevation map";
+
+ opt_swapsize = G_define_option();
+ opt_swapsize->key = "memory";
+ opt_swapsize->type = TYPE_INTEGER;
+ opt_swapsize->answer = "300";
+ opt_swapsize->description = _("Max memory used in memory swap mode (MB)");
+ opt_swapsize->guisection = _("Optional");
+
+ opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
+ opt_output->required = NO;
+ opt_output->description =
_("Name for output file (if omitted output to stdout)");
- flag_segmentation = G_define_flag();
- flag_segmentation->key = 'm';
- flag_segmentation->description = _("Use memory swap (operation is slow)");
-
- flag_catchment_total = G_define_flag();
- flag_catchment_total->key = 'c';
- flag_catchment_total->description = _("Print only catchment's statistics");
-
- flag_orders_summary = G_define_flag();
- flag_orders_summary->key = 'o';
- flag_orders_summary->description = _("Print only orders' statistics");
+ flag_segmentation = G_define_flag();
+ flag_segmentation->key = 'm';
+ flag_segmentation->description = _("Use memory swap (operation is slow)");
+ flag_catchment_total = G_define_flag();
+ flag_catchment_total->key = 'c';
+ flag_catchment_total->description =
+ _("Print only catchment's statistics");
+
+ flag_orders_summary = G_define_flag();
+ flag_orders_summary->key = 'o';
+ flag_orders_summary->description = _("Print only orders' statistics");
+
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
- segmentation = (flag_segmentation->answer != 0);
- catchment_total = (flag_catchment_total->answer != 0);
- orders_summary = (flag_orders_summary->answer !=0);
+ segmentation = (flag_segmentation->answer != 0);
+ catchment_total = (flag_catchment_total->answer != 0);
+ orders_summary = (flag_orders_summary->answer != 0);
- filename = opt_output->answer;
- if (filename != NULL)
- if (NULL == freopen(filename, "w", stdout))
- G_fatal_error(_("Unable to open file <%s> for writing"), filename);
-
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
+ filename = opt_output->answer;
+ if (filename != NULL)
+ if (NULL == freopen(filename, "w", stdout))
+ G_fatal_error(_("Unable to open file <%s> for writing"),
+ filename);
-if(!segmentation) {
- G_message(_("ALL IN RAM CALCULATION"));
- MAP map_dirs, map_streams, map_distance, map_elevation;
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+
+ if (!segmentation) {
+ MAP map_dirs, map_streams, map_elevation;
CELL **streams, **dirs;
- FCELL **elevation;
+ FCELL **elevation;
- ram_create_map(&map_streams,CELL_TYPE);
- ram_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_dirs,CELL_TYPE);
- ram_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- ram_create_map(&map_elevation,FCELL_TYPE);
- ram_read_map(&map_elevation,in_elev_opt->answer,0,-1);
+ G_message(_("ALL IN RAM CALCULATION"));
- streams=(CELL**)map_streams.map;
- dirs=(CELL**)map_dirs.map;
- elevation=(FCELL**)map_elevation.map;
+ ram_create_map(&map_streams, CELL_TYPE);
+ ram_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_dirs, CELL_TYPE);
+ ram_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ ram_create_map(&map_elevation, FCELL_TYPE);
+ ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+
+ streams = (CELL **) map_streams.map;
+ dirs = (CELL **) map_dirs.map;
+ elevation = (FCELL **) map_elevation.map;
order_max = (int)map_streams.max;
-
- ram_init_streams(streams,dirs,elevation);
- ram_calculate_streams(streams,dirs,elevation);
+
+ ram_init_streams(streams, dirs, elevation);
+ ram_calculate_streams(streams, dirs, elevation);
ram_calculate_basins(dirs);
-
+
stats(order_max);
- if(!catchment_total && !orders_summary)
- print_stats(order_max);
- if(catchment_total)
- print_stats_total();
- if(orders_summary)
- print_stats_orders(order_max);
-
+ if (!catchment_total && !orders_summary)
+ print_stats(order_max);
+ if (catchment_total)
+ print_stats_total();
+ if (orders_summary)
+ print_stats_orders(order_max);
+
G_free(stat_streams);
G_free(ord_stats);
-
+
ram_release_map(&map_streams);
ram_release_map(&map_dirs);
ram_release_map(&map_elevation);
-}
+ }
-if(segmentation) {
- G_message(_("MEMORY SWAP CALCULATION - MAY TAKE SOME TIME"));
+ if (segmentation) {
SEG map_dirs, map_streams, map_elevation;
SEGMENT *streams, *dirs, *elevation;
+ G_message(_("MEMORY SWAP CALCULATION - MAY TAKE SOME TIME"));
+
number_of_segs = (int)atof(opt_swapsize->answer);
- number_of_segs < 32 ? (int)(32/0.18) : number_of_segs/0.18;
+ number_of_segs = number_of_segs < 32 ? (int)(32 / 0.18) : number_of_segs / 0.18;
- seg_create_map(&map_streams,SROWS, SCOLS, number_of_segs, CELL_TYPE);
- seg_read_map(&map_streams,in_stm_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_dirs,SROWS, SCOLS, number_of_segs,CELL_TYPE);
- seg_read_map(&map_dirs,in_dir_opt->answer,1,CELL_TYPE);
- seg_create_map(&map_elevation,SROWS, SCOLS, number_of_segs,FCELL_TYPE);
- seg_read_map(&map_elevation,in_elev_opt->answer,0,-1);
-
- streams=&map_streams.seg;
- dirs=&map_dirs.seg;
- elevation=&map_elevation.seg;
+ seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_streams, in_stm_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_dirs, SROWS, SCOLS, number_of_segs, CELL_TYPE);
+ seg_read_map(&map_dirs, in_dir_opt->answer, 1, CELL_TYPE);
+ seg_create_map(&map_elevation, SROWS, SCOLS, number_of_segs,
+ FCELL_TYPE);
+ seg_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
+
+ streams = &map_streams.seg;
+ dirs = &map_dirs.seg;
+ elevation = &map_elevation.seg;
order_max = (int)map_streams.max;
- seg_init_streams(streams,dirs,elevation);
- seg_calculate_streams(streams,dirs,elevation);
+ seg_init_streams(streams, dirs, elevation);
+ seg_calculate_streams(streams, dirs, elevation);
seg_calculate_basins(dirs);
- stats(order_max);
+ stats(order_max);
- if(!catchment_total && !orders_summary)
- print_stats(order_max);
- if(catchment_total)
- print_stats_total();
- if(orders_summary)
- print_stats_orders(order_max);
-
+ if (!catchment_total && !orders_summary)
+ print_stats(order_max);
+ if (catchment_total)
+ print_stats_total();
+ if (orders_summary)
+ print_stats_orders(order_max);
+
G_free(stat_streams);
G_free(ord_stats);
@@ -189,6 +189,6 @@
seg_release_map(&map_dirs);
seg_release_map(&map_elevation);
-}
- exit(EXIT_SUCCESS);
+ }
+ exit(EXIT_SUCCESS);
}
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/stats_calculate.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/stats_calculate.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/stats_calculate.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -1,73 +1,73 @@
#include "local_proto.h"
-double stats_linear_reg(int max_order, double* statistic) {
+double stats_linear_reg(int max_order, double *statistic)
+{
-int i;
-double avg_x=0, avg_y=0;
-double sum_x_square=0;
-double sum_x_y=0;
-double sum_x=0;
-double avg_x_y=0;
-double avg_x_square;
-double result;
+ int i;
+ double avg_x = 0, avg_y = 0;
+ double sum_x_square = 0;
+ double sum_x_y = 0;
+ double sum_x = 0;
+ double avg_x_y = 0;
+ double avg_x_square;
+ double result;
- for (i=1;i<=max_order;++i) {
- avg_y += statistic[i];
- sum_x += i;
- sum_x_square += i*i;
- sum_x_y += i*statistic[i];
- }
- avg_y /= (float)max_order;
- avg_x = sum_x/(float)max_order;
- avg_x_y = sum_x_y/(float)max_order;
- avg_x_square = sum_x_square/max_order;
-
+ for (i = 1; i <= max_order; ++i) {
+ avg_y += statistic[i];
+ sum_x += i;
+ sum_x_square += i * i;
+ sum_x_y += i * statistic[i];
+ }
+ avg_y /= (float)max_order;
+ avg_x = sum_x / (float)max_order;
+ avg_x_y = sum_x_y / (float)max_order;
+ avg_x_square = sum_x_square / max_order;
- result = (avg_x_y - avg_x * avg_y) / (avg_x_square - avg_x * avg_x);
- return result;
+
+ result = (avg_x_y - avg_x * avg_y) / (avg_x_square - avg_x * avg_x);
+ return result;
}
int stats(order_max)
{
- int i;
- int num, ord_num;
- float snum, ord_snum;
- float tmp_num;
- double* statistic;
- double tmp_bif_ratio;
+ int i;
+ int num, ord_num;
+ float snum, ord_snum;
+ float tmp_num;
+ double *statistic;
ord_stats = (STATS *) G_malloc((order_max + 1) * sizeof(STATS));
- stats_total.order = 0;
- stats_total.stream_num = 0;
- stats_total.sum_length = 0.;
- stats_total.avg_length = 0.;
- stats_total.std_length = 0.;
- stats_total.avg_slope = 0.;
- stats_total.std_slope = 0.;
- stats_total.avg_gradient = 0.;
- stats_total.std_gradient = 0.;
- stats_total.sum_area = 0.;
- stats_total.avg_area = 0.;
- stats_total.std_area = 0.;
- stats_total.avg_elev_diff = 0.;
- stats_total.std_elev_diff = 0.;
- stats_total.bifur_ratio = 0.;
- stats_total.std_bifur_ratio = 0.;
- stats_total.reg_bifur_ratio = 0.;
- stats_total.length_ratio = 0.;
- stats_total.std_length_ratio = 0.;
- stats_total.reg_length_ratio = 0.;
- stats_total.area_ratio = 0.;
- stats_total.std_area_ratio = 0.;
- stats_total.reg_area_ratio = 0.;
- stats_total.slope_ratio = 0.;
- stats_total.std_slope_ratio = 0.;
- stats_total.reg_slope_ratio = 0.;
- stats_total.gradient_ratio = 0.;
- stats_total.std_gradient_ratio = 0.;
- stats_total.reg_gradient_ratio = 0.;
- stats_total.stream_frequency = 0.;
- stats_total.drainage_density = 0.;
+ stats_total.order = 0;
+ stats_total.stream_num = 0;
+ stats_total.sum_length = 0.;
+ stats_total.avg_length = 0.;
+ stats_total.std_length = 0.;
+ stats_total.avg_slope = 0.;
+ stats_total.std_slope = 0.;
+ stats_total.avg_gradient = 0.;
+ stats_total.std_gradient = 0.;
+ stats_total.sum_area = 0.;
+ stats_total.avg_area = 0.;
+ stats_total.std_area = 0.;
+ stats_total.avg_elev_diff = 0.;
+ stats_total.std_elev_diff = 0.;
+ stats_total.bifur_ratio = 0.;
+ stats_total.std_bifur_ratio = 0.;
+ stats_total.reg_bifur_ratio = 0.;
+ stats_total.length_ratio = 0.;
+ stats_total.std_length_ratio = 0.;
+ stats_total.reg_length_ratio = 0.;
+ stats_total.area_ratio = 0.;
+ stats_total.std_area_ratio = 0.;
+ stats_total.reg_area_ratio = 0.;
+ stats_total.slope_ratio = 0.;
+ stats_total.std_slope_ratio = 0.;
+ stats_total.reg_slope_ratio = 0.;
+ stats_total.gradient_ratio = 0.;
+ stats_total.std_gradient_ratio = 0.;
+ stats_total.reg_gradient_ratio = 0.;
+ stats_total.stream_frequency = 0.;
+ stats_total.drainage_density = 0.;
for (i = 0; i <= order_max; ++i) {
ord_stats[i].order = i;
@@ -147,35 +147,41 @@
ord_stats[i].avg_length)) * snum);
ord_stats[i].std_slope = sqrt((ord_stats[i].std_slope / num -
- (ord_stats[i].avg_slope * ord_stats[i].avg_slope)) * snum);
+ (ord_stats[i].avg_slope *
+ ord_stats[i].avg_slope)) * snum);
ord_stats[i].std_gradient = sqrt((ord_stats[i].std_gradient / num -
- (ord_stats[i].avg_gradient * ord_stats[i].avg_gradient)) * snum);
+ (ord_stats[i].avg_gradient *
+ ord_stats[i].avg_gradient)) *
+ snum);
ord_stats[i].std_area = sqrt((ord_stats[i].std_area / num -
- (ord_stats[i].avg_area * ord_stats[i].avg_area)) * snum);
+ (ord_stats[i].avg_area *
+ ord_stats[i].avg_area)) * snum);
ord_stats[i].std_elev_diff = sqrt((ord_stats[i].std_elev_diff / num -
- (ord_stats[i].avg_elev_diff * ord_stats[i].avg_elev_diff)) * snum);
+ (ord_stats[i].avg_elev_diff *
+ ord_stats[i].avg_elev_diff)) *
+ snum);
ord_stats[i - 1].bifur_ratio =
ord_stats[i - 1].stream_num / (float)ord_stats[i].stream_num;
-
- ord_stats[i - 1].length_ratio = (i==1) ? 0 :
- ord_stats[i].avg_length / ord_stats[i-1].avg_length;
-
- ord_stats[i].area_ratio = (i==1) ? 0 :
- ord_stats[i].avg_area / ord_stats[i-1].avg_area;
-
+
+ ord_stats[i - 1].length_ratio = (i == 1) ? 0 :
+ ord_stats[i].avg_length / ord_stats[i - 1].avg_length;
+
+ ord_stats[i].area_ratio = (i == 1) ? 0 :
+ ord_stats[i].avg_area / ord_stats[i - 1].avg_area;
+
ord_stats[i - 1].slope_ratio =
ord_stats[i - 1].avg_slope / ord_stats[i].avg_slope;
-
+
ord_stats[i - 1].gradient_ratio =
ord_stats[i - 1].avg_gradient / ord_stats[i].avg_gradient;
-
+
ord_stats[i].stream_frequency =
ord_stats[i].stream_num / ord_stats[i].sum_area;
-
+
ord_stats[i].drainage_density =
ord_stats[i].sum_length / ord_stats[i].sum_area;
@@ -190,81 +196,91 @@
stats_total.gradient_ratio += ord_stats[i - 1].gradient_ratio;
stats_total.std_bifur_ratio +=
- (ord_stats[i - 1].bifur_ratio * ord_stats[i - 1].bifur_ratio);
+ (ord_stats[i - 1].bifur_ratio * ord_stats[i - 1].bifur_ratio);
stats_total.std_length_ratio +=
- (ord_stats[i - 1].length_ratio * ord_stats[i - 1].length_ratio);
+ (ord_stats[i - 1].length_ratio * ord_stats[i - 1].length_ratio);
stats_total.std_area_ratio +=
- (ord_stats[i - 1].area_ratio * ord_stats[i - 1].area_ratio);
+ (ord_stats[i - 1].area_ratio * ord_stats[i - 1].area_ratio);
stats_total.std_slope_ratio +=
- (ord_stats[i - 1].slope_ratio * ord_stats[i - 1].slope_ratio);
+ (ord_stats[i - 1].slope_ratio * ord_stats[i - 1].slope_ratio);
stats_total.std_gradient_ratio +=
- (ord_stats[i - 1].gradient_ratio * ord_stats[i - 1].gradient_ratio);
+ (ord_stats[i - 1].gradient_ratio *
+ ord_stats[i - 1].gradient_ratio);
} /* end for ... orders */
- ord_num = order_max - 1;
- ord_snum = (ord_num == 1) ? 0 : (float)ord_num / (ord_num - 1);
+ ord_num = order_max - 1;
+ ord_snum = (ord_num == 1) ? 0 : (float)ord_num / (ord_num - 1);
- stats_total.order = order_max;
- stats_total.sum_area = total_basins;
- stats_total.sum_length = stats_total.sum_length;
+ stats_total.order = order_max;
+ stats_total.sum_area = total_basins;
+ stats_total.sum_length = stats_total.sum_length;
- stats_total.bifur_ratio = stats_total.bifur_ratio / ord_num;
- stats_total.length_ratio = stats_total.length_ratio / ord_num;
- stats_total.area_ratio = stats_total.area_ratio / ord_num;
- stats_total.slope_ratio = stats_total.slope_ratio / ord_num;
- stats_total.gradient_ratio = stats_total.gradient_ratio / ord_num;
+ stats_total.bifur_ratio = stats_total.bifur_ratio / ord_num;
+ stats_total.length_ratio = stats_total.length_ratio / ord_num;
+ stats_total.area_ratio = stats_total.area_ratio / ord_num;
+ stats_total.slope_ratio = stats_total.slope_ratio / ord_num;
+ stats_total.gradient_ratio = stats_total.gradient_ratio / ord_num;
- stats_total.std_bifur_ratio =
- sqrt((stats_total.std_bifur_ratio / ord_num -
- (stats_total.bifur_ratio * stats_total.bifur_ratio)) * ord_snum);
+ stats_total.std_bifur_ratio =
+ sqrt((stats_total.std_bifur_ratio / ord_num -
+ (stats_total.bifur_ratio * stats_total.bifur_ratio)) *
+ ord_snum);
- stats_total.std_length_ratio = sqrt((stats_total.std_length_ratio / ord_num -
- (stats_total.length_ratio * stats_total.length_ratio)) * ord_snum);
+ stats_total.std_length_ratio =
+ sqrt((stats_total.std_length_ratio / ord_num -
+ (stats_total.length_ratio * stats_total.length_ratio)) *
+ ord_snum);
- stats_total.std_area_ratio = sqrt((stats_total.std_area_ratio / ord_num -
- (stats_total.area_ratio * stats_total.area_ratio)) * ord_snum);
+ stats_total.std_area_ratio = sqrt((stats_total.std_area_ratio / ord_num -
+ (stats_total.area_ratio *
+ stats_total.area_ratio)) * ord_snum);
- stats_total.std_slope_ratio =
- sqrt((stats_total.std_slope_ratio / ord_num -
- (stats_total.slope_ratio * stats_total.slope_ratio)) * ord_snum);
+ stats_total.std_slope_ratio =
+ sqrt((stats_total.std_slope_ratio / ord_num -
+ (stats_total.slope_ratio * stats_total.slope_ratio)) *
+ ord_snum);
- stats_total.std_gradient_ratio =
- sqrt((stats_total.std_gradient_ratio / ord_num -
- (stats_total.gradient_ratio * stats_total.gradient_ratio)) * ord_snum);
+ stats_total.std_gradient_ratio =
+ sqrt((stats_total.std_gradient_ratio / ord_num -
+ (stats_total.gradient_ratio * stats_total.gradient_ratio)) *
+ ord_snum);
- stats_total.stream_frequency =
- stats_total.stream_num / stats_total.sum_area;
- stats_total.drainage_density =
- stats_total.sum_length / stats_total.sum_area;
-
-
- /* linerar regresion statistics */
- statistic = (double*)G_malloc((order_max+1)*sizeof(double));
+ stats_total.stream_frequency =
+ stats_total.stream_num / stats_total.sum_area;
+ stats_total.drainage_density =
+ stats_total.sum_length / stats_total.sum_area;
- for (i=1;i<=order_max;++i)
- statistic[i]=log10((double)ord_stats[i].stream_num);
- stats_total.reg_bifur_ratio=1/pow(10,stats_linear_reg(order_max,statistic));
-
- for (i=1;i<=order_max;++i)
- statistic[i]=log10((double)ord_stats[i].avg_length);
- stats_total.reg_length_ratio=pow(10,stats_linear_reg(order_max,statistic));
-
- for (i=1;i<=order_max;++i)
- statistic[i]=log10((double)ord_stats[i].avg_area);
- stats_total.reg_area_ratio=pow(10,stats_linear_reg(order_max,statistic));
- for (i=1;i<=order_max;++i)
- statistic[i]=log10((double)ord_stats[i].avg_slope);
- stats_total.reg_slope_ratio=1/pow(10,stats_linear_reg(order_max,statistic));
-
- for (i=1;i<=order_max;++i)
- statistic[i]=log10((double)ord_stats[i].avg_gradient);
- stats_total.reg_gradient_ratio=1/pow(10,stats_linear_reg(order_max,statistic));
+ /* linerar regresion statistics */
+ statistic = (double *)G_malloc((order_max + 1) * sizeof(double));
- G_free(statistic);
+ for (i = 1; i <= order_max; ++i)
+ statistic[i] = log10((double)ord_stats[i].stream_num);
+ stats_total.reg_bifur_ratio =
+ 1 / pow(10, stats_linear_reg(order_max, statistic));
-return 0;
-}
+ for (i = 1; i <= order_max; ++i)
+ statistic[i] = log10((double)ord_stats[i].avg_length);
+ stats_total.reg_length_ratio =
+ pow(10, stats_linear_reg(order_max, statistic));
+ for (i = 1; i <= order_max; ++i)
+ statistic[i] = log10((double)ord_stats[i].avg_area);
+ stats_total.reg_area_ratio =
+ pow(10, stats_linear_reg(order_max, statistic));
+ for (i = 1; i <= order_max; ++i)
+ statistic[i] = log10((double)ord_stats[i].avg_slope);
+ stats_total.reg_slope_ratio =
+ 1 / pow(10, stats_linear_reg(order_max, statistic));
+
+ for (i = 1; i <= order_max; ++i)
+ statistic[i] = log10((double)ord_stats[i].avg_gradient);
+ stats_total.reg_gradient_ratio =
+ 1 / pow(10, stats_linear_reg(order_max, statistic));
+
+ G_free(statistic);
+
+ return 0;
+}
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/stats_prepare.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/stats_prepare.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/stats_prepare.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -22,13 +22,13 @@
head = -1;
}
fifo_count--;
-
+
return fifo_points[++head];
}
-int ram_init_streams(CELL** streams, CELL** dirs, FCELL** elevation)
+int ram_init_streams(CELL **streams, CELL **dirs, FCELL **elevation)
{
- int d, i, j; /* d: direction, i: iteration */
+ int d, i; /* d: direction, i: iteration */
int r, c;
int next_stream = -1, cur_stream;
int out_max = ncols + nrows;
@@ -37,39 +37,40 @@
outlets = (POINT *) G_malloc((out_max) * sizeof(POINT));
outlets_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c)
- if (streams[r][c] > 0) {
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c)
+ if (streams[r][c] > 0) {
if (outlets_num > (out_max - 1)) {
out_max *= 2;
- outlets = (POINT *) G_realloc(outlets, out_max * sizeof(POINT));
+ outlets =
+ (POINT *) G_realloc(outlets, out_max * sizeof(POINT));
}
- d = abs(dirs[r][c]);
- if (NOT_IN_REGION(d))
- next_stream = -1; /* border */
- else {
+ d = abs(dirs[r][c]);
+ if (NOT_IN_REGION(d))
+ next_stream = -1; /* border */
+ else {
next_stream = streams[NR(d)][NC(d)];
- if (next_stream < 1)
- next_stream = -1;
- }
-
- if (d == 0)
- next_stream = -1;
+ if (next_stream < 1)
+ next_stream = -1;
+ }
+
+ if (d == 0)
+ next_stream = -1;
cur_stream = streams[r][c];
-
+
if (cur_stream != next_stream) { /* is outlet or node! */
outlets[outlets_num].r = r;
outlets[outlets_num].c = c;
-
- if (next_stream==-1)
- outlets[outlets_num].is_outlet=1;
- else
- outlets[outlets_num].is_outlet=0;
-
+
+ if (next_stream == -1)
+ outlets[outlets_num].is_outlet = 1;
+ else
+ outlets[outlets_num].is_outlet = 0;
+
outlets_num++;
-
+
}
- } /* end if streams */
+ } /* end if streams */
stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
@@ -89,62 +90,63 @@
stat_streams[i].cell_num = 0;
}
- G_free(outlets);
- return 0;
+ G_free(outlets);
+ return 0;
}
+
/////
-int seg_init_streams(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation)
+int seg_init_streams(SEGMENT *streams, SEGMENT *dirs, SEGMENT *elevation)
{
- int d, i, j; /* d: direction, i: iteration */
+ int d, i; /* d: direction, i: iteration */
int r, c;
int next_stream = -1, cur_stream;
int out_max = ncols + nrows;
- CELL streams_cell, dirs_cell, stream_next_cell;
- FCELL elevation_cell;
+ CELL streams_cell, dirs_cell;
POINT *outlets;
outlets = (POINT *) G_malloc((out_max) * sizeof(POINT));
outlets_num = 0;
- for (r = 0; r < nrows; ++r)
- for (c = 0; c < ncols; ++c) {
- segment_get(streams,&streams_cell,r,c);
- if (streams_cell > 0) {
+ for (r = 0; r < nrows; ++r)
+ for (c = 0; c < ncols; ++c) {
+ segment_get(streams, &streams_cell, r, c);
+ if (streams_cell > 0) {
if (outlets_num > (out_max - 1)) {
out_max *= 2;
- outlets = (POINT *) G_realloc(outlets, out_max * sizeof(POINT));
+ outlets =
+ (POINT *) G_realloc(outlets, out_max * sizeof(POINT));
}
-
- segment_get(dirs,&dirs_cell,r,c);
- d = abs(dirs_cell);
- if (NOT_IN_REGION(d))
- next_stream = -1; /* border */
- else {
- segment_get(streams,&next_stream,NR(d),NC(d));
- if (next_stream < 1)
- next_stream = -1;
- }
-
- if (d == 0)
- next_stream = -1;
- cur_stream=streams_cell;
-
+
+ segment_get(dirs, &dirs_cell, r, c);
+ d = abs(dirs_cell);
+ if (NOT_IN_REGION(d))
+ next_stream = -1; /* border */
+ else {
+ segment_get(streams, &next_stream, NR(d), NC(d));
+ if (next_stream < 1)
+ next_stream = -1;
+ }
+
+ if (d == 0)
+ next_stream = -1;
+ cur_stream = streams_cell;
+
if (cur_stream != next_stream) { /* is outlet or node! */
outlets[outlets_num].r = r;
outlets[outlets_num].c = c;
-
- if (next_stream==-1)
- outlets[outlets_num].is_outlet=1;
- else
- outlets[outlets_num].is_outlet=0;
-
+
+ if (next_stream == -1)
+ outlets[outlets_num].is_outlet = 1;
+ else
+ outlets[outlets_num].is_outlet = 0;
+
outlets_num++;
-
-
+
+
}
- } /* end if streams */
- }
+ } /* end if streams */
+ }
stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
@@ -158,303 +160,305 @@
stat_streams[i].length = 0.;
stat_streams[i].elev_diff = 0.;
stat_streams[i].elev_spring = 0.;
- segment_get(elevation,&(stat_streams[i].elev_outlet),outlets[i].r,outlets[i].c);
- segment_get(streams,&(stat_streams[i].order),outlets[i].r,outlets[i].c);
+ segment_get(elevation, &(stat_streams[i].elev_outlet), outlets[i].r,
+ outlets[i].c);
+ segment_get(streams, &(stat_streams[i].order), outlets[i].r,
+ outlets[i].c);
stat_streams[i].basin_area = 0.;
stat_streams[i].cell_num = 0;
}
- G_free(outlets);
- return 0;
+ G_free(outlets);
+ return 0;
}
-int ram_calculate_streams(CELL** streams, CELL** dirs, FCELL** elevation)
+int ram_calculate_streams(CELL **streams, CELL **dirs, FCELL **elevation)
{
- int i, j, s, d; /* s - streams index */
- int done = 1;
- int r, c;
- int next_r, next_c;
- float cur_northing, cur_easting;
- float next_northing, next_easting;
- float diff_elev;
- double cur_length;
- int cur_stream_order;
- struct Cell_head window;
-
- G_get_window(&window);
- G_begin_distance_calculations();
+ int i, j, s, d; /* s - streams index */
+ int done = 1;
+ int r, c;
+ int next_r, next_c;
+ float cur_northing, cur_easting;
+ float next_northing, next_easting;
+ float diff_elev;
+ double cur_length;
+ struct Cell_head window;
+ G_get_window(&window);
+ G_begin_distance_calculations();
+
for (s = 0; s < outlets_num; ++s) {
r = stat_streams[s].r;
c = stat_streams[s].c;
-
+
cur_northing = window.north - (r + .5) * window.ns_res;
cur_easting = window.west + (c + .5) * window.ew_res;
- d= (dirs[r][c]==0) ? 2 : abs(dirs[r][c]);
-
- next_northing =
- window.north - (NR(d) + .5) * window.ns_res;
- next_easting =
- window.west + (NC(d) + .5) * window.ew_res;
+ d = (dirs[r][c] == 0) ? 2 : abs(dirs[r][c]);
- stat_streams[s].length=
- G_distance(next_easting, next_northing, cur_easting,cur_northing);
-
+ next_northing = window.north - (NR(d) + .5) * window.ns_res;
+ next_easting = window.west + (NC(d) + .5) * window.ew_res;
+
+ stat_streams[s].length =
+ G_distance(next_easting, next_northing, cur_easting,
+ cur_northing);
+
done = 1;
while (done) {
- done = 0;
- cur_northing = window.north - (r + .5) * window.ns_res;
- cur_easting = window.west + (c + .5) * window.ew_res;
+ done = 0;
+ cur_northing = window.north - (r + .5) * window.ns_res;
+ cur_easting = window.west + (c + .5) * window.ew_res;
- stat_streams[s].cell_num++;
- stat_streams[s].elev_spring = elevation[r][c];
+ stat_streams[s].cell_num++;
+ stat_streams[s].elev_spring = elevation[r][c];
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue; /* border */
-
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue; /* border */
+
j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- if (streams[next_r][next_c] == stat_streams[s].order &&
- dirs[next_r][next_c] == j) {
+ next_r = NR(i);
+ next_c = NC(i);
- next_northing =
+ if (streams[next_r][next_c] == stat_streams[s].order &&
+ dirs[next_r][next_c] == j) {
+
+ next_northing =
window.north - (next_r + .5) * window.ns_res;
- next_easting =
+ next_easting =
window.west + (next_c + .5) * window.ew_res;
- cur_length =
- G_distance(next_easting, next_northing, cur_easting,cur_northing);
- diff_elev =
- elevation[next_r][next_c] - elevation[r][c];
- diff_elev = (diff_elev < 0) ? 0. : diff_elev; /* water cannot flow up */
+ cur_length =
+ G_distance(next_easting, next_northing, cur_easting,
+ cur_northing);
+ diff_elev = elevation[next_r][next_c] - elevation[r][c];
+ diff_elev = (diff_elev < 0) ? 0. : diff_elev; /* water cannot flow up */
- stat_streams[s].length += cur_length;
- stat_streams[s].slope += (diff_elev / cur_length);
+ stat_streams[s].length += cur_length;
+ stat_streams[s].slope += (diff_elev / cur_length);
- r = next_r;
- c = next_c;
- done = 1;
- break;
- } /* end if */
- } /* end for i */
+ r = next_r;
+ c = next_c;
+ done = 1;
+ break;
+ } /* end if */
+ } /* end for i */
} /* end while */
- } /* end for s */
- return 0;
+ } /* end for s */
+ return 0;
}
////
-int seg_calculate_streams(SEGMENT* streams, SEGMENT* dirs, SEGMENT* elevation)
+int seg_calculate_streams(SEGMENT *streams, SEGMENT *dirs,
+ SEGMENT *elevation)
{
- int i, j, s, d; /* s - streams index */
- int done = 1;
- int r, c;
- int next_r, next_c;
- float cur_northing, cur_easting;
- float next_northing, next_easting;
- float diff_elev;
- double cur_length;
- int cur_stream_order;
- CELL streams_cell, dirs_cell;
- FCELL elevation_cell, elevation_next_cell;
- struct Cell_head window;
-
- G_get_window(&window);
- G_begin_distance_calculations();
+ int i, j, s, d; /* s - streams index */
+ int done = 1;
+ int r, c;
+ int next_r, next_c;
+ float cur_northing, cur_easting;
+ float next_northing, next_easting;
+ float diff_elev;
+ double cur_length;
+ CELL streams_cell, dirs_cell;
+ FCELL elevation_cell, elevation_next_cell;
+ struct Cell_head window;
+ G_get_window(&window);
+ G_begin_distance_calculations();
+
for (s = 0; s < outlets_num; ++s) {
r = stat_streams[s].r;
c = stat_streams[s].c;
-
+
cur_northing = window.north - (r + .5) * window.ns_res;
cur_easting = window.west + (c + .5) * window.ew_res;
-
- segment_get(dirs,&dirs_cell,r,c);
- d= (dirs_cell==0) ? 2 : abs(dirs_cell);
-
- next_northing =
- window.north - (NR(d) + .5) * window.ns_res;
- next_easting =
- window.west + (NC(d) + .5) * window.ew_res;
- stat_streams[s].length=
- G_distance(next_easting, next_northing, cur_easting,cur_northing);
-
+ segment_get(dirs, &dirs_cell, r, c);
+ d = (dirs_cell == 0) ? 2 : abs(dirs_cell);
+
+ next_northing = window.north - (NR(d) + .5) * window.ns_res;
+ next_easting = window.west + (NC(d) + .5) * window.ew_res;
+
+ stat_streams[s].length =
+ G_distance(next_easting, next_northing, cur_easting,
+ cur_northing);
+
done = 1;
while (done) {
- done = 0;
- cur_northing = window.north - (r + .5) * window.ns_res;
- cur_easting = window.west + (c + .5) * window.ew_res;
+ done = 0;
+ cur_northing = window.north - (r + .5) * window.ns_res;
+ cur_easting = window.west + (c + .5) * window.ew_res;
- stat_streams[s].cell_num++;
- segment_get(elevation,&(stat_streams[s].elev_spring),r,c);
+ stat_streams[s].cell_num++;
+ segment_get(elevation, &(stat_streams[s].elev_spring), r, c);
- for (i = 1; i < 9; ++i) {
- if (NOT_IN_REGION(i))
- continue; /* border */
-
+ for (i = 1; i < 9; ++i) {
+ if (NOT_IN_REGION(i))
+ continue; /* border */
+
j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- segment_get(streams,&streams_cell,next_r,next_c);
- segment_get(dirs,&dirs_cell,next_r,next_c);
-
- if (streams_cell == stat_streams[s].order &&
- dirs_cell == j) {
+ next_r = NR(i);
+ next_c = NC(i);
- next_northing =
+ segment_get(streams, &streams_cell, next_r, next_c);
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+
+ if (streams_cell == stat_streams[s].order && dirs_cell == j) {
+
+ next_northing =
window.north - (next_r + .5) * window.ns_res;
- next_easting =
+ next_easting =
window.west + (next_c + .5) * window.ew_res;
- cur_length =
- G_distance(next_easting, next_northing, cur_easting,cur_northing);
-
- segment_get(elevation,&elevation_next_cell,next_r,next_c);
- segment_get(elevation,&elevation_cell,r,c);
- diff_elev = elevation_next_cell - elevation_cell;
- diff_elev = (diff_elev < 0) ? 0. : diff_elev; /* water cannot flow up */
+ cur_length =
+ G_distance(next_easting, next_northing, cur_easting,
+ cur_northing);
- stat_streams[s].length += cur_length;
- stat_streams[s].slope += (diff_elev / cur_length);
+ segment_get(elevation, &elevation_next_cell, next_r,
+ next_c);
+ segment_get(elevation, &elevation_cell, r, c);
+ diff_elev = elevation_next_cell - elevation_cell;
+ diff_elev = (diff_elev < 0) ? 0. : diff_elev; /* water cannot flow up */
- r = next_r;
- c = next_c;
- done = 1;
- break;
- } /* end if */
- } /* end for i */
+ stat_streams[s].length += cur_length;
+ stat_streams[s].slope += (diff_elev / cur_length);
+
+ r = next_r;
+ c = next_c;
+ done = 1;
+ break;
+ } /* end if */
+ } /* end for i */
} /* end while */
- } /* end for s */
- return 0;
+ } /* end for s */
+ return 0;
}
-double ram_calculate_basins_area(CELL** dirs, int r, int c)
+
+double ram_calculate_basins_area(CELL **dirs, int r, int c)
{
- int i, j;
- int next_r, next_c;
- double area;
- POINT n_cell;
+ int i, j;
+ int next_r, next_c;
+ double area;
+ POINT n_cell;
- tail = 0;
- head = -1;
+ tail = 0;
+ head = -1;
- area = G_area_of_cell_at_row(r);
+ area = G_area_of_cell_at_row(r);
while (tail != head) {
for (i = 1; i < 9; ++i) {
-
+
if (NOT_IN_REGION(i))
continue; /* border */
-
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- if (dirs[next_r][next_c] == j) { /* countributing cell */
- area += G_area_of_cell_at_row(r);
- n_cell.r = next_r;
- n_cell.c = next_c;
- fifo_insert(n_cell);
- }
+
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
+ if (dirs[next_r][next_c] == j) { /* countributing cell */
+ area += G_area_of_cell_at_row(r);
+ n_cell.r = next_r;
+ n_cell.c = next_c;
+ fifo_insert(n_cell);
+ }
} /* end for i... */
n_cell = fifo_return_del();
r = n_cell.r;
c = n_cell.c;
- } /* end while */
- return area;
+ } /* end while */
+ return area;
}
-int ram_calculate_basins(CELL** dirs)
+int ram_calculate_basins(CELL **dirs)
{
- int i;
- total_basins=0.;
+ int i;
- G_begin_cell_area_calculations();
- fifo_max = 4 * (nrows + ncols);
- fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+ total_basins = 0.;
+ G_begin_cell_area_calculations();
+ fifo_max = 4 * (nrows + ncols);
+ fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+
for (i = 0; i < outlets_num; ++i) {
stat_streams[i].basin_area =
- ram_calculate_basins_area(dirs, stat_streams[i].r, stat_streams[i].c);
+ ram_calculate_basins_area(dirs, stat_streams[i].r,
+ stat_streams[i].c);
- if (stat_streams[i].is_outlet)
- total_basins += stat_streams[i].basin_area;
- }
-
- G_free(fifo_points);
- return 0;
+ if (stat_streams[i].is_outlet)
+ total_basins += stat_streams[i].basin_area;
+ }
+
+ G_free(fifo_points);
+ return 0;
}
-double seg_calculate_basins_area(SEGMENT* dirs, int r, int c)
+double seg_calculate_basins_area(SEGMENT *dirs, int r, int c)
{
- int i, j;
- int next_r, next_c;
- double area;
- CELL dirs_cell;
- POINT n_cell;
+ int i, j;
+ int next_r, next_c;
+ double area;
+ CELL dirs_cell;
+ POINT n_cell;
- tail = 0;
- head = -1;
+ tail = 0;
+ head = -1;
- area = G_area_of_cell_at_row(r);
+ area = G_area_of_cell_at_row(r);
while (tail != head) {
for (i = 1; i < 9; ++i) {
-
- if (NOT_IN_REGION(i))
- continue; /* border */
-
- j = DIAG(i);
- next_r=NR(i);
- next_c=NC(i);
-
- segment_get(dirs,&dirs_cell,next_r,next_c);
-
- if (dirs_cell == j) { /* countributing cell */
- area += G_area_of_cell_at_row(r);
- n_cell.r = next_r;
- n_cell.c = next_c;
- fifo_insert(n_cell);
- }
+
+ if (NOT_IN_REGION(i))
+ continue; /* border */
+
+ j = DIAG(i);
+ next_r = NR(i);
+ next_c = NC(i);
+
+ segment_get(dirs, &dirs_cell, next_r, next_c);
+
+ if (dirs_cell == j) { /* countributing cell */
+ area += G_area_of_cell_at_row(r);
+ n_cell.r = next_r;
+ n_cell.c = next_c;
+ fifo_insert(n_cell);
+ }
} /* end for i... */
n_cell = fifo_return_del();
r = n_cell.r;
c = n_cell.c;
- } /* end while */
- return area;
+ } /* end while */
+ return area;
}
-int seg_calculate_basins(SEGMENT* dirs)
+int seg_calculate_basins(SEGMENT *dirs)
{
- int i;
- total_basins=0.;
+ int i;
- G_begin_cell_area_calculations();
- fifo_max = 4 * (nrows + ncols);
- fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+ total_basins = 0.;
+ G_begin_cell_area_calculations();
+ fifo_max = 4 * (nrows + ncols);
+ fifo_points = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+
for (i = 0; i < outlets_num; ++i) {
stat_streams[i].basin_area =
- seg_calculate_basins_area(dirs, stat_streams[i].r, stat_streams[i].c);
- if (stat_streams[i].is_outlet)
- total_basins += stat_streams[i].basin_area;
- }
-
- G_free(fifo_points);
- return 0;
+ seg_calculate_basins_area(dirs, stat_streams[i].r,
+ stat_streams[i].c);
+ if (stat_streams[i].is_outlet)
+ total_basins += stat_streams[i].basin_area;
+ }
+
+ G_free(fifo_points);
+ return 0;
}
-
-
-
Modified: grass-addons/grass7/raster/r.stream/r.stream.stats/stats_print.c
===================================================================
--- grass-addons/grass7/raster/r.stream/r.stream.stats/stats_print.c 2011-04-28 12:10:02 UTC (rev 46126)
+++ grass-addons/grass7/raster/r.stream/r.stream.stats/stats_print.c 2011-04-28 12:12:21 UTC (rev 46127)
@@ -2,9 +2,10 @@
int print_stats(int order_max)
{
-
+
int i;
- fflush(stdout);
+
+ fflush(stdout);
/* summary statistics */
fprintf(stdout, "\n");
fprintf(stdout, "Summary:\n");
@@ -25,24 +26,21 @@
stats_total.reg_bifur_ratio,
stats_total.reg_length_ratio,
stats_total.reg_area_ratio,
- stats_total.reg_slope_ratio,
- stats_total.reg_gradient_ratio);
- fprintf(stdout, "\n");
+ stats_total.reg_slope_ratio, stats_total.reg_gradient_ratio);
+ fprintf(stdout, "\n");
fprintf(stdout, "Avaraged stream ratios with standard deviations:\n");
fprintf(stdout, " Bif.rt. | Len.rt. | Area.rt. | Slo.rt. | Grd.rt. \n");
fprintf(stdout, " %7.4f | %7.4f | %8.4f | %7.4f | %7.4f\n",
stats_total.bifur_ratio,
stats_total.length_ratio,
stats_total.area_ratio,
- stats_total.slope_ratio,
- stats_total.gradient_ratio);
+ stats_total.slope_ratio, stats_total.gradient_ratio);
fprintf(stdout, " %7.4f | %7.4f | %8.4f | %7.4f | %7.4f\n",
stats_total.std_bifur_ratio,
stats_total.std_length_ratio,
stats_total.std_area_ratio,
- stats_total.std_slope_ratio,
- stats_total.std_gradient_ratio);
+ stats_total.std_slope_ratio, stats_total.std_gradient_ratio);
fprintf(stdout, "\n");
/* base parameters */
@@ -99,31 +97,41 @@
return 0;
}
-int print_stats_total(void) {
- fflush(stdout);
- fprintf(stdout,"Catchment's characteristics (based on regresion): \n");
- fprintf(stdout,"Max order: %d \n",stats_total.order);
- fprintf(stdout,"Total number of streams: %d \n",stats_total.stream_num);
- fprintf(stdout,"Total stream length (km): %2.4f \n",stats_total.sum_length / 1000);
- fprintf(stdout,"Total cachment area (km2): %2.4f \n",stats_total.sum_area / 1000000);
- fprintf(stdout,"Drainage density: %2.4f\n",stats_total.drainage_density * 1000);
- fprintf(stdout,"Stream frequency: %2.4f \n",stats_total.stream_frequency * 1000000);
- fprintf(stdout,"Bifurcation ratio: %2.4f \n",stats_total.reg_bifur_ratio);
- fprintf(stdout,"Length ratio: %2.4f \n",stats_total.reg_length_ratio);
- fprintf(stdout,"Area ratio: %2.4f \n",stats_total.reg_area_ratio);
- fprintf(stdout,"Slope ratio: %2.4f \n",stats_total.reg_slope_ratio);
- fprintf(stdout,"Gradient ratio: %2.4f \n",stats_total.reg_gradient_ratio);
- fflush(stdout);
- return 0;
+int print_stats_total(void)
+{
+ fflush(stdout);
+ fprintf(stdout, "Catchment's characteristics (based on regresion): \n");
+ fprintf(stdout, "Max order: %d \n", stats_total.order);
+ fprintf(stdout, "Total number of streams: %d \n", stats_total.stream_num);
+ fprintf(stdout, "Total stream length (km): %2.4f \n",
+ stats_total.sum_length / 1000);
+ fprintf(stdout, "Total cachment area (km2): %2.4f \n",
+ stats_total.sum_area / 1000000);
+ fprintf(stdout, "Drainage density: %2.4f\n",
+ stats_total.drainage_density * 1000);
+ fprintf(stdout, "Stream frequency: %2.4f \n",
+ stats_total.stream_frequency * 1000000);
+ fprintf(stdout, "Bifurcation ratio: %2.4f \n",
+ stats_total.reg_bifur_ratio);
+ fprintf(stdout, "Length ratio: %2.4f \n", stats_total.reg_length_ratio);
+ fprintf(stdout, "Area ratio: %2.4f \n", stats_total.reg_area_ratio);
+ fprintf(stdout, "Slope ratio: %2.4f \n", stats_total.reg_slope_ratio);
+ fprintf(stdout, "Gradient ratio: %2.4f \n",
+ stats_total.reg_gradient_ratio);
+ fflush(stdout);
+ return 0;
}
-int print_stats_orders(int order_max) {
+int print_stats_orders(int order_max)
+{
- int i;
- fflush(stdout);
- fprintf(stdout,"Order's summary: \n");
- fprintf(stdout,"order,num_of_streams,avg_length,avg_area,avg_slope,avg_grad,avg_elev.diff,sum_length,sum_area\n");
+ int i;
+ fflush(stdout);
+ fprintf(stdout, "Order's summary: \n");
+ fprintf(stdout,
+ "order,num_of_streams,avg_length,avg_area,avg_slope,avg_grad,avg_elev.diff,sum_length,sum_area\n");
+
for (i = 1; i <= order_max; ++i) {
fprintf(stdout, "%d,%d,%f,%f,%f,%f,%f,%f,%f\n",
ord_stats[i].order,
@@ -131,11 +139,11 @@
ord_stats[i].avg_length / 1000,
ord_stats[i].avg_area / 1000000,
ord_stats[i].avg_slope,
- ord_stats[i].avg_gradient,
+ ord_stats[i].avg_gradient,
ord_stats[i].avg_elev_diff,
ord_stats[i].sum_length / 1000,
ord_stats[i].sum_area / 1000000);
}
- fflush(stdout);
- return 0;
+ fflush(stdout);
+ return 0;
}
More information about the grass-commit
mailing list