Browse Source

selected r.stream modules: removed r.stream.channel, r.stream.distance, r.stream.order, r.stream.segment, r.stream.slope, r.stream.snap, r.stream.stats as moved back to Addons (https://trac.osgeo.org/grass/changeset/62511 and https://trac.osgeo.org/grass/changeset/62512)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@62560 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Neteler 10 years ago
parent
commit
0a0612f377
68 changed files with 7 additions and 13123 deletions
  1. 0 21
      gui/wxpython/xml/toolboxes.xml
  2. 0 7
      raster/Makefile
  3. 0 10
      raster/r.stream.channel/Makefile
  4. 0 563
      raster/r.stream.channel/io.c
  5. 0 58
      raster/r.stream.channel/io.h
  6. 0 57
      raster/r.stream.channel/local_proto.h
  7. 0 35
      raster/r.stream.channel/local_vars.h
  8. 0 316
      raster/r.stream.channel/main.c
  9. 0 138
      raster/r.stream.channel/r.stream.channel.html
  10. 0 434
      raster/r.stream.channel/stream_topology.c
  11. 0 532
      raster/r.stream.channel/stream_write.c
  12. 0 10
      raster/r.stream.distance/Makefile
  13. 0 628
      raster/r.stream.distance/distance_calc.c
  14. 0 211
      raster/r.stream.distance/distance_init.c
  15. 0 563
      raster/r.stream.distance/io.c
  16. 0 58
      raster/r.stream.distance/io.h
  17. 0 20
      raster/r.stream.distance/local_proto.h
  18. 0 36
      raster/r.stream.distance/local_vars.h
  19. 0 311
      raster/r.stream.distance/main.c
  20. 0 195
      raster/r.stream.distance/r.stream.distance.html
  21. 7 7
      raster/r.stream.extract/r.stream.extract.html
  22. 0 13
      raster/r.stream.order/Makefile
  23. 0 563
      raster/r.stream.order/io.c
  24. 0 58
      raster/r.stream.order/io.h
  25. 0 27
      raster/r.stream.order/local_proto.h
  26. 0 61
      raster/r.stream.order/local_vars.h
  27. 0 286
      raster/r.stream.order/main.c
  28. BIN
      raster/r.stream.order/orders.png
  29. 0 360
      raster/r.stream.order/r.stream.order.html
  30. 0 134
      raster/r.stream.order/stream_init.c
  31. 0 279
      raster/r.stream.order/stream_order.c
  32. 0 134
      raster/r.stream.order/stream_raster_close.c
  33. 0 431
      raster/r.stream.order/stream_topology.c
  34. 0 361
      raster/r.stream.order/stream_vector.c
  35. 0 13
      raster/r.stream.segment/Makefile
  36. BIN
      raster/r.stream.segment/dirs.png
  37. 0 563
      raster/r.stream.segment/io.c
  38. 0 58
      raster/r.stream.segment/io.h
  39. 0 30
      raster/r.stream.segment/local_proto.h
  40. 0 71
      raster/r.stream.segment/local_vars.h
  41. 0 291
      raster/r.stream.segment/main.c
  42. 0 225
      raster/r.stream.segment/r.stream.segment.html
  43. BIN
      raster/r.stream.segment/sectors.png
  44. 0 284
      raster/r.stream.segment/stream_segment.c
  45. 0 573
      raster/r.stream.segment/stream_topology.c
  46. 0 338
      raster/r.stream.segment/stream_vector.c
  47. 0 10
      raster/r.stream.slope/Makefile
  48. 0 408
      raster/r.stream.slope/main.c
  49. 0 81
      raster/r.stream.slope/r.stream.slope.html
  50. 0 13
      raster/r.stream.snap/Makefile
  51. 0 563
      raster/r.stream.snap/io.c
  52. 0 58
      raster/r.stream.snap/io.h
  53. 0 10
      raster/r.stream.snap/local_proto.h
  54. 0 34
      raster/r.stream.snap/local_vars.h
  55. 0 152
      raster/r.stream.snap/main.c
  56. 0 116
      raster/r.stream.snap/points_io.c
  57. 0 133
      raster/r.stream.snap/r.stream.snap.html
  58. 0 143
      raster/r.stream.snap/snap.c
  59. 0 10
      raster/r.stream.stats/Makefile
  60. 0 563
      raster/r.stream.stats/io.c
  61. 0 58
      raster/r.stream.stats/io.h
  62. 0 25
      raster/r.stream.stats/local_proto.h
  63. 0 82
      raster/r.stream.stats/local_vars.h
  64. 0 195
      raster/r.stream.stats/main.c
  65. 0 207
      raster/r.stream.stats/r.stream.stats.html
  66. 0 286
      raster/r.stream.stats/stats_calculate.c
  67. 0 464
      raster/r.stream.stats/stats_prepare.c
  68. 0 149
      raster/r.stream.stats/stats_print.c

+ 0 - 21
gui/wxpython/xml/toolboxes.xml

@@ -857,30 +857,9 @@
         <label>Watershed basin creation</label>
       </module-item>
       <separator/>
-      <module-item name="r.stream.channel">
-        <label>Stream channel characteristics</label>
-      </module-item>
-      <module-item name="r.stream.distance">
-        <label>Stream distance and elevation characteristics</label>
-      </module-item>
       <module-item name="r.stream.extract">
         <label>Extraction of stream networks</label>
       </module-item>
-      <module-item name="r.stream.order">
-        <label>Stream ordering and hierarchy</label>
-      </module-item>
-      <module-item name="r.stream.segment">
-        <label>Stream segment characteristics</label>
-      </module-item>
-      <module-item name="r.stream.slope">
-        <label>Stream elevation change</label>
-      </module-item>
-      <module-item name="r.stream.snap">
-        <label>Position correction of water outlet point</label>
-      </module-item>
-      <module-item name="r.stream.stats">
-        <label>Horton's statistics</label>
-      </module-item>
       <separator/>
       <module-item name="r.sim.water">
         <label>SIMWE Overland flow modeling</label>

+ 0 - 7
raster/Makefile

@@ -95,14 +95,7 @@ SUBDIRS = \
 	r.stats.zonal \
 	r.stats.quantile \
 	r.stats \
-	r.stream.channel \
-	r.stream.distance \
 	r.stream.extract \
-	r.stream.order \
-	r.stream.segment \
-	r.stream.slope \
-	r.stream.snap \
-	r.stream.stats \
 	r.sun \
 	r.sunhours \
 	r.sunmask \

+ 0 - 10
raster/r.stream.channel/Makefile

@@ -1,10 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.channel
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 563
raster/r.stream.channel/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.channel/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 57
raster/r.stream.channel/local_proto.h

@@ -1,57 +0,0 @@
-#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);

+ 0 - 35
raster/r.stream.channel/local_vars.h

@@ -1,35 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-
-#define SQRT2 1.414214
-	
-typedef struct {
-	int stream_num;
-	int number_of_cells;
-	int order;
-	unsigned long int * points;
-	float * elevation;
-	double * distance;
-	unsigned int init_r;
-	unsigned int init_c;
-} STREAM;	
-
-
-#ifdef MAIN
-#	define GLOBAL
-#else
-#	define GLOBAL extern
-#endif
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-
-GLOBAL int nrows, ncols; 
-GLOBAL STREAM* stream_attributes;
-
-GLOBAL struct Cell_head window;

+ 0 - 316
raster/r.stream.channel/main.c

@@ -1,316 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.stream.channel
- * AUTHOR(S):    Jarek Jasiewicz jarekj amu.edu.pl
- *               
- * PURPOSE:      Program calculate some channel properties:
- * 			local elevation change, curvature along stream,
- * 			distance to channel init/join, elevation below channel init, 
- * 			optionally distance to outlet, elevation above outlet;
-        
- * COPYRIGHT:    (C) 2002,2010-2014 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 main(int argc, char *argv[])
-{
-    /*
-       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;
-
-    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]);
-
-    /* initialize module */
-    module = G_define_module();
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    module->description =
-	_("Calculates local parameters for individual streams.");
-
-    in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_stm_opt->key = "stream_rast";
-    in_stm_opt->description = _("Name of input raster map with stream network");
-
-    in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_dir_opt->key = "direction";
-    in_dir_opt->description = _("Name of input raster map with flow direction");
-
-    in_elev_opt = G_define_standard_option(G_OPT_R_ELEV);
-
-    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 = _("Name for output unique stream identifier raster map");
-    out_identifier_opt->guisection = _("Output maps");
-
-    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 = _("Name for output init/join/outlet distance raster map");
-    out_distance_opt->guisection = _("Output maps");
-
-    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 =
-        _("Name for output elevation init/join/outlet difference raster map");
-    out_difference_opt->guisection = _("Output maps");
-
-    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 =
-	_("Name for output mean init/join/outlet gradient of stream raster map");
-    out_gradient_opt->guisection = _("Output maps");
-
-    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 = _("Name for output local stream curvature raster map");
-    out_curvature_opt->guisection = _("Output maps");
-
-    opt_swapsize = G_define_option();
-    opt_swapsize->key = "memory";
-    opt_swapsize->type = TYPE_INTEGER;
-    opt_swapsize->answer = "300";
-    opt_swapsize->description = _("Maximum memory used in memory swap mode (MB)");
-    opt_swapsize->guisection = _("Memory settings");
-
-    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 count (ignored local)");
-
-    flag_segmentation = G_define_flag();
-    flag_segmentation->key = 'm';
-    flag_segmentation->description = _("Use memory swap (operation is slow)");
-    flag_segmentation->guisection = _("Memory settings");
-
-    if (G_parser(argc, argv))	/* parser */
-	exit(EXIT_FAILURE);
-
-    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)
-	G_fatal_error(_("You must select at least one output raster 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 (!segmentation) {
-	MAP map_dirs, map_streams, map_elevation, map_output, map_identifier;
-	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);
-	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_release_map(&map_output);
-    }
-
-
-    if (segmentation) {
-	SEG map_dirs, map_streams, map_elevation, map_output, map_identifier;
-	SEGMENT *streams, *dirs, *elevation, *output, *identifier;
-
-	G_message(_("Calculating segments in direction <%s> (may take some time)..."),
-		  method_name[downstream]);
-
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	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_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);
-	}
-
-	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);
-}

+ 0 - 138
raster/r.stream.channel/r.stream.channel.html

@@ -1,138 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-The module <em>r.stream.channel</em> is prepared to calculate some local properties
-of the stream network. It is supplementary module for <em>r.stream.order</em>, and
-<em>r.stream.distance</em> to investigate channel subsystem. For slope subsystem
-parameters is <em>r.stream.slope</em>. It may use ordered or unordered network. It
-calculate parameters for every segment between it init to outlet/join to the
-next stream. it also may calculate parameters between outlet and segment's init.
-It can calculate parameters for every orders but best results are for these
-orders where order remains unchanged from stream init to natural outlet (Hack
-and Horton ordering).
-<p>
-
-<h2>OPTIONS</h2>
-<dl>
-<dt><b>-d</b></dt>
-<!-- TODO: understand what -d does, see main.c description -->
-<dd>Calculate downstream distance (from current cell DOWNSTREAM to outlet/join).
-Default is upstream (from current cell upstream to init/join.</dd>
-<dt><b>-m</b></dt>
-<dd>Only for very large data sets. Uses segment library to optimize memory
-consumption during analysis</dd>
-<dt><b>stream_rast</b></dt>
-<dd>Stream network: name of input stream map. Map may be ordered according to one
-of the <em>r.stream.order</em> ordering systems as well as unordered (with original stream
-identifiers). Because the streams network produced by <em>r.watershed</em> and
-<em>r.stream.extract</em> may slightly differ in detail it is required to use both stream
-and direction map produced by the same module. Non-stream cell values must be
-set to NULL.</dd>
-<dt><b>direction</b></dt>
-<dd>Flow direction: name of input direction map produced by
-<em>r.watershed</em> or <em>r.stream.extract</em>. If the <em>r.stream.extract</em>
-output map is used, it contains non-NULL values only in places where streams are
-present. NULL cells are ignored; zero and negative values are valid direction data
-if they vary from -8 to 8 (CCW from East in steps of 45 degrees). The direction 
-map shall be of integer type (CELL). The region resolution and map resolution
-must be the same. Also the <em>stream_rast</em> network map must have the same
-resolution. If resolutions differ the module informs about it and stops. Region
-boundary and maps boundary may be different but it may lead to unexpected
-results.</dd>
-<dt><b>elevation</b></dt>
-<dd>Elevation: name of input elevation map. Map can be of type CELL, FCELL or
-DCELL. It is not restricted to resolution of region settings as streams and
-direction.</dd>
-<dt><b>distance</b></dt>
-<dd>Upstream distance of current cell to the init/join. Flag modifications: <br>
-<b>-d:</b> downstream distance of current cell to the join/outlet;<br>
-<b>-l:</b> local distance between current cell and next cell. In most cases cell
-resolution and sqrt2 of cell resolution. useful when projection is LL or NS and
-WE resolutions differs. Flag <b>-d</b> ignored<br>
-<b>-c:</b> distance in cells. Map is written as double. Use <em>r.mapcalc</em> to convert
-to integer. Flags <b>-l</b> and <b>-d</b> ignored.<br>
-</dd>
-<dt><b>difference</b></dt>
-<dd>Upstream elevation difference between current cell to the init/join. It we
-need to calculate parameters different than elevation. If we need to calculate
-different parameters than elevation along streams (for example precipitation or
-so) use necessary map as elevation. Flag modifications: <br>
-<b>-d:</b> downstream difference of current cell to the join/outlet;<br>
-<b>-l:</b> local difference between current cell and next cell. With flag
-calculates difference between previous cell and current cell<br>
-<b>-c:</b> Ignored.
-</dd>
-<dt><b>gradient</b></dt>
-<dd>Upstream mean gradient between current cell and the init/join. Flag
-modifications: <br>
-<b>-d:</b> downstream mean gradient between current cell and the
-join/outlet;<br>
-<b>-l:</b> local gradient between current cell and next cell. Flag <b>-d</b> ignored<br>
-<b>-c:</b> Ignored.
-</dd>
-<dt><b>curvature</b></dt>
-<dd>Local stream course curvature  of current cell. Calculated according
-formula: <i>first_derivative/(1-second_derivative<sup>2</sup>)<sup>3/2</sup></i>
-Flag modifications: <br>
-<b>-d:</b> ignored;<br>
-<b>-l:</b> Ignored.<br>
-<b>-c:</b> Ignored.
-</dd>
-<dt><b>identifier</b></dt>
-<dd> Integer map: In ordered stream network there are more than one segment
-(segment: a part of the network where order remains unchanged) with the same
-order. To identify particular segments (for further analysis) every segment
-receive his unique identifier.</dd>
-</dl>
-
-<h2>EXAMPLE</h2>
-
-This example shows how to visualise the change of gradient map along the main
-stream of the catchment:
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=direction stream=streams
-r.stream.order streams=streams direction=direction hack=hack
-r.stream.channel streams=hack direction=direction elevation=elevation \
-  identifier=stream_identifier distance=stream_distance gradient=stream_gradient
-
-# Eg., 495 is a stream identifier. May be different in different situation
-r.mapcalc "stgrad = if(stream_identifier==495,float(stream_gradient),null())"
-r.mapcalc "stdist = if(stream_identifier==495,float(stream_distance),null())"
-
-# Use R for plotting
-R
-library(spgrass6)
-r=readRAST6(c("stdist","stgrad"),NODATA=-9999)
-p=subset(r@data,!is.na(r@data$dist))
-sorted=p[order(p$stdist),]
-plot(sorted,stdist~stgrad,type="l")
-</pre></div>
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation
-Institute.
-
-<p>
-<i>Last changed: $Date$</i>
-

+ 0 - 434
raster/r.stream.channel/stream_topology.c

@@ -1,434 +0,0 @@
-#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);
-}
-
-int ram_trib_nums(int r, int c, CELL ** streams, CELL ** dirs)
-{				/* calculate number of tributaries */
-
-    int trib_num = 0;
-    int i, j;
-    int next_r, next_c;
-
-    for (i = 1; i < 9; ++i) {
-	if (NOT_IN_REGION(i))
-	    continue;
-
-	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 (trib_num > 1)
-	for (i = 1; i < 9; ++i) {
-	    if (NOT_IN_REGION(i))
-		continue;
-
-	    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)
-		trib_num--;
-	}
-
-    if (trib_num > 5)
-	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 ram_number_of_streams(CELL **streams, CELL **dirs)
-{
-    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;
-}
-
-int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
-			  int number_of_streams)
-{
-    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_num = 1;
-
-    Rast_get_window(&window);
-
-    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_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;
-	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];
-		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 many 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_trib_nums(int r, int c, SEGMENT * streams, SEGMENT * dirs)
-{				/* calculate number of tributaries */
-
-    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;
-
-	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)
-	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 == 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..."));
-    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 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 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;
-
-    /* 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"));
-
-		    SA[stream_num].stream_num = stream_num;
-		    SA[stream_num].init_r = r;
-		    SA[stream_num++].init_c = c;
-		}
-	}
-
-    /* building streamline */
-    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) {
-
-	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 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;
-
-    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;
-}
-
-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;
-
-    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;
-}
-
-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;
-}

+ 0 - 532
raster/r.stream.channel/stream_write.c

@@ -1,532 +0,0 @@
-#include "local_proto.h"
-int ram_set_null_output(DCELL **output)
-{
-    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 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;
-
-    G_debug(3, "ram_calculate_identifiers(): downstream=%d", downstream);
-    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;
-}
-
-int seg_calculate_identifiers(SEGMENT *identifier, int number_of_streams,
-			      int downstream)
-{
-    int r, c;
-    int i, j;
-    STREAM *SA;
-
-    G_debug(3, "seg_calculate_identifiers(): downstream=%d", downstream);
-    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;
-}
-
-
-int ram_calculate_distance(DCELL **output, int number_of_streams,
-			   int downstream)
-{
-    int r, c;
-    double cum_length;
-    int i, j;
-    STREAM *SA;
-
-    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 r, c;
-    double cum_length;
-    int i, j;
-    STREAM *SA;
-
-    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;
-	}
-    }
-
-    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);
-	}
-    }
-
-    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) {
-
-	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;
-}
-
-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) {
-
-	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;
-}
-
-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;
-	    }
-	}
-    }
-
-    return 0;
-}
-
-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);
-	    }
-	}
-    }
-
-    return 0;
-}
-
-int ram_calculate_gradient(DCELL **output, int number_of_streams,
-			   int downstream)
-{
-    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;
-	    }
-	}
-	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 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);
-	    }
-	}
-    }
-
-    return 0;
-}
-
-int ram_calculate_local_gradient(DCELL **output, int number_of_streams,
-				 int downstream)
-{
-    int r, c;
-    int i, j;
-    double elev_diff;
-    STREAM *SA;
-
-    G_debug(3, "ram_calculate_local_gradient(): downstream=%d", downstream);
-    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;
-}
-
-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;
-
-    G_debug(3, "seg_calculate_local_gradient(): downstream=%d", downstream);
-    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;
-}
-
-
-int ram_calculate_local_distance(DCELL **output, int number_of_streams,
-				 int downstream)
-{
-    int r, c;
-    int i, j;
-    STREAM *SA;
-
-    G_debug(3, "ram_calculate_local_distance(): downstream=%d", downstream);
-    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;
-}
-
-int seg_calculate_local_distance(SEGMENT *output, int number_of_streams,
-				 int downstream)
-{
-    int r, c;
-    int i, j;
-    double output_cell;
-    STREAM *SA;
-
-    G_debug(3, "seg_calculate_local_distance(): downstream=%d", downstream);
-    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;
-}
-
-int ram_calculate_curvature(DCELL **output, int number_of_streams,
-			    int downstream)
-{
-    int r, c;
-    int i, j;
-    STREAM *SA;
-    double first_derivative, second_derivative;
-
-    G_debug(3, "ram_calculate_curvature(): downstream=%d", downstream);
-    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;
-}
-
-int seg_calculate_curvature(SEGMENT *output, int number_of_streams,
-			    int downstream)
-{
-    int r, c;
-    int i, j;
-    double output_cell;
-    STREAM *SA;
-    double first_derivative, second_derivative;
-
-    G_debug(3, "seg_calculate_curvature(): downstream=%d", downstream);
-    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;
-}

