Ticket #76: d.measure_bearing.patch

File d.measure_bearing.patch, 5.6 KB (added by dylan, 16 years ago)
  • d.measure/local_proto.h

     
    11/* draw_line.c */
    22int draw_line(int, int, int, int, int, int);
    33/* msurements.c */
    4 int measurements(int, int, int, int, int);
     4int measurements(int, int, int, int, int, int, int);
    55int print_en(double, double, int);
    66int print_length(double, int, int);
     7int print_bearing_deg (double, double, double, double, int, int);
    78int add_point(double **, double **, int *, int *, double, double);
  • d.measure/msurements.c

     
    22#include <grass/display.h>
    33#include <grass/raster.h>
    44#include "local_proto.h"
     5#include <math.h>
     6#include <unistd.h>
    57
     8#define PI  M_PI
    69
    710FILE *output;
    811
    9 int measurements(int color1,int color2, int s_flag, int m_flag, int k_flag)
     12int measurements(int color1,int color2, int s_flag, int m_flag, int k_flag, int b_flag, int c_flag)
    1013{
    1114    double *x, *y;
    1215    int npoints, nalloc;
     
    8992                draw_line(screen_x,screen_y,cur_screen_x,cur_screen_y,color1,color2)  ;
    9093                add_point (&x, &y, &npoints, &nalloc, ux, uy);
    9194                length += G_distance(cur_ux, cur_uy, ux, uy) ;
     95               
     96                if(b_flag)
     97                                        print_bearing_deg(cur_ux, ux, cur_uy, uy, s_flag, c_flag);
     98                                       
    9299                print_length(length, s_flag, k_flag);
     100               
     101               
     102               
    93103                cur_screen_x = screen_x ;
    94104                cur_screen_y = screen_y ;
    95105                cur_ux = ux ;
     
    163173    return 0;
    164174}
    165175
     176/**
     177\brief compute the bearing in degrees from click to next click.
     178
     179\note North is at 90 degrees by default
     180\note north can be shifted to 0 degrees, and increment degrees increase counter-clockwise (like a compass) with the '-c' flag
     181*/
     182int print_bearing_deg (double cur_ux, double ux, double cur_uy, double uy, int s_flag, int c_flag)
     183{       
     184        double bearing_deg ;
     185        double dx, dy;
     186       
     187    /* Use stderr for TCLTK-Output */
     188    if( s_flag )
     189      output = stderr;
     190    else
     191      output = stdout;
     192       
     193        dx = ux - cur_ux ;
     194        dy = uy - cur_uy ;
     195       
     196        /* format output to compass-like bearings:
     197       
     198           put north at 0 deg by subtracting PI/2
     199           set to counter-clockwise like a compass by setting 'dx' to '-dx' */
     200        if( c_flag)
     201                {
     202                bearing_deg = (atan2(dy, -dx) - PI/2) * 180/PI ;
     203                if(bearing_deg < 0)
     204                        bearing_deg = 360 + bearing_deg;
     205                }
     206       
     207        /* standard math-style
     208           north is at 90 deg */
     209        else
     210                bearing_deg = atan2(dy, dx) * 180/PI ;
     211       
     212    fprintf (output,"Bearing:   %3.2f degrees\n", bearing_deg) ;
     213
     214    return 0;
     215}
     216
    166217int add_point (double **x, double **y,
    167218    int *npoints, int *nalloc, double ux, double uy)
    168219{
  • d.measure/main.c

     
    2020 *
    2121 *****************************************************************************/
    2222#include <stdlib.h>
     23
    2324#include <grass/gis.h>
    2425#include <grass/display.h>
    2526#include <grass/raster.h>
     
    3839           struct Flag *s;
    3940           struct Flag *m;
    4041           struct Flag *k;
     42           struct Flag *b;
     43           struct Flag *c;
    4144        } parm;
    42         int color1, color2, s_flag, m_flag, k_flag;
     45        int color1, color2, s_flag, m_flag, k_flag, b_flag, c_flag;
    4346
    4447        /* Initialize the GIS calls */
    4548        G_gisinit(argv[0]) ;
     
    7982        parm.k->key = 'k';
    8083        parm.k->description = _("Output in kilometers as well");
    8184       
     85        parm.b = G_define_flag();
     86        parm.b->key = 'b';
     87        parm.b->description = _("Compute bearing between points, relative to x-axis (north is 90 degrees)");
     88       
     89        parm.c = G_define_flag();
     90        parm.c->key = 'c';
     91        parm.c->description = _("Use compass-style bearing (north is 0 degrees, increases CCW)");
     92       
    8293        if (argc > 1 && G_parser(argc,argv))
    8394            exit(EXIT_FAILURE);
    8495
     
    91102        if (D_set_cur_wind(frame))
    92103            G_fatal_error(_("Current frame not available"));
    93104
     105        /* test for bearing calculation request in a LL location */
     106                if (parm.b->answer && G_projection() == PROJECTION_LL)
     107                        G_fatal_error(_("bearing calculation is not available for LL projection"));
     108                       
     109        /* issue an error if the c flag is issued without the b flag */
     110        if (parm.c->answer && !parm.b->answer)
     111                        G_fatal_error(_("the -c flag can only be used in conjunction with the -b flag"));
     112       
    94113        color1 = D_translate_color (parm.c1->answer);
    95114        color2 = D_translate_color (parm.c2->answer);
    96115        s_flag = parm.s->answer;
    97116        m_flag = parm.m->answer;
    98117        k_flag = parm.k->answer;
     118        b_flag = parm.b->answer;
     119        c_flag = parm.c->answer;
    99120
    100         measurements(color1, color2, s_flag, m_flag, k_flag ) ;
     121        measurements(color1, color2, s_flag, m_flag, k_flag, b_flag, c_flag ) ;
    101122
    102123        R_close_driver();
    103124
  • d.measure/description.html

     
    3535If the user draws an area within another area, the combined
    3636area of both regions will be output.
    3737
     38<EM>Bearing Calculations</EM>
     39Calcuation of bearings can be done in two ways:
     40<ol>
     41<li>math-style: angles are computed relative to the positive x-axis, with the starting point at the origin (north at 90 degrees)</li>
     42<li>compass-style: angles are computed relative to the positive y-axis, with the starting point at the origin (north at 0 degrees)</li>
     43</ol>
     44
    3845<H2>TODO</H2>
    3946
    4047Output lengths in the same units as those of the current LOCATION as