GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
units.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/units.c
3 
4  \brief GIS Library - Units management and conversion
5 
6  (C) 2001-2010 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12  \author Adopted for libgis by Martin Landa <landa.martin gmail.com> (2010)
13  \author Temporal units and unit type check from Soeren gebbert <soerengebbert
14  googlemail.com> (2012)
15  */
16 
17 #include <string.h>
18 
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 /*!
23  \brief Units conversion from meters to units
24 
25  Units codes (gis.h):
26  - U_METERS
27  - U_KILOMETERS
28  - U_MILES
29  - U_FEET
30  - U_USFEET
31 
32  Returns a factor which converts meters to units (by multiplication).
33 
34  \param units units code
35 
36  \return factor
37  */
38 double G_meters_to_units_factor(int units)
39 {
40  switch (units) {
41  case U_METERS:
42  return 1.0;
43  break;
44 
45  case U_KILOMETERS:
46  return 1.0e-3;
47  break;
48 
49  case U_MILES:
50  return 6.21371192237334e-4; /* 1 / (0.0254 * 12 * 5280) */
51  break;
52 
53  case U_FEET:
54  return 3.28083989501312; /* 1 / (0.0254 * 12) */
55  break;
56 
57  case U_USFEET:
58  return 3.28083333333333; /* 1 / (1200/3937) */
59  break;
60 
61  default:
62  return 1.0;
63  break;
64  }
65 
66  return 1.0;
67 }
68 
69 /*!
70  \brief Units conversion from square meters to square units
71 
72  Units codes (gis.h):
73  - U_METERS
74  - U_KILOMETERS
75  - U_ACRES
76  - U_HECTARES
77  - U_MILES
78  - U_FEET
79  - U_USFEET
80 
81  Returns a factor which converts square meters to square units (by
82  multiplication).
83 
84  \param units units code
85 
86  \return factor
87  */
88 double G_meters_to_units_factor_sq(int units)
89 {
90  switch (units) {
91  case U_METERS:
92  return 1.0;
93  break;
94 
95  case U_KILOMETERS:
96  return 1.0e-6;
97  break;
98 
99  case U_ACRES:
100  return 2.47105381467165e-4; /* 640 acres in a sq mile */
101  break;
102 
103  case U_HECTARES:
104  return 1.0e-4;
105  break;
106 
107  case U_MILES:
108  return 3.86102158542446e-7; /* 1 / (0.0254 * 12 * 5280)^2 */
109  break;
110 
111  case U_FEET:
112  return 10.7639104167097; /* 1 / (0.0254 * 12)^2 */
113  break;
114 
115  case U_USFEET:
116  return 10.7638673611111; /* 1 / (1200/3937)^2 */
117  break;
118 
119  default:
120  return 1.0;
121  break;
122  }
123 
124  return 1.0;
125 }
126 
127 /** \brief Check if the unit is of spatial type
128 
129  \param units units code from gis.h
130 
131  \return 1 if True, 0 otherwise
132  */
133 
135 {
136  switch (units) {
137  case U_METERS:
138  return 1;
139  case U_KILOMETERS:
140  return 1;
141  case U_HECTARES:
142  return 1;
143  case U_ACRES:
144  return 1;
145  case U_MILES:
146  return 1;
147  case U_FEET:
148  return 1;
149  case U_USFEET:
150  return 1;
151  case U_RADIANS:
152  return 1;
153  case U_DEGREES:
154  return 1;
155  }
156  return 0;
157 }
158 
159 /** \brief Check if the unit is of temporal type
160 
161  \param units units code from gis.h
162 
163  \return 1 if True, 0 otherwise
164  */
165 
167 {
168  switch (units) {
169  case U_YEARS:
170  return 1;
171  case U_MONTHS:
172  return 1;
173  case U_DAYS:
174  return 1;
175  case U_HOURS:
176  return 1;
177  case U_MINUTES:
178  return 1;
179  case U_SECONDS:
180  return 1;
181  }
182  return 0;
183 }
184 
185 /*!
186  \brief Get localized units name
187 
188  Units codes (gis.h):
189  - U_METERS
190  - U_KILOMETERS
191  - U_ACRES
192  - U_HECTARES
193  - U_MILES
194  - U_FEET
195  - U_USFEET
196 
197  \param units units code
198  \param plural plural form if true
199  \param square area units if true
200 
201  \return units name
202  \return NULL if units not found
203  */
204 const char *G_get_units_name(int units, int plural, int square)
205 {
206  switch (units) {
207  case U_UNKNOWN:
208  if (square)
209  return plural ? _("square units") : _("square unit");
210  else
211  return plural ? _("units") : _("unit");
212  break;
213 
214  case U_METERS:
215  if (square)
216  return plural ? _("square meters") : _("square meter");
217  else
218  return plural ? _("meters") : _("meter");
219  break;
220 
221  case U_KILOMETERS:
222  if (square)
223  return plural ? _("square kilometers") : _("square kilometer");
224  else
225  return plural ? _("kilometers") : _("kilometer");
226  break;
227 
228  case U_ACRES:
229  if (square)
230  return plural ? _("acres") : _("acre");
231  else
232  return G_get_units_name(G_units(G_database_unit_name(1)), plural,
233  square);
234  break;
235 
236  case U_HECTARES:
237  if (square)
238  return plural ? _("hectares") : _("hectare");
239  else
240  return G_get_units_name(G_units(G_database_unit_name(1)), plural,
241  square);
242  break;
243 
244  case U_MILES:
245  if (square)
246  return plural ? _("square miles") : _("square mile");
247  else
248  return plural ? _("miles") : _("mile");
249  break;
250 
251  case U_FEET:
252  if (square)
253  return plural ? _("square feet") : _("square foot");
254  else
255  return plural ? _("feet") : _("foot");
256  break;
257 
258  case U_USFEET:
259  if (square)
260  return plural ? _("square US feet") : _("square US foot");
261  else
262  return plural ? _("US feet") : _("US foot");
263  break;
264 
265  case U_DEGREES:
266  if (square)
267  return plural ? _("square degrees") : _("square degree");
268  else
269  return plural ? _("degrees") : _("degree");
270  break;
271 
272  case U_YEARS:
273  return plural ? _("years") : _("year");
274  break;
275 
276  case U_MONTHS:
277  return plural ? _("months") : _("month");
278  break;
279 
280  case U_DAYS:
281  return plural ? _("days") : _("day");
282  break;
283 
284  case U_HOURS:
285  return plural ? _("hours") : _("hour");
286  break;
287 
288  case U_MINUTES:
289  return plural ? _("minutes") : _("minute");
290  break;
291 
292  case U_SECONDS:
293  return plural ? _("seconds") : _("second");
294  break;
295  }
296 
297  return NULL;
298 }
299 
300 /*!
301  \brief Get units code by name
302 
303  Units codes (gis.h):
304  - U_METERS
305  - U_KILOMETERS
306  - U_ACRES
307  - U_HECTARES
308  - U_MILES
309  - U_FEET
310  - U_USFEET
311  - ...
312  - U_YEARS
313  - ...
314 
315  \param units_name units name (singular or plural form)
316 
317  \return units code
318  \return U_UNKNOWN if not found
319  */
320 int G_units(const char *units_name)
321 {
322  if (units_name == NULL) {
323  return G_units(G_database_unit_name(1));
324  }
325 
326  if (strcasecmp(units_name, "meter") == 0 ||
327  strcasecmp(units_name, "meters") == 0)
328  return U_METERS;
329  else if (strcasecmp(units_name, "kilometer") == 0 ||
330  strcasecmp(units_name, "kilometers") == 0)
331  return U_KILOMETERS;
332  else if (strcasecmp(units_name, "acre") == 0 ||
333  strcasecmp(units_name, "acres") == 0)
334  return U_ACRES;
335  else if (strcasecmp(units_name, "hectare") == 0 ||
336  strcasecmp(units_name, "hectares") == 0)
337  return U_HECTARES;
338  else if (strcasecmp(units_name, "mile") == 0 ||
339  strcasecmp(units_name, "miles") == 0)
340  return U_MILES;
341  else if (strcasecmp(units_name, "foot") == 0 ||
342  strcasecmp(units_name, "feet") == 0)
343  return U_FEET;
344  else if (strcasecmp(units_name, "foot_us") == 0 ||
345  strcasecmp(units_name, "foot_uss") == 0)
346  return U_USFEET;
347  else if (strcasecmp(units_name, "degree") == 0 ||
348  strcasecmp(units_name, "degrees") == 0)
349  return U_DEGREES;
350  else if (strcasecmp(units_name, "year") == 0 ||
351  strcasecmp(units_name, "years") == 0)
352  return U_YEARS;
353  else if (strcasecmp(units_name, "month") == 0 ||
354  strcasecmp(units_name, "months") == 0)
355  return U_MONTHS;
356  else if (strcasecmp(units_name, "day") == 0 ||
357  strcasecmp(units_name, "days") == 0)
358  return U_DAYS;
359  else if (strcasecmp(units_name, "hour") == 0 ||
360  strcasecmp(units_name, "hours") == 0)
361  return U_HOURS;
362  else if (strcasecmp(units_name, "minute") == 0 ||
363  strcasecmp(units_name, "minutes") == 0)
364  return U_MINUTES;
365  else if (strcasecmp(units_name, "secons") == 0 ||
366  strcasecmp(units_name, "seconds") == 0)
367  return U_SECONDS;
368 
369  return U_UNKNOWN;
370 }
#define NULL
Definition: ccmath.h:32
const char * G_database_unit_name(int)
Get units (localized) name for the current location.
Definition: proj3.c:53
#define U_SECONDS
Definition: gis.h:121
#define U_HOURS
Definition: gis.h:119
#define U_MINUTES
Definition: gis.h:120
#define U_DAYS
Definition: gis.h:118
#define U_FEET
Definition: gis.h:111
#define U_ACRES
Definition: gis.h:106
#define U_MONTHS
Definition: gis.h:117
#define U_RADIANS
Definition: gis.h:112
#define U_METERS
Definition: gis.h:109
#define U_DEGREES
Definition: gis.h:113
#define U_YEARS
Definition: gis.h:116
#define U_UNKNOWN
Definition: gis.h:105
#define U_HECTARES
Definition: gis.h:107
#define U_USFEET
Definition: gis.h:114
#define U_MILES
Definition: gis.h:110
#define U_KILOMETERS
Definition: gis.h:108
#define _(str)
Definition: glocale.h:10
double G_meters_to_units_factor(int units)
Units conversion from meters to units.
Definition: units.c:38
const char * G_get_units_name(int units, int plural, int square)
Get localized units name.
Definition: units.c:204
double G_meters_to_units_factor_sq(int units)
Units conversion from square meters to square units.
Definition: units.c:88
int G_is_units_type_spatial(int units)
Check if the unit is of spatial type.
Definition: units.c:134
int G_is_units_type_temporal(int units)
Check if the unit is of temporal type.
Definition: units.c:166
int G_units(const char *units_name)
Get units code by name.
Definition: units.c:320