+ 0 - 10
raster/r.stream.distance/Makefile

@@ -1,10 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.distance
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 628
raster/r.stream.distance/distance_calc.c

@@ -1,628 +0,0 @@
-#include "local_proto.h"
-static int tail, head, fifo_count;
-
-int fifo_insert(POINT point)
-{
-    if (fifo_count == fifo_max)
-        G_fatal_error(_("Circular buffer too small"));
-
-    fifo_points[tail++] = point;
-    if (tail > fifo_max) {
-	G_debug(1, "tail > fifo_max");
-	tail = 0;
-    }
-    fifo_count++;
-    return 0;
-}
-
-POINT fifo_return_del(void)
-{
-    if (head >= fifo_max) {
-	G_debug(1, "head >= fifo_max");
-	head = -1;
-    }
-    fifo_count--;
-
-    return fifo_points[++head];
-}
-
-
-int ram_calculate_downstream(CELL ** dirs, DCELL ** distance,
-			     DCELL ** elevation, OUTLET outlet, int outs)
-{
-
-    int r, c, i, j;
-    int next_r, next_c;
-    POINT n_cell;
-    double cur_dist = 0;
-    double tmp_dist = 0;
-    double target_elev;		/* eleavation at stream or outlet */
-    double easting, northing;
-    double 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.;
-    }
-
-    while (tail != head) {
-	easting = window.west + (c + .5) * window.ew_res;
-	northing = window.north - (r + .5) * window.ns_res;
-
-	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[NR(i)][NC(i)] == j) {	/* countributing cell, reset distance and elevation */
-
-		if (outs) {	/* outlet mode */
-
-		    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);
-			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);
-			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;
-		}
-
-		n_cell.r = next_r;
-		n_cell.c = next_c;
-		n_cell.cur_dist = cur_dist;
-		fifo_insert(n_cell);
-	    }
-	}			/* end for i... */
-
-	n_cell = fifo_return_del();
-	r = n_cell.r;
-	c = n_cell.c;
-	tmp_dist = n_cell.cur_dist;
-	target_elev = n_cell.target_elev;
-
-    }				/* end while */
-    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;
-    double cur_dist = 0;
-    double tmp_dist = 0;
-    double target_elev;		/* eleavation at stream or outlet */
-    double easting, northing;
-    double cell_easting, cell_northing;
-    CELL dirs_cell;
-    DCELL distance_cell, elevation_cell;
-    DCELL 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);
-    }
-
-    while (tail != head) {
-	easting = window.west + (c + .5) * window.ew_res;
-	northing = window.north - (r + .5) * window.ns_res;
-
-	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, reset distance and elevation */
-
-		if (outs) {	/* outlet mode */
-		    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 {		/* 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;
-		}
-
-		n_cell.r = next_r;
-		n_cell.c = next_c;
-		n_cell.cur_dist = cur_dist;
-		fifo_insert(n_cell);
-	    }
-	}			/* end for i... */
-
-	n_cell = fifo_return_del();
-	r = n_cell.r;
-	c = n_cell.c;
-	tmp_dist = n_cell.cur_dist;
-	target_elev = n_cell.target_elev;
-
-    }				/* end while */
-    return 0;
-}
-
-int ram_fill_basins(OUTLET outlet, DCELL ** 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;
-    double stop, val;
-    POINT n_cell;
-
-    tail = 0;
-    head = -1;
-    r = outlet.r;
-    c = outlet.c;
-    val = 1;
-    stop = 0;
-
-    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);
-
-	    if (dirs[next_r][next_c] == j) {	/* countributing cell */
-
-		distance[next_r][next_c] =
-		    (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();
-	r = n_cell.r;
-	c = n_cell.c;
-    }
-
-    return 0;
-}
-
-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;
-    double stop, val;
-    POINT n_cell;
-    CELL dirs_cell;
-    DCELL distance_cell;
-
-    tail = 0;
-    head = -1;
-    r = outlet.r;
-    c = outlet.c;
-    val = 1;
-    stop = 0;
-
-    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);
-		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;
-    }
-
-    return 0;
-}
-
-int ram_calculate_upstream(DCELL ** distance, CELL ** dirs,
-			   DCELL ** elevation, DCELL ** tmp_elevation,
-			   int near)
-{
-    int r, c;
-    int next_r, next_c;
-    double easting, northing;
-    double cell_easting, cell_northing;
-    int i, j, k, d;
-    int done;
-    int counter;
-    int n_inits = 0;
-    double cur_dist;
-    POINT *d_inits;
-    double tmp_dist = 0;
-    double target_elev = 0;
-    size_t elevation_data_size;
-    struct Cell_head window;
-
-    Rast_get_window(&window);
-
-    if (elevation) {
-	elevation_data_size = Rast_cell_size(DCELL_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))
-		    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;
-		}
-	    }
-	    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));
-
-    k = 0;
-    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)
-		    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];
-
-		k++;
-	    }
-	}
-
-    counter = n_inits = k;
-    /* return 0; */
-    G_message(_("Calculate upstream parameters..."));
-    while (n_inits > 0) {
-	k = 0;
-	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];
-	    next_r = NR(d);
-	    next_c = NC(d);
-	    tmp_dist = d_inits[i].cur_dist;
-
-	    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;
-
-	    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;
-	    else
-		done = (distance[next_r][next_c] < cur_dist ||
-			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 (dirs[NR(d)][NC(d)] < 1)
-		    continue;
-
-		d_inits[k].r = next_r;
-		d_inits[k].c = next_c;
-		d_inits[k].cur_dist = cur_dist;
-
-		if (elevation)
-		    d_inits[k].target_elev = target_elev;
-		k++;
-	    }			/* end of if done */
-	}
-	n_inits = k;
-    }
-    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 r, c;
-    int next_r, next_c;
-    double easting, northing;
-    double cell_easting, cell_northing;
-    int i, j, k, d, d_next;
-    DCELL minus_one_cell = -1;
-    DCELL zero_cell = 0;
-    int done;
-    int counter;
-    int n_inits = 0;
-    double cur_dist;
-    POINT *d_inits;
-    double tmp_dist = 0;
-    double target_elev = 0;
-    CELL dirs_cell;
-    DCELL 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(DCELL_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);
-
-	    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 && 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);
-	    }
-
-	}
-
-    d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
-
-    k = 0;
-    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(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);
-		k++;
-	    }
-	}
-
-    counter = n_inits = k;
-
-    G_message(_("Calculate upstream parameters..."));
-    while (n_inits > 0) {
-	k = 0;
-	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);
-	    next_r = NR(d);
-	    next_c = NC(d);
-	    tmp_dist = d_inits[i].cur_dist;
-
-	    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;
-
-	    cur_dist = tmp_dist +
-		G_distance(easting, northing, cell_easting, cell_northing);
-
-	    Segment_get(distance, &distance_cell, next_r, next_c);
-
-	    if (near)
-		done = (distance_cell > cur_dist ||
-			distance_cell <= 0) ? 1 : 0;
-	    else
-		done = (distance_cell < cur_dist ||
-			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));
-		if (dirs_cell < 1)
-		    continue;
-
-		d_inits[k].r = next_r;
-		d_inits[k].c = next_c;
-		d_inits[k].cur_dist = cur_dist;
-
-		if (elevation)
-		    d_inits[k].target_elev = target_elev;
-		k++;
-	    }			/* end of if done */
-	}
-	n_inits = k;
-    }
-    G_percent(1, 1, 1);
-    return 0;
-}

+ 0 - 211
raster/r.stream.distance/distance_init.c

