Changeset 56190


Ignore:
Timestamp:
May 10, 2013, 1:46:38 PM (11 years ago)
Author:
neteler
Message:

r.traveltime: attempt to port it to GRASS 7 (using http://trac.osgeo.org/grass/wiki/Grass7/RasterLib/ListOfFunctions)

Location:
grass-addons/grass7/raster
Files:
1 deleted
3 edited
2 copied

Legend:

Unmodified
Added
Removed
  • grass-addons/grass7/raster/Makefile

    r56056 r56190  
    2828        r.stream.snap \
    2929        r.stream.stats \
    30         r.threshold
     30        r.threshold \
     31        r.traveltime
    3132
    3233include $(MODULE_TOPDIR)/include/Make/Dir.make
  • grass-addons/grass7/raster/r.traveltime/Makefile

    r55236 r56190  
    1 # Uncoment and fix MODULE_TOPDIR line OR provide MODULE_TOPDIR variable for make
    2 # fix this relative to include/
    3 # or use absolute path to the GRASS source code
    4 #MODULE_TOPDIR = ../..
     1MODULE_TOPDIR = ../..
    52
    63PGM = r.traveltime
    74
    8 LIBES = $(GISLIB)
    9 DEPENDENCIES = $(GISDEP)
     5LIBES = $(GMATHLIB) $(RASTERLIB) $(GISLIB)
     6DEPENDENCIES = $(GMATHDEP) $(RASTERDEP) $(GISDEP)
    107
    118include $(MODULE_TOPDIR)/include/Make/Module.make
  • grass-addons/grass7/raster/r.traveltime/main.c

    r53876 r56190  
    2828#include <float.h>
    2929#include <grass/gis.h>
     30#include <grass/raster.h>
    3031#include <grass/glocale.h>
    3132#include <grass/config.h>
     
    5859    int value;
    5960
    60     if (G_get_raster_row(in_dir, inrast_dir, loc_y, data_type_dir) < 0)
    61         G_fatal_error(_("Could not read from map"));
     61    Rast_get_row(in_dir, inrast_dir, loc_y, data_type_dir);
    6262    value = ((CELL *) inrast_dir)[loc_x];
    6363
     
    151151    double manningsn;
    152152
    153     if (G_get_raster_row(in_dir, inrast_dir, y, data_type_dir) < 0)
    154         G_fatal_error(_("Could not read from map"));
     153    Rast_get_row(in_dir, inrast_dir, y, data_type_dir);
    155154    dir = ((CELL *) inrast_dir)[x];
    156155
    157     if (G_get_raster_row(in_accu, inrast_accu, y, data_type_accu) < 0)
    158         G_fatal_error(_("Could not read from map"));
     156    Rast_get_row(in_accu, inrast_accu, y, data_type_accu);
    159157    accu = ((CELL *) inrast_accu)[x];
    160158
    161     if (G_get_raster_row(in_n, inrast_n, y, data_type_n) < 0)
    162         G_fatal_error(_("Could not read from map"));
     159    Rast_get_row(in_n, inrast_n, y, data_type_n);
    163160    switch (data_type_n) {
    164161    case DCELL_TYPE:
     
    170167    }
    171168
    172     if (G_get_raster_row(in_dtm, inrast_dtm, y, data_type_dtm) < 0)
    173         G_fatal_error(_("Could not read from map"));
     169    Rast_get_row(in_dtm, inrast_dtm, y, data_type_dtm);
    174170    switch (data_type_dtm) {
    175171    case CELL_TYPE:
     
    184180    }
    185181
    186     if (G_get_raster_row(in_dtm, inrast_dtm, y - dy, data_type_dtm) < 0)
    187         G_fatal_error(_("Could not read from map"));
     182    Rast_get_row(in_dtm, inrast_dtm, y - dy, data_type_dtm);
    188183    switch (data_type_dtm) {
    189184    case CELL_TYPE:
     
    197192        break;
    198193    }
    199 
    200 
    201194
    202195    for (i = -1; i < 2; i++) {
     
    252245        *input_ep, *input_fdis;
    253246
    254 
    255 
    256 
    257247    /* initialize GIS environment */
    258248    G_gisinit(argv[0]);         /* reads grass env, stores program name to G_program_name() */
    259249
    260 
    261 
    262250    /* initialize module */
    263251    module = G_define_module();
    264     module->description = _("Estimation of travel times/isochrones");
     252    G_add_keyword(_("raster"));
     253    G_add_keyword(_("hydrology"));
     254    module->label = _("Estimation of travel times/isochrones.");
     255    module->description =
     256        _("Computes the travel time of surface runoff to an outlet");
    265257
    266258    /* Define the different options as defined in gis.h */
     
    362354        fdis = factor;
    363355    else {
    364         printf("\nWARNING! Reduction factor is not valid!\n");
    365         exit(EXIT_FAILURE);
    366     }
    367 
    368     /* returns NULL if the map was not found in any mapset,
    369      * mapset name otherwise */
    370     mapset = G_find_cell2(map_dir, "");
    371     if (mapset == NULL)
    372         G_fatal_error(_("cell file [%s] not found"), map_dir);
    373 
    374     mapset = G_find_cell2(map_accu, "");
    375     if (mapset == NULL)
    376         G_fatal_error(_("cell file [%s] not found"), map_accu);
    377 
    378     mapset = G_find_cell2(map_dtm, "");
    379     if (mapset == NULL)
    380         G_fatal_error(_("cell file [%s] not found"), map_dtm);
    381 
    382     mapset = G_find_cell2(map_n, "");
    383     if (mapset == NULL)
    384         G_fatal_error(_("cell file [%s] not found"), map_n);
    385 
    386 
    387     if (G_legal_filename(result) < 0)
    388         G_fatal_error(_("[%s] is an illegal name"), result);
    389 
    390 
     356           G_fatal_error("Reduction factor is not valid!");
     357    }
     358
     359    mapset = G_find_raster2(map_dir, "");
     360    mapset = G_find_raster2(map_accu, "");
     361    mapset = G_find_raster2(map_dtm, "");
     362    mapset = G_find_raster2(map_n, "");
    391363
    392364    /* determine the inputmap type (CELL/FCELL/DCELL) */
    393     data_type_dir = G_raster_map_type(map_dir, mapset);
    394     data_type_accu = G_raster_map_type(map_accu, mapset);
    395     data_type_n = G_raster_map_type(map_n, mapset);
    396     data_type_dtm = G_raster_map_type(map_dtm, mapset);
    397 
    398     /* G_open_cell_old - returns file destriptor (>0) */
    399     if ((in_dir = G_open_cell_old(map_dir, mapset)) < 0)
    400         G_fatal_error(_("Cannot open cell file [%s]"), map_dir);
    401 
    402     if ((in_accu = G_open_cell_old(map_accu, mapset)) < 0)
    403         G_fatal_error(_("Cannot open cell file [%s]"), map_accu);
    404 
    405     if ((in_n = G_open_cell_old(map_n, mapset)) < 0)
    406         G_fatal_error(_("Cannot open cell file [%s]"), map_n);
    407 
    408     if ((in_dtm = G_open_cell_old(map_dtm, mapset)) < 0)
    409         G_fatal_error(_("Cannot open cell file [%s]"), map_dtm);
    410 
    411     /* controlling, if we can open input raster */
    412     if (G_get_cellhd(map_accu, mapset, &cellhd_accu) < 0)
    413         G_fatal_error(_("Cannot read file header of [%s]"), map_accu);
    414 
    415     if (G_get_cellhd(map_dir, mapset, &cellhd_dir) < 0)
    416         G_fatal_error(_("Cannot read file header of [%s]"), map_dir);
    417 
    418     if (G_get_cellhd(map_dtm, mapset, &cellhd_dtm) < 0)
    419         G_fatal_error(_("Cannot read file header of [%s]"), map_dtm);
    420 
    421     if (G_get_cellhd(map_n, mapset, &cellhd_n) < 0)
    422         G_fatal_error(_("Cannot read file header of [%s]"), map_n);
     365    data_type_dir = Rast_map_type(map_dir, mapset);
     366    data_type_accu = Rast_map_type(map_accu, mapset);
     367    data_type_n = Rast_map_type(map_n, mapset);
     368    data_type_dtm = Rast_map_type(map_dtm, mapset);
     369
     370    in_dir = Rast_open_old(map_dir, mapset);
     371    in_accu = Rast_open_old(map_accu, mapset);
     372    in_n = Rast_open_old(map_n, mapset);
     373    in_dtm = Rast_open_old(map_dtm, mapset);
    423374
    424375    G_debug(3, "number of rows %d", cellhd_accu.rows);
     
    428379
    429380    /* Allocate input buffer */
    430     inrast_accu = G_allocate_raster_buf(data_type_accu);
    431     inrast_dir = G_allocate_raster_buf(data_type_dir);
    432     inrast_n = G_allocate_raster_buf(data_type_n);
    433     inrast_dtm = G_allocate_raster_buf(data_type_dtm);
     381    inrast_accu = Rast_allocate_buf(data_type_accu);
     382    inrast_dir = Rast_allocate_buf(data_type_dir);
     383    inrast_n = Rast_allocate_buf(data_type_n);
     384    inrast_dtm = Rast_allocate_buf(data_type_dtm);
    434385
    435386
    436387    /* Allocate output buffer, use input map data_type */
    437     nrows = G_window_rows();
    438     ncols = G_window_cols();
    439     outrast = G_allocate_raster_buf(FCELL_TYPE);
     388    nrows = Rast_window_rows();
     389    ncols = Rast_window_cols();
     390    outrast = Rast_allocate_buf(FCELL_TYPE);
    440391
    441392    /* controlling, if we can write the raster */
    442     if ((outfd = G_open_raster_new(result, FCELL_TYPE)) < 0)
    443         G_fatal_error(_("Could not open <%s>"), result);
     393    outfd = Rast_open_new(result, FCELL_TYPE);
    444394
    445395    /* output array */
    446396
    447397    array_out = (double **)malloc(nrows * sizeof(double *));
    448     if ((NULL == array_out)) {
    449         printf("out of memory ... !");
    450         exit(EXIT_FAILURE);
    451     }
     398    if ((NULL == array_out))
     399        G_fatal_error("Out of memory ... !");
    452400    int i, j;
    453401
    454402    for (i = 0; i < nrows; i++) {
    455403        array_out[i] = (double *)malloc(ncols * sizeof(double));
    456         if ((NULL == array_out[i])) {
    457             printf("out of memory ... !");
    458             exit(EXIT_FAILURE);
    459         }
     404        if ((NULL == array_out[i]))
     405            G_fatal_error("Out of memory ... !");
    460406    }
    461407
     
    470416     */
    471417
    472     if (G_get_window(&window) < 0) {
    473         sprintf(map_dir, "can't read current window parameters");
    474         G_fatal_error(map_dir);
    475         exit(EXIT_FAILURE);
    476     }
     418    G_get_window(&window);
    477419
    478420    // map units to matrix locations
     
    533475        }
    534476        /* write raster row to output raster file */
    535         if (G_put_raster_row(outfd, outrast, CELL_TYPE) < 0)
    536             G_fatal_error(_("Cannot write to <%s>"), result);
    537     }
    538 
    539 
    540 
    541 
    542     /* memory cleanup */
    543     G_free(inrast_accu);
    544     G_free(inrast_dir);
    545     G_free(inrast_dtm);
    546     G_free(inrast_n);
    547     G_free(outrast);
     477        Rast_put_row(outfd, outrast, CELL_TYPE);
     478    }
    548479
    549480    /* closing raster files */
    550     G_close_cell(inrast_accu);
    551     G_close_cell(inrast_dir);
    552     G_close_cell(inrast_dtm);
    553     G_close_cell(inrast_n);
    554     G_close_cell(outfd);
    555 
     481//    Rast_close(inrast_accu);
     482//    Rast_close(inrast_dir);
     483//    Rast_close(inrast_dtm);
     484//    Rast_close(inrast_n);
     485
     486    Rast_close(in_dir);
     487    Rast_close(in_accu);
     488    Rast_close(in_n);
     489    Rast_close(in_dtm);
     490    Rast_close(outfd);
    556491
    557492    return 0;
  • grass-addons/grass7/raster/r.traveltime/r.traveltime.html

    r56189 r56190  
    33outlet. The program starts at the basin outlet and calculates the travel
    44time at each raster cell recursively. A drainage area related threhold
    5 considers even&nbsp; surface and also channel runoff. Travel times are
     5considers even surface and also channel runoff. Travel times are
    66derived by assuming kinematic wave approximation.<br>
    77To derive channel flow velocities an equilibrium discharge for each
    88cell is calculated (Q=Area*Excess_Prcipitation, Assumption: storm
    9 duration &gt;= time of concentration).&nbsp; This assumption may result
     9duration &gt;= time of concentration). This assumption may result
    1010in overestimated velocities. Therefor a factor is implemented to reduce
    1111velocities biased towards too large values.<br>
     
    2424The program does not work correctly if Manning's roughness grid is
    2525defined as double (float expected). To define a simple uniform
    26 roughness distribution try: r.mapcalc 'roughness=0.1f'
     26roughness distribution try: r.mapcalc 'roughness = 0.1f'
    2727<br>
    2828The region has to be set one row and column larger than the elevation
     
    3333<p>
    3434<div class="code"><pre>
    35   g.region rast=elev_lid792_1m n=n+1 s=s-1 w=w-1 e=e+1 -p
    36   r.watershed elev_lid792_1m thresh=5000 accum=accum_5K   drain=draindir_5K
    37   r.fill.dir elev_lid792_1m elev=elev_filled dir=elev_dir
    38   r.mapcalc rough=0.1f
    39   r.traveltime --overwrite dir=draindir_5K@user1 accu=accum_5K@user1 \
    40         dtm=elev_filled@user1 manningsn=rough out_x=638741.43125 \
    41         out_y=220269.7 threshold=1 b=1 nchannel=0.1 ep=40 fdis=1 \
    42         out=travel_time
     35g.region rast=elev_lid792_1m n=n+1 s=s-1 w=w-1 e=e+1 -p
     36r.watershed elev_lid792_1m thresh=5000 accum=accum_5K   drain=draindir_5K
     37r.fill.dir elev_lid792_1m output=elev_filled outdir=elev_dir
     38
     39r.mapcalc "rough = 0.1f"
     40r.traveltime dir=draindir_5K@user1 accu=accum_5K@user1 \
     41      dtm=elev_filled@user1 manningsn=rough out_x=638741.43125 \
     42      out_y=220269.7 threshold=1 b=1 nchannel=0.1 ep=40 fdis=1 \
     43      out=travel_time
     44r.colors travel_time col=gyr
    4345</pre></div>
    44 </p>
    4546
    4647<h2>SEE ALSO</h2>
    47 <em><a href="r.watershed.html">r.watershed</a>,
    48     <a href="r.fill.dir.html">r.fill.dir</a>
    49     <br>
    50     <br>
    51     <a href="http://jesbergwetter.twoday.net/stories/4845555/">http://jesbergwetter.twoday.net/stories/4845555/</a>
     48<em>
     49<a href="r.watershed.html">r.watershed</a>,
     50<a href="r.fill.dir.html">r.fill.dir</a>
     51</em>
     52<br>
     53<a href="http://jesbergwetter.twoday.net/stories/4845555/">http://jesbergwetter.twoday.net/stories/4845555/</a>
    5254
    53     <h2>REFERENCES</h2>
    54     <li>Kilgore, J. L. (1997): <em>Development and evaluation of a GIS-based
     55<h2>REFERENCES</h2>
     56
     57<ul>
     58<li>Kilgore, J. L. (1997): <em>Development and evaluation of a GIS-based
    5559spatially distributed unit hydrograph model</em>, master thesis,
    5660Virginia
     
    6367<li>Muzik, I. (1996): <em>Flood modelling with GIS-derived distributed
    6468    unit hydrographs</em>, Hydrological Processes, 10, 1401-1409.</li>
     69</ul>
    6570
    6671<h2>AUTHOR</h2>
    6772Kristian Foerster<br>
    6873<p><i>Last changed: $Date$</i>
    69 </p>
Note: See TracChangeset for help on using the changeset viewer.