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