@@ -1,211 +0,0 @@
-#include "local_proto.h"
-
-int ram_find_outlets(CELL ** streams, int number_of_streams, CELL ** dirs,
-		     int subs, int outs)
-{
-    int d;			/* d: direction */
-    int r, c;
-    int next_stream = -1, cur_stream;
-    int out_max = ncols + nrows;
-    int outlets_num;
-
-    G_debug(3, "ram_find_outlets(): number_of_streams=%d", number_of_streams);
-
-    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)
-	    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));
-		}
-
-		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)
-			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;
-		    }
-		}
-		else {
-		    if (next_stream < 0) {	/* is outlet! */
-			outlets[outlets_num].r = r;
-			outlets[outlets_num++].c = c;
-		    }
-		}		/* end lasts */
-	    }			/* end if streams */
-
-    return outlets_num;
-}
-
-
-int seg_find_outlets(SEGMENT * streams, int number_of_streams, SEGMENT * dirs,
-		     int subs, int outs)
-{
-    int d;			/* d: direction */
-    int r, c;
-    int next_stream = -1;
-    int out_max = ncols + nrows;
-    int outlets_num;
-    CELL streams_cell;
-    CELL dirs_cell;
-
-    G_debug(3, "ram_find_outlets(): number_of_streams=%d", number_of_streams);
-
-    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) {
-	    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);
-		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)
-			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;
-		    }
-		}
-		else {
-		    if (next_stream < 0) {	/* is outlet! */
-			outlets[outlets_num].r = r;
-			outlets[outlets_num++].c = c;
-		    }
-		}		/* end lasts */
-	    }			/* end if streams */
-	}			/* end for c */
-    return outlets_num;
-}
-
-int ram_init_distance(CELL ** streams, DCELL ** distance, int outlets_num,
-		      int outs)
-{
-    int r, c, i;
-    /* size_t data_size; 
-
-    data_size = Rast_cell_size(DCELL_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;
-}
-
-int seg_init_distance(SEGMENT * streams, SEGMENT * distance, int outlets_num,
-		      int outs)
-{
-    int r, c, i;
-    CELL streams_cell;
-    DCELL distance_cell;
-    DCELL minus_one_cell = -1;
-    DCELL 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 ram_prep_null_elevation(DCELL ** distance, DCELL ** 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;
-	    }
-
-    return 0;
-}
-
-
-int seg_prep_null_elevation(SEGMENT * distance, SEGMENT * elevation)
-{
-    int r, c;
-    DCELL 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;
-}

+ 0 - 563
raster/r.stream.distance/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.distance/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 20
raster/r.stream.distance/local_proto.h

@@ -1,20 +0,0 @@
-#include "io.h"
-#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, DCELL **distance, int outlets_num, int outs);
-int ram_prep_null_elevation(DCELL **distance, DCELL **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);
-
-/* calculate */
-int ram_calculate_downstream(CELL **dirs, DCELL **distance, DCELL **elevation, OUTLET outlet, int outs);
-int ram_fill_basins(OUTLET outlet, DCELL **distance, CELL **dirs);
-int ram_calculate_upstream(DCELL **distance, CELL **dirs, DCELL **elevation, DCELL **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);

+ 0 - 36
raster/r.stream.distance/local_vars.h

@@ -1,36 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-
-#ifdef MAIN
-#  define GLOBAL
-#else
-#  define GLOBAL extern
-#endif
-
-#define SQRT2 1.414214
-#define UPSTREAM 0
-#define DOWNSTREAM 1
-
-typedef struct {
-	int r,c;
-} OUTLET;
-
-typedef struct {
-	int r,c;
-  double cur_dist;
-  double target_elev;
-} POINT;
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-
-GLOBAL OUTLET* outlets;
-GLOBAL int nrows, ncols;
-GLOBAL int fifo_max;
-GLOBAL POINT* fifo_points;
-GLOBAL int accum;

+ 0 - 311
raster/r.stream.distance/main.c

@@ -1,311 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.stream.distance
- * AUTHOR(S):    Jarek Jasiewicz jarekj amu.edu.pl
- *               
- * PURPOSE:      Calculate distance to and elevation above streams and outlet 
- *               according to user's input data. The elevation and distance are calculated
- *               along watercourses.
- *               It uses any stream map and r.watershed or r.stream.extract direction map.
- *               Stream network input map shall contains streams or points outlets with or
- *               without unique categories.
- *
- * COPYRIGHT:    (C) 2002,2009-2014 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 process_cat(char **cat_list);
-int main(int argc, char *argv[])
-{
-
-    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 j;
-
-    G_gisinit(argv[0]);
-
-    module = G_define_module();
-    module->label = _("Calculates distance to and elevation above streams and outlet.");
-    module->description =
-      _("The module can work in stream mode where target are streams and "
-        "outlets mode where targets are outlets.");
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("watercourse distance"));
-
-    in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_stm_opt->key = "stream_rast";
-    in_stm_opt->description = _("Name of input raster map with stream network "
-                                "(in outlet mode is the name for outlet raster map)");
-
-    in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_dir_opt->key = "direction";
-    in_dir_opt->description = _("Name of input raster map with flow direction");
-
-    in_elev_opt = G_define_standard_option(G_OPT_R_ELEV);
-    in_elev_opt->required = NO;
-    in_elev_opt->guisection = _("Input maps");
-
-    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";
-
-    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 = _("Name for output distance raster map");
-    out_dist_opt->guisection = _("Output maps");
-
-    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 = _("Name for output elevation difference raster map");
-    out_diff_opt->guisection = _("Output maps");
-
-    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 = _("Memory settings");
-
-    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 =
-	_("Calculate parameters for subbasins (ignored in stream mode)");
-
-    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->guisection = _("Memory settings");
-
-    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 raster map"));
-    if (out_diff_opt->answer && !in_elev_opt->answer)
-	G_fatal_error(_("Output elevation difference raster map requires "
-                        "input elevation raster map to be specified"));
-    
-    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();
-
-    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;
-	DCELL **distance;
-	DCELL **elevation = NULL;
-	DCELL **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, DCELL_TYPE);
-
-	streams = (CELL **) map_streams.map;
-	dirs = (CELL **) map_dirs.map;
-	distance = (DCELL **) 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, DCELL_TYPE);
-	    ram_read_map(&map_elevation, in_elev_opt->answer, 0, -1);
-	    elevation = (DCELL **) 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 (out_diff_opt->answer) {
-		ram_create_map(&map_tmp_elevation, DCELL_TYPE);
-		tmp_elevation = (DCELL **) 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);
-
-	}
-	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, DCELL_TYPE, 1,
-			  -1);
-	}
-
-	if (out_dist_opt->answer)
-	    ram_write_map(&map_distance, out_dist_opt->answer, DCELL_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 (segmentation) {
-	G_message(_("Calculating segments in direction <%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;
-
-	number_of_segs = (int)atof(opt_swapsize->answer);
-	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,
-		       DCELL_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,
-			   DCELL_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 (out_diff_opt->answer) {
-		seg_create_map(&map_tmp_elevation, SROWS, SCOLS,
-			       number_of_segs, DCELL_TYPE);
-		tmp_elevation = &map_tmp_elevation.seg;
-	    }
-
-	    for (j = 0; j < outlets_num; ++j)
-		seg_fill_basins(outlets[j], distance, dirs);
-	    seg_calculate_upstream(distance, dirs, elevation, tmp_elevation,
-				   near);
-
-	}
-	else {
-	    G_fatal_error(_("Unrecognised method of processing"));
-	}			/* end methods */
-
-	if (out_dist_opt->answer)
-	    seg_write_map(&map_distance, out_dist_opt->answer, DCELL_TYPE, 1,
-			  -1);
-
-	if (out_diff_opt->answer) {
-	    seg_prep_null_elevation(distance, elevation);
-	    seg_write_map(&map_elevation, out_diff_opt->answer, DCELL_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);
-    }
-
-    G_free(fifo_points);
-    exit(EXIT_SUCCESS);
-}

+ 0 - 195
raster/r.stream.distance/r.stream.distance.html

@@ -1,195 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<p>
-The module <em>r.stream.distance</em> can calculate: the distance to streams; 
-the distance to outlet; the relative elevation above streams; the relative 
-elevation above the outlet. The distance and the elevation are calculated 
-along watercourses.
-
-<p>
-The module may calculate the distance either <em>downstream</em> or 
-<em>upstream</em>. The default is set on downstream. 
-The upstream method calculates the distance to the local maximum or divide.
-Opposite to downstream method, where every cell has one and only one downstream
-cell, in upstream method every cell has usually more than one upstream cell. So
-it is impossible to determine interchangeable path from any cell. 
-
-<p>
-The upstream method offers two alternative modes switched with <em>-n</em> 
-flag: the <em>nearest local maximum/divide</em> is the shortest path to 
-the local maximum; and the <em>farthest maximum/divide</em>
-is the longest path (default option). 
-In hydrological sense, the <em>nearest</em> option means the shortest path
-which a particle of water must run from the divide to reach a particular pixel,
-while the <em>farthest</em> option means the possible longest path.
-
-<p>
-In <em>outlets</em> mode, the module can optionally be used for subbasins.
-
-<p>
-In <em>streams</em> mode (default) it calculates the distance (downstream)
-to the stream network taken in input. In <em>outlets</em> mode there are 
-some additional possibilities. If the <em>subbasin</em> option is set off,
-it calculates the distance only for the outlet (downstream).
-If the <em>subbasin</em> option is set on, it calculates the distance to
-outlet for every subbasin separately. The <em>subbasin</em> option acts 
-similarly to a subbasin mask. The module <em>r.stream.basins</em> can be
-used to prepare the stream network map taken in input by
-<em>r.stream.distance</em>.
-In fact it can be used to individuate basins and subbasins.
-
-<p>
-
-<h2>OPTIONS</h2>
-
-<dl>
-<dt><b>-o</b></dt>
-<dd>Outlets. Downstream method only. Calculate distance to or elevation
-above the outlet instead of streams. It chooses only the last outlet in 
-the network ignoring nodes.</dd>
-
-<dt><b>-s</b></dt>
-<dd>Subbasins. Downstream method only. Calculate distance to or elevation 
-above stream nodes instead of streams. The distance and the elevation 
-difference are relative to elementary subbasins instead of the whole basin.</dd>
-
-<dt><b>-n</b></dt>
-<dd>Near. For upstream method only. Calculate distance to or elevation 
-above the nearest local maximum/divide. With the default option, the 
-distance/elevation is calculated to the farthest possible maximum/divide.</dd>
-
-<dt><b>stream_rast</b></dt>
-<dd>Stream network: name of input stream network map, produced using either
-<em>r.watershed</em> or <em>r.stream.extract</em>. Since stream
-network maps produced by <em>r.watershed</em> and <em>r.stream.extract</em> may
-slightly differ in detail, it is required to use both stream and direction maps
-produced by the same module. Non-stream cell values must be set to NULL.
-Alternatively, in <em>outlet</em> mode, it's the raster map of the outlet.</dd>
-
-<dt><b>direction</b></dt>
-<dd>Flow direction: name of input input raster map with flow direction, 
-produced using either <em>r.watershed</em> or <em>r.stream.extract</em>. 
-If <em>r.stream.extract</em> output map is used, it is non-NULL only
-where streams occur and NULL elsewhere. NULL (nodata) cells are ignored, 
-zero and negative values are valid direction data only if they vary
-from -8 to 8 (CCW from East in steps of 45 degrees). Flow direction map 
-shall be of integer type (CELL). </dd>
-
-<dt><b>elevation</b></dt>
-<dd>Elevation: name of input elevation map. It can be of type CELL, FCELL or
-DCELL. </dd>
-
-<dt><b>method</b></dt>
-<dd>It is possible to calculate the distance with two method: <b>downstream</b>
-from any raster cell to the nearest stream cell / junction cell or outlet or
-<b>upstream</b> from any cell upstream to the nearest maximum or divide.</dd>
-
-<dt><b>difference</b></dt>
-<dd>Name of output map of elevation difference to the target (outlet, node, 
-stream, divide, maximum) along watercoures. The map is of DCELL type.</dd>
-
-<dt><b>distance</b></dt>
-<dd>Name of output map of distance to the target (outlet, node, stream, 
-divide, maximum) along watercoures. The map is of DCELL type.</dd>
-</dl>
-
-<h2>NOTES</h2>
-    
-<!--
-<p>
-If there are more than one point or one stream network and some separate points
-or separate stream networks are in catchment area defined by others it will
-results as in subbasin mode. 
--->
-<!--TODO: I don't understand this sentence, to be rephrased-->
-
-<p>
-In <em>stream</em> mode subbasin options is omitted. Input maps must be in 
-CELL format (default output of <em>r.watershed</em>, <em>r.stream.order</em> 
-and <em>r.stream.extract</em>).
-The distance is calculated in meters, for flat areas not corrected by 
-topography. Distance correction by topography may be done using the
-following <em>r.mapcalc</em> formula:
-
-<div class="code"><pre>
-r.mapcalc expression = "dist_corrected = sqrt(distance^2 + elevation^2)"
-</pre></div>
-
-<p>
-The module can work only if direction map, streams map and region have the same
-settings.
-This is checked by default. If resolutions differ, the module informs about 
-it and stops. Region boundary and maps boundary may differ but it may lead 
-to unexpected results. The elevation map is not affected by this restriction 
-and can have whatever resolution.
-
-<p>
-It is also required that <em>stream_rast</em> and <em>direction</em> maps come
-from the same source, e.g. both from <em>r.stream.extract</em>. If the stream
-network was generated with MFD method also MFD direction map must be used.
-
-<p>
-Probably one of the most important features of <em>r.stream.distance</em> is
-the ability to calculate the distance not only for streams generated by
-<em>r.stream.extract</em>, but also for any integer map, as long ast the
-resolution corresponds to that of <em>direction</em> map. 
-It can be a lake, swamp, depression and lake boundaries even divided into
-smaller fragments each with its own category.
-
-<h2>EXAMPLE</h2>
-
-<div class="code"><pre>
-    
-# Set the region to match with elevation map
-g.region -ap rast=elevation
-
-# Calculate flow direction and stream network
-r.watershed elevation=elevation threshold=10000 drainage=direction stream=streams
-
-# Calculate elevation above and distance to stream network using downstream method
-r.stream.distance stream_rast=streams direction=direction elevation=elevation \
-  method=downstream distance=distance_stream_downstream difference=difference_stream_downstream
-  
-# Calculate elevation above and distance to stream network using upstream method
-r.stream.distance stream_rast=streams direction=direction elevation=elevation \
-  method=upstream distance=distance_stream_upstream difference=difference_stream_upstream
-  
-# Create outlet
-echo "636645,218835" | v.in.ascii -n input=- output=outlet separator=","
-
-# Convert outlet to raster
-v.to.rast input=outlet output=outlet use=cat
-
-# Calculate distance to and elevation above outlet
-r.stream.distance -o stream_rast=outlet direction=direction elevation=elevation \
-  method=downstream distance=distance2outlet difference=difference2outlet
-
-</pre></div>
-
-<h2>SEE ALSO</h2>
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.reclass.html">r.reclass</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation
-Institute.
-
-<p>
-<i>Last changed: $Date$</i>
-

+ 7 - 7
raster/r.stream.extract/r.stream.extract.html

@@ -229,13 +229,13 @@ representation using digital elevation models.</i>
 <em>
 <a href="r.mapcalc.html">r.mapcalc</a>,
 <a href="r.param.scale.html">r.param.scale</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.channel.html">r.stream.channel</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.distance.html">r.stream.distance</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.order.html">r.stream.order</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.segment.html">r.stream.segment</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.slope.html">r.stream.slope</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.snap.html">r.stream.snap</a> (Addon),
+<a href="http://grass.osgeo.org/grass70/manuals/addons/r.stream.stats.html">r.stream.stats</a> (Addon),
 <a href="r.terraflow.html">r.terraflow</a>,
 <a href="r.thin.html">r.thin</a>,
 <a href="r.to.vect.html">r.to.vect</a>,

+ 0 - 13
raster/r.stream.order/Makefile

@@ -1,13 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.order
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB) $(VECTORLIB) $(DBMILIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP) $(VECTORDEP) $(DBMIDEP)
-
-EXTRA_INC = $(VECT_INC)
-EXTRA_CFLAGS = $(VECT_CFLAGS)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 563
raster/r.stream.order/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.order/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 27
raster/r.stream.order/local_proto.h

@@ -1,27 +0,0 @@
-#include "io.h"
-#include "local_vars.h"
-
-/* stream init */
-int stream_init(int min_index_of_stream, int max_index_of_stream);
-int stream_sample_map(char* input_map_name, int number_of_streams, int what);
-
-/* stream topology */
-int ram_stream_topology(CELL** streams, CELL** dirs, int number_of_streams);
-int ram_stream_geometry(CELL** streams, CELL** dirs);
-int seg_stream_topology(SEGMENT* streams, SEGMENT* dirs, int number_of_streams);
-int seg_stream_geometry(SEGMENT* streams, SEGMENT* dirs);
-
-/* stream order */
-int strahler(int* strahler);
-int shreve(int* shreve);
-int horton(const int* strahler, int* horton, int number_of_streams);
-int hack(int* hack, int* topo_dim, int number_of_streams);
-
-/* stream raster close */
-int ram_close_raster_order(CELL** streams, int number_of_streams, int zerofill);
-int seg_close_raster_order(SEGMENT* streams, int number_of_streams, int zerofill);
-
-/* stream vector */
-int ram_create_vector(CELL** streams, CELL** dirs, char* out_vector, int number_of_streams);
-int seg_create_vector(SEGMENT* streams, SEGMENT* dirs, char* out_vector, int number_of_streams);
-int stream_add_table (int number_of_streams);

+ 0 - 61
raster/r.stream.order/local_vars.h

@@ -1,61 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/vector.h>
-#include <grass/dbmi.h>
-
-#ifdef MAIN
-#  define GLOBAL
-#else
-#  define GLOBAL extern
-#endif
-
-typedef enum {o_streams, o_dirs, o_elev, o_accum, input_size } inputs;
-/* eny new oredering systems must be also declared here before orders_size*/
-typedef enum {o_strahler, o_horton, o_shreve, o_hack, o_topo, orders_size } orders;
-
-typedef struct {
-	char* name;
-	int required;
-	char* description;
-} IO;
-
-typedef struct {
-    int stream;	/* topology */
-    int next_stream;
-    int trib_num;
-    int trib[5];
-    int cells_num;
-    int init; /* index to recalculate into r,c*/
-    int outlet; /* index to recalculate into r,c */
-    double length;   /* geometry */ 
-    double accum_length;
-    double distance; /* distance to outlet */
-    double stright; /* use next to calculate sinusoid */
-    double accum;
-    double init_elev;
-    double outlet_elev; /* use next to calculate drop and gradient */
-} STREAM;
-
-GLOBAL char** output_map_names;
-GLOBAL int ** all_orders;
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-GLOBAL int nrows, ncols;
-GLOBAL int use_vector, use_accum;
-
-
-/* stream topo */
-GLOBAL int init_num, outlet_num;
-GLOBAL STREAM* stream_attributes;
-GLOBAL unsigned int* init_streams;
-GLOBAL unsigned int* outlet_streams;
-GLOBAL unsigned long int* init_cells;
-
-/* output vector */
-GLOBAL struct Map_info Out; 

+ 0 - 286
raster/r.stream.order/main.c

@@ -1,286 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:		r.stream.order
- * AUTHOR(S):		Jarek Jasiewicz jarekj amu.edu.pl
- *							 
- * PURPOSE:		Calculate Strahler's and more streams hierarchy
- *			It use r.stream.extract or r.watershed output files: 
- * 			stream, direction, accumulation and elevation. 
- * 			The output are set of raster maps and vector file containing
- * 			additional stream attributes.
- *
- * COPYRIGHT:		(C) 2009-2014 by the GRASS Development Team
- *
- *			This program is free software under the GNU General Public 
- *			License (>=v2). Read the file COPYING that comes with GRASS
- *			for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 main(int argc, char *argv[])
-{
-
-    IO input[] = {
-        {"stream_rast", YES, _("Name of input raster map with stream network")},
-	{"direction", YES, _("Name of input flow direction raster map")},
-	{"elevation", NO, _("Name of input elevation raster map")},
-	{"accumulation", NO, _("Name of input accumulation raster map")}
-    };
-
-    /* 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, _("Name for output Strahler's stream order raster map")},
-	{"horton", NO, _("Name for output original Hortons's stream order raster map")},
-	{"shreve", NO, _("Name for output Shereve's stream magnitude raster map")},
-	{"hack", NO, _("Name for output Hack's streams or Gravelius stream hierarchy raster map")},
-	{"topo", NO, _("Name for output topological dimension of streams raster map")}
-    };
-    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->label = _("Calculates Strahler's and more streams hierarchy.");
-    module->description =
-	_("Basic module for topological analysis of drainage network.");
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("stream order"));
-
-    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_input[i]->guisection = _("Input maps");
-    }
-
-    opt_vector = G_define_standard_option(G_OPT_V_OUTPUT);
-    opt_vector->key = "stream_vect";
-    opt_vector->required = NO;
-    opt_vector->description =
-	_("Name for output vector map to write stream attributes");
-    opt_vector->guisection = _("Output maps");
-
-    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 maps");
-    }
-
-    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 = _("Memory settings");
-
-    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_segmentation->guisection = _("Memory settings");
-
-    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 = 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 map both accumulation and elevation raster maps are required"));
-    if (use_accum)
-	if (!opt_input[o_accum]->answer)
-	    G_fatal_error(_("Flag -a (use accumulation) accumulation raster map is required"));
-
-    for (i = 0; i < orders_size; ++i) {
-	if (!opt_output[i]->answer)
-	    continue;
-	output_num++;
-    }				/* end for */
-
-    if (!output_num && !opt_vector->answer)
-	G_fatal_error(_("You must select one or more output orders raster 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) {
-        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);
-
-	/* 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_strahler] || output_map_names[o_horton] ||
-	    out_vector)
-	    strahler(all_orders[o_strahler]);
-
-	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_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)..."));
-
-	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);
-
-	if (out_vector || output_map_names[o_horton] ||
-	    output_map_names[o_hack] || output_map_names[o_topo])
-	    seg_stream_geometry(streams, dirs);
-
-	/* 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_strahler] || output_map_names[o_horton] ||
-	    out_vector)
-	    strahler(all_orders[o_strahler]);
-
-	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_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)
-	    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);
-
-    exit(EXIT_SUCCESS);
-
-}

BIN
raster/r.stream.order/orders.png


+ 0 - 360
raster/r.stream.order/r.stream.order.html

@@ -1,360 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<p>
-The module <em>r.stream.order</em> calculates Strahler's and other stream
-hierarchy methods. It is a basic module for topological analysis of
-drainage networks.
-
-<h2>NOTES</h2>
-
-The module can work only if <b>direction</b> map, <b>stream_rast</b> 
-map and the computational region have the same settings.
-It is also required that the <b>stream_rast</b> map and the 
-<b>direction</b> map come from the same source. For lots of reason this 
-limitation probably cannot be omitted. This means if <b>stream_rast</b> 
-map comes from
-<em><a href="r.stream.extract.html">r.stream.extract</a></em> also the
-<b>direction</b> map
-from <em><a href="r.stream.extract.html">r.stream.extract</a></em>
-must be used. If stream network was generated with MFD method also MFD
-direction map must be used. Nowadays if direction map comes from
-<em><a href="r.stream.extract.html">r.stream.extract</a></em> must be
-patched by direction map
-from <em><a href="r.watershed.html">r.watershed</a></em> (with 
-<em><a href="r.patch.html">r.patch</a></em>).
-
-<p>
-Input stream network map (<b>stream_rast</b>) may be produced using
-either
-<em><a href="r.watershed.html">r.watershed</a></em>
-or <em><a href="r.stream.extract.html">r.stream.extract</a></em>. Since
-stream network maps produced
-by <em><a href="r.watershed.html">r.watershed</a></em>
-and <em><a href="r.stream.extract.html">r.stream.extract</a></em> may
-slightly differ in detail, it is required to use both stream and
-direction maps produced by the same module. Non-stream cell values
-must be set to NULL.
-
-<p>
-Input <b>direction</b> raster map with flow direction may be produced
-using either <em><a href="r.watershed.html">r.watershed</a></em>
-or <em><a href="r.stream.extract.html">r.stream.extract</a></em>.
-If <em><a href="r.stream.extract.html">r.stream.extract</a></em>
-output map is used, it is non-NULL only where streams occur and NULL
-elsewhere. NULL (nodata) cells are ignored, zero and negative values
-are valid direction data only if they vary from -8 to 8 (CCW from East
-in steps of 45 degrees). Flow direction map shall be of integer type
-(CELL).
-
-<p>
-Optional input flow <b>accumulation</b> map may be produced
-by <em><a href="r.watershed.html">r.watershed</a></em>
-or <em><a href="r.stream.extract.html">r.stream.extract</a></em>. This
-map is an option only if Horton's or Hack's ordering is
-performed. Normally both Horton and Hack ordering is calculated on
-cumulative stream length which is calculated internally. Flow
-accumulation can be used if user wants to calculate the main channel
-as the stream with the highest value of aqccumulation.  Flow
-accumulation map shall be of DCELL type, as is by default produced by
-<em><a href="r.watershed.html">r.watershed</a></em> or converted to
-DCELL with <em><a href="r.mapcalc.html">r.mapcalc</a></em>.
-
-<p>
-If <b>-z</b> is specified than <em>r.stream.order</em> creates
-zero-value background instead of NULL (no-data). For some reason (like
-map algebra calculation) zero-valued background may be required.
-
-<p>
-If <b>-a</b> is specified than <em>r.stream.order</em> uses
-accumulation raster map instead of cumulated stream length to
-determine main branch at bifurcation. Works well only with stream
-network produced with SFD algorithm.
-
-<p>
-Flag <b>-m</b> force to use segment library to optimise memory
-consumption during analysis. Recommended only for very large data
-sets.
-
-<p>
-Input <b>elevation</b> map can be of type CELL, FCELL or DCELL. It is
-used to calculate geometrical properties of the network stored in the
-table.
-
-<p>
-Output <b>stream_vect</b> vector map stores stream network. It has a
-table associated, where stream network topology can be stored. Because
-<em>r.stream.order</em> is prepared to work either
-with <em><a href="r.watershed.html">r.watershed</a></em> or
-<em><a href="r.stream.extract.html">r.stream.extract</a></em>, it may
-be used to create corrected stream vector from 
-<em><a href="r.watershed.html">r.watershed</a></em> results.
-
-<p>
-<em>r.stream.order</em> may produce various output raster
-maps <b>strahler</b> (Strahler's stream order raster
-map), <b>shreve</b> (Shreve's stream magnitude raster
-map), <b>horton</b> (Horton's stream order raster map - requires flow
-accumulation map), <b>hack</b> (Hack's main streams output raster
-map), and <b>topo</b> (topological dimension streams raster map).
-
-<h3>Stream ordering example</h3>
-<center>
-<img src="orders.png" border="1"><br>
-</center>
-
-<h4>Strahler's stream order</h4>
-
-Strahler's stream order is a modification of Horton's streams order
-which fixes the ambiguity of Horton's ordering. In Strahler's
-ordering the main channel is not determined; instead the ordering is
-based on the hierarchy of tributaries. The ordering follows these
-rules:
-
-<ol>
-<li>if the node has no children, its Strahler order is 1.
-<li>if the node has one and only one tributary with Strahler greatest
-order i, and all other tributaries have order less than i, then the
-order remains i.
-<li>if the node has two or more tributaries with greatest order i,
-then the Strahler order of the node is i + 1.
-</ol>
-Strahler's stream ordering starts in initial links which assigns order
-one. It proceeds downstream. At every node it verifies that there are
-at least 2 equal tributaries with maximum order. If not it continues
-with highest order, if yes it increases the node's order by 1 and
-continues downstream with new order.
-
-<h4>Advantages and disadvantages of Strahler's ordering</h4>
-
-Strahler's stream order has a good mathematical background. All
-catchments with streams in this context are directed graphs, oriented
-from the root towards the leaves. Equivalent definition of the
-Strahler number of a tree is that it is the height of the largest
-complete binary tree that can be homeomorphically embedded into the
-given tree; the Strahler number of a node in a tree is equivalent to
-the height of the largest complete binary tree that can be embedded
-below that node. The disadvantage of that methods is the lack of
-distinguishing a main channel which may interfere with the analytical
-process in highly elongated catchments
-
-<h4>Horton's stream ordering</h4>
-
-Horton's stream order applies to the stream as a whole but not to
-segments or links since the order on any channel remains unchanged
-from source till it "dies" in the higher order stream or in the outlet
-of the catchment. The main segment of the catchment gets the order of
-the whole catchment, while its tributaries get the order of their own
-subcatchments. The main difficulties of the Horton's order are
-criteria to be considered to distinguish between "true" first order
-segments and extension of higher order segments. That is the reason
-why Horton's ordering has rather historical sense and is substituted
-by the more unequivocal Strahler's ordering system. There are no
-natural algorithms to order stream network according to Horton'
-paradigm. The algorithm used in <em>r.stream.order</em> requires to first
-calculate Strahler's stream order (downstream) and next recalculate to
-Horton ordering (upstream). To make a decision about proper ordering
-it uses first Strahler ordering, and next, if both branches have the
-same orders it uses flow accumulation to choose the actual link. The
-algorithm starts with the outlet, where the outlet link is assigned
-the corresponding Strahler order. Next it goes upstream and determines
-links according to Strahler ordering. If the orders of tributaries
-differ, the algorithm proceeds with the channel of highest order, if
-all orders are the same, it chooses that one with higher flow length
-rate or higher catchment area if accumulation is used. When it reaches
-the initial channel it goes back to the last undetermined branch,
-assign its Strahler order as Horton order and goes upstream to the
-next initial links. In that way stream orders remain unchanged from
-the point where Horton's order have been determined to the source.
-  
-<h4>Advantages and disadvantages of Horton's ordering</h4> 
-
-The main advantages of Horton's ordering is that it produces natural
-stream ordering with main streams and its tributaries. The main
-disadvantage is that it requires prior Strahler's ordering. In some
-cases this may result in unnatural ordering, where the highest order
-will be ascribed not to the channel with higher accumulation but to
-the channel which leads to the most branched parts of the catchment.
-
-<h4>Shreve's stream magnitude</h4>
-
-That ordering method is similar to Consisted Associated Integers
-proposed by Scheidegger. It assigns magnitude of 1 for every initial
-channel. The magnitude of the following channel is the sum of
-magnitudes of its tributaries. The number of a particular link is the
-number of initials which contribute to it.
-
-<h4>Scheidegger's stream magnitude</h4>
-
-That ordering method is similar to Shreve's stream magnitude. It
-assigns magnitude of 2 for every initial channel. The magnitude of the
-following channel is the sum of magnitudes of its tributaries. The
-number of a particular link is the number of streams -1 contributing
-to it. Consisted Associated Integers (Scheidegger) is available only
-in attribute table. To achieve Consisted Associated Integers
-(Scheidegger) raster the result of Shreve's magnitude is to be
-multiplied by 2:
-
-<div class="code"><pre>
-r.mapcalc expr="scheidegger = shreve * 2.0"
-</pre></div>
-
-<h4>Drwal's stream hierarchy (old style)</h4>
-
-That ordering method is a compromise between Strahler ordering and
-Shreve magnitude. It assigns order of 1 for every initial channel. The
-order of the following channel is calculated according Strahler
-formula, except, that streams which do not increase order of next
-channel are not lost. To increase next channel to the higher order R+1
-are require two channels of order R, or one R and two R-1 or one R,
-and four R-2 or one R, one R-1 and two R-2 etc. The order of
-particular link show the possible value of Strahler'order if the
-network was close to idealised binary tree. Drwal's order is
-aviallable only in attribute table.To achieve Drwal's raster the
-result of Shreve's magnitude is to be recalculated according
-formula: <tt>floor(log(shreve,2))+1</tt>
-
-<div class="code"><pre>
-r.mapcalc expr="drwal = int(log(shreve,2.0)) + 1.0"
-</pre></div>
-
-<h4>Advantages and disadvantages of Drwal's hierarhy</h4> 
-
-The main advantages of Drwal's hierarchy is that it produces natural
-stream ordering with which takes into advantage both ordering and
-magnitude. It shows the real impact of particular links of the network
-run-off. The main disadvantage is that it minimise bifuraction ratio
-of the network.
-
-<h4>Hack's main streams or Gravelius order</h4>
-
-This method of ordering calculates main streams of main catchment and
-every subcatchments. Main stream of every catchment is set to 1, and
-consequently all its tributaries receive order 2. Their tributaries
-receive order 3 etc. The order of every stream remains constant up to
-its initial link. The route of every main stream is determined
-according to the maximum flow length value of particular streams. So
-the main stream of every subcatchment is the longest stream or stream
-with highest accumulation rate if accumulation map is used. In most
-cases the main stream is the longest watercourse of the catchment, but
-in some cases, when a catchment consists of both rounded and elongated
-subcatchments these rules may not be maintained. The algorithm assigns
-1 to every outlets stream and goes upstream according to maximum flow
-accumulation of every branch. When it reaches an initial stream it
-step back to the first unassigned confluence. It assigns order 2 to
-unordered tributaries and again goes upstream to the next initial
-stream. The process runs until all branches of all outlets are
-ordered.
-
-<h4>Advantages and disadvantages of main stream ordering</h4>
-
-The biggest advantage of that method is the possibility to compare and
-analyze topology upstream, according to main streams. Because all
-tributaries of main channel have order of 2, streams can be quickly
-and easily filtered and its proprieties and relation to main stream
-determined. The main disadvantage of that method is the problem with
-the comparison of subcatchment topology of the same
-order. Subcatchments of the same order may be both highly branched and
-widespread in the catchment area and a small subcatchment with only
-one stream.
-
-<h4>Topological dimension streams order</h4>
-
-This method of ordering calculates topological distance of every
-stream from catchment outlet.
-
-<h4>Stream network topology table description connected with vector file</h4>
-
-<ul>
-	<li><tt>cat</tt> integer: category;
-	<li><tt>stream</tt>integer: stream number, usually equal to cat;
-	<li><tt>next_stream</tt> integer: stream to which contribute current
-stream (downstream);
-	<li><tt>prev_streams</tt>; two or more contributing streams (upstream);
-	<li><tt>strahler</tt> integer: Strahler's stream order:
-	<li><tt>horton</tt> integer: Hortons's stream order:
-	<li><tt>shreve</tt> integer: Shreve's stream magnitude;
-	<li><tt>scheidegger</tt> integer: Scheidegger's Consisted Associated
-Integers;
-	<li><tt>drwal</tt> integer: Drwal's stream hierarchy;
-	<li><tt>hack</tt> integer: Hack's main streams or Gravelius order;
-	<li><tt>topo</tt> integer: Topological dimension streams order;
-	<li><tt>length</tt> double precision: stream length;
-	<li><tt>cum_length</tt> double precision: length of stream from source;
-	<li><tt>out_dist</tt> double precision: distance of current stream init
-from outlet;
-	<li><tt>stright</tt> double precision: length of stream as stright line;
-	<li><tt>sinusiod</tt> double precision: fractal dimension: stream
-length/stright stream length;
-	<li><tt>elev_init</tt> double precision: elevation of  stream init;
-	<li><tt>elev_outlet</tt> double precision: elevation of  stream outlet;
-	<li><tt>drop</tt> double precision: difference ttetween stream init and
-outlet + drop outlet;
-	<li><tt>out_drop</tt> double precision: drop at the outlet of the stream;
-	<li><tt>gradient</tt> double precision: drop/length;
-</ul>
-
-<h2>EXAMPLE</h2>
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=direction stream=streams
-r.stream.order stream_rast=streams direction=direction strahler=riverorder_strahler \
-  horton=riverorder_horton shreve=riverorder_shreve hack=riverorder_hack \
-  topo=river_topodim
-
-# vector river network
-r.watershed elevation=elevation threshold=10000 accumulation=accum
-r.stream.order stream_rast=streams direction=direction elevation=elevation \
-  accumulation=accum stream_vect=river_vector
-</pre></div>
-
-<h2>REFERENCES</h2>
-<ul>
-<li>Drwal, J., (1982), <i>Wyksztalecenie i organizacja sieci hydrograficznej jako
-podstawa oceny struktury odplywu na terenach m;odoglacjalnych</i>, Rozprawy i
-monografie, Gdansk 1982, 130 pp (in Polish)
-<li>Hack, J., (1957), <i>Studies of longitudinal stream profiles in Virginia and
-Maryland</i>, 
-U.S. Geological Survey Professional Paper, 294-B
-<li>Horton, R. E. (1945), <i>Erosional development of streams and their drainage
-basins: hydro-physical approach to quantitative morphology</i>, Geological
-Society of America Bulletin 56 (3): 275-370<br>
-Scheidegger A. E., (1966), <i>Statistical Description of River Networks</i>.
-Water Resour. Res., 2(4): 785-790
-<li>Shreve, R.,  (1966),<i>Statistical Law of Stream Numbers</i>, J. Geol.,
-74, 17-37.
-<li>Strahler, A. N. (1952), <i>Hypsometric (area-altitude) analysis of erosional
-topology</i>,Geological Society of America Bulletin 63 (11): 1117-1142
-<li>Strahler, A. N. (1957), <i>Quantitative analysis of watershed
-geomorphology</i>, Transactions of the American Geophysical Union 8 (6):
-913-920.
-<li>Woldenberg, M. J., (1967), <i>Geography and properties of surfaces</i>,
-Harvard Papers in Theoretical Geography, 1: 95-189.
-</ul>
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz
-
-<p>
-<i>Last changed: $Date$</i>
-

+ 0 - 134
raster/r.stream.order/stream_init.c

@@ -1,134 +0,0 @@
-#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;
-
-    if (max_index_of_stream == 0)
-        G_fatal_error(_("Empty stream input raster map"));
-    if (min_index_of_stream < 0)
-	G_fatal_error(_("Stream map has negative values"));
-
-    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;
-	stream_attributes[i].trib[0] = 0;
-	stream_attributes[i].trib[1] = 0;
-	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].length = 0.;
-	stream_attributes[i].accum_length = 0.;
-	stream_attributes[i].distance = 0.;
-	stream_attributes[i].stright = 0.;
-	stream_attributes[i].accum = 0.;
-	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;
-
-    return 0;
-}
-
-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 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);
-
-
-    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);
-
-    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;
-	}
-    }
-    G_free(input_buffer);
-    G_free(pointers_to_stream);
-    return 0;
-}

+ 0 - 279
raster/r.stream.order/stream_order.c

@@ -1,279 +0,0 @@
-/* 
-   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 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..."));
-
-    for (j = 0; j < init_num; ++j) {	/* main loop on inits */
-
-	cur_stream = SA[init_streams[j]].stream;
-	do {			/* we must go at least once, if stream is of first order and is outlet */
-	    max_strahler_num = 1;
-	    max_strahler = 0;
-	    next_stream = SA[cur_stream].next_stream;
-
-	    if (SA[cur_stream].trib_num == 0) {	/* assign 1 for spring stream */
-		strahler[cur_stream] = 1;
-		cur_stream = next_stream;
-		done = 1;
-	    }
-	    else {
-		done = 1;
-
-		for (i = 0; i < SA[cur_stream].trib_num; ++i) {	/* loop for determining strahler */
-		    if (strahler[SA[cur_stream].trib[i]] < 0) {
-			done = 0;
-			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_num = 1;
-		    }
-		    else if (strahler[SA[cur_stream].trib[i]] == max_strahler) {
-			++max_strahler_num;
-		    }
-		}		/* 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 */
-		}
-
-	    }
-	} while (done && next_stream > 0);
-    }				/* end for of main loop */
-    return 0;
-}				/* end strahler */
-
-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 */
-
-    G_message(_("Calculating Shreve's stream magnitude, "
-                "Scheidegger's consistent integer and Drwal's streams hierarchy (old style)..."));
-
-    for (j = 0; j < init_num; ++j) {	/* main loop on inits */
-
-	cur_stream = SA[init_streams[j]].stream;
-	do {			/* we must go at least once, if stream is of first order and is outlet */
-
-	    max_shreve = 0;
-	    next_stream = SA[cur_stream].next_stream;
-
-	    if (SA[cur_stream].trib_num == 0) {	/* assign 1 for spring stream */
-
-		shreve[cur_stream] = 1;
-		cur_stream = next_stream;
-		done = 1;
-
-	    }
-	    else {
-		done = 1;
-
-		for (i = 0; i < SA[cur_stream].trib_num; ++i) {	/* loop for determining shreve */
-		    if (shreve[SA[cur_stream].trib[i]] < 0) {
-			done = 0;
-			break;	/* shreeve is not determined, break for loop */
-		    }
-		    else {
-			max_shreve += shreve[SA[cur_stream].trib[i]];
-		    }
-		}		/* end determining shreve */
-
-		if (done == 1) {
-		    shreve[cur_stream] = max_shreve;
-		    cur_stream = next_stream;	/* if next_stream<0 we in outlet stream */
-		}
-	    }
-
-	} while (done && next_stream > 0);
-    }				/* end main loop */
-    return 0;
-}				/* end shreeve */
-
-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 */
-
-    G_message(_("Calculating Hortons's stream order..."));
-    stack = (int *)G_malloc(stack_max * sizeof(int));
-
-    for (j = 0; j < outlet_num; ++j) {
-	cur_stream = SA[outlet_streams[j]].stream;	/* outlet: init */
-	cur_horton = strahler[cur_stream];
-	stack[0] = 0;
-	stack[1] = cur_stream;
-	top = 1;
-
-	do {			/* on every stream */
-	    max_strahler = 0;
-	    max_accum = 0;
-
-	    if (SA[cur_stream].trib_num == 0) {	/* spring: go back on stack */
-
-		horton[cur_stream] = cur_horton;
-		cur_stream = stack[--top];
-
-	    }
-	    else if (SA[cur_stream].trib_num > 1) {	/* node */
-
-		up_stream = 0;	/* calculating up_stream */
-		for (i = 0; i < SA[cur_stream].trib_num; ++i) {
-		    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;
-			    up_stream = SA[cur_stream].trib[i];
-
-			}
-			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;
-
-				up_stream = SA[cur_stream].trib[i];
-			    }
-			}
-		    }
-		}		/* end determining up_stream */
-
-		if (up_stream) {	/* at least one branch is not assigned */
-		    if (horton[cur_stream] < 0) {
-			horton[cur_stream] = cur_horton;
-		    }
-		    else {
-			cur_horton = strahler[up_stream];
-		    }
-		    cur_stream = up_stream;
-		    stack[++top] = cur_stream;
-
-		}
-		else {		/* all asigned, go downstream */
-		    cur_stream = stack[--top];
-
-		}		/* end up_stream */
-	    }			/* end spring/node */
-	} while (cur_stream);
-    }				/* end for outlets */
-    G_free(stack);
-    return 0;
-}
-
-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;
-    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));
-
-    for (j = 0; j < outlet_num; ++j) {
-
-	cur_stream = SA[outlet_streams[j]].stream;	/* outlet: init */
-	cur_hack = 1;
-	stack[0] = 0;
-	stack[1] = cur_stream;
-	top = 1;
-
-	topo_dim[cur_stream] = top;
-	cur_distance = SA[cur_stream].distance = SA[cur_stream].length;
-	do {
-	    max_accum = 0;
-
-	    if (SA[cur_stream].trib_num == 0) {	/* spring: go back on stack */
-
-		hack[cur_stream] = cur_hack;
-		cur_stream = stack[--top];
-
-	    }
-	    else if (SA[cur_stream].trib_num > 1) {	/* node */
-		up_stream = 0;	/* calculating up_stream */
-
-		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;
-
-			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];
-			}
-		    }
-		}		/* end determining up_stream */
-
-		if (up_stream) {	/* at least one branch is not assigned */
-
-		    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;
-		}
-		else {		/* all asigned, go downstream */
-		    cur_stream = stack[--top];
-
-		}		/* end up_stream */
-	    }			/* end spring/node */
-	} while (cur_stream);
-    }				/* end for outlets */
-    G_free(stack);
-    return 0;
-}

