QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsunittypes.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsunittypes.cpp
3 --------------
4 begin : February 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsunittypes.h"
18#include "qgis.h"
19
20/***************************************************************************
21 * This class is considered CRITICAL and any change MUST be accompanied with
22 * full unit tests in test_qgsunittypes.py.
23 * See details in QEP #17
24 ****************************************************************************/
25
27{
28 switch ( type )
29 {
30 case Qgis::UnitType::Distance:
31 return QStringLiteral( "distance" );
32
33 case Qgis::UnitType::Area:
34 return QStringLiteral( "area" );
35
36 case Qgis::UnitType::Volume:
37 return QStringLiteral( "volume" );
38
39 case Qgis::UnitType::Temporal:
40 return QStringLiteral( "temporal" );
41
42 case Qgis::UnitType::Unknown:
43 return QStringLiteral( "<unknown>" );
44
45 }
46 return QString();
47}
48
49Qgis::UnitType QgsUnitTypes::decodeUnitType( const QString &string, bool *ok )
50{
51 const QString normalized = string.trimmed().toLower();
52
53 if ( ok )
54 *ok = true;
55
56 if ( normalized == encodeUnitType( Qgis::UnitType::Distance ) )
57 return Qgis::UnitType::Distance;
58 if ( normalized == encodeUnitType( Qgis::UnitType::Area ) )
59 return Qgis::UnitType::Area;
60 if ( normalized == encodeUnitType( Qgis::UnitType::Volume ) )
61 return Qgis::UnitType::Volume;
62 if ( normalized == encodeUnitType( Qgis::UnitType::Temporal ) )
63 return Qgis::UnitType::Temporal;
64 if ( normalized == encodeUnitType( Qgis::UnitType::Unknown ) )
65 return Qgis::UnitType::Unknown;
66
67 if ( ok )
68 *ok = false;
69
70 return Qgis::UnitType::Unknown;
71}
72
74{
75 switch ( unit )
76 {
77 case Qgis::DistanceUnit::Meters:
78 case Qgis::DistanceUnit::Feet:
79 case Qgis::DistanceUnit::NauticalMiles:
80 case Qgis::DistanceUnit::Yards:
81 case Qgis::DistanceUnit::Miles:
82 case Qgis::DistanceUnit::Kilometers:
83 case Qgis::DistanceUnit::Centimeters:
84 case Qgis::DistanceUnit::Millimeters:
86
87 case Qgis::DistanceUnit::Degrees:
89
90 case Qgis::DistanceUnit::Unknown:
91 return Qgis::DistanceUnitType::Unknown;
92 }
93 return Qgis::DistanceUnitType::Unknown;
94}
95
97{
98 switch ( unit )
99 {
100 case Qgis::AreaUnit::SquareMeters:
101 case Qgis::AreaUnit::SquareKilometers:
102 case Qgis::AreaUnit::SquareFeet:
103 case Qgis::AreaUnit::SquareYards:
104 case Qgis::AreaUnit::SquareMiles:
105 case Qgis::AreaUnit::Hectares:
106 case Qgis::AreaUnit::Acres:
107 case Qgis::AreaUnit::SquareNauticalMiles:
108 case Qgis::AreaUnit::SquareCentimeters:
109 case Qgis::AreaUnit::SquareMillimeters:
111
112 case Qgis::AreaUnit::SquareDegrees:
114
115 case Qgis::AreaUnit::Unknown:
116 return Qgis::DistanceUnitType::Unknown;
117 }
118
119 return Qgis::DistanceUnitType::Unknown;
120}
121
123{
124 switch ( unit )
125 {
126 case Qgis::DistanceUnit::Meters:
127 return QStringLiteral( "meters" );
128
129 case Qgis::DistanceUnit::Kilometers:
130 return QStringLiteral( "km" );
131
132 case Qgis::DistanceUnit::Feet:
133 return QStringLiteral( "feet" );
134
135 case Qgis::DistanceUnit::Yards:
136 return QStringLiteral( "yd" );
137
138 case Qgis::DistanceUnit::Miles:
139 return QStringLiteral( "mi" );
140
141 case Qgis::DistanceUnit::Degrees:
142 return QStringLiteral( "degrees" );
143
144 case Qgis::DistanceUnit::Unknown:
145 return QStringLiteral( "<unknown>" );
146
147 case Qgis::DistanceUnit::NauticalMiles:
148 return QStringLiteral( "nautical miles" );
149
150 case Qgis::DistanceUnit::Centimeters:
151 return QStringLiteral( "cm" );
152
153 case Qgis::DistanceUnit::Millimeters:
154 return QStringLiteral( "mm" );
155 }
156 return QString();
157}
158
159/***************************************************************************
160 * This class is considered CRITICAL and any change MUST be accompanied with
161 * full unit tests in test_qgsunittypes.py.
162 * See details in QEP #17
163 ****************************************************************************/
164
165Qgis::DistanceUnit QgsUnitTypes::decodeDistanceUnit( const QString &string, bool *ok )
166{
167 const QString normalized = string.trimmed().toLower();
168
169 if ( ok )
170 *ok = true;
171
172 if ( normalized == encodeUnit( Qgis::DistanceUnit::Meters ) )
173 return Qgis::DistanceUnit::Meters;
174 if ( normalized == encodeUnit( Qgis::DistanceUnit::Feet ) )
175 return Qgis::DistanceUnit::Feet;
176 if ( normalized == encodeUnit( Qgis::DistanceUnit::Degrees ) )
177 return Qgis::DistanceUnit::Degrees;
178 if ( normalized == encodeUnit( Qgis::DistanceUnit::NauticalMiles ) )
179 return Qgis::DistanceUnit::NauticalMiles;
180 if ( normalized == encodeUnit( Qgis::DistanceUnit::Kilometers ) )
181 return Qgis::DistanceUnit::Kilometers;
182 if ( normalized == encodeUnit( Qgis::DistanceUnit::Yards ) )
183 return Qgis::DistanceUnit::Yards;
184 if ( normalized == encodeUnit( Qgis::DistanceUnit::Miles ) )
185 return Qgis::DistanceUnit::Miles;
186 if ( normalized == encodeUnit( Qgis::DistanceUnit::Centimeters ) )
187 return Qgis::DistanceUnit::Centimeters;
188 if ( normalized == encodeUnit( Qgis::DistanceUnit::Millimeters ) )
189 return Qgis::DistanceUnit::Millimeters;
190 if ( normalized == encodeUnit( Qgis::DistanceUnit::Unknown ) )
191 return Qgis::DistanceUnit::Unknown;
192
193 if ( ok )
194 *ok = false;
195
196 return Qgis::DistanceUnit::Unknown;
197}
198
200{
201 switch ( unit )
202 {
203 case Qgis::DistanceUnit::Meters:
204 return QObject::tr( "meters", "distance" );
205
206 case Qgis::DistanceUnit::Kilometers:
207 return QObject::tr( "kilometers", "distance" );
208
209 case Qgis::DistanceUnit::Feet:
210 return QObject::tr( "feet", "distance" );
211
212 case Qgis::DistanceUnit::Yards:
213 return QObject::tr( "yards", "distance" );
214
215 case Qgis::DistanceUnit::Miles:
216 return QObject::tr( "miles", "distance" );
217
218 case Qgis::DistanceUnit::Degrees:
219 return QObject::tr( "degrees", "distance" );
220
221 case Qgis::DistanceUnit::Centimeters:
222 return QObject::tr( "centimeters", "distance" );
223
224 case Qgis::DistanceUnit::Millimeters:
225 return QObject::tr( "millimeters", "distance" );
226
227 case Qgis::DistanceUnit::Unknown:
228 return QObject::tr( "<unknown>", "distance" );
229
230 case Qgis::DistanceUnit::NauticalMiles:
231 return QObject::tr( "nautical miles", "distance" );
232 }
233 return QString();
234}
235
237{
238 switch ( unit )
239 {
240 case Qgis::RenderUnit::Millimeters:
241 return QObject::tr( "mm", "render" );
242
243 case Qgis::RenderUnit::MapUnits:
244 return QObject::tr( "map units", "render" );
245
246 case Qgis::RenderUnit::Pixels:
247 return QObject::tr( "px", "render" );
248
249 case Qgis::RenderUnit::Percentage:
250 return QObject::tr( "%", "render" );
251
252 case Qgis::RenderUnit::Points:
253 return QObject::tr( "pt", "render" );
254
255 case Qgis::RenderUnit::Inches:
256 return QObject::tr( "in", "render" );
257
258 case Qgis::RenderUnit::Unknown:
259 return QObject::tr( "unknown", "render" );
260
261 case Qgis::RenderUnit::MetersInMapUnits:
262 return QObject::tr( "m", "render" );
263
264 }
265
266 return QString();
267}
268
270{
271 switch ( unit )
272 {
273 case Qgis::DistanceUnit::Meters:
274 return QObject::tr( "m", "distance" );
275
276 case Qgis::DistanceUnit::Kilometers:
277 return QObject::tr( "km", "distance" );
278
279 case Qgis::DistanceUnit::Feet:
280 return QObject::tr( "ft", "distance" );
281
282 case Qgis::DistanceUnit::Yards:
283 return QObject::tr( "yd", "distance" );
284
285 case Qgis::DistanceUnit::Miles:
286 return QObject::tr( "mi", "distance" );
287
288 case Qgis::DistanceUnit::Degrees:
289 return QObject::tr( "deg", "distance" );
290
291 case Qgis::DistanceUnit::Centimeters:
292 return QObject::tr( "cm", "distance" );
293
294 case Qgis::DistanceUnit::Millimeters:
295 return QObject::tr( "mm", "distance" );
296
297 case Qgis::DistanceUnit::Unknown:
298 return QString();
299
300 case Qgis::DistanceUnit::NauticalMiles:
301 return QObject::tr( "NM", "distance" );
302 }
303 return QString();
304}
305
306/***************************************************************************
307 * This class is considered CRITICAL and any change MUST be accompanied with
308 * full unit tests in test_qgsunittypes.py.
309 * See details in QEP #17
310 ****************************************************************************/
311
313{
314 const QString normalized = string.trimmed().toLower();
315
316 if ( ok )
317 *ok = true;
318
319 if ( normalized == toString( Qgis::DistanceUnit::Meters ) )
320 return Qgis::DistanceUnit::Meters;
321 if ( normalized == toString( Qgis::DistanceUnit::Kilometers ) )
322 return Qgis::DistanceUnit::Kilometers;
323 if ( normalized == toString( Qgis::DistanceUnit::Feet ) )
324 return Qgis::DistanceUnit::Feet;
325 if ( normalized == toString( Qgis::DistanceUnit::Yards ) )
326 return Qgis::DistanceUnit::Yards;
327 if ( normalized == toString( Qgis::DistanceUnit::Miles ) )
328 return Qgis::DistanceUnit::Miles;
329 if ( normalized == toString( Qgis::DistanceUnit::Degrees ) )
330 return Qgis::DistanceUnit::Degrees;
331 if ( normalized == toString( Qgis::DistanceUnit::Centimeters ) )
332 return Qgis::DistanceUnit::Centimeters;
333 if ( normalized == toString( Qgis::DistanceUnit::Millimeters ) )
334 return Qgis::DistanceUnit::Millimeters;
335 if ( normalized == toString( Qgis::DistanceUnit::NauticalMiles ) )
336 return Qgis::DistanceUnit::NauticalMiles;
337 if ( normalized == toString( Qgis::DistanceUnit::Unknown ) )
338 return Qgis::DistanceUnit::Unknown;
339
340 if ( ok )
341 *ok = false;
342
343 return Qgis::DistanceUnit::Unknown;
344}
345
346/***************************************************************************
347 * This class is considered CRITICAL and any change MUST be accompanied with
348 * full unit tests in test_qgsunittypes.py.
349 * See details in QEP #17
350 ****************************************************************************/
351
353{
354#define DEGREE_TO_METER 111319.49079327358
355#define FEET_TO_METER 0.3048
356#define NMILE_TO_METER 1852.0
357#define KILOMETERS_TO_METER 1000.0
358#define CENTIMETERS_TO_METER 0.01
359#define MILLIMETERS_TO_METER 0.001
360#define YARDS_TO_METER 0.9144
361#define YARDS_TO_FEET 3.0
362#define MILES_TO_METER 1609.344
363
364 // Calculate the conversion factor between the specified units
365 switch ( fromUnit )
366 {
367 case Qgis::DistanceUnit::Meters:
368 {
369 switch ( toUnit )
370 {
371 case Qgis::DistanceUnit::Meters:
372 return 1.0;
373 case Qgis::DistanceUnit::Kilometers:
374 return 1.0 / KILOMETERS_TO_METER;
375 case Qgis::DistanceUnit::Millimeters:
376 return 1.0 / MILLIMETERS_TO_METER;
377 case Qgis::DistanceUnit::Centimeters:
378 return 1.0 / CENTIMETERS_TO_METER;
379 case Qgis::DistanceUnit::Feet:
380 return 1.0 / FEET_TO_METER;
381 case Qgis::DistanceUnit::Yards:
382 return 1.0 / YARDS_TO_METER;
383 case Qgis::DistanceUnit::Miles:
384 return 1.0 / MILES_TO_METER;
385 case Qgis::DistanceUnit::Degrees:
386 return 1.0 / DEGREE_TO_METER;
387 case Qgis::DistanceUnit::NauticalMiles:
388 return 1.0 / NMILE_TO_METER;
389 case Qgis::DistanceUnit::Unknown:
390 break;
391 }
392
393 break;
394 }
395 case Qgis::DistanceUnit::Kilometers:
396 {
397 switch ( toUnit )
398 {
399 case Qgis::DistanceUnit::Meters:
400 return KILOMETERS_TO_METER;
401 case Qgis::DistanceUnit::Kilometers:
402 return 1.0;
403 case Qgis::DistanceUnit::Centimeters:
405 case Qgis::DistanceUnit::Millimeters:
407 case Qgis::DistanceUnit::Feet:
409 case Qgis::DistanceUnit::Yards:
411 case Qgis::DistanceUnit::Miles:
413 case Qgis::DistanceUnit::Degrees:
415 case Qgis::DistanceUnit::NauticalMiles:
417 case Qgis::DistanceUnit::Unknown:
418 break;
419 }
420
421 break;
422 }
423 case Qgis::DistanceUnit::Feet:
424 {
425 switch ( toUnit )
426 {
427 case Qgis::DistanceUnit::Meters:
428 return FEET_TO_METER;
429 case Qgis::DistanceUnit::Kilometers:
431 case Qgis::DistanceUnit::Centimeters:
433 case Qgis::DistanceUnit::Millimeters:
435 case Qgis::DistanceUnit::Feet:
436 return 1.0;
437 case Qgis::DistanceUnit::Yards:
438 return 1.0 / YARDS_TO_FEET;
439 case Qgis::DistanceUnit::Miles:
441 case Qgis::DistanceUnit::Degrees:
443 case Qgis::DistanceUnit::NauticalMiles:
445 case Qgis::DistanceUnit::Unknown:
446 break;
447 }
448
449 break;
450 }
451 case Qgis::DistanceUnit::Yards:
452 {
453 switch ( toUnit )
454 {
455 case Qgis::DistanceUnit::Meters:
456 return YARDS_TO_METER;
457 case Qgis::DistanceUnit::Kilometers:
459 case Qgis::DistanceUnit::Centimeters:
461 case Qgis::DistanceUnit::Millimeters:
463 case Qgis::DistanceUnit::Feet:
464 return YARDS_TO_FEET;
465 case Qgis::DistanceUnit::Yards:
466 return 1.0;
467 case Qgis::DistanceUnit::Miles:
469 case Qgis::DistanceUnit::Degrees:
471 case Qgis::DistanceUnit::NauticalMiles:
473 case Qgis::DistanceUnit::Unknown:
474 break;
475 }
476
477 break;
478 }
479 case Qgis::DistanceUnit::Miles:
480 {
481 switch ( toUnit )
482 {
483 case Qgis::DistanceUnit::Meters:
484 return MILES_TO_METER;
485 case Qgis::DistanceUnit::Kilometers:
487 case Qgis::DistanceUnit::Centimeters:
489 case Qgis::DistanceUnit::Millimeters:
491 case Qgis::DistanceUnit::Feet:
493 case Qgis::DistanceUnit::Yards:
495 case Qgis::DistanceUnit::Miles:
496 return 1.0;
497 case Qgis::DistanceUnit::Degrees:
499 case Qgis::DistanceUnit::NauticalMiles:
501 case Qgis::DistanceUnit::Unknown:
502 break;
503 }
504
505 break;
506 }
507 case Qgis::DistanceUnit::Degrees:
508 {
509 switch ( toUnit )
510 {
511 case Qgis::DistanceUnit::Meters:
512 return DEGREE_TO_METER;
513 case Qgis::DistanceUnit::Kilometers:
515 case Qgis::DistanceUnit::Centimeters:
517 case Qgis::DistanceUnit::Millimeters:
519 case Qgis::DistanceUnit::Feet:
521 case Qgis::DistanceUnit::Yards:
523 case Qgis::DistanceUnit::Miles:
525 case Qgis::DistanceUnit::Degrees:
526 return 1.0;
527 case Qgis::DistanceUnit::NauticalMiles:
529 case Qgis::DistanceUnit::Unknown:
530 break;
531 }
532
533 break;
534 }
535 case Qgis::DistanceUnit::NauticalMiles:
536 {
537 switch ( toUnit )
538 {
539 case Qgis::DistanceUnit::Meters:
540 return NMILE_TO_METER;
541 case Qgis::DistanceUnit::Kilometers:
543 case Qgis::DistanceUnit::Centimeters:
545 case Qgis::DistanceUnit::Millimeters:
547 case Qgis::DistanceUnit::Feet:
549 case Qgis::DistanceUnit::Yards:
551 case Qgis::DistanceUnit::Miles:
553 case Qgis::DistanceUnit::Degrees:
555 case Qgis::DistanceUnit::NauticalMiles:
556 return 1.0;
557 case Qgis::DistanceUnit::Unknown:
558 break;
559 }
560
561 break;
562 }
563 case Qgis::DistanceUnit::Centimeters:
564 {
565 switch ( toUnit )
566 {
567 case Qgis::DistanceUnit::Meters:
569 case Qgis::DistanceUnit::Kilometers:
571 case Qgis::DistanceUnit::Centimeters:
572 return 1.0;
573 case Qgis::DistanceUnit::Millimeters:
575 case Qgis::DistanceUnit::Feet:
577 case Qgis::DistanceUnit::Yards:
579 case Qgis::DistanceUnit::Miles:
581 case Qgis::DistanceUnit::Degrees:
583 case Qgis::DistanceUnit::NauticalMiles:
585 case Qgis::DistanceUnit::Unknown:
586 break;
587 }
588
589 break;
590 }
591 case Qgis::DistanceUnit::Millimeters:
592 {
593 switch ( toUnit )
594 {
595 case Qgis::DistanceUnit::Meters:
597 case Qgis::DistanceUnit::Kilometers:
599 case Qgis::DistanceUnit::Centimeters:
601 case Qgis::DistanceUnit::Millimeters:
602 return 1.0;
603 case Qgis::DistanceUnit::Feet:
605 case Qgis::DistanceUnit::Yards:
607 case Qgis::DistanceUnit::Miles:
609 case Qgis::DistanceUnit::Degrees:
611 case Qgis::DistanceUnit::NauticalMiles:
613 case Qgis::DistanceUnit::Unknown:
614 break;
615 }
616
617 break;
618 }
619 case Qgis::DistanceUnit::Unknown:
620 break;
621 }
622 return 1.0;
623}
624
626{
627 switch ( unit )
628 {
629 case Qgis::AreaUnit::SquareMeters:
630 return QStringLiteral( "m2" );
631 case Qgis::AreaUnit::SquareKilometers:
632 return QStringLiteral( "km2" );
633 case Qgis::AreaUnit::SquareFeet:
634 return QStringLiteral( "ft2" );
635 case Qgis::AreaUnit::SquareYards:
636 return QStringLiteral( "y2" );
637 case Qgis::AreaUnit::SquareMiles:
638 return QStringLiteral( "mi2" );
639 case Qgis::AreaUnit::Hectares:
640 return QStringLiteral( "ha" );
641 case Qgis::AreaUnit::Acres:
642 return QStringLiteral( "ac" );
643 case Qgis::AreaUnit::SquareNauticalMiles:
644 return QStringLiteral( "nm2" );
645 case Qgis::AreaUnit::SquareDegrees:
646 return QStringLiteral( "deg2" );
647 case Qgis::AreaUnit::SquareCentimeters:
648 return QStringLiteral( "cm2" );
649 case Qgis::AreaUnit::SquareMillimeters:
650 return QStringLiteral( "mm2" );
651 case Qgis::AreaUnit::Unknown:
652 return QStringLiteral( "<unknown>" );
653 }
654 return QString();
655}
656
657Qgis::AreaUnit QgsUnitTypes::decodeAreaUnit( const QString &string, bool *ok )
658{
659 const QString normalized = string.trimmed().toLower();
660
661 if ( ok )
662 *ok = true;
663
664 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMeters ) )
665 return Qgis::AreaUnit::SquareMeters;
666 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareKilometers ) )
667 return Qgis::AreaUnit::SquareKilometers;
668 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareFeet ) )
669 return Qgis::AreaUnit::SquareFeet;
670 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareYards ) )
671 return Qgis::AreaUnit::SquareYards;
672 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMiles ) )
673 return Qgis::AreaUnit::SquareMiles;
674 if ( normalized == encodeUnit( Qgis::AreaUnit::Hectares ) )
675 return Qgis::AreaUnit::Hectares;
676 if ( normalized == encodeUnit( Qgis::AreaUnit::Acres ) )
677 return Qgis::AreaUnit::Acres;
678 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareNauticalMiles ) )
679 return Qgis::AreaUnit::SquareNauticalMiles;
680 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareDegrees ) )
681 return Qgis::AreaUnit::SquareDegrees;
682 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareCentimeters ) )
683 return Qgis::AreaUnit::SquareCentimeters;
684 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMillimeters ) )
685 return Qgis::AreaUnit::SquareMillimeters;
686 if ( normalized == encodeUnit( Qgis::AreaUnit::Unknown ) )
687 return Qgis::AreaUnit::Unknown;
688
689 if ( ok )
690 *ok = false;
691
692 return Qgis::AreaUnit::Unknown;
693}
694
696{
697 switch ( unit )
698 {
699 case Qgis::AreaUnit::SquareMeters:
700 return QObject::tr( "square meters", "area" );
701 case Qgis::AreaUnit::SquareKilometers:
702 return QObject::tr( "square kilometers", "area" );
703 case Qgis::AreaUnit::SquareFeet:
704 return QObject::tr( "square feet", "area" );
705 case Qgis::AreaUnit::SquareYards:
706 return QObject::tr( "square yards", "area" );
707 case Qgis::AreaUnit::SquareMiles:
708 return QObject::tr( "square miles", "area" );
709 case Qgis::AreaUnit::Hectares:
710 return QObject::tr( "hectares", "area" );
711 case Qgis::AreaUnit::Acres:
712 return QObject::tr( "acres", "area" );
713 case Qgis::AreaUnit::SquareNauticalMiles:
714 return QObject::tr( "square nautical miles", "area" );
715 case Qgis::AreaUnit::SquareDegrees:
716 return QObject::tr( "square degrees", "area" );
717 case Qgis::AreaUnit::SquareMillimeters:
718 return QObject::tr( "square millimeters", "area" );
719 case Qgis::AreaUnit::SquareCentimeters:
720 return QObject::tr( "square centimeters", "area" );
721 case Qgis::AreaUnit::Unknown:
722 return QObject::tr( "<unknown>", "area" );
723 }
724 return QString();
725}
726
728{
729 switch ( unit )
730 {
731 case Qgis::AreaUnit::SquareMeters:
732 return QObject::tr( "m²", "area" );
733 case Qgis::AreaUnit::SquareKilometers:
734 return QObject::tr( "km²", "area" );
735 case Qgis::AreaUnit::SquareFeet:
736 return QObject::tr( "ft²", "area" );
737 case Qgis::AreaUnit::SquareYards:
738 return QObject::tr( "yd²", "area" );
739 case Qgis::AreaUnit::SquareMiles:
740 return QObject::tr( "mi²", "area" );
741 case Qgis::AreaUnit::Hectares:
742 return QObject::tr( "ha", "area" );
743 case Qgis::AreaUnit::Acres:
744 return QObject::tr( "ac", "area" );
745 case Qgis::AreaUnit::SquareNauticalMiles:
746 return QObject::tr( "NM²", "area" );
747 case Qgis::AreaUnit::SquareDegrees:
748 return QObject::tr( "deg²", "area" );
749 case Qgis::AreaUnit::SquareCentimeters:
750 return QObject::tr( "cm²", "area" );
751 case Qgis::AreaUnit::SquareMillimeters:
752 return QObject::tr( "mm²", "area" );
753 case Qgis::AreaUnit::Unknown:
754 return QString();
755 }
756 return QString();
757}
758
759Qgis::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString &string, bool *ok )
760{
761 const QString normalized = string.trimmed().toLower();
762
763 if ( ok )
764 *ok = true;
765
766 if ( normalized == toString( Qgis::AreaUnit::SquareMeters ) )
767 return Qgis::AreaUnit::SquareMeters;
768 if ( normalized == toString( Qgis::AreaUnit::SquareKilometers ) )
769 return Qgis::AreaUnit::SquareKilometers;
770 if ( normalized == toString( Qgis::AreaUnit::SquareFeet ) )
771 return Qgis::AreaUnit::SquareFeet;
772 if ( normalized == toString( Qgis::AreaUnit::SquareYards ) )
773 return Qgis::AreaUnit::SquareYards;
774 if ( normalized == toString( Qgis::AreaUnit::SquareMiles ) )
775 return Qgis::AreaUnit::SquareMiles;
776 if ( normalized == toString( Qgis::AreaUnit::Hectares ) )
777 return Qgis::AreaUnit::Hectares;
778 if ( normalized == toString( Qgis::AreaUnit::Acres ) )
779 return Qgis::AreaUnit::Acres;
780 if ( normalized == toString( Qgis::AreaUnit::SquareNauticalMiles ) )
781 return Qgis::AreaUnit::SquareNauticalMiles;
782 if ( normalized == toString( Qgis::AreaUnit::SquareDegrees ) )
783 return Qgis::AreaUnit::SquareDegrees;
784 if ( normalized == toString( Qgis::AreaUnit::SquareMillimeters ) )
785 return Qgis::AreaUnit::SquareMillimeters;
786 if ( normalized == toString( Qgis::AreaUnit::SquareCentimeters ) )
787 return Qgis::AreaUnit::SquareCentimeters;
788 if ( normalized == toString( Qgis::AreaUnit::Unknown ) )
789 return Qgis::AreaUnit::Unknown;
790 if ( ok )
791 *ok = false;
792
793 return Qgis::AreaUnit::Unknown;
794}
795
797{
798#define KM2_TO_M2 1000000.0
799#define CM2_TO_M2 0.0001
800#define MM2_TO_M2 0.000001
801#define FT2_TO_M2 0.09290304
802#define YD2_TO_M2 0.83612736
803#define MI2_TO_M2 2589988.110336
804#define HA_TO_M2 10000.0
805#define AC_TO_FT2 43560.0
806#define DEG2_TO_M2 12392029030.5
807#define NM2_TO_M2 3429904.0
808
809 // Calculate the conversion factor between the specified units
810 switch ( fromUnit )
811 {
812 case Qgis::AreaUnit::SquareMeters:
813 {
814 switch ( toUnit )
815 {
816 case Qgis::AreaUnit::SquareMeters:
817 return 1.0;
818 case Qgis::AreaUnit::SquareKilometers:
819 return 1.0 / KM2_TO_M2;
820 case Qgis::AreaUnit::SquareFeet:
821 return 1.0 / FT2_TO_M2;
822 case Qgis::AreaUnit::SquareYards:
823 return 1.0 / YD2_TO_M2;
824 case Qgis::AreaUnit::SquareMiles:
825 return 1.0 / MI2_TO_M2;
826 case Qgis::AreaUnit::Hectares:
827 return 1.0 / HA_TO_M2;
828 case Qgis::AreaUnit::Acres:
829 return 1.0 / AC_TO_FT2 / FT2_TO_M2;
830 case Qgis::AreaUnit::SquareNauticalMiles:
831 return 1.0 / NM2_TO_M2;
832 case Qgis::AreaUnit::SquareDegrees:
833 return 1.0 / DEG2_TO_M2;
834 case Qgis::AreaUnit::SquareCentimeters:
835 return 1.0 / CM2_TO_M2;
836 case Qgis::AreaUnit::SquareMillimeters:
837 return 1.0 / MM2_TO_M2;
838 case Qgis::AreaUnit::Unknown:
839 break;
840 }
841
842 break;
843 }
844 case Qgis::AreaUnit::SquareKilometers:
845 {
846 switch ( toUnit )
847 {
848 case Qgis::AreaUnit::SquareMeters:
849 return KM2_TO_M2;
850 case Qgis::AreaUnit::SquareKilometers:
851 return 1.0;
852 case Qgis::AreaUnit::SquareFeet:
853 return KM2_TO_M2 / FT2_TO_M2;
854 case Qgis::AreaUnit::SquareYards:
855 return KM2_TO_M2 / YD2_TO_M2;
856 case Qgis::AreaUnit::SquareMiles:
857 return KM2_TO_M2 / MI2_TO_M2;
858 case Qgis::AreaUnit::Hectares:
859 return KM2_TO_M2 / HA_TO_M2;
860 case Qgis::AreaUnit::Acres:
861 return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
862 case Qgis::AreaUnit::SquareNauticalMiles:
863 return KM2_TO_M2 / NM2_TO_M2;
864 case Qgis::AreaUnit::SquareDegrees:
865 return KM2_TO_M2 / DEG2_TO_M2;
866 case Qgis::AreaUnit::SquareCentimeters:
867 return KM2_TO_M2 / CM2_TO_M2;
868 case Qgis::AreaUnit::SquareMillimeters:
869 return KM2_TO_M2 / MM2_TO_M2;
870 case Qgis::AreaUnit::Unknown:
871 break;
872 }
873
874 break;
875 }
876 case Qgis::AreaUnit::SquareFeet:
877 {
878 switch ( toUnit )
879 {
880 case Qgis::AreaUnit::SquareMeters:
881 return FT2_TO_M2;
882 case Qgis::AreaUnit::SquareKilometers:
883 return FT2_TO_M2 / KM2_TO_M2;
884 case Qgis::AreaUnit::SquareFeet:
885 return 1.0;
886 case Qgis::AreaUnit::SquareYards:
887 return FT2_TO_M2 / YD2_TO_M2;
888 case Qgis::AreaUnit::SquareMiles:
889 return FT2_TO_M2 / MI2_TO_M2;
890 case Qgis::AreaUnit::Hectares:
891 return FT2_TO_M2 / HA_TO_M2;
892 case Qgis::AreaUnit::Acres:
893 return 1.0 / AC_TO_FT2;
894 case Qgis::AreaUnit::SquareNauticalMiles:
895 return FT2_TO_M2 / NM2_TO_M2;
896 case Qgis::AreaUnit::SquareDegrees:
897 return FT2_TO_M2 / DEG2_TO_M2;
898 case Qgis::AreaUnit::SquareCentimeters:
899 return FT2_TO_M2 / CM2_TO_M2;
900 case Qgis::AreaUnit::SquareMillimeters:
901 return FT2_TO_M2 / MM2_TO_M2;
902 case Qgis::AreaUnit::Unknown:
903 break;
904 }
905
906 break;
907 }
908
909 case Qgis::AreaUnit::SquareYards:
910 {
911 switch ( toUnit )
912 {
913 case Qgis::AreaUnit::SquareMeters:
914 return YD2_TO_M2;
915 case Qgis::AreaUnit::SquareKilometers:
916 return YD2_TO_M2 / KM2_TO_M2;
917 case Qgis::AreaUnit::SquareFeet:
918 return YD2_TO_M2 / FT2_TO_M2;
919 case Qgis::AreaUnit::SquareYards:
920 return 1.0;
921 case Qgis::AreaUnit::SquareMiles:
922 return YD2_TO_M2 / MI2_TO_M2;
923 case Qgis::AreaUnit::Hectares:
924 return YD2_TO_M2 / HA_TO_M2;
925 case Qgis::AreaUnit::Acres:
926 return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
927 case Qgis::AreaUnit::SquareNauticalMiles:
928 return YD2_TO_M2 / NM2_TO_M2;
929 case Qgis::AreaUnit::SquareDegrees:
930 return YD2_TO_M2 / DEG2_TO_M2;
931 case Qgis::AreaUnit::SquareCentimeters:
932 return YD2_TO_M2 / CM2_TO_M2;
933 case Qgis::AreaUnit::SquareMillimeters:
934 return YD2_TO_M2 / MM2_TO_M2;
935 case Qgis::AreaUnit::Unknown:
936 break;
937 }
938 break;
939 }
940
941 case Qgis::AreaUnit::SquareMiles:
942 {
943 switch ( toUnit )
944 {
945 case Qgis::AreaUnit::SquareMeters:
946 return MI2_TO_M2;
947 case Qgis::AreaUnit::SquareKilometers:
948 return MI2_TO_M2 / KM2_TO_M2;
949 case Qgis::AreaUnit::SquareFeet:
950 return MI2_TO_M2 / FT2_TO_M2;
951 case Qgis::AreaUnit::SquareYards:
952 return MI2_TO_M2 / YD2_TO_M2;
953 case Qgis::AreaUnit::SquareMiles:
954 return 1.0;
955 case Qgis::AreaUnit::Hectares:
956 return MI2_TO_M2 / HA_TO_M2;
957 case Qgis::AreaUnit::Acres:
958 return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
959 case Qgis::AreaUnit::SquareNauticalMiles:
960 return MI2_TO_M2 / NM2_TO_M2;
961 case Qgis::AreaUnit::SquareDegrees:
962 return MI2_TO_M2 / DEG2_TO_M2;
963 case Qgis::AreaUnit::SquareCentimeters:
964 return MI2_TO_M2 / CM2_TO_M2;
965 case Qgis::AreaUnit::SquareMillimeters:
966 return MI2_TO_M2 / MM2_TO_M2;
967 case Qgis::AreaUnit::Unknown:
968 break;
969 }
970
971 break;
972 }
973
974 case Qgis::AreaUnit::Hectares:
975 {
976 switch ( toUnit )
977 {
978 case Qgis::AreaUnit::SquareMeters:
979 return HA_TO_M2;
980 case Qgis::AreaUnit::SquareKilometers:
981 return HA_TO_M2 / KM2_TO_M2;
982 case Qgis::AreaUnit::SquareFeet:
983 return HA_TO_M2 / FT2_TO_M2;
984 case Qgis::AreaUnit::SquareYards:
985 return HA_TO_M2 / YD2_TO_M2;
986 case Qgis::AreaUnit::SquareMiles:
987 return HA_TO_M2 / MI2_TO_M2;
988 case Qgis::AreaUnit::Hectares:
989 return 1.0;
990 case Qgis::AreaUnit::Acres:
991 return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
992 case Qgis::AreaUnit::SquareNauticalMiles:
993 return HA_TO_M2 / NM2_TO_M2;
994 case Qgis::AreaUnit::SquareDegrees:
995 return HA_TO_M2 / DEG2_TO_M2;
996 case Qgis::AreaUnit::SquareCentimeters:
997 return HA_TO_M2 / CM2_TO_M2;
998 case Qgis::AreaUnit::SquareMillimeters:
999 return HA_TO_M2 / MM2_TO_M2;
1000 case Qgis::AreaUnit::Unknown:
1001 break;
1002 }
1003
1004 break;
1005 }
1006
1007 case Qgis::AreaUnit::Acres:
1008 {
1009 switch ( toUnit )
1010 {
1011 case Qgis::AreaUnit::SquareMeters:
1012 return AC_TO_FT2 * FT2_TO_M2;
1013 case Qgis::AreaUnit::SquareKilometers:
1014 return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
1015 case Qgis::AreaUnit::SquareFeet:
1016 return AC_TO_FT2;
1017 case Qgis::AreaUnit::SquareYards:
1018 return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
1019 case Qgis::AreaUnit::SquareMiles:
1020 return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
1021 case Qgis::AreaUnit::Hectares:
1022 return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
1023 case Qgis::AreaUnit::Acres:
1024 return 1.0;
1025 case Qgis::AreaUnit::SquareNauticalMiles:
1026 return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
1027 case Qgis::AreaUnit::SquareDegrees:
1028 return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
1029 case Qgis::AreaUnit::SquareCentimeters:
1030 return AC_TO_FT2 * FT2_TO_M2 / CM2_TO_M2;
1031 case Qgis::AreaUnit::SquareMillimeters:
1032 return AC_TO_FT2 * FT2_TO_M2 / MM2_TO_M2;
1033 case Qgis::AreaUnit::Unknown:
1034 break;
1035 }
1036
1037 break;
1038 }
1039
1040 case Qgis::AreaUnit::SquareNauticalMiles:
1041 {
1042 switch ( toUnit )
1043 {
1044 case Qgis::AreaUnit::SquareMeters:
1045 return NM2_TO_M2;
1046 case Qgis::AreaUnit::SquareKilometers:
1047 return NM2_TO_M2 / KM2_TO_M2;
1048 case Qgis::AreaUnit::SquareFeet:
1049 return NM2_TO_M2 / FT2_TO_M2;
1050 case Qgis::AreaUnit::SquareYards:
1051 return NM2_TO_M2 / YD2_TO_M2;
1052 case Qgis::AreaUnit::SquareMiles:
1053 return NM2_TO_M2 / MI2_TO_M2;
1054 case Qgis::AreaUnit::Hectares:
1055 return NM2_TO_M2 / HA_TO_M2;
1056 case Qgis::AreaUnit::Acres:
1057 return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1058 case Qgis::AreaUnit::SquareNauticalMiles:
1059 return 1.0;
1060 case Qgis::AreaUnit::SquareDegrees:
1061 return NM2_TO_M2 / DEG2_TO_M2;
1062 case Qgis::AreaUnit::SquareCentimeters:
1063 return NM2_TO_M2 / CM2_TO_M2;
1064 case Qgis::AreaUnit::SquareMillimeters:
1065 return NM2_TO_M2 / MM2_TO_M2;
1066 case Qgis::AreaUnit::Unknown:
1067 break;
1068 }
1069
1070 break;
1071 }
1072
1073 case Qgis::AreaUnit::SquareDegrees:
1074 {
1075 switch ( toUnit )
1076 {
1077 case Qgis::AreaUnit::SquareMeters:
1078 return DEG2_TO_M2;
1079 case Qgis::AreaUnit::SquareKilometers:
1080 return DEG2_TO_M2 / KM2_TO_M2;
1081 case Qgis::AreaUnit::SquareFeet:
1082 return DEG2_TO_M2 / FT2_TO_M2;
1083 case Qgis::AreaUnit::SquareYards:
1084 return DEG2_TO_M2 / YD2_TO_M2;
1085 case Qgis::AreaUnit::SquareMiles:
1086 return DEG2_TO_M2 / MI2_TO_M2;
1087 case Qgis::AreaUnit::Hectares:
1088 return DEG2_TO_M2 / HA_TO_M2;
1089 case Qgis::AreaUnit::Acres:
1090 return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1091 case Qgis::AreaUnit::SquareNauticalMiles:
1092 return DEG2_TO_M2 / NM2_TO_M2;
1093 case Qgis::AreaUnit::SquareDegrees:
1094 return 1.0;
1095 case Qgis::AreaUnit::SquareCentimeters:
1096 return DEG2_TO_M2 / CM2_TO_M2;
1097 case Qgis::AreaUnit::SquareMillimeters:
1098 return DEG2_TO_M2 / MM2_TO_M2;
1099 case Qgis::AreaUnit::Unknown:
1100 break;
1101 }
1102
1103 break;
1104 }
1105
1106 case Qgis::AreaUnit::SquareMillimeters:
1107 {
1108 switch ( toUnit )
1109 {
1110 case Qgis::AreaUnit::SquareMeters:
1111 return MM2_TO_M2;
1112 case Qgis::AreaUnit::SquareKilometers:
1113 return MM2_TO_M2 / KM2_TO_M2;
1114 case Qgis::AreaUnit::SquareFeet:
1115 return MM2_TO_M2 / FT2_TO_M2;
1116 case Qgis::AreaUnit::SquareYards:
1117 return MM2_TO_M2 / YD2_TO_M2;
1118 case Qgis::AreaUnit::SquareMiles:
1119 return MM2_TO_M2 / MI2_TO_M2;
1120 case Qgis::AreaUnit::Hectares:
1121 return MM2_TO_M2 / HA_TO_M2;
1122 case Qgis::AreaUnit::Acres:
1123 return MM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1124 case Qgis::AreaUnit::SquareNauticalMiles:
1125 return MM2_TO_M2 / NM2_TO_M2;
1126 case Qgis::AreaUnit::SquareDegrees:
1127 return MM2_TO_M2 / DEG2_TO_M2;
1128 case Qgis::AreaUnit::SquareCentimeters:
1129 return MM2_TO_M2 / CM2_TO_M2;
1130 case Qgis::AreaUnit::SquareMillimeters:
1131 return 1.0;
1132 case Qgis::AreaUnit::Unknown:
1133 break;
1134 }
1135
1136 break;
1137 }
1138 case Qgis::AreaUnit::SquareCentimeters:
1139 {
1140 switch ( toUnit )
1141 {
1142 case Qgis::AreaUnit::SquareMeters:
1143 return CM2_TO_M2;
1144 case Qgis::AreaUnit::SquareKilometers:
1145 return CM2_TO_M2 / KM2_TO_M2;
1146 case Qgis::AreaUnit::SquareFeet:
1147 return CM2_TO_M2 / FT2_TO_M2;
1148 case Qgis::AreaUnit::SquareYards:
1149 return CM2_TO_M2 / YD2_TO_M2;
1150 case Qgis::AreaUnit::SquareMiles:
1151 return CM2_TO_M2 / MI2_TO_M2;
1152 case Qgis::AreaUnit::Hectares:
1153 return CM2_TO_M2 / HA_TO_M2;
1154 case Qgis::AreaUnit::Acres:
1155 return CM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1156 case Qgis::AreaUnit::SquareNauticalMiles:
1157 return CM2_TO_M2 / NM2_TO_M2;
1158 case Qgis::AreaUnit::SquareDegrees:
1159 return CM2_TO_M2 / DEG2_TO_M2;
1160 case Qgis::AreaUnit::SquareCentimeters:
1161 return 1.0;
1162 case Qgis::AreaUnit::SquareMillimeters:
1163 return CM2_TO_M2 / MM2_TO_M2;
1164 case Qgis::AreaUnit::Unknown:
1165 break;
1166 }
1167
1168 break;
1169 }
1170 case Qgis::AreaUnit::Unknown:
1171 break;
1172 }
1173 return 1.0;
1174}
1175
1177{
1178 switch ( distanceUnit )
1179 {
1180 case Qgis::DistanceUnit::Meters:
1181 return Qgis::AreaUnit::SquareMeters;
1182
1183 case Qgis::DistanceUnit::Kilometers:
1184 return Qgis::AreaUnit::SquareKilometers;
1185
1186 case Qgis::DistanceUnit::Centimeters:
1187 return Qgis::AreaUnit::SquareCentimeters;
1188
1189 case Qgis::DistanceUnit::Millimeters:
1190 return Qgis::AreaUnit::SquareMillimeters;
1191
1192 case Qgis::DistanceUnit::Feet:
1193 return Qgis::AreaUnit::SquareFeet;
1194
1195 case Qgis::DistanceUnit::Yards:
1196 return Qgis::AreaUnit::SquareYards;
1197
1198 case Qgis::DistanceUnit::Miles:
1199 return Qgis::AreaUnit::SquareMiles;
1200
1201 case Qgis::DistanceUnit::Degrees:
1202 return Qgis::AreaUnit::SquareDegrees;
1203
1204 case Qgis::DistanceUnit::Unknown:
1205 return Qgis::AreaUnit::Unknown;
1206
1207 case Qgis::DistanceUnit::NauticalMiles:
1208 return Qgis::AreaUnit::SquareNauticalMiles;
1209 }
1210
1211 return Qgis::AreaUnit::Unknown;
1212}
1213
1215{
1216 switch ( areaUnit )
1217 {
1218 case Qgis::AreaUnit::SquareMeters:
1219 case Qgis::AreaUnit::Hectares:
1220 return Qgis::DistanceUnit::Meters;
1221
1222 case Qgis::AreaUnit::SquareKilometers:
1223 return Qgis::DistanceUnit::Kilometers;
1224
1225 case Qgis::AreaUnit::SquareCentimeters:
1226 return Qgis::DistanceUnit::Centimeters;
1227
1228 case Qgis::AreaUnit::SquareMillimeters:
1229 return Qgis::DistanceUnit::Millimeters;
1230
1231 case Qgis::AreaUnit::SquareFeet:
1232 return Qgis::DistanceUnit::Feet;
1233
1234 case Qgis::AreaUnit::SquareYards:
1235 case Qgis::AreaUnit::Acres:
1236 return Qgis::DistanceUnit::Yards;
1237
1238 case Qgis::AreaUnit::SquareMiles:
1239 return Qgis::DistanceUnit::Miles;
1240
1241 case Qgis::AreaUnit::SquareDegrees:
1242 return Qgis::DistanceUnit::Degrees;
1243
1244 case Qgis::AreaUnit::Unknown:
1245 return Qgis::DistanceUnit::Unknown;
1246
1247 case Qgis::AreaUnit::SquareNauticalMiles:
1248 return Qgis::DistanceUnit::NauticalMiles;
1249 }
1250
1251 return Qgis::DistanceUnit::Unknown;
1252}
1253
1255{
1256 switch ( unit )
1257 {
1258 case Qgis::TemporalUnit::Seconds:
1259 return QStringLiteral( "s" );
1260 case Qgis::TemporalUnit::Milliseconds:
1261 return QStringLiteral( "ms" );
1262 case Qgis::TemporalUnit::Minutes:
1263 return QStringLiteral( "min" );
1264 case Qgis::TemporalUnit::Hours:
1265 return QStringLiteral( "h" );
1266 case Qgis::TemporalUnit::Days:
1267 return QStringLiteral( "d" );
1268 case Qgis::TemporalUnit::Weeks:
1269 return QStringLiteral( "wk" );
1270 case Qgis::TemporalUnit::Months:
1271 return QStringLiteral( "mon" );
1272 case Qgis::TemporalUnit::Years:
1273 return QStringLiteral( "y" );
1274 case Qgis::TemporalUnit::Decades:
1275 return QStringLiteral( "dec" );
1276 case Qgis::TemporalUnit::Centuries:
1277 return QStringLiteral( "c" );
1278 case Qgis::TemporalUnit::IrregularStep:
1279 return QStringLiteral( "xxx" );
1280 case Qgis::TemporalUnit::Unknown:
1281 return QStringLiteral( "<unknown>" );
1282 }
1283 return QString();
1284}
1285
1287{
1288 const QString normalized = string.trimmed().toLower();
1289
1290 if ( ok )
1291 *ok = true;
1292
1293 if ( normalized == encodeUnit( Qgis::TemporalUnit::Seconds ) )
1294 return Qgis::TemporalUnit::Seconds;
1295 if ( normalized == encodeUnit( Qgis::TemporalUnit::Milliseconds ) )
1296 return Qgis::TemporalUnit::Milliseconds;
1297 if ( normalized == encodeUnit( Qgis::TemporalUnit::Minutes ) )
1298 return Qgis::TemporalUnit::Minutes;
1299 if ( normalized == encodeUnit( Qgis::TemporalUnit::Hours ) )
1300 return Qgis::TemporalUnit::Hours;
1301 if ( normalized == encodeUnit( Qgis::TemporalUnit::Days ) )
1302 return Qgis::TemporalUnit::Days;
1303 if ( normalized == encodeUnit( Qgis::TemporalUnit::Weeks ) )
1304 return Qgis::TemporalUnit::Weeks;
1305 if ( normalized == encodeUnit( Qgis::TemporalUnit::Months ) )
1306 return Qgis::TemporalUnit::Months;
1307 if ( normalized == encodeUnit( Qgis::TemporalUnit::Years ) )
1308 return Qgis::TemporalUnit::Years;
1309 if ( normalized == encodeUnit( Qgis::TemporalUnit::Decades ) )
1310 return Qgis::TemporalUnit::Decades;
1311 if ( normalized == encodeUnit( Qgis::TemporalUnit::Centuries ) )
1312 return Qgis::TemporalUnit::Centuries;
1313 if ( normalized == encodeUnit( Qgis::TemporalUnit::IrregularStep ) )
1314 return Qgis::TemporalUnit::IrregularStep;
1315 if ( normalized == encodeUnit( Qgis::TemporalUnit::Unknown ) )
1316 return Qgis::TemporalUnit::Unknown;
1317
1318 if ( ok )
1319 *ok = false;
1320
1321 return Qgis::TemporalUnit::Unknown;
1322}
1323
1325{
1326 switch ( unit )
1327 {
1328 case Qgis::TemporalUnit::Seconds:
1329 return QObject::tr( "seconds", "temporal" );
1330 case Qgis::TemporalUnit::Milliseconds:
1331 return QObject::tr( "milliseconds", "temporal" );
1332 case Qgis::TemporalUnit::Minutes:
1333 return QObject::tr( "minutes", "temporal" );
1334 case Qgis::TemporalUnit::Hours:
1335 return QObject::tr( "hours", "temporal" );
1336 case Qgis::TemporalUnit::Days:
1337 return QObject::tr( "days", "temporal" );
1338 case Qgis::TemporalUnit::Weeks:
1339 return QObject::tr( "weeks", "temporal" );
1340 case Qgis::TemporalUnit::Months:
1341 return QObject::tr( "months", "temporal" );
1342 case Qgis::TemporalUnit::Years:
1343 return QObject::tr( "years", "temporal" );
1344 case Qgis::TemporalUnit::Decades:
1345 return QObject::tr( "decades", "temporal" );
1346 case Qgis::TemporalUnit::Centuries:
1347 return QObject::tr( "centuries", "temporal" );
1348 case Qgis::TemporalUnit::IrregularStep:
1349 return QObject::tr( "steps", "temporal" );
1350 case Qgis::TemporalUnit::Unknown:
1351 return QObject::tr( "<unknown>", "temporal" );
1352 }
1353 return QString();
1354}
1355
1357{
1358 switch ( unit )
1359 {
1360 case Qgis::TemporalUnit::Seconds:
1361 return QObject::tr( "s", "temporal" );
1362 case Qgis::TemporalUnit::Milliseconds:
1363 return QObject::tr( "ms", "temporal" );
1364 case Qgis::TemporalUnit::Minutes:
1365 return QObject::tr( "min", "temporal" );
1366 case Qgis::TemporalUnit::Hours:
1367 return QObject::tr( "h", "temporal" );
1368 case Qgis::TemporalUnit::Days:
1369 return QObject::tr( "d", "temporal" );
1370 case Qgis::TemporalUnit::Weeks:
1371 return QObject::tr( "wk", "temporal" );
1372 case Qgis::TemporalUnit::Months:
1373 return QObject::tr( "mon", "temporal" );
1374 case Qgis::TemporalUnit::Years:
1375 return QObject::tr( "y", "temporal" );
1376 case Qgis::TemporalUnit::Decades:
1377 return QObject::tr( "dec", "temporal" );
1378 case Qgis::TemporalUnit::Centuries:
1379 return QObject::tr( "cen", "temporal" );
1380 case Qgis::TemporalUnit::IrregularStep:
1381 return QObject::tr( "steps", "temporal" );
1382 case Qgis::TemporalUnit::Unknown:
1383 return QObject::tr( "<unknown>", "temporal" );
1384 }
1385 return QString();
1386}
1387
1389{
1390 const QString normalized = string.trimmed().toLower();
1391
1392 if ( ok )
1393 *ok = true;
1394
1395 if ( normalized == toString( Qgis::TemporalUnit::Seconds ) )
1396 return Qgis::TemporalUnit::Seconds;
1397 if ( normalized == toString( Qgis::TemporalUnit::Milliseconds ) )
1398 return Qgis::TemporalUnit::Milliseconds;
1399 if ( normalized == toString( Qgis::TemporalUnit::Minutes ) )
1400 return Qgis::TemporalUnit::Minutes;
1401 if ( normalized == toString( Qgis::TemporalUnit::Hours ) )
1402 return Qgis::TemporalUnit::Hours;
1403 if ( normalized == toString( Qgis::TemporalUnit::Days ) )
1404 return Qgis::TemporalUnit::Days;
1405 if ( normalized == toString( Qgis::TemporalUnit::Weeks ) )
1406 return Qgis::TemporalUnit::Weeks;
1407 if ( normalized == toString( Qgis::TemporalUnit::Months ) )
1408 return Qgis::TemporalUnit::Months;
1409 if ( normalized == toString( Qgis::TemporalUnit::Years ) )
1410 return Qgis::TemporalUnit::Years;
1411 if ( normalized == toString( Qgis::TemporalUnit::Decades ) )
1412 return Qgis::TemporalUnit::Decades;
1413 if ( normalized == toString( Qgis::TemporalUnit::Centuries ) )
1414 return Qgis::TemporalUnit::Centuries;
1415 if ( normalized == toString( Qgis::TemporalUnit::IrregularStep ) )
1416 return Qgis::TemporalUnit::IrregularStep;
1417 if ( normalized == toString( Qgis::TemporalUnit::Unknown ) )
1418 return Qgis::TemporalUnit::Unknown;
1419
1420 if ( ok )
1421 *ok = false;
1422
1423 return Qgis::TemporalUnit::Unknown;
1424}
1425
1427{
1428 switch ( fromUnit )
1429 {
1430 case Qgis::TemporalUnit::Seconds:
1431 {
1432 switch ( toUnit )
1433 {
1434 case Qgis::TemporalUnit::Seconds:
1435 return 1.0;
1436 case Qgis::TemporalUnit::Milliseconds:
1437 return 1000.0;
1438 case Qgis::TemporalUnit::Minutes:
1439 return 1 / 60.0;
1440 case Qgis::TemporalUnit::Hours:
1441 return 1 / 3600.0;
1442 case Qgis::TemporalUnit::Days:
1443 return 1 / 86400.0;
1444 case Qgis::TemporalUnit::Weeks:
1445 return 1 / 604800.0;
1446 case Qgis::TemporalUnit::Months:
1447 return 1 / 2592000.0;
1448 case Qgis::TemporalUnit::Years:
1449 return 1 / 31557600.0;
1450 case Qgis::TemporalUnit::Decades:
1451 return 1 / 315576000.0;
1452 case Qgis::TemporalUnit::Centuries:
1453 return 1 / 3155760000.0;
1454 case Qgis::TemporalUnit::Unknown:
1455 case Qgis::TemporalUnit::IrregularStep:
1456 return 1.0;
1457 }
1458 break;
1459 }
1460 case Qgis::TemporalUnit::Milliseconds:
1461 {
1462 switch ( toUnit )
1463 {
1464 case Qgis::TemporalUnit::Seconds:
1465 return 1 / 1000.0;
1466 case Qgis::TemporalUnit::Milliseconds:
1467 return 1.0;
1468 case Qgis::TemporalUnit::Minutes:
1469 return 1 / 60000.0;
1470 case Qgis::TemporalUnit::Hours:
1471 return 1 / 3600000.0;
1472 case Qgis::TemporalUnit::Days:
1473 return 1 / 86400000.0;
1474 case Qgis::TemporalUnit::Weeks:
1475 return 1 / 60480000.0;
1476 case Qgis::TemporalUnit::Months:
1477 return 1 / 259200000.0;
1478 case Qgis::TemporalUnit::Years:
1479 return 1 / 3155760000.0;
1480 case Qgis::TemporalUnit::Decades:
1481 return 1 / 31557600000.0;
1482 case Qgis::TemporalUnit::Centuries:
1483 return 1 / 315576000000.0;
1484 case Qgis::TemporalUnit::Unknown:
1485 case Qgis::TemporalUnit::IrregularStep:
1486 return 1.0;
1487 }
1488 break;
1489 }
1490 case Qgis::TemporalUnit::Minutes:
1491 {
1492 switch ( toUnit )
1493 {
1494 case Qgis::TemporalUnit::Seconds:
1495 return 60.0;
1496 case Qgis::TemporalUnit::Milliseconds:
1497 return 60000.0;
1498 case Qgis::TemporalUnit::Minutes:
1499 return 1;
1500 case Qgis::TemporalUnit::Hours:
1501 return 1 / 60.0;
1502 case Qgis::TemporalUnit::Days:
1503 return 1 / 1440.0;
1504 case Qgis::TemporalUnit::Weeks:
1505 return 1 / 10080.0;
1506 case Qgis::TemporalUnit::Months:
1507 return 1 / 43200.0;
1508 case Qgis::TemporalUnit::Years:
1509 return 1 / 525960.0;
1510 case Qgis::TemporalUnit::Decades:
1511 return 1 / 5259600.0;
1512 case Qgis::TemporalUnit::Centuries:
1513 return 1 / 52596000.0;
1514 case Qgis::TemporalUnit::Unknown:
1515 case Qgis::TemporalUnit::IrregularStep:
1516 return 1.0;
1517 }
1518 break;
1519 }
1520 case Qgis::TemporalUnit::Hours:
1521 {
1522 switch ( toUnit )
1523 {
1524 case Qgis::TemporalUnit::Seconds:
1525 return 3600.0;
1526 case Qgis::TemporalUnit::Milliseconds:
1527 return 3600000.0;
1528 case Qgis::TemporalUnit::Minutes:
1529 return 60;
1530 case Qgis::TemporalUnit::Hours:
1531 return 1;
1532 case Qgis::TemporalUnit::Days:
1533 return 1 / 24.0;
1534 case Qgis::TemporalUnit::Weeks:
1535 return 1 / 168.0;
1536 case Qgis::TemporalUnit::Months:
1537 return 1 / 720.0;
1538 case Qgis::TemporalUnit::Years:
1539 return 1 / 8766.0;
1540 case Qgis::TemporalUnit::Decades:
1541 return 1 / 87660.0;
1542 case Qgis::TemporalUnit::Centuries:
1543 return 1 / 876600.0;
1544 case Qgis::TemporalUnit::Unknown:
1545 case Qgis::TemporalUnit::IrregularStep:
1546 return 1.0;
1547 }
1548 break;
1549 }
1550 case Qgis::TemporalUnit::Days:
1551 {
1552 switch ( toUnit )
1553 {
1554 case Qgis::TemporalUnit::Seconds:
1555 return 86400.0;
1556 case Qgis::TemporalUnit::Milliseconds:
1557 return 86400000.0;
1558 case Qgis::TemporalUnit::Minutes:
1559 return 1440;
1560 case Qgis::TemporalUnit::Hours:
1561 return 24;
1562 case Qgis::TemporalUnit::Days:
1563 return 1;
1564 case Qgis::TemporalUnit::Weeks:
1565 return 1 / 7.0;
1566 case Qgis::TemporalUnit::Months:
1567 return 1 / 30.0;
1568 case Qgis::TemporalUnit::Years:
1569 return 1 / 365.25;
1570 case Qgis::TemporalUnit::Decades:
1571 return 1 / 3652.5;
1572 case Qgis::TemporalUnit::Centuries:
1573 return 1 / 36525.0;
1574 case Qgis::TemporalUnit::Unknown:
1575 case Qgis::TemporalUnit::IrregularStep:
1576 return 1.0;
1577 }
1578 break;
1579 }
1580 case Qgis::TemporalUnit::Weeks:
1581 {
1582 switch ( toUnit )
1583 {
1584 case Qgis::TemporalUnit::Seconds:
1585 return 604800.0;
1586 case Qgis::TemporalUnit::Milliseconds:
1587 return 604800000.0;
1588 case Qgis::TemporalUnit::Minutes:
1589 return 10080;
1590 case Qgis::TemporalUnit::Hours:
1591 return 168;
1592 case Qgis::TemporalUnit::Days:
1593 return 7;
1594 case Qgis::TemporalUnit::Weeks:
1595 return 1;
1596 case Qgis::TemporalUnit::Months:
1597 return 7 / 30.0;
1598 case Qgis::TemporalUnit::Years:
1599 return 7 / 365.25;
1600 case Qgis::TemporalUnit::Decades:
1601 return 7 / 3652.5;
1602 case Qgis::TemporalUnit::Centuries:
1603 return 7 / 36525.0;
1604 case Qgis::TemporalUnit::Unknown:
1605 case Qgis::TemporalUnit::IrregularStep:
1606 return 1.0;
1607 }
1608 break;
1609 }
1610 case Qgis::TemporalUnit::Months:
1611 {
1612 switch ( toUnit )
1613 {
1614 case Qgis::TemporalUnit::Seconds:
1615 return 2592000.0;
1616 case Qgis::TemporalUnit::Milliseconds:
1617 return 2592000000.0;
1618 case Qgis::TemporalUnit::Minutes:
1619 return 43200;
1620 case Qgis::TemporalUnit::Hours:
1621 return 720;
1622 case Qgis::TemporalUnit::Days:
1623 return 30;
1624 case Qgis::TemporalUnit::Weeks:
1625 return 30 / 7.0;
1626 case Qgis::TemporalUnit::Months:
1627 return 1;
1628 case Qgis::TemporalUnit::Years:
1629 return 30 / 365.25;
1630 case Qgis::TemporalUnit::Decades:
1631 return 30 / 3652.5;
1632 case Qgis::TemporalUnit::Centuries:
1633 return 30 / 36525.0;
1634 case Qgis::TemporalUnit::Unknown:
1635 case Qgis::TemporalUnit::IrregularStep:
1636 return 1.0;
1637 }
1638 break;
1639 }
1640 case Qgis::TemporalUnit::Years:
1641 {
1642 switch ( toUnit )
1643 {
1644 case Qgis::TemporalUnit::Seconds:
1645 return 31557600.0;
1646 case Qgis::TemporalUnit::Milliseconds:
1647 return 31557600000.0;
1648 case Qgis::TemporalUnit::Minutes:
1649 return 525960.0;
1650 case Qgis::TemporalUnit::Hours:
1651 return 8766.0;
1652 case Qgis::TemporalUnit::Days:
1653 return 365.25;
1654 case Qgis::TemporalUnit::Weeks:
1655 return 365.25 / 7.0;
1656 case Qgis::TemporalUnit::Months:
1657 return 365.25 / 30.0;
1658 case Qgis::TemporalUnit::Years:
1659 return 1;
1660 case Qgis::TemporalUnit::Decades:
1661 return 0.1;
1662 case Qgis::TemporalUnit::Centuries:
1663 return 0.01;
1664 case Qgis::TemporalUnit::Unknown:
1665 case Qgis::TemporalUnit::IrregularStep:
1666 return 1.0;
1667 }
1668 break;
1669 }
1670 case Qgis::TemporalUnit::Decades:
1671 {
1672 switch ( toUnit )
1673 {
1674 case Qgis::TemporalUnit::Seconds:
1675 return 315576000.0;
1676 case Qgis::TemporalUnit::Milliseconds:
1677 return 315576000000.0;
1678 case Qgis::TemporalUnit::Minutes:
1679 return 5259600.0;
1680 case Qgis::TemporalUnit::Hours:
1681 return 87660.0;
1682 case Qgis::TemporalUnit::Days:
1683 return 3652.5;
1684 case Qgis::TemporalUnit::Weeks:
1685 return 3652.5 / 7.0;
1686 case Qgis::TemporalUnit::Months:
1687 return 3652.5 / 30.0;
1688 case Qgis::TemporalUnit::Years:
1689 return 10;
1690 case Qgis::TemporalUnit::Decades:
1691 return 1;
1692 case Qgis::TemporalUnit::Centuries:
1693 return 0.1;
1694 case Qgis::TemporalUnit::Unknown:
1695 case Qgis::TemporalUnit::IrregularStep:
1696 return 1.0;
1697 }
1698 break;
1699 }
1700
1701 case Qgis::TemporalUnit::Centuries:
1702 {
1703 switch ( toUnit )
1704 {
1705 case Qgis::TemporalUnit::Seconds:
1706 return 3155760000.0;
1707 case Qgis::TemporalUnit::Milliseconds:
1708 return 3155760000000.0;
1709 case Qgis::TemporalUnit::Minutes:
1710 return 52596000.0;
1711 case Qgis::TemporalUnit::Hours:
1712 return 876600.0;
1713 case Qgis::TemporalUnit::Days:
1714 return 36525;
1715 case Qgis::TemporalUnit::Weeks:
1716 return 36525 / 7.0;
1717 case Qgis::TemporalUnit::Months:
1718 return 36525 / 30.0;
1719 case Qgis::TemporalUnit::Years:
1720 return 100;
1721 case Qgis::TemporalUnit::Decades:
1722 return 10;
1723 case Qgis::TemporalUnit::Centuries:
1724 return 1;
1725 case Qgis::TemporalUnit::Unknown:
1726 case Qgis::TemporalUnit::IrregularStep:
1727 return 1.0;
1728 }
1729 break;
1730 }
1731
1732 case Qgis::TemporalUnit::Unknown:
1733 case Qgis::TemporalUnit::IrregularStep:
1734 {
1735 return 1.0;
1736 }
1737 }
1738 return 1.0;
1739}
1740
1741Qgis::VolumeUnit QgsUnitTypes::decodeVolumeUnit( const QString &string, bool *ok )
1742{
1743 const QString normalized = string.trimmed().toLower();
1744
1745 if ( ok )
1746 *ok = true;
1747
1748 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicMeters ) )
1749 return Qgis::VolumeUnit::CubicMeters;
1750 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicFeet ) )
1751 return Qgis::VolumeUnit::CubicFeet;
1752 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicYards ) )
1753 return Qgis::VolumeUnit::CubicYards;
1754 if ( normalized == encodeUnit( Qgis::VolumeUnit::Barrel ) )
1755 return Qgis::VolumeUnit::Barrel;
1756 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicDecimeter ) )
1757 return Qgis::VolumeUnit::CubicDecimeter;
1758 if ( normalized == encodeUnit( Qgis::VolumeUnit::Liters ) )
1759 return Qgis::VolumeUnit::Liters;
1760 if ( normalized == encodeUnit( Qgis::VolumeUnit::GallonUS ) )
1761 return Qgis::VolumeUnit::GallonUS;
1762 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicInch ) )
1763 return Qgis::VolumeUnit::CubicInch;
1764 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicCentimeter ) )
1765 return Qgis::VolumeUnit::CubicCentimeter;
1766 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicDegrees ) )
1767 return Qgis::VolumeUnit::CubicDegrees;
1768 if ( normalized == encodeUnit( Qgis::VolumeUnit::Unknown ) )
1769 return Qgis::VolumeUnit::Unknown;
1770
1771 if ( ok )
1772 *ok = false;
1773
1774 return Qgis::VolumeUnit::Unknown;
1775}
1776
1778{
1779 switch ( unit )
1780 {
1781 case Qgis::VolumeUnit::CubicMeters:
1782 return QObject::tr( "cubic meters", "volume" );
1783 case Qgis::VolumeUnit::CubicFeet:
1784 return QObject::tr( "cubic feet", "volume" );
1785 case Qgis::VolumeUnit::CubicYards:
1786 return QObject::tr( "cubic yards", "volume" );
1787 case Qgis::VolumeUnit::Barrel:
1788 return QObject::tr( "barrels", "volume" );
1789 case Qgis::VolumeUnit::CubicDecimeter:
1790 return QObject::tr( "cubic decimeters", "volume" );
1791 case Qgis::VolumeUnit::Liters:
1792 return QObject::tr( "liters", "volume" );
1793 case Qgis::VolumeUnit::GallonUS:
1794 return QObject::tr( "gallons", "volume" );
1795 case Qgis::VolumeUnit::CubicInch:
1796 return QObject::tr( "cubic inches", "volume" );
1797 case Qgis::VolumeUnit::CubicCentimeter:
1798 return QObject::tr( "cubic centimeters", "volume" );
1799 case Qgis::VolumeUnit::CubicDegrees:
1800 return QObject::tr( "cubic degrees", "volume" );
1801 case Qgis::VolumeUnit::Unknown:
1802 return QObject::tr( "<unknown>", "volume" );
1803 }
1804 return QString();
1805}
1806
1808{
1809 switch ( unit )
1810 {
1811 case Qgis::VolumeUnit::CubicMeters:
1812 return QObject::tr( "m³", "volume" );
1813 case Qgis::VolumeUnit::CubicFeet:
1814 return QObject::tr( "ft³", "volume" );
1815 case Qgis::VolumeUnit::CubicYards:
1816 return QObject::tr( "yds³", "volume" );
1817 case Qgis::VolumeUnit::Barrel:
1818 return QObject::tr( "bbl", "volume" );
1819 case Qgis::VolumeUnit::CubicDecimeter:
1820 return QObject::tr( "dm³", "volume" );
1821 case Qgis::VolumeUnit::Liters:
1822 return QObject::tr( "l", "volume" );
1823 case Qgis::VolumeUnit::GallonUS:
1824 return QObject::tr( "gal", "volume" );
1825 case Qgis::VolumeUnit::CubicInch:
1826 return QObject::tr( "in³", "volume" );
1827 case Qgis::VolumeUnit::CubicCentimeter:
1828 return QObject::tr( "cm³", "volume" );
1829 case Qgis::VolumeUnit::CubicDegrees:
1830 return QObject::tr( "deg³", "volume" );
1831 case Qgis::VolumeUnit::Unknown:
1832 return QObject::tr( "<unknown>", "volume" );
1833 }
1834 return QString();
1835
1836}
1837
1838Qgis::VolumeUnit QgsUnitTypes::stringToVolumeUnit( const QString &string, bool *ok )
1839{
1840 const QString normalized = string.trimmed().toLower();
1841
1842 if ( ok )
1843 *ok = true;
1844
1845 if ( normalized == toString( Qgis::VolumeUnit::CubicMeters ) )
1846 return Qgis::VolumeUnit::CubicMeters;
1847 if ( normalized == toString( Qgis::VolumeUnit::CubicFeet ) )
1848 return Qgis::VolumeUnit::CubicFeet;
1849 if ( normalized == toString( Qgis::VolumeUnit::CubicYards ) )
1850 return Qgis::VolumeUnit::CubicYards;
1851 if ( normalized == toString( Qgis::VolumeUnit::Barrel ) )
1852 return Qgis::VolumeUnit::Barrel;
1853 if ( normalized == toString( Qgis::VolumeUnit::CubicDecimeter ) )
1854 return Qgis::VolumeUnit::CubicDecimeter;
1855 if ( normalized == toString( Qgis::VolumeUnit::Liters ) )
1856 return Qgis::VolumeUnit::Liters;
1857 if ( normalized == toString( Qgis::VolumeUnit::GallonUS ) )
1858 return Qgis::VolumeUnit::GallonUS;
1859 if ( normalized == toString( Qgis::VolumeUnit::CubicInch ) )
1860 return Qgis::VolumeUnit::CubicInch;
1861 if ( normalized == toString( Qgis::VolumeUnit::CubicCentimeter ) )
1862 return Qgis::VolumeUnit::CubicCentimeter;
1863 if ( normalized == toString( Qgis::VolumeUnit::CubicDegrees ) )
1864 return Qgis::VolumeUnit::CubicDegrees;
1865 if ( normalized == toString( Qgis::VolumeUnit::Unknown ) )
1866 return Qgis::VolumeUnit::Unknown;
1867
1868 if ( ok )
1869 *ok = false;
1870
1871 return Qgis::VolumeUnit::Unknown;
1872}
1873
1874#define DEG2_TO_M3 1379474361572186.2
1876{
1877 switch ( fromUnit )
1878 {
1879 case Qgis::VolumeUnit::CubicMeters:
1880 {
1881 switch ( toUnit )
1882 {
1883 case Qgis::VolumeUnit::CubicMeters:
1884 return 1.0;
1885 case Qgis::VolumeUnit::CubicFeet:
1886 return 35.314666572222;
1887 case Qgis::VolumeUnit::CubicYards:
1888 return 1.307950613786;
1889 case Qgis::VolumeUnit::Barrel:
1890 return 6.2898107438466;
1891 case Qgis::VolumeUnit::CubicDecimeter:
1892 return 1000;
1893 case Qgis::VolumeUnit::Liters:
1894 return 1000;
1895 case Qgis::VolumeUnit::GallonUS:
1896 return 264.17205124156;
1897 case Qgis::VolumeUnit::CubicInch:
1898 return 61023.7438368;
1899 case Qgis::VolumeUnit::CubicCentimeter:
1900 return 1000000;
1901 case Qgis::VolumeUnit::CubicDegrees:
1902 return 1 / DEG2_TO_M3; // basically meaningless!
1903 case Qgis::VolumeUnit::Unknown:
1904 return 1.0;
1905 }
1906 break;
1907 }
1908 case Qgis::VolumeUnit::CubicFeet:
1909 {
1910 switch ( toUnit )
1911 {
1912 case Qgis::VolumeUnit::CubicMeters:
1913 return 0.028316846592;
1914 case Qgis::VolumeUnit::CubicFeet:
1915 return 1.0;
1916 case Qgis::VolumeUnit::CubicYards:
1917 return 0.037037037;
1918 case Qgis::VolumeUnit::Barrel:
1919 return 0.178107622;
1920 case Qgis::VolumeUnit::CubicDecimeter:
1921 return 28.31685;
1922 case Qgis::VolumeUnit::Liters:
1923 return 28.31685;
1924 case Qgis::VolumeUnit::GallonUS:
1925 return 7.480519954;
1926 case Qgis::VolumeUnit::CubicInch:
1927 return 1728.000629765;
1928 case Qgis::VolumeUnit::CubicCentimeter:
1929 return 28316.85;
1930 case Qgis::VolumeUnit::CubicDegrees:
1931 return 0.028316846592 / DEG2_TO_M3; // basically meaningless!
1932 case Qgis::VolumeUnit::Unknown:
1933 return 1.0;
1934 }
1935 break;
1936 }
1937 case Qgis::VolumeUnit::CubicYards:
1938 {
1939 switch ( toUnit )
1940 {
1941 case Qgis::VolumeUnit::CubicMeters:
1942 return 0.764554900;
1943 case Qgis::VolumeUnit::CubicFeet:
1944 return 26.999998234;
1945 case Qgis::VolumeUnit::CubicYards:
1946 return 1.0;
1947 case Qgis::VolumeUnit::Barrel:
1948 return 4.808905491;
1949 case Qgis::VolumeUnit::CubicDecimeter:
1950 return 764.5549;
1951 case Qgis::VolumeUnit::Liters:
1952 return 764.5549;
1953 case Qgis::VolumeUnit::GallonUS:
1954 return 201.974025549;
1955 case Qgis::VolumeUnit::CubicInch:
1956 return 46656.013952472;
1957 case Qgis::VolumeUnit::CubicCentimeter:
1958 return 764554.9;
1959 case Qgis::VolumeUnit::CubicDegrees:
1960 return 0.764554900 / DEG2_TO_M3; // basically meaningless!
1961 case Qgis::VolumeUnit::Unknown:
1962 return 1.0;
1963 }
1964 break;
1965 }
1966 case Qgis::VolumeUnit::Barrel:
1967 {
1968 switch ( toUnit )
1969 {
1970 case Qgis::VolumeUnit::CubicMeters:
1971 return 0.158987300;
1972 case Qgis::VolumeUnit::CubicFeet:
1973 return 5.614582837;
1974 case Qgis::VolumeUnit::CubicYards:
1975 return 0.207947526;
1976 case Qgis::VolumeUnit::Barrel:
1977 return 1.0;
1978 case Qgis::VolumeUnit::CubicDecimeter:
1979 return 158.9873;
1980 case Qgis::VolumeUnit::Liters:
1981 return 158.9873;
1982 case Qgis::VolumeUnit::GallonUS:
1983 return 41.999998943;
1984 case Qgis::VolumeUnit::CubicInch:
1985 return 9702.002677722;
1986 case Qgis::VolumeUnit::CubicCentimeter:
1987 return 158987.3;
1988 case Qgis::VolumeUnit::CubicDegrees:
1989 return 0.158987300 / DEG2_TO_M3; // basically meaningless!
1990 case Qgis::VolumeUnit::Unknown:
1991 return 1.0;
1992 }
1993 break;
1994 }
1995 case Qgis::VolumeUnit::CubicDecimeter:
1996 case Qgis::VolumeUnit::Liters:
1997 {
1998 switch ( toUnit )
1999 {
2000 case Qgis::VolumeUnit::CubicMeters:
2001 return 0.001;
2002 case Qgis::VolumeUnit::CubicFeet:
2003 return 0.035314662;
2004 case Qgis::VolumeUnit::CubicYards:
2005 return 0.001307951;
2006 case Qgis::VolumeUnit::Barrel:
2007 return 0.006289811;
2008 case Qgis::VolumeUnit::CubicDecimeter:
2009 case Qgis::VolumeUnit::Liters:
2010 return 1.0;
2011 case Qgis::VolumeUnit::GallonUS:
2012 return 0.264172037;
2013 case Qgis::VolumeUnit::CubicInch:
2014 return 61.023758990;
2015 case Qgis::VolumeUnit::CubicCentimeter:
2016 return 1000;
2017 case Qgis::VolumeUnit::CubicDegrees:
2018 return 0.001 / DEG2_TO_M3; // basically meaningless!
2019 case Qgis::VolumeUnit::Unknown:
2020 return 1.0;
2021 }
2022 break;
2023 }
2024 case Qgis::VolumeUnit::GallonUS:
2025 {
2026 switch ( toUnit )
2027 {
2028 case Qgis::VolumeUnit::CubicMeters:
2029 return 0.003785412;
2030 case Qgis::VolumeUnit::CubicFeet:
2031 return 0.133680547;
2032 case Qgis::VolumeUnit::CubicYards:
2033 return 0.004951132;
2034 case Qgis::VolumeUnit::Barrel:
2035 return 0.023809524;
2036 case Qgis::VolumeUnit::CubicDecimeter:
2037 case Qgis::VolumeUnit::Liters:
2038 return 3.785412000;
2039 case Qgis::VolumeUnit::GallonUS:
2040 return 1.0;
2041 case Qgis::VolumeUnit::CubicInch:
2042 return 231.000069567;
2043 case Qgis::VolumeUnit::CubicCentimeter:
2044 return 3785.412;
2045 case Qgis::VolumeUnit::CubicDegrees:
2046 return 0.003785412 / DEG2_TO_M3; // basically meaningless!
2047 case Qgis::VolumeUnit::Unknown:
2048 return 1.0;
2049 }
2050 break;
2051 }
2052 case Qgis::VolumeUnit::CubicInch:
2053 {
2054 switch ( toUnit )
2055 {
2056 case Qgis::VolumeUnit::CubicMeters:
2057 return 0.000016387;
2058 case Qgis::VolumeUnit::CubicFeet:
2059 return 0.000578703;
2060 case Qgis::VolumeUnit::CubicYards:
2061 return 0.000021433;
2062 case Qgis::VolumeUnit::Barrel:
2063 return 0.000103072;
2064 case Qgis::VolumeUnit::CubicDecimeter:
2065 case Qgis::VolumeUnit::Liters:
2066 return 0.016387060;
2067 case Qgis::VolumeUnit::GallonUS:
2068 return 0.004329003;
2069 case Qgis::VolumeUnit::CubicInch:
2070 return 1.0;
2071 case Qgis::VolumeUnit::CubicCentimeter:
2072 return 16.387060000;
2073 case Qgis::VolumeUnit::CubicDegrees:
2074 return 0.000016387 / DEG2_TO_M3; // basically meaningless!
2075 case Qgis::VolumeUnit::Unknown:
2076 return 1.0;
2077 }
2078 break;
2079 }
2080 case Qgis::VolumeUnit::CubicCentimeter:
2081 {
2082 switch ( toUnit )
2083 {
2084 case Qgis::VolumeUnit::CubicMeters:
2085 return 0.000001;
2086 case Qgis::VolumeUnit::CubicFeet:
2087 return 0.000035315;
2088 case Qgis::VolumeUnit::CubicYards:
2089 return 0.000001308;
2090 case Qgis::VolumeUnit::Barrel:
2091 return 0.000006290;
2092 case Qgis::VolumeUnit::CubicDecimeter:
2093 case Qgis::VolumeUnit::Liters:
2094 return 0.001;
2095 case Qgis::VolumeUnit::GallonUS:
2096 return 0.000264172 ;
2097 case Qgis::VolumeUnit::CubicInch:
2098 return 0.061023759;
2099 case Qgis::VolumeUnit::CubicCentimeter:
2100 return 1.0;
2101 case Qgis::VolumeUnit::CubicDegrees:
2102 return 0.000001 / DEG2_TO_M3; // basically meaningless!
2103 case Qgis::VolumeUnit::Unknown:
2104 return 1.0;
2105 }
2106 break;
2107 }
2108 case Qgis::VolumeUnit::CubicDegrees:
2109 if ( toUnit == Qgis::VolumeUnit::Unknown || toUnit == Qgis::VolumeUnit::CubicDegrees )
2110 return 1.0;
2111 else
2112 return fromUnitToUnitFactor( toUnit, Qgis::VolumeUnit::CubicMeters ) * DEG2_TO_M3;
2113
2114 case Qgis::VolumeUnit::Unknown:
2115 {
2116 return 1.0;
2117 }
2118 }
2119 return 1.0;
2120}
2121
2123{
2124 switch ( distanceUnit )
2125 {
2126 case Qgis::DistanceUnit::Meters:
2127 return Qgis::VolumeUnit::CubicMeters;
2128
2129 case Qgis::DistanceUnit::Kilometers:
2130 return Qgis::VolumeUnit::CubicMeters;
2131
2132 case Qgis::DistanceUnit::Centimeters:
2133 return Qgis::VolumeUnit::CubicCentimeter;
2134
2135 case Qgis::DistanceUnit::Millimeters:
2136 return Qgis::VolumeUnit::CubicCentimeter;
2137
2138 case Qgis::DistanceUnit::Feet:
2139 return Qgis::VolumeUnit::CubicFeet;
2140
2141 case Qgis::DistanceUnit::Yards:
2142 return Qgis::VolumeUnit::CubicYards;
2143
2144 case Qgis::DistanceUnit::Miles:
2145 return Qgis::VolumeUnit::CubicFeet;
2146
2147 case Qgis::DistanceUnit::Degrees:
2148 return Qgis::VolumeUnit::CubicDegrees;
2149
2150 case Qgis::DistanceUnit::Unknown:
2151 return Qgis::VolumeUnit::Unknown;
2152
2153 case Qgis::DistanceUnit::NauticalMiles:
2154 return Qgis::VolumeUnit::CubicFeet;
2155 }
2156
2157 return Qgis::VolumeUnit::Unknown;
2158}
2159
2161{
2162 switch ( volumeUnit )
2163 {
2164 case Qgis::VolumeUnit::CubicMeters:
2165 return Qgis::DistanceUnit::Meters;
2166 case Qgis::VolumeUnit::CubicFeet:
2167 return Qgis::DistanceUnit::Feet;
2168 case Qgis::VolumeUnit::CubicYards:
2169 return Qgis::DistanceUnit::Yards;
2170 case Qgis::VolumeUnit::Barrel:
2171 return Qgis::DistanceUnit::Feet;
2172 case Qgis::VolumeUnit::CubicDecimeter:
2173 return Qgis::DistanceUnit::Centimeters;
2174 case Qgis::VolumeUnit::Liters:
2175 return Qgis::DistanceUnit::Meters;
2176 case Qgis::VolumeUnit::GallonUS:
2177 return Qgis::DistanceUnit::Feet;
2178 case Qgis::VolumeUnit::CubicInch:
2179 return Qgis::DistanceUnit::Feet;
2180 case Qgis::VolumeUnit::CubicCentimeter:
2181 return Qgis::DistanceUnit::Centimeters;
2182 case Qgis::VolumeUnit::CubicDegrees:
2183 return Qgis::DistanceUnit::Degrees;
2184 case Qgis::VolumeUnit::Unknown:
2185 return Qgis::DistanceUnit::Unknown;
2186 }
2187 return Qgis::DistanceUnit::Unknown;
2188}
2189
2191{
2192 switch ( unit )
2193 {
2194 case Qgis::VolumeUnit::CubicMeters:
2195 case Qgis::VolumeUnit::CubicFeet:
2196 case Qgis::VolumeUnit::CubicYards:
2197 case Qgis::VolumeUnit::Barrel:
2198 case Qgis::VolumeUnit::CubicDecimeter:
2199 case Qgis::VolumeUnit::Liters:
2200 case Qgis::VolumeUnit::GallonUS:
2201 case Qgis::VolumeUnit::CubicInch:
2202 case Qgis::VolumeUnit::CubicCentimeter:
2204 case Qgis::VolumeUnit::CubicDegrees:
2206 case Qgis::VolumeUnit::Unknown:
2207 return Qgis::DistanceUnitType::Unknown;
2208 }
2209 return Qgis::DistanceUnitType::Unknown;
2210}
2211
2213{
2214 switch ( unit )
2215 {
2216 case Qgis::VolumeUnit::CubicMeters:
2217 return QStringLiteral( "m3" );
2218 case Qgis::VolumeUnit::CubicFeet:
2219 return QStringLiteral( "ft3" );
2220 case Qgis::VolumeUnit::CubicYards:
2221 return QStringLiteral( "yd3" );
2222 case Qgis::VolumeUnit::Barrel:
2223 return QStringLiteral( "bbl" );
2224 case Qgis::VolumeUnit::CubicDecimeter:
2225 return QStringLiteral( "dm3" );
2226 case Qgis::VolumeUnit::Liters:
2227 return QStringLiteral( "l" );
2228 case Qgis::VolumeUnit::GallonUS:
2229 return QStringLiteral( "gal" );
2230 case Qgis::VolumeUnit::CubicInch:
2231 return QStringLiteral( "in3" );
2232 case Qgis::VolumeUnit::CubicCentimeter:
2233 return QStringLiteral( "cm3" );
2234 case Qgis::VolumeUnit::CubicDegrees:
2235 return QStringLiteral( "deg3" );
2236 case Qgis::VolumeUnit::Unknown:
2237 return QStringLiteral( "<unknown>" );
2238 }
2239 return QString();
2240}
2241
2243{
2244 switch ( unit )
2245 {
2246 case Qgis::AngleUnit::Degrees:
2247 return QStringLiteral( "degrees" );
2248 case Qgis::AngleUnit::Radians:
2249 return QStringLiteral( "radians" );
2250 case Qgis::AngleUnit::Gon:
2251 return QStringLiteral( "gon" );
2252 case Qgis::AngleUnit::MinutesOfArc:
2253 return QStringLiteral( "moa" );
2254 case Qgis::AngleUnit::SecondsOfArc:
2255 return QStringLiteral( "soa" );
2256 case Qgis::AngleUnit::Turn:
2257 return QStringLiteral( "tr" );
2258 case Qgis::AngleUnit::MilliradiansSI:
2259 return QStringLiteral( "milliradians" );
2260 case Qgis::AngleUnit::MilNATO:
2261 return QStringLiteral( "mil" );
2262 case Qgis::AngleUnit::Unknown:
2263 return QStringLiteral( "<unknown>" );
2264 }
2265 return QString();
2266}
2267
2268Qgis::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
2269{
2270 const QString normalized = string.trimmed().toLower();
2271
2272 if ( ok )
2273 *ok = true;
2274
2275 if ( normalized == encodeUnit( Qgis::AngleUnit::Degrees ) )
2276 return Qgis::AngleUnit::Degrees;
2277 if ( normalized == encodeUnit( Qgis::AngleUnit::Radians ) )
2278 return Qgis::AngleUnit::Radians;
2279 if ( normalized == encodeUnit( Qgis::AngleUnit::Gon ) )
2280 return Qgis::AngleUnit::Gon;
2281 if ( normalized == encodeUnit( Qgis::AngleUnit::MinutesOfArc ) )
2282 return Qgis::AngleUnit::MinutesOfArc;
2283 if ( normalized == encodeUnit( Qgis::AngleUnit::SecondsOfArc ) )
2284 return Qgis::AngleUnit::SecondsOfArc;
2285 if ( normalized == encodeUnit( Qgis::AngleUnit::Turn ) )
2286 return Qgis::AngleUnit::Turn;
2287 if ( normalized == encodeUnit( Qgis::AngleUnit::MilliradiansSI ) )
2288 return Qgis::AngleUnit::MilliradiansSI;
2289 if ( normalized == encodeUnit( Qgis::AngleUnit::MilNATO ) )
2290 return Qgis::AngleUnit::MilNATO;
2291 if ( normalized == encodeUnit( Qgis::AngleUnit::Unknown ) )
2292 return Qgis::AngleUnit::Unknown;
2293 if ( ok )
2294 *ok = false;
2295
2296 return Qgis::AngleUnit::Unknown;
2297}
2298
2300{
2301 switch ( unit )
2302 {
2303 case Qgis::AngleUnit::Degrees:
2304 return QObject::tr( "degrees", "angle" );
2305 case Qgis::AngleUnit::Radians:
2306 return QObject::tr( "radians", "angle" );
2307 case Qgis::AngleUnit::Gon:
2308 return QObject::tr( "gon", "angle" );
2309 case Qgis::AngleUnit::MinutesOfArc:
2310 return QObject::tr( "minutes of arc", "angle" );
2311 case Qgis::AngleUnit::SecondsOfArc:
2312 return QObject::tr( "seconds of arc", "angle" );
2313 case Qgis::AngleUnit::Turn:
2314 return QObject::tr( "turns", "angle" );
2315 case Qgis::AngleUnit::MilliradiansSI:
2316 return QObject::tr( "milliradians", "angle" );
2317 case Qgis::AngleUnit::MilNATO:
2318 return QObject::tr( "mil", "angle" );
2319 case Qgis::AngleUnit::Unknown:
2320 return QObject::tr( "<unknown>", "angle" );
2321 }
2322 return QString();
2323}
2324
2326{
2327 // Calculate the conversion factor between the specified units
2328 switch ( fromUnit )
2329 {
2330 case Qgis::AngleUnit::Degrees:
2331 {
2332 switch ( toUnit )
2333 {
2334 case Qgis::AngleUnit::Degrees:
2335 return 1.0;
2336 case Qgis::AngleUnit::Radians:
2337 return M_PI / 180.0;
2338 case Qgis::AngleUnit::Gon:
2339 return 400.0 / 360.0;
2340 case Qgis::AngleUnit::MinutesOfArc:
2341 return 60;
2342 case Qgis::AngleUnit::SecondsOfArc:
2343 return 3600;
2344 case Qgis::AngleUnit::Turn:
2345 return 1.0 / 360.0;
2346 case Qgis::AngleUnit::MilliradiansSI:
2347 return M_PI / 180.0 * 1000;
2348 case Qgis::AngleUnit::MilNATO:
2349 return 3200.0 / 180;
2350 case Qgis::AngleUnit::Unknown:
2351 break;
2352 }
2353 break;
2354 }
2355 case Qgis::AngleUnit::Radians:
2356 {
2357 switch ( toUnit )
2358 {
2359 case Qgis::AngleUnit::Degrees:
2360 return 180.0 / M_PI;
2361 case Qgis::AngleUnit::Radians:
2362 return 1.0;
2363 case Qgis::AngleUnit::Gon:
2364 return 200.0 / M_PI;
2365 case Qgis::AngleUnit::MinutesOfArc:
2366 return 60 * 180.0 / M_PI;
2367 case Qgis::AngleUnit::SecondsOfArc:
2368 return 3600 * 180.0 / M_PI;
2369 case Qgis::AngleUnit::Turn:
2370 return 0.5 / M_PI;
2371 case Qgis::AngleUnit::MilliradiansSI:
2372 return 1000;
2373 case Qgis::AngleUnit::MilNATO:
2374 return 3200.0 / M_PI;
2375 case Qgis::AngleUnit::Unknown:
2376 break;
2377 }
2378 break;
2379 }
2380 case Qgis::AngleUnit::Gon:
2381 {
2382 switch ( toUnit )
2383 {
2384 case Qgis::AngleUnit::Degrees:
2385 return 360.0 / 400.0;
2386 case Qgis::AngleUnit::Radians:
2387 return M_PI / 200.0;
2388 case Qgis::AngleUnit::Gon:
2389 return 1.0;
2390 case Qgis::AngleUnit::MinutesOfArc:
2391 return 60 * 360.0 / 400.0;
2392 case Qgis::AngleUnit::SecondsOfArc:
2393 return 3600 * 360.0 / 400.0;
2394 case Qgis::AngleUnit::Turn:
2395 return 1.0 / 400.0;
2396 case Qgis::AngleUnit::MilliradiansSI:
2397 return M_PI / 200.0 * 1000;
2398 case Qgis::AngleUnit::MilNATO:
2399 return 3200.0 / 200.0;
2400 case Qgis::AngleUnit::Unknown:
2401 break;
2402 }
2403 break;
2404 }
2405 case Qgis::AngleUnit::MinutesOfArc:
2406 {
2407 switch ( toUnit )
2408 {
2409 case Qgis::AngleUnit::Degrees:
2410 return 1 / 60.0;
2411 case Qgis::AngleUnit::Radians:
2412 return M_PI / 180.0 / 60.0;
2413 case Qgis::AngleUnit::Gon:
2414 return 400.0 / 360.0 / 60.0;
2415 case Qgis::AngleUnit::MinutesOfArc:
2416 return 1.0;
2417 case Qgis::AngleUnit::SecondsOfArc:
2418 return 60.0;
2419 case Qgis::AngleUnit::Turn:
2420 return 1.0 / 360.0 / 60.0;
2421 case Qgis::AngleUnit::MilliradiansSI:
2422 return M_PI / 180.0 / 60.0 * 1000;
2423 case Qgis::AngleUnit::MilNATO:
2424 return 3200.0 / 180.0 / 60.0;
2425 case Qgis::AngleUnit::Unknown:
2426 break;
2427 }
2428 break;
2429 }
2430 case Qgis::AngleUnit::SecondsOfArc:
2431 {
2432 switch ( toUnit )
2433 {
2434 case Qgis::AngleUnit::Degrees:
2435 return 1 / 3600.0;
2436 case Qgis::AngleUnit::Radians:
2437 return M_PI / 180.0 / 3600.0;
2438 case Qgis::AngleUnit::Gon:
2439 return 400.0 / 360.0 / 3600.0;
2440 case Qgis::AngleUnit::MinutesOfArc:
2441 return 1.0 / 60.0;
2442 case Qgis::AngleUnit::SecondsOfArc:
2443 return 1.0;
2444 case Qgis::AngleUnit::Turn:
2445 return 1.0 / 360.0 / 3600.0;
2446 case Qgis::AngleUnit::MilliradiansSI:
2447 return M_PI / 180.0 / 3600.0 * 1000;
2448 case Qgis::AngleUnit::MilNATO:
2449 return 3200.0 / 180.0 / 3600.0;
2450 case Qgis::AngleUnit::Unknown:
2451 break;
2452 }
2453 break;
2454 }
2455 case Qgis::AngleUnit::Turn:
2456 {
2457 switch ( toUnit )
2458 {
2459 case Qgis::AngleUnit::Degrees:
2460 return 360.0;
2461 case Qgis::AngleUnit::Radians:
2462 return 2 * M_PI;
2463 case Qgis::AngleUnit::Gon:
2464 return 400.0;
2465 case Qgis::AngleUnit::MinutesOfArc:
2466 return 360.0 * 60.0;
2467 case Qgis::AngleUnit::SecondsOfArc:
2468 return 360.0 * 3600.0;
2469 case Qgis::AngleUnit::Turn:
2470 return 1.0;
2471 case Qgis::AngleUnit::MilliradiansSI:
2472 return 2 * M_PI * 1000;
2473 case Qgis::AngleUnit::MilNATO:
2474 return 2 * 3200;
2475 case Qgis::AngleUnit::Unknown:
2476 break;
2477 }
2478 break;
2479 }
2480 case Qgis::AngleUnit::MilliradiansSI:
2481 {
2482 switch ( toUnit )
2483 {
2484 case Qgis::AngleUnit::Degrees:
2485 return 180.0 / M_PI / 1000;
2486 case Qgis::AngleUnit::Radians:
2487 return 0.001;
2488 case Qgis::AngleUnit::Gon:
2489 return 200.0 / M_PI / 1000;
2490 case Qgis::AngleUnit::MinutesOfArc:
2491 return 180.0 * 60.0 / M_PI / 1000;
2492 case Qgis::AngleUnit::SecondsOfArc:
2493 return 180.0 * 3600.0 / M_PI / 1000;
2494 case Qgis::AngleUnit::Turn:
2495 return M_PI / 2 / 1000;
2496 case Qgis::AngleUnit::MilliradiansSI:
2497 return 1.0;
2498 case Qgis::AngleUnit::MilNATO:
2499 return 3200.0 / 1000.0 / M_PI;
2500 case Qgis::AngleUnit::Unknown:
2501 break;
2502 }
2503 break;
2504 }
2505
2506 case Qgis::AngleUnit::MilNATO:
2507 {
2508 switch ( toUnit )
2509 {
2510 case Qgis::AngleUnit::Degrees:
2511 return 180.0 / 3200;
2512 case Qgis::AngleUnit::Radians:
2513 return M_PI / 3200;
2514 case Qgis::AngleUnit::Gon:
2515 return 200.0 / 3200;
2516 case Qgis::AngleUnit::MinutesOfArc:
2517 return 60 * 180.0 / 3200;
2518 case Qgis::AngleUnit::SecondsOfArc:
2519 return 3600.0 * 180 / 3200;
2520 case Qgis::AngleUnit::Turn:
2521 return 1.0 / ( 2 * 32000 );
2522 case Qgis::AngleUnit::MilliradiansSI:
2523 return 1000.0 * M_PI / 3200.0;
2524 case Qgis::AngleUnit::MilNATO:
2525 return 1.0;
2526 case Qgis::AngleUnit::Unknown:
2527 break;
2528 }
2529 break;
2530 }
2531
2532 case Qgis::AngleUnit::Unknown:
2533 break;
2534 }
2535 return 1.0;
2536}
2537
2538QString QgsUnitTypes::formatAngle( double angle, int decimals, Qgis::AngleUnit unit )
2539{
2540 QString unitLabel;
2541 int decimalPlaces = 2;
2542
2543 switch ( unit )
2544 {
2545 case Qgis::AngleUnit::Degrees:
2546 unitLabel = QObject::tr( "°", "angle" );
2547 decimalPlaces = 0;
2548 break;
2549 case Qgis::AngleUnit::Radians:
2550 unitLabel = QObject::tr( " rad", "angle" );
2551 decimalPlaces = 2;
2552 break;
2553 case Qgis::AngleUnit::Gon:
2554 unitLabel = QObject::tr( " gon", "angle" );
2555 decimalPlaces = 0;
2556 break;
2557 case Qgis::AngleUnit::MinutesOfArc:
2558 unitLabel = QObject::tr( "′", "angle minutes" );
2559 decimalPlaces = 0;
2560 break;
2561 case Qgis::AngleUnit::SecondsOfArc:
2562 unitLabel = QObject::tr( "″", "angle seconds" );
2563 decimalPlaces = 0;
2564 break;
2565 case Qgis::AngleUnit::Turn:
2566 unitLabel = QObject::tr( " tr", "angle turn" );
2567 decimalPlaces = 3;
2568 break;
2569 case Qgis::AngleUnit::MilliradiansSI:
2570 unitLabel = QObject::tr( " millirad", "angular mil SI" );
2571 decimalPlaces = 0;
2572 break;
2573 case Qgis::AngleUnit::MilNATO:
2574 unitLabel = QObject::tr( " mil", "angular mil NATO" );
2575 decimalPlaces = 0;
2576 break;
2577 case Qgis::AngleUnit::Unknown:
2578 break;
2579 }
2580
2581 if ( decimals >= 0 )
2582 decimalPlaces = decimals;
2583
2584 return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimalPlaces ).arg( unitLabel );
2585}
2586
2587QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, Qgis::DistanceUnit unit, int decimals, bool keepBaseUnit )
2588{
2589 DistanceValue result;
2590
2591 switch ( unit )
2592 {
2593 case Qgis::DistanceUnit::Meters:
2594 if ( keepBaseUnit )
2595 {
2596 result.value = qgsRound( distance, decimals );
2597 result.unit = Qgis::DistanceUnit::Meters;
2598 }
2599 else if ( std::fabs( distance ) > 1000.0 )
2600 {
2601 result.value = qgsRound( distance / 1000, decimals );
2602 result.unit = Qgis::DistanceUnit::Kilometers;
2603 }
2604 else if ( std::fabs( distance ) < 0.01 )
2605 {
2606 result.value = qgsRound( distance * 1000, decimals );
2607 result.unit = Qgis::DistanceUnit::Millimeters;
2608 }
2609 else if ( std::fabs( distance ) < 0.1 )
2610 {
2611
2612 result.value = qgsRound( distance * 100, decimals );
2613 result.unit = Qgis::DistanceUnit::Centimeters;
2614 }
2615 else
2616 {
2617 result.value = qgsRound( distance, decimals );
2618 result.unit = Qgis::DistanceUnit::Meters;
2619 }
2620 break;
2621
2622 case Qgis::DistanceUnit::Kilometers:
2623 if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
2624 {
2625 result.value = qgsRound( distance, decimals );
2626 result.unit = Qgis::DistanceUnit::Kilometers;
2627 }
2628 else
2629 {
2630 result.value = qgsRound( distance * 1000, decimals );
2631 result.unit = Qgis::DistanceUnit::Meters;
2632 }
2633 break;
2634
2635 case Qgis::DistanceUnit::Feet:
2636 if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
2637 {
2638 result.value = qgsRound( distance, decimals );
2639 result.unit = Qgis::DistanceUnit::Feet;
2640 }
2641 else
2642 {
2643 result.value = qgsRound( distance / 5280.0, decimals );
2644 result.unit = Qgis::DistanceUnit::Miles;
2645 }
2646 break;
2647
2648 case Qgis::DistanceUnit::Yards:
2649 if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
2650 {
2651 result.value = qgsRound( distance, decimals );
2652 result.unit = Qgis::DistanceUnit::Yards;
2653 }
2654 else
2655 {
2656 result.value = qgsRound( distance / 1760.0, decimals );
2657 result.unit = Qgis::DistanceUnit::Miles;
2658 }
2659 break;
2660
2661 case Qgis::DistanceUnit::Miles:
2662 if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
2663 {
2664 result.value = qgsRound( distance, decimals );
2665 result.unit = Qgis::DistanceUnit::Miles;
2666 }
2667 else
2668 {
2669 result.value = qgsRound( distance * 5280.0, decimals );
2670 result.unit = Qgis::DistanceUnit::Feet;
2671 }
2672 break;
2673
2674 case Qgis::DistanceUnit::NauticalMiles:
2675 result.value = qgsRound( distance, decimals );
2676 result.unit = Qgis::DistanceUnit::NauticalMiles;
2677 break;
2678
2679 case Qgis::DistanceUnit::Degrees:
2680 result.value = qgsRound( distance, decimals );
2681 result.unit = Qgis::DistanceUnit::Degrees;
2682 break;
2683
2684 case Qgis::DistanceUnit::Unknown:
2685 result.value = qgsRound( distance, decimals );
2686 result.unit = Qgis::DistanceUnit::Unknown;
2687 break;
2688
2689 default:
2690 result.value = qgsRound( distance, decimals );
2691 result.unit = unit;
2692 break;
2693 }
2694
2695 return result;
2696}
2697
2698QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, Qgis::AreaUnit unit, int decimals, bool keepBaseUnit )
2699{
2701 result.value = -1.0;
2702 result.unit = Qgis::AreaUnit::Unknown;
2703
2704 // If we are not forced to keep the base units, switch to meter calculation
2705 if ( unit == Qgis::AreaUnit::SquareMillimeters )
2706 {
2707 if ( keepBaseUnit )
2708 {
2709 result.value = qgsRound( area, decimals );
2710 result.unit = Qgis::AreaUnit::SquareMillimeters;
2711 }
2712 else
2713 {
2714 area /= 1000000.0;
2715 unit = Qgis::AreaUnit::SquareMeters;
2716 }
2717 }
2718 else if ( unit == Qgis::AreaUnit::SquareCentimeters )
2719 {
2720 if ( keepBaseUnit )
2721 {
2722 result.value = qgsRound( area, decimals );
2723 result.unit = Qgis::AreaUnit::SquareCentimeters;
2724 }
2725 else
2726 {
2727 area /= 10000.0;
2728 unit = Qgis::AreaUnit::SquareMeters;
2729 }
2730 }
2731
2732 switch ( unit )
2733 {
2734 case Qgis::AreaUnit::SquareCentimeters:
2735 // handled in the if above
2736 break;
2737 case Qgis::AreaUnit::SquareMillimeters:
2738 // handled in the if above
2739 break;
2740 case Qgis::AreaUnit::SquareMeters:
2741 {
2742 if ( keepBaseUnit )
2743 {
2744 result.value = qgsRound( area, decimals );
2745 result.unit = Qgis::AreaUnit::SquareMeters;
2746 }
2747 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareKilometers, Qgis::AreaUnit::SquareMeters ) )
2748 {
2749 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMeters, Qgis::AreaUnit::SquareKilometers ), decimals );
2750 result.unit = Qgis::AreaUnit::SquareKilometers;
2751 }
2752 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::Hectares, Qgis::AreaUnit::SquareMeters ) )
2753 {
2754 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMeters, Qgis::AreaUnit::Hectares ), decimals );
2755 result.unit = Qgis::AreaUnit::Hectares;
2756 }
2757 else
2758 {
2759 result.value = qgsRound( area, decimals );
2760 result.unit = Qgis::AreaUnit::SquareMeters;
2761 }
2762 break;
2763 }
2764
2765 case Qgis::AreaUnit::SquareKilometers:
2766 {
2767 result.value = qgsRound( area, decimals );
2768 result.unit = Qgis::AreaUnit::SquareKilometers;
2769 break;
2770 }
2771
2772 case Qgis::AreaUnit::SquareFeet:
2773 {
2774 if ( keepBaseUnit )
2775 {
2776 result.value = qgsRound( area, decimals );
2777 result.unit = Qgis::AreaUnit::SquareFeet;
2778 }
2779 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMiles, Qgis::AreaUnit::SquareFeet ) )
2780 {
2781 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareFeet, Qgis::AreaUnit::SquareMiles ), decimals );
2782 result.unit = Qgis::AreaUnit::SquareMiles;
2783 }
2784 else
2785 {
2786 result.value = qgsRound( area, decimals );
2787 result.unit = Qgis::AreaUnit::SquareFeet;
2788 }
2789 break;
2790 }
2791
2792 case Qgis::AreaUnit::SquareYards:
2793 {
2794 if ( keepBaseUnit )
2795 {
2796 result.value = qgsRound( area, decimals );
2797 result.unit = Qgis::AreaUnit::SquareYards;
2798 }
2799 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMiles, Qgis::AreaUnit::SquareYards ) )
2800 {
2801 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareYards, Qgis::AreaUnit::SquareMiles ), decimals );
2802 result.unit = Qgis::AreaUnit::SquareMiles;
2803 }
2804 else
2805 {
2806 result.value = qgsRound( area, decimals );
2807 result.unit = Qgis::AreaUnit::SquareYards;
2808 }
2809 break;
2810 }
2811
2812 case Qgis::AreaUnit::SquareMiles:
2813 {
2814 result.value = qgsRound( area, decimals );
2815 result.unit = Qgis::AreaUnit::SquareMiles;
2816 break;
2817 }
2818
2819 case Qgis::AreaUnit::Hectares:
2820 {
2821 if ( keepBaseUnit )
2822 {
2823 result.value = qgsRound( area, decimals );
2824 result.unit = Qgis::AreaUnit::Hectares;
2825 }
2826 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareKilometers, Qgis::AreaUnit::Hectares ) )
2827 {
2828 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::Hectares, Qgis::AreaUnit::SquareKilometers ), decimals );
2829 result.unit = Qgis::AreaUnit::SquareKilometers;
2830 }
2831 else
2832 {
2833 result.value = qgsRound( area, decimals );
2834 result.unit = Qgis::AreaUnit::Hectares;
2835 }
2836 break;
2837 }
2838
2839 case Qgis::AreaUnit::Acres:
2840 {
2841 if ( keepBaseUnit )
2842 {
2843 result.value = qgsRound( area, decimals );
2844 result.unit = Qgis::AreaUnit::Acres;
2845 }
2846 else if ( std::fabs( area ) > QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMiles, Qgis::AreaUnit::Acres ) )
2847 {
2848 result.value = qgsRound( area * QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::Acres, Qgis::AreaUnit::SquareMiles ), decimals );
2849 result.unit = Qgis::AreaUnit::SquareMiles;
2850 }
2851 else
2852 {
2853 result.value = qgsRound( area, decimals );
2854 result.unit = Qgis::AreaUnit::Acres;
2855 }
2856 break;
2857 }
2858
2859 case Qgis::AreaUnit::SquareNauticalMiles:
2860 {
2861 result.value = qgsRound( area, decimals );
2862 result.unit = Qgis::AreaUnit::SquareNauticalMiles;
2863 break;
2864 }
2865
2866 case Qgis::AreaUnit::SquareDegrees:
2867 {
2868 result.value = qgsRound( area, decimals );
2869 result.unit = Qgis::AreaUnit::SquareDegrees;
2870 break;
2871 }
2872
2873 case Qgis::AreaUnit::Unknown:
2874 {
2875 result.value = qgsRound( area, decimals );
2876 result.unit = Qgis::AreaUnit::Unknown;
2877 break;
2878 }
2879 }
2880 return result;
2881}
2882
2883
2884QString QgsUnitTypes::formatDistance( double distance, int decimals, Qgis::DistanceUnit unit, bool keepBaseUnit )
2885{
2886 const DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
2887
2888 QString unitText;
2889
2890 if ( dist.unit != Qgis::DistanceUnit::Unknown )
2891 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
2892
2893 if ( qgsDoubleNear( dist.value, 0 ) )
2894 {
2895 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( unit );
2896 return QStringLiteral( "%L1%2" ).arg( distance, 0, 'e', decimals ).arg( unitText );
2897 }
2898 else
2899 {
2900 return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
2901 }
2902}
2903
2904QString QgsUnitTypes::formatArea( double area, int decimals, Qgis::AreaUnit unit, bool keepBaseUnit )
2905{
2906 const QgsUnitTypes::AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
2907
2908 QString unitText;
2909
2910 if ( areaValue.unit != Qgis::AreaUnit::Unknown )
2911 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
2912
2913 if ( qgsDoubleNear( areaValue.value, 0 ) )
2914 {
2915 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( unit );
2916 return QStringLiteral( "%L1%2" ).arg( area, 0, 'e', decimals ).arg( unitText );
2917 }
2918 else
2919 {
2920 return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
2921 }
2922}
2923
2925{
2926 switch ( unit )
2927 {
2928 case Qgis::RenderUnit::Millimeters:
2929 return QStringLiteral( "MM" );
2930 case Qgis::RenderUnit::MetersInMapUnits:
2931 return QStringLiteral( "RenderMetersInMapUnits" );
2932 case Qgis::RenderUnit::MapUnits:
2933 return QStringLiteral( "MapUnit" );
2934 case Qgis::RenderUnit::Pixels:
2935 return QStringLiteral( "Pixel" );
2936 case Qgis::RenderUnit::Percentage:
2937 return QStringLiteral( "Percentage" );
2938 case Qgis::RenderUnit::Points:
2939 return QStringLiteral( "Point" );
2940 case Qgis::RenderUnit::Inches:
2941 return QStringLiteral( "Inch" );
2942 case Qgis::RenderUnit::Unknown:
2943 return QString();
2944 }
2945 return QString();
2946}
2947
2948Qgis::RenderUnit QgsUnitTypes::decodeRenderUnit( const QString &string, bool *ok )
2949{
2950 const QString normalized = string.trimmed().toLower();
2951
2952 if ( ok )
2953 *ok = true;
2954
2955 if ( normalized == encodeUnit( Qgis::RenderUnit::Millimeters ).toLower() )
2956 return Qgis::RenderUnit::Millimeters;
2957 if ( normalized == encodeUnit( Qgis::RenderUnit::MetersInMapUnits ).toLower() )
2958 return Qgis::RenderUnit::MetersInMapUnits;
2959 if ( normalized == QLatin1String( "meters" ) )
2960 return Qgis::RenderUnit::MetersInMapUnits;
2961 if ( normalized == encodeUnit( Qgis::RenderUnit::MapUnits ).toLower() )
2962 return Qgis::RenderUnit::MapUnits;
2963 if ( normalized == QLatin1String( "mapunits" ) )
2964 return Qgis::RenderUnit::MapUnits;
2965 if ( normalized == encodeUnit( Qgis::RenderUnit::Pixels ).toLower() )
2966 return Qgis::RenderUnit::Pixels;
2967 if ( normalized == encodeUnit( Qgis::RenderUnit::Percentage ).toLower() )
2968 return Qgis::RenderUnit::Percentage;
2969 if ( normalized == QLatin1String( "percent" ) )
2970 return Qgis::RenderUnit::Percentage;
2971 if ( normalized == encodeUnit( Qgis::RenderUnit::Points ).toLower() )
2972 return Qgis::RenderUnit::Points;
2973 if ( normalized == QLatin1String( "points" ) )
2974 return Qgis::RenderUnit::Points;
2975 if ( normalized == encodeUnit( Qgis::RenderUnit::Inches ).toLower() )
2976 return Qgis::RenderUnit::Inches;
2977
2978 if ( ok )
2979 *ok = false;
2980
2981 // millimeters are default
2982 return Qgis::RenderUnit::Millimeters;
2983}
2984
2986{
2987 switch ( unit )
2988 {
2989 case Qgis::RenderUnit::Millimeters:
2990 return QObject::tr( "millimeters", "render" );
2991
2992 case Qgis::RenderUnit::MetersInMapUnits:
2993 return QObject::tr( "meters at scale", "render" );
2994
2995 case Qgis::RenderUnit::MapUnits:
2996 return QObject::tr( "map units", "render" );
2997
2998 case Qgis::RenderUnit::Pixels:
2999 return QObject::tr( "pixels", "render" );
3000
3001 case Qgis::RenderUnit::Percentage:
3002 return QObject::tr( "percent", "render" );
3003
3004 case Qgis::RenderUnit::Points:
3005 return QObject::tr( "points", "render" );
3006
3007 case Qgis::RenderUnit::Inches:
3008 return QObject::tr( "inches", "render" );
3009
3010 case Qgis::RenderUnit::Unknown:
3011 return QObject::tr( "<unknown>", "render" );
3012
3013 }
3014 return QString();
3015}
3016
3017
3018
3020{
3021 switch ( unit )
3022 {
3023 case Qgis::LayoutUnit::Centimeters:
3024 return QStringLiteral( "cm" );
3025 case Qgis::LayoutUnit::Meters:
3026 return QStringLiteral( "m" );
3027 case Qgis::LayoutUnit::Inches:
3028 return QStringLiteral( "in" );
3029 case Qgis::LayoutUnit::Feet:
3030 return QStringLiteral( "ft" );
3031 case Qgis::LayoutUnit::Points:
3032 return QStringLiteral( "pt" );
3033 case Qgis::LayoutUnit::Picas:
3034 return QStringLiteral( "pi" );
3035 case Qgis::LayoutUnit::Pixels:
3036 return QStringLiteral( "px" );
3037 case Qgis::LayoutUnit::Millimeters:
3038 return QStringLiteral( "mm" );
3039 }
3040 return QString();
3041}
3042
3043Qgis::LayoutUnit QgsUnitTypes::decodeLayoutUnit( const QString &string, bool *ok )
3044{
3045 const QString normalized = string.trimmed().toLower();
3046
3047 if ( ok )
3048 *ok = true;
3049
3050 if ( normalized == encodeUnit( Qgis::LayoutUnit::Millimeters ).toLower() )
3051 return Qgis::LayoutUnit::Millimeters;
3052 if ( normalized == encodeUnit( Qgis::LayoutUnit::Centimeters ).toLower() )
3053 return Qgis::LayoutUnit::Centimeters;
3054 if ( normalized == encodeUnit( Qgis::LayoutUnit::Meters ).toLower() )
3055 return Qgis::LayoutUnit::Meters;
3056 if ( normalized == encodeUnit( Qgis::LayoutUnit::Inches ).toLower() )
3057 return Qgis::LayoutUnit::Inches;
3058 if ( normalized == encodeUnit( Qgis::LayoutUnit::Feet ).toLower() )
3059 return Qgis::LayoutUnit::Feet;
3060 if ( normalized == encodeUnit( Qgis::LayoutUnit::Points ).toLower() )
3061 return Qgis::LayoutUnit::Points;
3062 if ( normalized == encodeUnit( Qgis::LayoutUnit::Picas ).toLower() )
3063 return Qgis::LayoutUnit::Picas;
3064 if ( normalized == encodeUnit( Qgis::LayoutUnit::Pixels ).toLower() )
3065 return Qgis::LayoutUnit::Pixels;
3066
3067 if ( ok )
3068 *ok = false;
3069
3070 // millimeters are default
3071 return Qgis::LayoutUnit::Millimeters;
3072}
3073
3075{
3076 switch ( units )
3077 {
3078 case Qgis::LayoutUnit::Pixels:
3079 return Qgis::LayoutUnitType::ScreenUnits;
3080 case Qgis::LayoutUnit::Millimeters:
3081 case Qgis::LayoutUnit::Centimeters:
3082 case Qgis::LayoutUnit::Meters:
3083 case Qgis::LayoutUnit::Inches:
3084 case Qgis::LayoutUnit::Feet:
3085 case Qgis::LayoutUnit::Points:
3086 case Qgis::LayoutUnit::Picas:
3087 return Qgis::LayoutUnitType::PaperUnits;
3088 }
3089
3090 // avoid warnings
3091 return Qgis::LayoutUnitType::PaperUnits;
3092}
3093
3095{
3096 switch ( unit )
3097 {
3098 case Qgis::LayoutUnit::Pixels:
3099 return QObject::tr( "px" );
3100 case Qgis::LayoutUnit::Millimeters:
3101 return QObject::tr( "mm" );
3102 case Qgis::LayoutUnit::Centimeters:
3103 return QObject::tr( "cm" );
3104 case Qgis::LayoutUnit::Meters:
3105 return QObject::tr( "m" );
3106 case Qgis::LayoutUnit::Inches:
3107 return QObject::tr( "in", "unit inch" );
3108 case Qgis::LayoutUnit::Feet:
3109 return QObject::tr( "ft" );
3110 case Qgis::LayoutUnit::Points:
3111 return QObject::tr( "pt" );
3112 case Qgis::LayoutUnit::Picas:
3113 return QObject::tr( "pica" );
3114 }
3115 return QString(); // no warnings
3116}
3117
3119{
3120 switch ( unit )
3121 {
3122 case Qgis::LayoutUnit::Pixels:
3123 return QObject::tr( "pixels" );
3124 case Qgis::LayoutUnit::Millimeters:
3125 return QObject::tr( "millimeters" );
3126 case Qgis::LayoutUnit::Centimeters:
3127 return QObject::tr( "centimeters" );
3128 case Qgis::LayoutUnit::Meters:
3129 return QObject::tr( "meters" );
3130 case Qgis::LayoutUnit::Inches:
3131 return QObject::tr( "inches" );
3132 case Qgis::LayoutUnit::Feet:
3133 return QObject::tr( "feet" );
3134 case Qgis::LayoutUnit::Points:
3135 return QObject::tr( "points" );
3136 case Qgis::LayoutUnit::Picas:
3137 return QObject::tr( "picas" );
3138 }
3139 return QString(); // no warnings
3140}
DistanceUnitType
Types of distance units.
Definition: qgis.h:3069
@ Geographic
Unit is a geographic (e.g., degree based) unit.
@ Standard
Unit is a standard measurement unit.
UnitType
Unit types.
Definition: qgis.h:3030
LayoutUnit
Layout measurement units.
Definition: qgis.h:3196
AngleUnit
Units of angles.
Definition: qgis.h:3131
DistanceUnit
Units of distance.
Definition: qgis.h:3047
AreaUnit
Units of area.
Definition: qgis.h:3084
TemporalUnit
Temporal units.
Definition: qgis.h:3152
RenderUnit
Rendering size units.
Definition: qgis.h:3176
LayoutUnitType
Types of layout units.
Definition: qgis.h:3216
VolumeUnit
Units of volume.
Definition: qgis.h:3108
static Q_INVOKABLE Qgis::VolumeUnit decodeVolumeUnit(const QString &string, bool *ok=nullptr)
Decodes a volume unit from a string.
static Q_INVOKABLE Qgis::DistanceUnit stringToDistanceUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a distance unit.
static Q_INVOKABLE QgsUnitTypes::AreaValue scaledArea(double area, Qgis::AreaUnit unit, int decimals, bool keepBaseUnit=false)
Will convert an area with a given unit to an area value which is nice to display.
static Q_INVOKABLE QgsUnitTypes::DistanceValue scaledDistance(double distance, Qgis::DistanceUnit unit, int decimals, bool keepBaseUnit=false)
Will convert a distance with a given unit to a distance value which is nice to display.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
static Q_INVOKABLE QString formatArea(double area, int decimals, Qgis::AreaUnit unit, bool keepBaseUnit=false)
Returns an area formatted as a friendly string.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE Qgis::VolumeUnit stringToVolumeUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a volume unit.
static Q_INVOKABLE QString formatDistance(double distance, int decimals, Qgis::DistanceUnit unit, bool keepBaseUnit=false)
Returns an distance formatted as a friendly string.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE Qgis::DistanceUnitType unitType(Qgis::DistanceUnit unit)
Returns the type for a distance unit.
static Q_INVOKABLE Qgis::UnitType decodeUnitType(const QString &string, bool *ok=nullptr)
Decodes a unit type from a string.
static Q_INVOKABLE Qgis::AreaUnit decodeAreaUnit(const QString &string, bool *ok=nullptr)
Decodes an areal unit from a string.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE Qgis::TemporalUnit stringToTemporalUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a temporal unit.
static Q_INVOKABLE Qgis::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
static Q_INVOKABLE Qgis::AreaUnit stringToAreaUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to an areal unit.
static Q_INVOKABLE Qgis::AreaUnit distanceToAreaUnit(Qgis::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding area unit, e.g., meters to square meters.
static Q_INVOKABLE Qgis::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
static Q_INVOKABLE Qgis::AngleUnit decodeAngleUnit(const QString &string, bool *ok=nullptr)
Decodes an angular unit from a string.
static Q_INVOKABLE Qgis::DistanceUnit areaToDistanceUnit(Qgis::AreaUnit areaUnit)
Converts an area unit to its corresponding distance unit, e.g., square meters to meters.
static Q_INVOKABLE Qgis::DistanceUnit volumeToDistanceUnit(Qgis::VolumeUnit volumeUnit)
Converts a volume unit to its corresponding distance unit, e.g., cubic meters to meters.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QString encodeUnitType(Qgis::UnitType type)
Encodes a unit type to a string.
static Q_INVOKABLE QString formatAngle(double angle, int decimals, Qgis::AngleUnit unit)
Returns an angle formatted as a friendly string.
static Q_INVOKABLE Qgis::VolumeUnit distanceToVolumeUnit(Qgis::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding volume unit, e.g., meters to cubic meters.
static Q_INVOKABLE Qgis::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
Definition: qgis.h:3563
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3509
#define DEGREE_TO_METER
#define MILLIMETERS_TO_METER
#define KM2_TO_M2
#define CENTIMETERS_TO_METER
#define DEG2_TO_M3
#define MI2_TO_M2
#define AC_TO_FT2
#define NMILE_TO_METER
#define KILOMETERS_TO_METER
#define YARDS_TO_FEET
#define FEET_TO_METER
#define YD2_TO_M2
#define NM2_TO_M2
#define MM2_TO_M2
#define DEG2_TO_M2
#define CM2_TO_M2
#define FT2_TO_M2
#define MILES_TO_METER
#define YARDS_TO_METER
#define HA_TO_M2
A combination of area value and unit.
Definition: qgsunittypes.h:70
double value
The value part of the distance.
Definition: qgsunittypes.h:75
Qgis::AreaUnit unit
The value part of the distance.
Definition: qgsunittypes.h:80
A combination of distance value and unit.
Definition: qgsunittypes.h:51
double value
The value part of the distance.
Definition: qgsunittypes.h:56
Qgis::DistanceUnit unit
The value part of the distance.
Definition: qgsunittypes.h:61