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