+ 0 - 134
raster/r.stream.order/stream_raster_close.c

@@ -1,134 +0,0 @@
-#include "local_proto.h"
-int ram_close_raster_order(CELL **streams, int number_of_streams,
-			   int zerofill)
-{
-
-    G_debug(3, "ram_close_raster_order(): number_of_streams=%d", number_of_streams);
-
-    G_message("Writing outpout raster maps...");
-    int *output_fd;
-    int r, c, i;
-    CELL *output_buffer, *streams_buffer;
-    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]);
-    }
-
-    /* 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);
-
-	    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 */
-    }
-
-    G_free(output_buffer);
-    for (i = 0; i < orders_size; ++i)
-	G_free(all_orders[i]);
-    G_free(all_orders);
-
-
-    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;
-}
-
-
-int seg_close_raster_order(SEGMENT *streams, int number_of_streams,
-			   int zerofill)
-{
-
-    int *output_fd;
-    int r, c, i;
-    CELL *output_buffer, *streams_buffer;
-    struct History history;
-    size_t data_size;
-
-    G_debug(3, "seg_close_raster_order(): number_of_streams=%d", number_of_streams);
-
-    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(_("Unable to segment read row %d for raster 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);
-
-    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;
-}

+ 0 - 431
raster/r.stream.order/stream_topology.c

@@ -1,431 +0,0 @@
-#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++;
-    }
-
-    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;
-}
-
-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..."));
-
-    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)];
-
-		cur_stream = streams[r][c];
-
-		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;
-
-		    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))
-			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 */
-		}
-
-		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. "
-                                            "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];
-		    }		/* end for i... */
-		}
-	    }			/* end if streams */
-    return 0;
-}
-
-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);
-
-	    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;
-	    }
-
-	    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];
-
-		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;
-}
-
-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;
-
-	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++;
-    }
-
-    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;
-}
-
-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..."));
-
-    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));
-
-		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;
-
-		    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))
-			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 */
-		}
-
-		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. "
-                                            "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;
-		    }		/* end for i... */
-		}
-	    }			/* end if streams */
-	}
-    }				/* end r, c */
-    G_percent(r, nrows, 2);
-    return 0;
-}
-
-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 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;
-	    }
-
-	    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(1, 1, 1);
-    return 0;
-}

+ 0 - 361
raster/r.stream.order/stream_vector.c

