GRASS 8 Programmer's Manual 8.6.0dev(2026)-43c934e9d2
Loading...
Searching...
No Matches
gis/distance.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/distance.c
3
4 \brief GIS Library - Distance calculation functions.
5
6 WARNING: this code is preliminary and may be changed,
7 including calling sequences to any of the functions
8 defined here.
9
10 (C) 2001-2009, 2011 by the GRASS Development Team
11
12 This program is free software under the GNU General Public License
13 (>=v2). Read the file COPYING that comes with GRASS for details.
14
15 \author Original author CERL
16 */
17
18#include <math.h>
19#include <grass/gis.h>
20#include <grass/glocale.h>
21
22static double min4(double, double, double, double);
23static double min2(double, double);
24
25static struct state {
26 int projection;
27 double factor;
28} state;
29
30static struct state *st = &state;
31
32/*!
33 \brief Begin distance calculations.
34
35 Initializes the distance calculations. It is used both for the
36 planimetric and latitude-longitude projections.
37
38 \return 0 if projection has no metrics (ie. imagery)
39 \return 1 if projection is planimetric
40 \return 2 if projection is latitude-longitude
41 */
43{
44 double a, e2;
45
46 st->factor = 1.0;
47
48 if ((st->projection = G_projection()) == PROJECTION_LL) {
51
52 return 2;
53 }
54
56 if (st->factor > 0.0)
57 return 1;
58
59 st->factor = 1.0; /* assume meter grid */
60
61 return 0;
62}
63
64/*!
65 \brief Returns distance in meters.
66
67 This routine computes the distance, in meters, from
68 <i>x1</i>,<i>y1</i> to <i>x2</i>,<i>y2</i>. If the projection is
69 latitude-longitude, this distance is measured along the
70 geodesic. Two routines perform geodesic distance calculations.
71
72 \param[in] e1,n1 east-north coordinates of first point
73 \param[in] e2,n2 east-north coordinates of second point
74
75 \return distance
76 */
77double G_distance(double e1, double n1, double e2, double n2)
78{
79 if (st->projection == PROJECTION_LL)
80 return G_geodesic_distance(e1, n1, e2, n2);
81 else
82 return st->factor * hypot(e1 - e2, n1 - n2);
83}
84
85/*!
86 \brief Returns distance between two line segments in meters.
87
88 \param ax1,ay1,ax2,ay2 first segment
89 \param bx1,by1,bx2,by2 second segment
90
91 \return distance value
92 */
93double G_distance_between_line_segments(double ax1, double ay1, double ax2,
94 double ay2, double bx1, double by1,
95 double bx2, double by2)
96{
97 double ra, rb;
98 double x, y;
99
100 /* if the segments intersect, then the distance is zero */
102 &rb, &x, &y) > 0) {
103 return 0.0;
104 }
105
110}
111
112/*!
113 \brief Returns distance between a point and line segment in meters.
114
115 \param xp,yp point coordinates
116 \param x1,y1 segment point coordinates
117 \param x2,y2 segment point coordinates
118
119 \return distance
120 */
121double G_distance_point_to_line_segment(double xp, double yp, double x1,
122 double y1, double x2, double y2)
123{
124 double dx, dy;
125 double x, y;
126 double xq, yq, ra, rb;
127 int t;
128
129 /* define the perpendicular to the segment through the point */
130 dx = x1 - x2;
131 dy = y1 - y2;
132
133 if (dx == 0.0 && dy == 0.0)
134 return G_distance(x1, y1, xp, yp);
135
136 if (fabs(dy) > fabs(dx)) {
137 xq = xp + dy;
138 yq = (dx / dy) * (xp - xq) + yp;
139 }
140 else {
141 yq = yp + dx;
142 xq = (dy / dx) * (yp - yq) + xp;
143 }
144
145 /* find the intersection of the perpendicular with the segment */
146 t = G_intersect_line_segments(xp, yp, xq, yq, x1, y1, x2, y2, &ra, &rb, &x,
147 &y);
148 switch (t) {
149 case 0:
150 case 1:
151 break;
152 default:
153 /* parallel/colinear cases shouldn't occur with perpendicular lines */
154 G_warning(_("%s: shouldn't happen: "
155 "code=%d P=(%f,%f) S=(%f,%f)(%f,%f)"),
156 "G_distance_point_to_line_segment", t, xp, yp, x1, y1, x2,
157 y2);
158 return -1.0;
159 }
160
161 /* if x,y lies on the segment, then the distance is from to x,y */
162 if (rb >= 0 && rb <= 1.0)
163 return G_distance(x, y, xp, yp);
164
165 /* otherwise the distance is the short of the distances to the endpoints
166 * of the segment
167 */
168 return min2(G_distance(x1, y1, xp, yp), G_distance(x2, y2, xp, yp));
169}
170
171static double min4(double a, double b, double c, double d)
172{
173 return min2(min2(a, b), min2(c, d));
174}
175
176static double min2(double a, double b)
177{
178 return a < b ? a : b;
179}
void G_begin_geodesic_distance(double, double)
Begin geodesic distance.
Definition geodist.c:51
void G_warning(const char *,...) __attribute__((format(printf
int G_get_ellipsoid_parameters(double *, double *)
get ellipsoid parameters
Definition get_ellipse.c:66
double G_database_units_to_meters_factor(void)
Conversion to meters.
Definition proj3.c:146
double G_geodesic_distance(double, double, double, double)
Calculates geodesic distance.
Definition geodist.c:197
int G_intersect_line_segments(double, double, double, double, double, double, double, double, double *, double *, double *, double *)
int G_projection(void)
Query cartographic projection.
Definition proj1.c:32
double G_distance_point_to_line_segment(double xp, double yp, double x1, double y1, double x2, double y2)
Returns distance between a point and line segment in meters.
int G_begin_distance_calculations(void)
Begin distance calculations.
double G_distance_between_line_segments(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2)
Returns distance between two line segments in meters.
double G_distance(double e1, double n1, double e2, double n2)
Returns distance in meters.
#define PROJECTION_LL
Projection code - Latitude-Longitude.
Definition gis.h:129
#define _(str)
Definition glocale.h:10
double b
Definition r_raster.c:39
double t
Definition r_raster.c:39
#define x