@@ -1,361 +0,0 @@
-#include "local_proto.h"
-int ram_create_vector(CELL ** streams, CELL ** dirs, char *out_vector,
-		      int number_of_streams)
-{
-
-    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();
-
-    if (Vect_open_new(&Out, out_vector, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_vector);
-
-    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_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);
-
-	    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 (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(_("Unable to add attribute table to vector map <%s>"), out_vector);
-    Vect_hist_command(&Out);
-    Vect_build(&Out);
-    Vect_close(&Out);
-
-    return 0;
-}
-
-int seg_create_vector(SEGMENT * streams, SEGMENT * dirs, char *out_vector,
-		      int number_of_streams)
-{
-
-    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();
-    
-    if (Vect_open_new(&Out, out_vector, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_vector);
-
-    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_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);
-
-	    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(_("Unable to add attribute table to vector map <%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;
-    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;
-    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,
-					   Vect_subst_var(Fi->database,
-							         &Out));
-    db_set_error_handler_driver(driver);
-
-    /* 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";
-	break;
-    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";
-	break;
-    default:
-	G_fatal_error(_("Error with number of tributuaries"));
-	break;
-    }
-
-    sprintf(buf, "create table %s (%s, %s, %s, %s, %s,"
-            "%s, %s, %s, %s, %s,"                                                     
-            "%s, %s, %s, %s, %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);
-
-    if (db_execute_immediate(driver, &db_sql) != DB_OK) {
-	G_fatal_error("Unable to create table: '%s'", db_get_string(&db_sql));
-    }
-
-    if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
-	G_warning(_("Unable to 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(_("Unable to 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;
-
-	/* 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;
-
-	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;
-	}
-
-	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, "
-                "%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 */
-	    );
-
-	db_set_string(&db_sql, buf);
-
-	if (db_execute_immediate(driver, &db_sql) != DB_OK) {
-            G_fatal_error(_("Unable to inset new row: '%s'"), db_get_string(&db_sql));
-	}
-    }				/* 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;
-}

+ 0 - 13
raster/r.stream.segment/Makefile

@@ -1,13 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.segment
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB) $(VECTORLIB) $(DBMILIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP) $(VECTORDEP) $(DBMIDEP)
-
-EXTRA_INC = $(VECT_INC)
-EXTRA_CFLAGS = $(VECT_CFLAGS)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

BIN
raster/r.stream.segment/dirs.png


+ 0 - 563
raster/r.stream.segment/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.segment/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 30
raster/r.stream.segment/local_proto.h

@@ -1,30 +0,0 @@
-#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);

+ 0 - 71
raster/r.stream.segment/local_vars.h

@@ -1,71 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/vector.h>
-#include <grass/dbmi.h>
-
-#define SQRT2 1.414214
-	
-typedef struct {
-	int stream;
-	int next_stream;
-	int number_of_cells;
-	int order;
-	unsigned long int * points;
-	float * elevation;
-	double * distance;
-	unsigned long int init;
-	unsigned long int outlet; /* outlet is cell from next stream */
-	int last_cell_dir; /* to add outlet to vector */
-	float direction;
-	float length;
-	float stright;
-	float drop;
-	float tangent;
-	float continuation;
-	int number_of_sectors;
-	int* sector_breakpoints; /* index of breakpoints in *points vector */
-	int* sector_cats;
-	float* sector_directions;
-	float* sector_strights;
-	double* sector_lengths;
-	float* sector_drops; /* gradient calculated at the end */
-} STREAM;	
-
-typedef struct {
-	float long_dir_diff;
-	float short_dir_diff;
-	int long_break;
-	int decision;
-} DIRCELLS;
-
-#ifdef MAIN
-#	define GLOBAL
-#else
-#	define GLOBAL extern
-#endif
-
-#ifdef MAIN
-#	define GLOBAL
-#else
-#	define GLOBAL extern
-#endif
-
-#ifndef PI
- #define PI (4*atan(1))
-#endif
-
-#define DEG2RAD(d) ((d)*PI/180)
-#define RAD2DEG(r) ((r)*180/PI)
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-
-GLOBAL int nrows, ncols; 
-GLOBAL STREAM* stream_attributes;
-
-GLOBAL struct Cell_head window;

+ 0 - 291
raster/r.stream.segment/main.c

@@ -1,291 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: r.stream.segment
- * AUTHOR(S): Jarek Jasiewicz jarekj amu.edu.pl
- *							 
- * PURPOSE:	 Calculate geometrical attributes for segments of current order, 
- * 			 divide segments on near straight line portions and 
- * 			 segment orientation and angles between streams and its
- *           tributaries. For stream direction it use algorithm to divide
- *           particular streams of the same order into near-straight line
- *           portions.
- * 				
- *							
- *
- * COPYRIGHT:		 (C) 2002,2010-2014 by the GRASS Development Team
- *
- *			 This program is free software under the GNU General Public
- *			 License (>=v2). Read the file COPYING that comes with GRASS
- *			 for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 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;
-    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 =
-	_("Divides network into near straight-line segments and calculate its order.");
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("stream divide"));
-
-    in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_stm_opt->key = "stream_rast";
-    in_stm_opt->description = _("Name of input raster map with stream network");
-
-    in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_dir_opt->key = "direction";
-    in_dir_opt->description = _("Name of input raster map with flow direction");
-
-    in_elev_opt = G_define_standard_option(G_OPT_R_ELEV);
-
-    out_segment_opt = G_define_standard_option(G_OPT_V_OUTPUT);
-    out_segment_opt->key = "segments";
-    out_segment_opt->description =
-	_("Name for output vector map to write segment attributes");
-
-    out_sector_opt = G_define_standard_option(G_OPT_V_OUTPUT);
-    out_sector_opt->key = "sectors";
-    out_sector_opt->description =
-	_("Name for output vector map to write sector attributes");
-
-    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_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_threshold = G_define_option();
-    opt_threshold->key = "threshold";
-    opt_threshold->label = _("Max angle (degrees) between stream segments");
-    opt_threshold->description = _("Must be > 0");
-    opt_threshold->answer = "160";
-    opt_threshold->type = TYPE_DOUBLE;
-
-    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 = _("Memory setings");
-
-    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)");
-    flag_segmentation->guisection = _("Memory setings");
-
-    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)
-        G_fatal_error(_("Search's length must be > 0"));
-    if (seg_treshold < 0 || seg_treshold > 180)
-	G_fatal_error(_("Threshold must be between 0 and 180"));
-    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();
-
-
-
-    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);
-
-	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_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 = 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);
-
-	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)
-       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);
-}

+ 0 - 225
raster/r.stream.segment/r.stream.segment.html

@@ -1,225 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<h2>OPTIONS</h2>
-<dl>
-<dt><b>-r</b></dt>
-<dd>Directions and azimut output in radians. Default is degrees.</dd>
-
-<dt><b>-m</b></dt>
-<dd>Only for very large data sets. Use segment library to optimize memory
-consumption during analysis</dd>
-
-<dt><b>stream_rast</b></dt>
-<dd>Stream network: name of input stream map. Streams shall be ordered according
-one of the <em>r.stream.order</em> ordering system as well as unordered (with original
-stream identifiers)  Because streams network produced by <em>r.watershed</em> and
-<em>r.stream.extract</em> may slightly differ in detail, it is required to use both stream
-and direction map produced by the same module. Stream background shall have NULL
-value or zero value. Background values of NULL are by default produced by
-<em>r.watershed</em> and <em>r.stream.extract</em>. If not 0 or NULL use <em><a
-href="r.mapcalc.html">r.mapcalc</a></em> to set background values to null.  
-</dd>
-
-<dt><b>direction</b></dt>
-<dd>Flow direction: name of input direction map produced by <em>r.watershed</em> or
-<em>r.stream.extract</em>. If r.stream.extract output map is used, it only has non-NULL
-values in places where streams occur. NULL (nodata) cells are ignored, zero and
-negative values are valid direction data if they vary from -8 to 8 (CCW from
-East in steps of 45 degrees). Direction map shall be of type CELL values. Region
-resolution and map resolution must be the same. 
-Also <em>stream_rast</em> network map must have the same resolution. It is checked by
-default. If resolutions differ the module informs about it and stops. Region
-boundary and maps boundary may be differ but it may lead to unexpected
-results.</dd>
-
-<dt><b>elevation</b></dt>
-<dd>Elevation: name of input elevation map. Map can be of type CELL, FCELL or
-DCELL. It is not restricted to resolution of region settings as stream_rast and
-direction.</dd>
-
-<dt><b>length</b></dt>
-<dd>Integer values indicating the search length (in cells) to determine straight
-line. The longest length parameter the module treats more tolerant local stream
-undulation and inequalities. Default value of 15 is suitable for 30 meters
-DEMs. More detail DEMs may requre longer length.</dd>
-
-<dt><b>skip</b></dt>
-<dd>Integer values indicating the length (in cells) local short segment to skip
-and join them to the longer neighbour. The shortest length parameter the more
-short segments will be produced by the module due to undulation and
-inequalities. Default value of 5 is suitable for 30 meters DEMS. More details
-DEMS may require longer length.</dd>
-
-<dt><b>threshold</b></dt>
-<dd>real value indicates the internal angle between upstream and downstream
-direction to treat actual cell as lying on the straight line. Greater values (up to
-180 degrees) produce more segments. Lower values produce less segments.
-Values below 90 in most cases will not produce any additional segments to those
-resulting from ordering.</dl>
-
-<dl>
-<h2>OUTPUTS</h2>
-<p>The module produces two vector maps: one representing original segments
-(where a segment is a streamline where its order remains unchanged) and the second
-divided into near straight line sectors resulting form segmentation process.
-Most of the segment and sectors attributes are the same as in <em>r.stream.order</em> vector
-output.</p>
-
-<dl>
-<dt><b>segments</b></dt>
-<dd>
-Vector map where every segment has its own category and following attributes:
-<ul>
-<li><b>segment</b>: integer, segment identifier
-<li><b>next_segment</b>: integer, topological next segment identifier
-<li><b>s_order</b>: integer, segment order
-<li><b>next_order</b>: integer, topological next segment order
-<li><b>direction</b>: double precision, full segment direction (0-360)
-<li><b>azimuth</b>: double precision, full segment azimuth (0-180) 
-<li><b>length</b>: double precision, segment length
-<li><b>straight</b>: double precision, length of straight line between segment
-nodes
-<li><b>sinusoid</b>: double precision, sinusoid (length/straight)
-<li><b>elev_min</b>: double precision, minimum elevation (elevation at segment
-start)
-<li><b>elev_max</b>: double precision, maximum elevation (elevation at segment
-end)
-<li><b>s_drop</b>: double precision, difference between start and end of the
-segment
-<li><b>gradient</b>: double precision, drop/length
-<li><b>out_direction</b>: double precision, direction (0-360) of segment end
-sector
-<li><b>out_azimuth</b>: double precision,  azimuth (0-180) of segment end sector
-<li><b>out_length</b>: double precision, length of segment end sector
-<li><b>out_drop</b>: double precision, drop of segment end sector
-<li><b>out_gradient</b>: double precision, gradient of segment end sector
-<li><b>tangent_dir</b>: double precision, direction of tangent in segment outlet
-to the next stream 
-<li><b>tangent_azimuth</b>: double precision, azimuth of tangent in segment
-outlet to the next stream 
-<li><b>next_direction</b>: double precision, direction of next stream in join
-with current segment 
-<li><b>next_azimuth</b>: double precision, azimuth of next stream in join with
-current segment 
-</ul>
-<img src="dirs.png">
-</dd>
-<dt><b>sectors</b></dt>
-<dd>Vector map where every sector has its own category and following attributes:
-<ul>
-<li><b>sector</b>: integer, sector category
-<li><b>segment</b>: integer, segment category (to establish relationship)
-<li><b>s_order</b>: integer, segment order
-<li><b>direction</b>: double precision, sector direction
-<li><b>azimuth</b>: double precision, sector azimuth
-<li><b>length</b>: double precision, sector length
-<li><b>straight</b>: double precision, length of straight line between sector
-nodes
-<li><b>sinusoid</b>: double precision, sinusoid (length/straight)
-<li><b>elev_min</b>: double precision, minimum elevation (elevation at sector
-start)
-<li><b>elev_max</b>: double precision, minimum elevation (elevation at sector
-end)
-<li><b>s_drop</b>: double precision, difference between start and end of the
-sector
-<li><b>gradient</b>: double precision, drop/length
-</ul>
-<img src="sectors.png">
-Relation between segments and sector may be set up by segment key.
-</dd>
-</dl>
-<p>
-The main idea comes from works of Horton (1932) and Howard (1971, 1990). The
-module is designed to investigate network lineaments and calculate angle
-relations between tributaries and its major streams. The main problem in
-calculating directional parameters is that streams usually are not straight
-lines. Therefore as the first step of the procedure, partitioning of streams
-into near-straight-line segments is required.
-<p>
-The segmentation process uses a method similar to the one used by Van & Ventura
-(1997) to detect corners and partition curves into straight lines and gentle
-arcs. Because it is almost impossible to determine exactly straight sections
-without creating numerous very short segments, the division process requires
-some approximation. The approximation is made based on three parameters: (1) the
-downstream/upstream search length, (2) the short segment skipping threshold, and
-(3) the maximum angle between downstream/upstream segments to be considered as a
-straight line. In order to designate straight sections of the streams, the
-algorithm is searching for those points where curves significantly change their
-direction.
-The definition of stream segments depends on the ordering method selected by the
-user, Strahler's, Horton's or Hack's main stream, or the network may remain
-unordered. All junctions of streams to streams of higher order are always split
-points, but for ordered networks, streams of higher order may be divided into
-sections which ignore junctions with streams of lower order. In unordered
-networks all junctions are always split points.
-In extended mode the module also calculates the direction of a stream to its
-higher order stream. If the higher order stream begins at the junction with the
-current stream (Strahler's ordering only) or if the network is unordered, the
-direction is calculated as the direction of the line between junction point and
-downstream point (Howard 1971) within the user-defined global search distance.
-If a higher order stream continues at the junction, its direction is calculated
-as the direction of the tangent line to the stream of higher order at the
-junction point. To avoid local fluctuation, the tangent line is approximated as
-a secant line joining downstream/upstream points at a distance globally defined
-by the search length parameter (1). Such a definition of the angle between
-streams is not fully compatible with Horton's original criterion.
-
-<h2>NOTES</h2>
-<p>
-The module can work only if direction map, stream_rast map and region have the same
-settings. It is also required that stream_rast map and direction map come from the
-same source. For lots of reason this limitation probably cannot be omitted.  
-This means that if stream_rast map comes from <em>r.stream.extract</em>, also direction map from
-<em>r.stream.extract</em> must be used. If stream_rast network was generated with MFD method,
-also MFD direction map must be used.
-
-<h2>EXAMPLE</h2>
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=direction stream=streams
-r.stream.order stream_vect=streams direction=direction strahler=riverorder_strahler
-r.stream.segment stream_rast=riverorder_strahler direction=direction \
-  elevation=elevation segments=river_segment sectors=river_sector
-</pre></div>
-
-<h2>REFERENCES</h2>
-
-<p>Horton, R. E., (1932). Drainage basin characteristics: Am. Geophys. Union
-Trans., (3), 350-361.
-<p>Howard, A.D. (1971). Optimal angles of stream junction: Geometric, Stability
-to capture and Minimum Power Criteria, Water Resour. Res. 7(4), 863-873.
-<p>Howard, A.D. (1990). Theoretical model of optimal drainage networks Water
-Resour. Res., 26(9),  2107-2117.
-<p>Van, W., Ventura, J.A. (1997). Segmentation of Planar Curves into
-Straight-Line Segments and Elliptical Arcs, Graphical Models and Image
-Processing 59(6), 484-494.
-
-<h2>SEE ALSO</h2>
-
-<em>
-
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation
-Institute.
-
-<p>
-<i>Last changed: $Date$</i>
-

BIN
raster/r.stream.segment/sectors.png


+ 0 - 284
raster/r.stream.segment/stream_segment.c

@@ -1,284 +0,0 @@
-#include "local_proto.h"
-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_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;
-}
-
-double calc_drop(float *elevation, int start, int stop)
-{
-    float result;
-
-    result = elevation[start] - elevation[stop];
-    return result < 0 ? 0 : result;
-}
-
-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);
-}
-
-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;
-
-    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;
-
-    number_of_cells = cur_stream->number_of_cells - 1;
-    P = cur_stream->points;
-
-    streamline =
-	(DIRCELLS *) G_malloc((number_of_cells + 1) * sizeof(DIRCELLS));
-
-    /* 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;
-
-	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;
-	}
-    }
-
-    /* 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 attributes */
-    for (i = 0, prev_i = 0; i < number_of_cells + 1; ++i) {
-	if (streamline[i].decision == 1 || i == (number_of_cells - 1)) {
-
-	    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;
-
-	    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); 
-
-     */
-    G_free(streamline);
-    return 0;
-}
-
-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;
-    unsigned int outlet = cur_stream->outlet;
-    int last_cell = cur_stream->number_of_cells - 1;
-    int reached_end = 1;
-
-    G_debug(3, "calc_tangents(): number_streams=%d", number_streams);
-    /*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;
-
-    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) {
-	G_warning(_("Network topology error: cannot identify stream join for stream %d"),
-		  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);
-
-    return 0;
-}

+ 0 - 573
raster/r.stream.segment/stream_topology.c

@@ -1,573 +0,0 @@
-#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);
-}
-
-int ram_trib_nums(int r, int c, CELL ** streams, CELL ** dirs)
-{				/* calculate number of tributaries */
-
-    int trib_num = 0;
-    int i, j;
-    int next_r, next_c;
-
-    for (i = 1; i < 9; ++i) {
-	if (NOT_IN_REGION(i))
-	    continue;
-
-	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 (trib_num > 1)
-	for (i = 1; i < 9; ++i) {
-	    if (NOT_IN_REGION(i))
-		continue;
-
-	    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)
-		trib_num--;
-	}
-
-    if (trib_num > 5)
-	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_trib_nums(int r, int c, SEGMENT *streams, SEGMENT *dirs)
-{				/* calculate number of tributaries */
-
-    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;
-
-	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)
-	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 == 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"));
-    if (trib_num > 3)
-	G_warning(_("Stream network may be too dense"));
-
-    return trib_num;
-}				/* end trib_num */
-
-
-int ram_number_of_streams(CELL **streams, CELL **dirs, int *ordered)
-{
-    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)
-			one++;
-		    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;
-}
-
-int seg_number_of_streams(SEGMENT *streams, SEGMENT *dirs, int *ordered)
-{
-    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)
-		if (seg_trib_nums(r, c, streams, dirs) != 1) {
-		    stream_num++;
-		    if (streams_cell == 1)
-			one++;
-		    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;
-}
-
-int ram_build_streamlines(CELL **streams, CELL **dirs, FCELL **elevation,
-			  int number_of_streams)
-{
-    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;
-
-    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) {
-
-
-	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+ */
-    }
-
-    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 many 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 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;
-
-    /* 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"));
-
-		    SA[stream_num].stream = stream_num;
-		    SA[stream_num].init = INDEX(r, c);
-		    stream_num++;
-		}
-	}
-
-    /* building 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;
-
-	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) {
-
-	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 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;
-	}
-    }
-
-    return j;
-}
-
-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;
-
-    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;
-}
-
-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;
-}
-
-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;
-}
-
-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;
-    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 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 broader cell direction */
-    if (dir)
-	return dir;
-
-    if (r == 0 && c == 0)
-	return -3;
-    else if (r == 0 && c == ncols - 1)
-	return -1;
-    else if (r == nrows - 1 && c == ncols - 1)
-	return -7;
-    else if (r == nrows - 1 && c == 0)
-	return -5;
-    else if (r == 0)
-	return -2;
-    else if (r == nrows - 1)
-	return -6;
-    else if (c == 0)
-	return -4;
-    else if (c == ncols - 1)
-	return -8;
-    else
-	return 0;
-}

+ 0 - 338
raster/r.stream.segment/stream_vector.c

@@ -1,338 +0,0 @@
-#include "local_proto.h"
-
-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 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();
-    
-    if (Vect_open_new(&Out, out_vector, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_vector);
-
-    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);
-
-    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, "
-            "segment integer, "
-            "sector integer, "
-            "s_order integer, "
-            "direction double precision, "
-            "azimuth double precision, "
-            "length double precision, "
-            "stright double precision, "
-            "sinusoid double precision, "
-            "elev_min double precision, "
-            "elev_max double precision, "
-            "s_drop double precision, "
-            "gradient double precision)", Fi->table, cat_col_name);
-    
-    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(_("Unable to create table: '%s'"), db_get_string(&db_sql));
-    }
-
-    if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
-        G_warning(_("Unable to create 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_fatal_error(_("Unable to grant privileges on table <%s>"), Fi->table);
-
-    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];
-
-	    /* 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 */
-            
-	    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(_("Unable to 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);
-
-    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 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();
-    
-    if (Vect_open_new(&Out, out_vector, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_vector);
-
-    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);
-
-    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, "
-            "segment integer, "
-            "next_segment integer, "
-            "s_order integer, "
-            "next_order integer, "
-            "direction double precision, "
-            "azimuth double precision, "
-            "length double precision, "
-            "stright double precision, "
-            "sinusoid double precision, "
-            "elev_min double precision, "
-            "elev_max double precision, "
-            "s_drop double precision, "
-            "gradient double precision, "
-            "out_direction double precision, "
-            "out_azimuth double precision, "
-            "out_length double precision, "
-            "out_drop double precision, "
-            "out_gradient double precision, "
-            "tangent_dir double precision, "
-            "tangent_azimuth double precision, "
-            "next_direction double precision, "
-            "next_azimuth double precision)", Fi->table, cat_col_name);
-    
-    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(_("Unable to create table '%s'"), db_get_string(&db_sql));
-    }
-
-    if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
-	G_warning(_("Unable to 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_fatal_error(_("Unable 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);
-	}
-
-	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 */
-
-	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(_("Unable to insert 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);
-
-    Vect_hist_command(&Out);
-    Vect_build(&Out);
-    Vect_close(&Out);
-    return 0;
-}

+ 0 - 10
raster/r.stream.slope/Makefile

@@ -1,10 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.slope
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 408
raster/r.stream.slope/main.c

@@ -1,408 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.stream.slope
- * AUTHOR(S):    Jarek Jasiewicz jarekj amu.edu.pl
- *               
- * PURPOSE:      Supplementary module for r.stream.distance for slope subsystem, 
- * 		 calculate local downstream elevation change 
- * 		 and local downstream minimum and maximum curvature
-        
- * COPYRIGHT:    (C) 2002, 2010-2014 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	         for details.
- *
- *****************************************************************************/
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/glocale.h>
-#define MAIN
-
-
-#define SQRT2 1.414214
-#define NR(x) r + nextr[(x)]
-#define NC(x) c + nextc[(x)]
-#define DIAG(x) (((x) + 4) > 8 ? ((x) - 4) : ((x) + 4))
-#define NOT_IN_REGION(x) (r+nextr[(x)] < 0 || r+nextr[(x)] > 2 || c+nextc[(x)] < 0 || c+nextc[(x)] > (ncols-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 nrows, ncols;
-CELL **dir_rows;
-DCELL **elev_rows;
-struct Cell_head window;
-
-DCELL calculate_difference(int r, int c);
-DCELL calculate_gradient(int r, int c);
-DCELL calculate_max_curvature(int r, int c);
-DCELL calculate_min_curvature(int r, int c);
-
-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; */
-
-    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();
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("stream local parameters"));
-    module->description = _("Calculates local parameters for slope subsystem.");
-
-    in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_dir_opt->key = "direction";
-    in_dir_opt->description = _("Name of input raster map with flow direction");
-
-    in_elev_opt = G_define_standard_option(G_OPT_R_INPUT);
-
-    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 =
-      _("Name for output local downstream elevation difference raster map");
-    out_differnce_opt->guisection = _("Output maps");
-
-    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 = _("Name for output local downstream gradient raster map");
-    out_gradient_opt->guisection = _("Output maps");
-
-    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 =
-        _("Name for output local downstream maximum curvature raster map");
-    out_max_curv_opt->guisection = _("Output maps");
-
-    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 =
-	_("Name for output local downstream minimum curvature raster map");
-    out_min_curv_opt->guisection = _("Output maps");
-
-    if (G_parser(argc, argv))	/* parser */
-	exit(EXIT_FAILURE);
-
-    /* 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_fatal_error(_("Region resolution and raster 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(_("Raster <%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();
-
-    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();
-    }
-
-    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 (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];
-
-	    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);
-
-	    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);
-
-    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_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_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];
-
-    if (NOT_IN_REGION(d))
-	return 0.;
-    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))
-	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;
-}
-
-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))
-	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);
-
-}
-
-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))
-	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);
-
-    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);
-
-}

+ 0 - 81
raster/r.stream.slope/r.stream.slope.html

@@ -1,81 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<em>r.stream.slope</em> calculates local downstream elevation change 
-and local downstream minimum and maximum curvature.
-
-<h2>OPTIONS</h2>
-
-<dl>
-<dt><em>direction</em></dt>
-<dd>Flow direction: name of input direction map produced by <em>r.watershed</em> or
-<em>r.stream.extract</em>. If r.stream.extract output map is used, it only has non-NULL
-values in places where streams occur. NULL (nodata) cells are ignored, zero and
-negative values are valid direction data if they vary from -8 to 8 (CCW from
-East in steps of 45 degrees). Direction map shall be of type CELL values. Region
-resolution and map resolution must be the same.</dd>
-
-<dt><em>elevation</em></dt>
-<dd>Elevation: name of input elevation map or any other map we want to calculate. 
-Map can be of type CELL, FCELL or DCELL. It is not restricted to resolution of
-region settings like <em>direction</em>. </dd>
-
-<dt><em>difference</em></dt>
-<dd>Output downstream elevation difference: difference between elevation of current
-cell and downstream cell. Shall always be positive. Negative values show, that
-current cell is pit or depression cell. Module is prepared to be used with
-elevation but can be also used to calculate local difference of any feature
-along watercourses in slope subsystem. In that way elevation map must be
-replaced by map we want to calculate. If we use different map than elevation,
-rest of parameters have no sense to calculate</dd>
-
-<dt><em>gradient</em></dt>
-<dd>Output downstream gradient: Downstream elevation difference divided by
-distance.</dd>
-
-<dt><em>maxcurv</em></dt>
-<dd>Output maximum linear curvature along watercourse. Calculated along watercourse
-between highest upstream cell, current cell and downstream cell (there can be
-only one or no downstream cell but more than on upstream)</dd>
-
-<dt><em>mincurv</em></dt>
-<dd>Output minimum linear curvature along watercourse.
-Calculated along watercourse between lowest upstream cell, current cell and
-downstream cell (there can be only one or no downstream cell but more than on
-upstream)</dd>
-</dl>
-
-<h2>EXAMPLE</h2>
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=dirs stream=streams
-r.stream.slope dir=dirs elevation=elevation difference=downstream_elev_difference \
-  gradient=downstream_gradient maxcurv=downstream_maxcurv mincurv=downstream_mincurv
-</pre></div>
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation
-Institute.
-
-<p>
-<i>Last changed: $Date$</i>

+ 0 - 13
raster/r.stream.snap/Makefile

@@ -1,13 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.snap
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB) $(VECTORLIB) $(DBMILIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP) $(VECTORDEP) $(DBMIDEP)
-
-EXTRA_INC = $(VECT_INC)
-EXTRA_CFLAGS = $(VECT_CFLAGS)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 563
raster/r.stream.snap/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.snap/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 10
raster/r.stream.snap/local_proto.h

@@ -1,10 +0,0 @@
-#include "io.h"
-#include "local_vars.h"
-
-/* snap.c */
-int create_distance_mask(int);
-int read_points(char *, SEGMENT *, SEGMENT *);
-int snap_point(OUTLET *, int, SEGMENT *, SEGMENT *, double);
-
-/* points_io.c */
-int write_points(char *, int);

+ 0 - 34
raster/r.stream.snap/local_vars.h

@@ -1,34 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-#include <grass/vector.h>
-#include <grass/dbmi.h>
-
-#ifdef MAIN
-#  define GLOBAL
-#else
-#  define GLOBAL extern
-#endif
-
-#define SQR(x) ((x) * (x))
-
-typedef struct
-{
-    int r, c;
-    int di, dj;			/* shift */
-    int cat;
-    double accum;
-    int stream;
-    int status;			/* 1=skipped,2=unresolved,3=snapped,4=correct */
-} OUTLET;
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-
-GLOBAL OUTLET *points;
-GLOBAL int nrows, ncols;
-GLOBAL float **distance_mask;

+ 0 - 152
raster/r.stream.snap/main.c

@@ -1,152 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.stream.snap
- * AUTHOR(S):    Jarek Jasiewicz jarekj amu.edu.pl
- *               
- * PURPOSE:      Snap points features to nearest pour points. Useful both to
- *               snap outlets as well as sources. It uses two parameters: 
- *               maximum snap distance and minimum accumulation value to snap
- *               
- *
- * COPYRIGHT:    (C) 2002,2010-2014 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 main(int argc, char *argv[])
-{
-
-    struct GModule *module;
-    struct Option *in_points_opt,
-	*out_points_opt,
-	*in_stream_opt,
-	*in_accum_opt,
-	*opt_accum_treshold, *opt_distance_treshold, *opt_swapsize;
-
-    int i;
-    SEG map_streams, map_accum;
-    SEGMENT *streams = NULL, *accum = NULL;
-    int number_of_segs;
-    int number_of_points;
-    int radius;
-    double accum_treshold;
-
-    G_gisinit(argv[0]);
-
-    module = G_define_module();
-    module->label = _("Snap point to modelled stream network.");
-    module->description = _("Input can be stream network, point vector map with outlets or outlet coordinates.");
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("basins creation"));
-
-    in_points_opt = G_define_standard_option(G_OPT_V_INPUT);
-    in_points_opt->description = _("Name of input vector points map");
-
-    out_points_opt = G_define_standard_option(G_OPT_V_OUTPUT);
-    out_points_opt->description = _("Name for output vector points map");
-
-    in_stream_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_stream_opt->key = "stream_rast";
-    in_stream_opt->required = NO;
-    in_stream_opt->description = _("Name of input raster map with stream network");
-    in_stream_opt->guisection = _("Input maps");
-
-    in_accum_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_accum_opt->key = "accumulation";
-    in_accum_opt->required = NO;
-    in_accum_opt->description = _("Name of input accumulation raster map");
-    in_accum_opt->guisection = _("Input maps");
- 
-    opt_accum_treshold = G_define_option();
-    opt_accum_treshold->key = "threshold";
-    opt_accum_treshold->type = TYPE_DOUBLE;
-    opt_accum_treshold->answer = "-1";
-    opt_accum_treshold->description =
-	_("Minimum accumulation threshold to snap");
-
-    opt_distance_treshold = G_define_option();
-    opt_distance_treshold->key = "radius";
-    opt_distance_treshold->answer = "1";
-    opt_distance_treshold->type = TYPE_INTEGER;
-    opt_distance_treshold->description =
-	_("Maximum distance to snap (in cells)");
-
-    opt_swapsize = G_define_option();
-    opt_swapsize->key = "memory";
-    opt_swapsize->type = TYPE_INTEGER;
-    opt_swapsize->answer = "300";
-    opt_swapsize->required = NO;
-    opt_swapsize->description = _("Max memory used (MB)");
-    opt_swapsize->guisection = _("Memory settings");
-    
-    if (G_parser(argc, argv))	/* parser */
-	exit(EXIT_FAILURE);
-
-    number_of_segs = (int)atof(opt_swapsize->answer);
-    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);
-
-    nrows = Rast_window_rows();
-    ncols = Rast_window_cols();
-
-    if (G_legal_filename(out_points_opt->answer) < 0)
-	G_fatal_error(_("<%s> is an illegal basin name"),
-		      out_points_opt->answer);
-
-    if (!in_stream_opt->answer && !in_accum_opt->answer)
-	G_fatal_error(_("At least one of accumulation or streams raster maps is required"));
-
-    if (!in_accum_opt->answer)
-	accum_treshold = -1;
-
-
-    /* SEGMENT VERSION ONLY */
-
-    if (in_stream_opt->answer) {
-	seg_create_map(&map_streams, SROWS, SCOLS, number_of_segs, CELL_TYPE);
-	seg_read_map(&map_streams, in_stream_opt->answer, 1, CELL_TYPE);
-	streams = &map_streams.seg;
-    }
-
-    if (in_accum_opt->answer) {
-	seg_create_map(&map_accum, SROWS, SCOLS, number_of_segs, DCELL_TYPE);
-	seg_read_map(&map_accum, in_accum_opt->answer, 0, -1);
-	accum = &map_accum.seg;
-    }
-
-    create_distance_mask(radius);
-    number_of_points = read_points(in_points_opt->answer, streams, accum);
-
-    for (i = 0; i < number_of_points; ++i)
-	snap_point(&points[i], radius, streams, accum, accum_treshold);
-
-    write_points(out_points_opt->answer, number_of_points);
-
-
-    /*
-    for (i = 0; i < number_of_points; ++i)
-	G_message("AFTER %d %d %d %d",
-		  points[i].r, points[i].c, points[i].di, points[i].dj);
-    */
-
-    if (in_stream_opt->answer)
-	seg_release_map(&map_streams);
-    if (in_accum_opt->answer)
-	seg_release_map(&map_accum);
-
-    exit(EXIT_SUCCESS);
-}

+ 0 - 116
raster/r.stream.snap/points_io.c

@@ -1,116 +0,0 @@
-#include "local_proto.h"
-
-int read_points(char *in_point, SEGMENT * streams, SEGMENT * accum)
-{
-    struct Cell_head window;
-    struct Map_info Map;
-    struct bound_box box;
-    int num_point = 0;
-    int total_points = 0;
-    int type, i, j, cat;
-    struct line_pnts *sites;
-    struct line_cats *cats;
-    double absaccum;
-
-    sites = Vect_new_line_struct();
-    cats = Vect_new_cats_struct();
-
-    if (Vect_open_old(&Map, in_point, "") < 0)
-	G_fatal_error(_("Unable to open vector map <%s>"), in_point);
-    
-    G_get_window(&window);
-    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))
-	    num_point++;
-    }
-
-    points = (OUTLET *) G_malloc(num_point * sizeof(OUTLET));
-    total_points = Vect_get_num_lines(&Map);
-    i = 0;
-
-    for (j = 0; j < total_points; ++j) {
-
-	type = Vect_read_line(&Map, sites, cats, j + 1);
-
-	if (type != GV_POINT)
-	    continue;
-
-	if (!Vect_point_in_box(sites->x[0], sites->y[0], sites->z[0], &box))
-	    continue;
-
-	Vect_cat_get(cats, 1, &cat);
-
-	points[i].r = (int)Rast_northing_to_row(sites->y[0], &window);
-	points[i].c = (int)Rast_easting_to_col(sites->x[0], &window);
-	points[i].di = 0;
-	points[i].dj = 0;
-	points[i].cat = cat;
-	if (streams)
-	    Segment_get(streams, &points[i].stream, points[i].r, points[i].c);
-	else
-	    points[i].stream = 0;
-	if (accum) {
-	    Segment_get(accum, &absaccum, points[i].r, points[i].c);
-	    points[i].accum = fabs(absaccum);
-	}
-	else {
-	    points[i].accum = 0;
-	    points[i].status = 4;	/* default status is 'correct' */
-	}
-	//dodać skip category
-
-	i++;
-    }
-    return num_point;
-}
-
-int write_points(char *out_vector, int number_of_points)
-{
-
-    int i;
-    int r, c;
-    int cat_layer_1, cat_layer_2;
-    float northing, easting;
-    struct Cell_head window;
-    struct Map_info Out;
-    struct line_pnts *Segments;
-    struct line_cats *Cats;
-
-    G_get_window(&window);
-    Segments = Vect_new_line_struct();
-    Cats = Vect_new_cats_struct();
-    
-    if (Vect_open_new(&Out, out_vector, 0) < 0)
-	G_fatal_error(_("Unable to create vector map <%s>"), out_vector);
-
-    Vect_reset_line(Segments);
-    Vect_reset_cats(Cats);
-
-    for (i = 0; i < number_of_points; ++i) {
-
-	r = points[i].r + points[i].di;
-	c = points[i].c + points[i].dj;
-
-	cat_layer_1 = points[i].cat;
-	cat_layer_2 = points[i].status;
-	Vect_cat_set(Cats, 1, cat_layer_1);
-	Vect_cat_set(Cats, 2, cat_layer_2);
-	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);
-    }
-
-    /* build vector */
-    Vect_hist_command(&Out);
-    Vect_build(&Out);
-    Vect_close(&Out);
-
-    return 0;
-}

+ 0 - 133
raster/r.stream.snap/r.stream.snap.html

@@ -1,133 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<p>
-The module <em>r.stream.snap</em> is a supplementary module for
-<em><a href="r.stream.extract.html">r.stream.extract</a></em> and
-<em><a href="r.stream.basins.html">r.stream.basins</a></em> to correct
-the position of outlets or stream initial points as they do not lie on 
-the streamlines.
-
-<p>
-For the outlet, the point is snapped to the nearest point which lies on 
-the streamline. 
-
-<p>
-For the stream initial points, when there is a small tributary near 
-the main stream, the accumulation threshold shall be high enough to 
-force the program ignoring this tributary and snap to the main stream. 
-If there is no accumulation map, the points will be snapped to the 
-nearest stream line, which in particular situations may be wrong. 
-Because the <em>r.stream.*</em> modules are prepared to work with MFD 
-accumulation maps, both stream network and accumulation map are 
-necessary.
-
-<p>
-While it is assumed that the accumulation map is a MFD map, if the
-stream network is not supplied, the snap point is calculated in
-different way: the threshold is used to select only those points in the
-search radius which have accumulation value greater than the given
-threshold. The next mean value of these points is calculated and its
-value is taken as a new threshold. This procedure guarantees that
-points are snapped to the center of the stream tube. While for inits 
-small thresholds are in use, it is probable that points were snapped to 
-the stream tube border instead of its center.
-
-<p>
-It is strongly recommended to use both stream network (even
-pre-generated with small accumulation threshold) and accumulation
-raster map, than accumulation or stream raster map only.
-
-<h2>OPTIONS</h2>
-
-<dl>
-<dt><b>stream_rast</b></dt>
-<dd>Stream network created
-by <em><a href="r.stream.extract.html">r.stream.extract</a></em> or
-<em><a href="r.watershed.html">r.watershed</a></em>. If used, the
-points are snapped to the nearest streamline point whose accumulation
-is greater than the threshold. If the accumulation is not used, the 
-point is snapped to the nearest stream.
-</dd>
-
-<dt><b>accumulation</b></dt>
-<dd>Accumulation map created with 
-<em><a href="r.watershed.html">r.watershed</a></em> and used to
-generate the stream network with 
-<em><a href="r.stream.extract.html">r.stream.extract</a></em>. If
-the stream network is not used, the point is adaptively snapped to the
-point where the value is greater than mean values of accumulation
-greater than given threshold in a search radius. See the description for
-details.
-</dd>
-
-<dt><b>radius</b></dt>
-<dd>Search radius (in cells). If there are no streams in the search
-radius, the point is not snapped. If there are no cells with
-accumulation greater than accumulation threshold, the point also is
-not snapped.
-</dd>
-
-<dt><b>threshold</b></dt>
-<dd>Minimum value of accumulation to snap the point. This option is 
-added to the snap stream inits to the stream tubes
-and to distinguish between local tributaries and main streams.
-</dd>
-
-<dt><b>input</b></dt>
-<dd>Vector file containing outlets or inits as vector points. Only
-point's categories are used. Any table attached to it is
-ignored. Every point shall have its own unique category.
-</dd>
-
-<dt><b>output</b></dt>
-<dd>Vector file containing outlets or inits after snapping. On layer 1,
-the original categories are preserved, on layer 2 there are four
-categories which mean:
-<ol>
-<li>skipped (not in use yet)
-<li>unresolved (points remain unsnapped due to lack of streams in search radius
-<li>snapped (points snapped to streamlines)
-<li>correct (points which remain on their original position, which were
-originally corrected)
-</ol>
-</dd>
-</dl>
-
-<h2>EXAMPLE</h2>
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=dirs stream=streams accumulation=accum
-# snap a point sampled in the riverine landscape to the calculated river network
-r.stream.snap input=mysampleoutlet output=mysampleoutlet_snapped stream_rast=streams accumulation=accum
-</pre></div>
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.patch.html">r.patch</a>,
-<a href="r.reclass.html">r.reclass</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.stats.html">r.stream.stats</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation
-Institute.
-
-<p>
-<i>Last changed: $Date$</i>
-

+ 0 - 143
raster/r.stream.snap/snap.c

@@ -1,143 +0,0 @@
-#include "local_proto.h"
-
-#define SQRT(x) ((x) * (x))   /* ??? */
-
-int create_distance_mask(int radius)
-{
-
-    int i, j;
-    int window = 2 * radius + 1;
-
-    distance_mask = G_malloc(window * sizeof(float *));
-
-    for (i = 0; i < window; i++)
-	distance_mask[i] = G_malloc(window * sizeof(float));
-
-    for (i = 0; i < window; i++)
-	for (j = 0; j < window; j++)
-	    distance_mask[i][j] =
-		(SQR(i - radius) + SQR(j - radius) <= SQR(radius)) ?
-		sqrt(SQR(i - radius) + SQR(j - radius)) : 0;
-
-    return 0;
-}
-
-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 teststream = 0;
-    float cur_distance = radius;
-    float distance = 0;
-    double absaccum = 0;
-    double sumaccum = 0;
-    double maxaccum = 0;
-    int naccum = -1;
-
-    if (point->stream > 0 && point->accum > accum_treshold)
-	return 0;		/* point lies on line(or skipped) and has proper treshold */
-
-    if (streams) {
-	/* stream version: assume ve have stream network and points 
-	 * are snapped to stream points where accum is greater than treshold
-	 * or to nearest stream point in accum is not supplied */
-
-	for (i = -radius; i <= radius; ++i)
-	    for (j = -radius; j <= radius; ++j) {
-
-		if (point->r + i < 0 || point->r + i >= nrows ||
-		    point->c + j < 0 || point->c + j >= ncols)
-		    continue;
-
-		if (!distance_mask[i + radius][j + radius])
-		    continue;
-
-		Segment_get(streams, &teststream, point->r + i, point->c + j);
-		distance = distance_mask[i + radius][j + radius];
-
-		if (teststream) {	/* is stream line */
-
-		    if (accum) {
-			Segment_get(accum, &absaccum, point->r + i,
-				    point->c + j);
-			absaccum = fabs(absaccum);
-		    }
-
-		    if (absaccum >= accum_treshold)	/* if no accum absaccum always =-1 */
-			if (cur_distance > distance) {
-			    cur_distance = distance;
-			    di = i;
-			    dj = j;
-			}
-		}
-	    }
-    }				/* end of streams version */
-
-    if (!streams) {
-	/* no stream version: problem for MFD. the snap point is found
-	 * in different manner. It is not only point where accum exceed the 
-	 * treshold (may be far for potential streamline) but must exceed the
-	 * mean value of accums in searach area taken in cells where treshold is exceeded */
-
-	for (i = -radius; i <= radius; ++i)
-	    for (j = -radius; j <= radius; ++j) {
-
-		if (point->r + i < 0 || point->r + i >= nrows ||
-		    point->c + j < 0 || point->c + j >= ncols)
-		    continue;
-
-		if (!distance_mask[i + radius][j + radius])
-		    continue;
-
-		Segment_get(accum, &absaccum, point->r + i, point->c + j);
-		absaccum = fabs(absaccum);
-
-		if (absaccum > maxaccum)
-		    maxaccum = absaccum;
-
-		if (absaccum > accum_treshold) {
-		    sumaccum += absaccum;
-		    naccum++;
-		}
-	    }
-
-        /* TODO: this should be fixed by someone who knows the code */
-	if (sumaccum > 0) 
-	    /* accum_treshold=(sumaccum/naccum+maxaccum)/2 */ ;
-	accum_treshold = sumaccum / naccum;
-
-	for (i = -radius; i <= radius; ++i)
-	    for (j = -radius; j <= radius; ++j) {
-
-		if (point->r + i < 0 || point->r + i >= nrows ||
-		    point->c + j < 0 || point->c + j >= ncols)
-		    continue;
-
-		if (!distance_mask[i + radius][j + radius])
-		    continue;
-
-		Segment_get(accum, &absaccum, point->r + i, point->c + j);
-		absaccum = fabs(absaccum);
-
-		if (accum_treshold > 0 && absaccum > accum_treshold)
-		    if (cur_distance > distance) {
-			cur_distance = distance;
-			di = i;
-			dj = j;
-		    }
-	    }
-    }				/* end of non-streams version */
-    if (di == -1 && dj == -1) {
-	G_warning(_("Unable to snap point with cat %d, in a given radius. "
-                    "Increase search radius."), point->cat);
-	di = 0;
-	dj = 0;
-	status = 2;
-    }
-    point->di = di;
-    point->dj = dj;
-    point->status = status;
-    return 0;
-}

+ 0 - 10
raster/r.stream.stats/Makefile

@@ -1,10 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.stream.stats
-
-LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB)
-DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP)
-
-include $(MODULE_TOPDIR)/include/Make/Module.make
-
-default: cmd

+ 0 - 563
raster/r.stream.stats/io.c

@@ -1,563 +0,0 @@
-#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;
-
-    if (data_type < 0 || data_type > 2)
-	G_fatal_error(_("Unable to create raster 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;
-
-    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)
-{
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * map pointer to map structure (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;
-
-    /* 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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 */
-	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 */
-
-    input_buffer = Rast_allocate_buf(input_data_type);
-
-    /* start reading */
-    G_message(_("Reading raster map <%s>..."), input_map_name);
-
-    for (r = 0; r < map->nrows; ++r) {
-	G_percent(r, map->nrows, 2);
-
-	Rast_get_row(input_map_fd, input_buffer, r, input_data_type);
-	input_pointer = input_buffer;
-
-	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(_("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;
-
-    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;
-
-    /* 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 raster 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;
-
-    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;
-     */
-
-    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();
-
-    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(_("Unrecognisable 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(_("Unable to format segment"));
-    }
-
-    close(fd);
-    if (0 > (fd = open(filename, 2))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to re-open file '%s'"), filename);
-    }
-
-    if (0 > (fd = Segment_init(&(seg->seg), fd, number_of_segs))) {
-	unlink(filename);
-	G_fatal_error(_("Unable to 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)
-{
-
-    /*
-     * Function 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 necessary.
-     * input_map_name: name of the map to be read;
-     * seg: pointer to map structure (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(_("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 any integer 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 raster map <%s> resolution differs. "
-                          "Run 'g.region rast=%s' to set proper region resolution."),
-                        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(_("Raster map <%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);
-
-    {				/* 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));
-	}
-    }
-
-    /* end opening and checking */
-
-    G_message(_("Reading raster 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;
-		}
-	    }
-
-	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(_("Unable to segment put row %d for raster 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;
-}
-
-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);
-
-    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 raster 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(_("Unable to segment read row %d for raster map <%s>"),
-		      r, output_map_name);
-
-	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(_("Unable to 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;
-}
-
-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)
-	G_free(seg->map_name);
-    if (seg->mapset)
-	G_free(seg->mapset);
-
-    return 0;
-}

+ 0 - 58
raster/r.stream.stats/io.h

@@ -1,58 +0,0 @@
-#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>
-
-#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 require 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 */
-} SEG;
-
-
-/* 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_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 *);

+ 0 - 25
raster/r.stream.stats/local_proto.h

@@ -1,25 +0,0 @@
-#include "io.h"
-#include "local_vars.h"
-
-/* stats prepare */
-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);
-/* 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);
-
-/* stats calculate */
-double stats_linear_reg(int max_order, double* statistic);
-int stats(int order_max);
-
-/* stats print */
-int print_stats(int order_max);
-int print_stats_total(void);
-int print_stats_orders(int order_max);

+ 0 - 82
raster/r.stream.stats/local_vars.h

@@ -1,82 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-#include <grass/glocale.h>
-#include <grass/gis.h>
-#include <grass/raster.h>
-
-#define SQRT2 1.414214
-	
-typedef struct {
-	int r, c;
-	int is_outlet;
-	} POINT;
-	
-typedef struct { 
-	int index;
-	int is_outlet;
-	int r, c; /* outlet */
-	float elev_diff;
-	float elev_spring, elev_outlet;
-	float slope; /* cumulative */
-	float gradient;
-	float length; /* cumulative */
-	int order;
-	double basin_area; /* basin order */
-	int cell_num;
-	} STREAM;	
-	
-typedef struct {
-	int order;
-	int stream_num;
-	double sum_length;
-	double avg_length;
-	double std_length;
-	float avg_slope;
-	float std_slope;
-	float avg_gradient;
-	float std_gradient;
-	double sum_area;
-	double avg_area;
-	double std_area;
-	float avg_elev_diff;
-	float std_elev_diff;
-	float bifur_ratio;
-	float std_bifur_ratio;
-	float reg_bifur_ratio;
-	float length_ratio;
-	float std_length_ratio;
-	float reg_length_ratio;
-	float area_ratio;
-	float std_area_ratio;
-	float reg_area_ratio;
-	float slope_ratio;
-	float std_slope_ratio;
-	float reg_slope_ratio;
-	float gradient_ratio;
-	float std_gradient_ratio;
-	float reg_gradient_ratio;
-	float stream_frequency;
-	float drainage_density;
-} STATS;
-
-#ifdef MAIN
-#	define GLOBAL
-#else
-#	define GLOBAL extern
-#endif
-
-GLOBAL int nextr[9];
-GLOBAL int nextc[9];
-
-GLOBAL double total_basins;
-GLOBAL int nrows, ncols; 
-
-GLOBAL POINT *fifo_points;
-GLOBAL int fifo_max;
-	
-GLOBAL int outlets_num; /* number outlets: index for stream statistics*/
-GLOBAL STREAM *stat_streams;
-GLOBAL STATS *ord_stats;
-GLOBAL STATS stats_total;

+ 0 - 195
raster/r.stream.stats/main.c

@@ -1,195 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE:       r.stream.stats
- * AUTHOR(S):    Jarek Jasiewicz jarekj amu.edu.pl
- *               
- * PURPOSE:      Calculate Horton's statistics according stream network and elevation map.
- *               Program calculates: Bifurcation ratio, length ratio, area ratio, 
- *               slope ratio and drainage density.
- *               It uses r.stream.order stream map, r.watershed direction map and DEM
- *               Stream input map shall contain streams ordered according Strahler's or
- *               Horton's orders. It also can calculate Hack's laws as an option.
- *               If input stream comes from r.stream.extract direction map 
- *               from r.stream.extract dir map must be patched with that of r.watershed.
- *
- * COPYRIGHT:    (C) 2002,2010-2014 by the GRASS Development Team
- *
- *               This program is free software under the GNU General Public
- *   	    	 License (>=v2). Read the file COPYING that comes with GRASS
- *   	    	 for details.
- *
- *****************************************************************************/
-#define MAIN
-#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 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 order_max;
-    int segmentation, catchment_total, orders_summary;	/*flags */
-
-    /* initialize GIS environment */
-    G_gisinit(argv[0]);
-
-    /* initialize module */
-    module = G_define_module();
-    module->description =
-	_("Calculates Horton's statistics for Strahler and Horton ordered networks created with r.stream.order.");
-    G_add_keyword(_("raster"));
-    G_add_keyword(_("hydrology"));
-    G_add_keyword(_("stream network"));
-    G_add_keyword(_("Horton's statistics"));
-
-    in_stm_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_stm_opt->key = "stream_rast";
-    in_stm_opt->description = _("Name of input raster map with stream network");
-
-    in_dir_opt = G_define_standard_option(G_OPT_R_INPUT);
-    in_dir_opt->key = "direction";
-    in_dir_opt->description = _("Name of input flow direction raster map");
-
-    in_elev_opt = G_define_standard_option(G_OPT_R_ELEV);
-
-    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 = _("Memory settings");
-    
-    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_segmentation->guisection = _("Memory settings");
-
-    flag_catchment_total = G_define_flag();
-    flag_catchment_total->key = 'c';
-    flag_catchment_total->description =
-	_("Print only catchment's statistics");
-    flag_catchment_total->guisection = _("Print");
-
-    flag_orders_summary = G_define_flag();
-    flag_orders_summary->key = 'o';
-    flag_orders_summary->description = _("Print only orders' statistics");
-    flag_orders_summary->guisection = _("Print");
-
-    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);
-
-    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();
-
-    if (!segmentation) {
-	MAP map_dirs, map_streams, map_elevation;
-	CELL **streams, **dirs;
-	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;
-	order_max = (int)map_streams.max;
-
-	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);
-
-	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) {
-	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 = 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;
-	order_max = (int)map_streams.max;
-
-	seg_init_streams(streams, dirs, elevation);
-	seg_calculate_streams(streams, dirs, elevation);
-	seg_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);
-
-	G_free(stat_streams);
-	G_free(ord_stats);
-
-	seg_release_map(&map_streams);
-	seg_release_map(&map_dirs);
-	seg_release_map(&map_elevation);
-
-    }
-    exit(EXIT_SUCCESS);
-}

+ 0 - 207
raster/r.stream.stats/r.stream.stats.html

@@ -1,207 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-The module <em>r.stream.stats</em> is prepared to calculate Horton's
-statistics of drainage network.
-
-<h2>OPTIONS</h2>
-<dl>
-<dt><b>-c</b></dt>
-<dd>Print only catchment's characteristics. Useful for shell script calculation
-or collecting data in external tables.</dd>
-<dt><b>-o</b></dt>
-<dd>Print only parameters for every order. Useful to visualise Horton's law with
-external software (see example bellow).</dd>
-<dt><b>-m</b></dt>
-<dd>Only for very large data sets. Use segment library to optimise memory
-consumption during analysis.</dd>
-<dt><b>stream_rast</b></dt>
-<dd>Stream network: name of input stream raster map produced
-by <em><a href="r.watershed.html">r.watershed</a></em> or
-  <em><a href="r.stream.extract.html">r.stream.extract</a></em>, on
-which ordering will be performed. Because stream network produced by
-<em><a href="r.watershed.html">r.watershed</a></em>
-and <em><a href="r.stream.extract.html">r.stream.extract</a></em> may
-slighty differ in detail, it is required to use
-both <b>stream_rast</b> and direction map produced by the same
-module. The <b>stream_rast</b> background shall have NULL value or
-zero value. Background values of NULL are by default produced by
-<em><a href="r.watershed.html">r.watershed</a></em>
-and <em><a href="r.stream.extract.html">r.stream.extract</a></em>. If
-not 0 or NULL use <em><a href="r.mapcalc.html">r.mapcalc</a></em> to
-set background values to NULL.
-</dd>
-<dt><b>direction</b></dt>
-<dd>Flow direction: name of input direction raster map produced by
-<em><a href="r.watershed.html">r.watershed</a></em> or
-  <em><a href="r.stream.extract.html">r.stream.extract</a></em>. If
-<em><a href="r.stream.extract.html">r.stream.extract</a></em> output
-raster map is used, it only has non-NULL values in places where
-streams occur. NULL (nodata) cells are ignored, zero and negative
-values are valid direction data if they vary from -8 to 8 (CCW from
-East in steps of 45 degrees). Direction map shall be of type CELL
-values. Region resolution and map resolution must be the same.
-Also <b>stream_rast</b> network map must have the same resolution. It
-is checked by default. If resolutions differ the module informs about
-it and stops. Region boundary and maps boundary may differ but it may
-lead to unexpected results.</dd>
-
-<dt><b>elevation</b></dt>
-<dd>Elevation: name of input elevation map. Map can be of type CELL,
-FCELL or DCELL. It is not restricted to resolution of region settings
-as <b>stream_rast</b> and <b>direction</b> raster map.</dd>
-</dl>
-
-<h3>OUTPUTS</h3>
-
-Output statistics are send to standard output. To redirect output to
-file use redirection operators: <tt>&lt;</tt> or <tt>&lt;&lt;</tt>
-(Unix only). If redirection is used, output messages are printed on
-stderr (usually terminal) while statistics are written to the file.
-Statistics can be print as a formatted summary information with number
-of parameters or as a catchement's descriptive statistics and table
-with statistics for every order.
-
-<h2>NOTES</h2>
-
-These statistics are calculated according to formulas given by
-R.Horton (1945). Because Horton do not defined precisely what is
-stream slope, 2 different approaches have been proposed: the first
-(slope) uses cell-by-cell slope calculation; the second (gradient)
-uses the difference between elevation of outlet and source of every
-channel to its length to calculate formula. Bifurcation ratio for
-every order is calculated acording to the formula:
-<tt>n_streams[1]/n_stream[i+1]</tt> where i is the current order
-and <tt>i+1</tt> is the next higher order. For max order of the cell
-value of streams is zero. Rest of the ratios are calculated in similar
-mode. The bifurcation and other ratios for the whole catchment (map)
-is calculated as mean i.e. sum of all bifurcation <tt>ratio /
-max_order-1</tt> (for max_order stream bifurcation <tt>ratio =
-0</tt>). It is strongly recommended to extract the stream network
-using basin map created with <em>r.stream.basins</em>. If the whole
-stream order raster map is used, the calculation will be performed but
-results may not have hydrological sense.
-
-For every order (std) means that statistics are calculated with standard deviation:
-
-<ul>
-<li>number of streams
-<li>total length of streams of a given order
-<li>total area of basins of a given order 
-<li>drainage density
-<li>stream density
-<li>average length of streams of a given order (std)
-<li>average slope (cell by cell inclination) of streams of a given order (std)
-<li>average gradient (spring to outlet inclination) of streams of a given order
-(std)
-<li>average area of basins of a given order (std)
-<li>avarage elevation difference of a given order (std)
-</ul>
-
-Ratios:
-<ul>
-<li>bifuracation ratio
-<li>length ratio
-<li>slope and gradient ratios
-<li>area ratio
-</ul>
-
-For the whole basin:
-
-<ul>
-<li>total number of streams
-<li>total length of streams 
-<li>total basin area
-<li>drainage density
-<li>stream density
-</ul>
-
-Ratios:
-<ul>
-<li>bifurcation ratio (std)
-<li>length ratio (std)
-<li>slope and gradient ratios (std)
-<li>area ratio (std)
-</ul>
-
-<p>
-For the whole basins ratios are calculated acording two formulas: as a
-mean of ratios for every order, or as a antilog of slope coefficient
-of the regression model: order vs. <tt>log10(parameter)</tt>
-
-<p>
-The module calculates statistics for all streams in
-input <b>stream_rast</b> map. It is strongly recommended to extract
-only network of one basin, but it is not necessary for
-computation. Streams for the desired basin can be extracted by the
-following <em><a href="r.mapcalc.html">r.mapcalc</a></em> formula:
-
-<div class="code"><pre>
-#xxx category of desired basin
-r.mapcalc "sel_streams = if(basin==xxx,streams,null())"
-</pre></div>
-
-<p>
-It is also possible to calculate Horton's statistics for Shreve
-ordering but it has no hydrological sense. Hack (or Gravelius
-hierarchy) main stream is not the same what so called Horton's reverse
-ordering (see: Horton 1945).
-
-<p>
-The module can work only if <b>direction</b> raster
-map, <b>stream_rast</b> map and region have the same settings. It is
-also required that stream_rast map and direction map come from the
-same source. For lots of reason this limitation probably cannot be
-omitted.  This means that if stream_rast map comes from
-<em><a href="r.stream.extract.html">r.stream.extract</a></em> also
-direction map
-from <em><a href="r.stream.extract.html">r.stream.extract</a></em>
-must be used. If the stream network was generated with MFD method also
-MFD direction map must be used.
-
-<h2>EXAMPLE</h2>
-
-Create table with order statistics. This table can easily be sent to
-external program (like R) to be visualised:
-
-<div class="code"><pre>
-g.region -p -a rast=elevation
-r.watershed elevation=elevation threshold=10000 drainage=direction stream=streams
-r.stream.stats stream_rast=horton direction=direction elevation=elevation
-# export for processing in R
-r.stream.stats -o stream_rast=horton direction=direction elevation=elevation > tmp_file
-
-R
-r=read.csv("tmp_file",skip=1,header=TRUE)
-plot(num_of_streams~order,data=r,log="y", 
-	main="Sperafish area", 
-	sub=paste("Bifurcation ratio: ",
-		round(1/10^model$coefficients[2],3)))
-model=lm(log10(num_of_streams)~order,data=r)
-abline(model)
-</pre></div>
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.mapcalc.html">r.mapcalc</a>,
-<a href="r.stream.channel.html">r.stream.channel</a>,
-<a href="r.stream.distance.html">r.stream.distance</a>,
-<a href="r.stream.extract.html">r.stream.extract</a>,
-<a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.segment.html">r.stream.segment</a>,
-<a href="r.stream.slope.html">r.stream.slope</a>,
-<a href="r.stream.snap.html">r.stream.snap</a>,
-<a href="r.watershed.html">r.watershed</a>
-</em>
-
-<p>
-See
-also <a href="http://grasswiki.osgeo.org/wiki/R.stream.*_modules">r.streams.*
-modules</a> wiki page.
-
-<h2>AUTHOR</h2>
-Jarek Jasiewicz, Adam Mickiewicz University, Geoecology and Geoinformation Institute.
-
-<p>
-<i>Last changed: $Date$</i>
-

+ 0 - 286
raster/r.stream.stats/stats_calculate.c

@@ -1,286 +0,0 @@
-#include "local_proto.h"
-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;
-
-    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;
-}
-
-int stats(int order_max)
-{
-    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.;
-
-    for (i = 0; i <= order_max; ++i) {
-	ord_stats[i].order = i;
-	ord_stats[i].stream_num = 0;
-	ord_stats[i].sum_length = 0.;
-	ord_stats[i].avg_length = 0.;
-	ord_stats[i].std_length = 0.;
-	ord_stats[i].avg_slope = 0.;
-	ord_stats[i].std_slope = 0.;
-	ord_stats[i].avg_gradient = 0.;
-	ord_stats[i].std_gradient = 0.;
-	ord_stats[i].sum_area = 0.;
-	ord_stats[i].avg_area = 0.;
-	ord_stats[i].std_area = 0.;
-	ord_stats[i].avg_elev_diff = 0.;
-	ord_stats[i].std_elev_diff = 0.;
-	ord_stats[i].bifur_ratio = 0.;
-	ord_stats[i].std_bifur_ratio = 0.;
-	ord_stats[i].length_ratio = 0.;
-	ord_stats[i].std_length_ratio = 0.;
-	ord_stats[i].area_ratio = 0.;
-	ord_stats[i].std_area_ratio = 0.;
-	ord_stats[i].slope_ratio = 0.;
-	ord_stats[i].std_slope_ratio = 0.;
-	ord_stats[i].gradient_ratio = 0.;
-	ord_stats[i].std_gradient_ratio = 0.;
-	ord_stats[i].stream_frequency = 0.;
-	ord_stats[i].drainage_density = 0.;
-    }
-    for (i = 0; i < outlets_num; ++i) {	/* recalculate and unify */
-	stat_streams[i].elev_diff =
-	    stat_streams[i].elev_spring - stat_streams[i].elev_outlet;
-	tmp_num =
-	    ((stat_streams[i].cell_num - 1) <
-	     1) ? 1 : stat_streams[i].cell_num - 1;
-	stat_streams[i].slope /= tmp_num;
-	stat_streams[i].gradient =
-	    stat_streams[i].elev_diff / stat_streams[i].length;
-
-	/* calculation */
-	ord_stats[stat_streams[i].order].stream_num++;
-	ord_stats[stat_streams[i].order].sum_length += stat_streams[i].length;
-	ord_stats[stat_streams[i].order].std_length +=
-	    (stat_streams[i].length * stat_streams[i].length);
-	ord_stats[stat_streams[i].order].avg_slope += stat_streams[i].slope;
-	ord_stats[stat_streams[i].order].std_slope +=
-	    (stat_streams[i].slope * stat_streams[i].slope);
-	ord_stats[stat_streams[i].order].avg_gradient +=
-	    stat_streams[i].gradient;
-	ord_stats[stat_streams[i].order].std_gradient +=
-	    (stat_streams[i].gradient * stat_streams[i].gradient);
-	ord_stats[stat_streams[i].order].sum_area +=
-	    stat_streams[i].basin_area;
-	ord_stats[stat_streams[i].order].std_area +=
-	    (stat_streams[i].basin_area * stat_streams[i].basin_area);
-	ord_stats[stat_streams[i].order].avg_elev_diff +=
-	    stat_streams[i].elev_diff;
-	ord_stats[stat_streams[i].order].std_elev_diff +=
-	    (stat_streams[i].elev_diff * stat_streams[i].elev_diff);
-    }
-
-    for (i = 1; i <= order_max; ++i) {
-
-	num = ord_stats[i].stream_num;
-	snum = (ord_stats[i].stream_num > 1) ?
-	    ((float)ord_stats[i].stream_num) / (ord_stats[i].stream_num -
-						1) : 0.;
-
-	ord_stats[i].avg_length = ord_stats[i].sum_length / num;
-	ord_stats[i].avg_slope = ord_stats[i].avg_slope / num;
-	ord_stats[i].avg_gradient = ord_stats[i].avg_gradient / num;
-	ord_stats[i].avg_area = ord_stats[i].sum_area / num;
-	ord_stats[i].avg_elev_diff = ord_stats[i].avg_elev_diff / num;
-
-	ord_stats[i].std_length = sqrt((ord_stats[i].std_length / num -
-					(ord_stats[i].avg_length *
-					 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].std_gradient = sqrt((ord_stats[i].std_gradient / num -
-					  (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].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 - 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].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;
-
-	/* total */
-	stats_total.stream_num += ord_stats[i].stream_num;
-	stats_total.sum_length += ord_stats[i].sum_length;
-
-	stats_total.bifur_ratio += ord_stats[i - 1].bifur_ratio;
-	stats_total.length_ratio += ord_stats[i - 1].length_ratio;
-	stats_total.area_ratio += ord_stats[i - 1].area_ratio;
-	stats_total.slope_ratio += ord_stats[i - 1].slope_ratio;
-	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);
-	stats_total.std_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);
-	stats_total.std_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);
-
-    }				/* end for ... orders */
-    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.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_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_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.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));
-
-    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));
-
-    G_free(statistic);
-
-    return 0;
-}

+ 0 - 464
raster/r.stream.stats/stats_prepare.c

@@ -1,464 +0,0 @@
-#include "local_proto.h"
-static int tail, head, fifo_count;
-
-int fifo_insert(POINT point)
-{
-    if (fifo_count == fifo_max)
-        G_fatal_error(_("Circular buffer too small"));
-
-    fifo_points[tail++] = point;
-    if (tail > fifo_max) {
-	G_debug(1, "tail > fifo_max");
-	tail = 0;
-    }
-    fifo_count++;
-    return 0;
-}
-
-POINT fifo_return_del(void)
-{
-    if (head >= fifo_max) {
-	G_debug(1, "head >= fifo_max");
-	head = -1;
-    }
-    fifo_count--;
-
-    return fifo_points[++head];
-}
-
-int ram_init_streams(CELL **streams, CELL **dirs, FCELL **elevation)
-{
-    int d, i;		/* d: direction, i: iteration */
-    int r, c;
-    int next_stream = -1, cur_stream;
-    int out_max = ncols + nrows;
-    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)
-	    if (streams[r][c] > 0) {
-		if (outlets_num > (out_max - 1)) {
-		    out_max *= 2;
-		    outlets =
-			(POINT *) G_realloc(outlets, out_max * sizeof(POINT));
-		}
-		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;
-		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;
-
-		    outlets_num++;
-
-		}
-	    }			/* end if streams */
-
-    stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
-
-    for (i = 0; i < outlets_num; ++i) {
-	stat_streams[i].r = outlets[i].r;
-	stat_streams[i].c = outlets[i].c;
-	stat_streams[i].is_outlet = outlets[i].is_outlet;
-	stat_streams[i].index = i;
-	stat_streams[i].slope = 0.;
-	stat_streams[i].gradient = 0.;
-	stat_streams[i].length = 0.;
-	stat_streams[i].elev_diff = 0.;
-	stat_streams[i].elev_spring = 0.;
-	stat_streams[i].elev_outlet = elevation[outlets[i].r][outlets[i].c];
-	stat_streams[i].order = streams[outlets[i].r][outlets[i].c];
-	stat_streams[i].basin_area = 0.;
-	stat_streams[i].cell_num = 0;
-    }
-
-    G_free(outlets);
-    return 0;
-}
-
-/////
-
-int seg_init_streams(SEGMENT *streams, SEGMENT *dirs, SEGMENT *elevation)
-{
-    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;
-    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) {
-		if (outlets_num > (out_max - 1)) {
-		    out_max *= 2;
-		    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;
-
-		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;
-
-		    outlets_num++;
-
-
-		}
-	    }			/* end if streams */
-	}
-
-    stat_streams = (STREAM *) G_malloc((outlets_num) * sizeof(STREAM));
-
-    for (i = 0; i < outlets_num; ++i) {
-	stat_streams[i].r = outlets[i].r;
-	stat_streams[i].c = outlets[i].c;
-	stat_streams[i].is_outlet = outlets[i].is_outlet;
-	stat_streams[i].index = i;
-	stat_streams[i].slope = 0.;
-	stat_streams[i].gradient = 0.;
-	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);
-	stat_streams[i].basin_area = 0.;
-	stat_streams[i].cell_num = 0;
-    }
-
-    G_free(outlets);
-    return 0;
-}
-
-
-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;
-    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;
-
-	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;
-
-	    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 */
-
-		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_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);
-		    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);
-
-		    r = next_r;
-		    c = next_c;
-		    done = 1;
-		    break;
-		}		/* end if */
-	    }			/* end for i */
-	}			/* end while */
-    }				/* end for s */
-    return 0;
-}
-
-
-////
-
-
-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;
-    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);
-
-	done = 1;
-
-	while (done) {
-	    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);
-
-	    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_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);
-
-		    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 */
-
-		    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;
-}
-
-double ram_calculate_basins_area(CELL **dirs, int r, int c)
-{
-    int i, j;
-    int next_r, next_c;
-    double area;
-    POINT n_cell;
-
-    tail = 0;
-    head = -1;
-
-    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);
-	    }
-	}			/* end for i... */
-
-	n_cell = fifo_return_del();
-	r = n_cell.r;
-	c = n_cell.c;
-    }				/* end while */
-    return area;
-}
-
-
-int ram_calculate_basins(CELL **dirs)
-{
-    int i;
-
-    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);
-
-	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)
-{
-    int i, j;
-    int next_r, next_c;
-    double area;
-    CELL dirs_cell;
-    POINT n_cell;
-
-    tail = 0;
-    head = -1;
-
-    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);
-	    }
-	}			/* end for i... */
-
-	n_cell = fifo_return_del();
-	r = n_cell.r;
-	c = n_cell.c;
-    }				/* end while */
-    return area;
-}
-
-int seg_calculate_basins(SEGMENT *dirs)
-{
-    int i;
-
-    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;
-}

+ 0 - 149
raster/r.stream.stats/stats_print.c

@@ -1,149 +0,0 @@
-#include "local_proto.h"
-
-int print_stats(int order_max)
-{
-
-    int i;
-
-    fflush(stdout);
-    /* summary statistics */
-    fprintf(stdout, "\n");
-    fprintf(stdout, "Summary:\n");
-    fprintf(stdout,
-	    "Max order | Tot.N.str. | Tot.str.len. | Tot.area. | Dr.dens. | Str.freq. \n");
-    fprintf(stdout,
-	    "  (num)   |    (num)   |     (km)     |   (km2)   | (km/km2) | (num/km2) \n");
-    fprintf(stdout, " %8d | %10d | %12.4f | %9.4f | %8.4f | %7.4f \n",
-	    stats_total.order, stats_total.stream_num,
-	    stats_total.sum_length / 1000, stats_total.sum_area / 1000000,
-	    stats_total.drainage_density * 1000,
-	    stats_total.stream_frequency * 1000000);
-
-    fprintf(stdout, "\n");
-    fprintf(stdout, "Stream ratios based on regresion coefficient:\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.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");
-    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);
-
-    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);
-    fprintf(stdout, "\n");
-
-    /* base parameters */
-    fprintf(stdout,
-	    "Order | Avg.len |  Avg.ar  |  Avg.sl |  Avg.grad. | Avg.el.dif\n");
-    fprintf(stdout,
-	    " num  |   (km)  |  (km2)   |  (m/m)  |    (m/m)   |     (m)   \n");
-    for (i = 1; i <= order_max; ++i) {
-	fprintf(stdout, "%5d | %7.4f | %8.4f | %7.4f | %10.4f | %7.4f\n",
-		ord_stats[i].order,
-		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_elev_diff);
-    }
-    fprintf(stdout, "\n");
-    /* std dev of base parameters */
-    fprintf(stdout,
-	    "Order | Std.len |  Std.ar  |  Std.sl |  Std.grad. | Std.el.dif\n");
-    fprintf(stdout,
-	    " num  |   (km)  |  (km2)   |  (m/m)  |    (m/m)   |     (m)   \n");
-    for (i = 1; i <= order_max; ++i) {
-	fprintf(stdout, "%5d | %7.4f | %8.4f | %7.4f | %10.4f | %7.4f\n",
-		ord_stats[i].order,
-		ord_stats[i].std_length / 1000,
-		ord_stats[i].std_area / 1000000,
-		ord_stats[i].std_slope,
-		ord_stats[i].std_gradient, ord_stats[i].std_elev_diff);
-    }
-    /* sum statistics of orders */
-    fprintf(stdout, "\n");
-    fprintf(stdout, "Order | N.streams | Tot.len (km) | Tot.area (km2)\n");
-    for (i = 1; i <= order_max; ++i) {
-	fprintf(stdout, "%5d | %9d | %12.4f | %7.4f\n",
-		ord_stats[i].order,
-		ord_stats[i].stream_num,
-		ord_stats[i].sum_length / 1000,
-		ord_stats[i].sum_area / 1000000);
-    }
-    /* order ratios */
-    fprintf(stdout, "\n");
-    fprintf(stdout,
-	    "Order | Bif.rt. | Len.rt. | Area.rt. | Slo.rt. | Grd.rt. | d.dens. | str.freq.\n");
-    for (i = 1; i <= order_max; ++i) {
-	fprintf(stdout,
-		"%5d | %7.4f | %7.4f | %8.4f | %7.4f | %7.4f | %7.4f | %7.4f\n",
-		ord_stats[i].order, ord_stats[i].bifur_ratio,
-		ord_stats[i].length_ratio, ord_stats[i].area_ratio,
-		ord_stats[i].slope_ratio, ord_stats[i].gradient_ratio,
-		ord_stats[i].drainage_density * 1000,
-		ord_stats[i].stream_frequency * 1000000);
-    }
-    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 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,
-		ord_stats[i].stream_num,
-		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_elev_diff,
-		ord_stats[i].sum_length / 1000,
-		ord_stats[i].sum_area / 1000000);
-    }
-    fflush(stdout);
-    return 0;
-}