QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 TypeDistance:
31  return QStringLiteral( "distance" );
32 
33  case TypeArea:
34  return QStringLiteral( "area" );
35 
36  case TypeVolume:
37  return QStringLiteral( "volume" );
38 
39  case TypeUnknown:
40  return QStringLiteral( "<unknown>" );
41 
42  }
43  return QString();
44 }
45 
46 QgsUnitTypes::UnitType QgsUnitTypes::decodeUnitType( const QString &string, bool *ok )
47 {
48  QString normalized = string.trimmed().toLower();
49 
50  if ( ok )
51  *ok = true;
52 
53  if ( normalized == encodeUnitType( TypeDistance ) )
54  return TypeDistance;
55  if ( normalized == encodeUnitType( TypeArea ) )
56  return TypeArea;
57  if ( normalized == encodeUnitType( TypeVolume ) )
58  return TypeVolume;
59  if ( normalized == encodeUnitType( TypeUnknown ) )
60  return TypeUnknown;
61 
62  if ( ok )
63  *ok = false;
64 
65  return TypeUnknown;
66 }
67 
69 {
70  switch ( unit )
71  {
72  case DistanceMeters:
73  case DistanceFeet:
75  case DistanceYards:
76  case DistanceMiles:
77  case DistanceKilometers:
80  return Standard;
81 
82  case DistanceDegrees:
83  return Geographic;
84 
86  return UnknownType;
87  }
88  return UnknownType;
89 }
90 
92 {
93  switch ( unit )
94  {
95  case AreaSquareMeters:
97  case AreaSquareFeet:
98  case AreaSquareYards:
99  case AreaSquareMiles:
100  case AreaHectares:
101  case AreaAcres:
105  return Standard;
106 
107  case AreaSquareDegrees:
108  return Geographic;
109 
110  case AreaUnknownUnit:
111  return UnknownType;
112  }
113 
114  return UnknownType;
115 }
116 
118 {
119  switch ( unit )
120  {
121  case DistanceMeters:
122  return QStringLiteral( "meters" );
123 
124  case DistanceKilometers:
125  return QStringLiteral( "km" );
126 
127  case DistanceFeet:
128  return QStringLiteral( "feet" );
129 
130  case DistanceYards:
131  return QStringLiteral( "yd" );
132 
133  case DistanceMiles:
134  return QStringLiteral( "mi" );
135 
136  case DistanceDegrees:
137  return QStringLiteral( "degrees" );
138 
139  case DistanceUnknownUnit:
140  return QStringLiteral( "<unknown>" );
141 
143  return QStringLiteral( "nautical miles" );
144 
145  case DistanceCentimeters:
146  return QStringLiteral( "cm" );
147 
148  case DistanceMillimeters:
149  return QStringLiteral( "mm" );
150  }
151  return QString();
152 }
153 
154 /***************************************************************************
155  * This class is considered CRITICAL and any change MUST be accompanied with
156  * full unit tests in test_qgsunittypes.py.
157  * See details in QEP #17
158  ****************************************************************************/
159 
161 {
162  QString normalized = string.trimmed().toLower();
163 
164  if ( ok )
165  *ok = true;
166 
167  if ( normalized == encodeUnit( DistanceMeters ) )
168  return DistanceMeters;
169  if ( normalized == encodeUnit( DistanceFeet ) )
170  return DistanceFeet;
171  if ( normalized == encodeUnit( DistanceDegrees ) )
172  return DistanceDegrees;
173  if ( normalized == encodeUnit( DistanceNauticalMiles ) )
174  return DistanceNauticalMiles;
175  if ( normalized == encodeUnit( DistanceKilometers ) )
176  return DistanceKilometers;
177  if ( normalized == encodeUnit( DistanceYards ) )
178  return DistanceYards;
179  if ( normalized == encodeUnit( DistanceMiles ) )
180  return DistanceMiles;
181  if ( normalized == encodeUnit( DistanceCentimeters ) )
182  return DistanceCentimeters;
183  if ( normalized == encodeUnit( DistanceMillimeters ) )
184  return DistanceMillimeters;
185  if ( normalized == encodeUnit( DistanceUnknownUnit ) )
186  return DistanceUnknownUnit;
187 
188  if ( ok )
189  *ok = false;
190 
191  return DistanceUnknownUnit;
192 }
193 
195 {
196  switch ( unit )
197  {
198  case DistanceMeters:
199  return QObject::tr( "meters", "distance" );
200 
201  case DistanceKilometers:
202  return QObject::tr( "kilometers", "distance" );
203 
204  case DistanceFeet:
205  return QObject::tr( "feet", "distance" );
206 
207  case DistanceYards:
208  return QObject::tr( "yards", "distance" );
209 
210  case DistanceMiles:
211  return QObject::tr( "miles", "distance" );
212 
213  case DistanceDegrees:
214  return QObject::tr( "degrees", "distance" );
215 
216  case DistanceCentimeters:
217  return QObject::tr( "centimeters", "distance" );
218 
219  case DistanceMillimeters:
220  return QObject::tr( "millimeters", "distance" );
221 
222  case DistanceUnknownUnit:
223  return QObject::tr( "<unknown>", "distance" );
224 
226  return QObject::tr( "nautical miles", "distance" );
227  }
228  return QString();
229 }
230 
232 {
233  switch ( unit )
234  {
235  case RenderMillimeters:
236  return QObject::tr( "mm", "render" );
237 
238  case RenderMapUnits:
239  return QObject::tr( "map units", "render" );
240 
241  case RenderPixels:
242  return QObject::tr( "px", "render" );
243 
244  case RenderPercentage:
245  return QObject::tr( "%", "render" );
246 
247  case RenderPoints:
248  return QObject::tr( "pt", "render" );
249 
250  case RenderInches:
251  return QObject::tr( "in", "render" );
252 
253  case RenderUnknownUnit:
254  return QObject::tr( "unknown", "render" );
255 
257  return QObject::tr( "m", "render" );
258 
259  }
260 
261  return QString();
262 }
263 
265 {
266  switch ( unit )
267  {
268  case DistanceMeters:
269  return QObject::tr( "m", "distance" );
270 
271  case DistanceKilometers:
272  return QObject::tr( "km", "distance" );
273 
274  case DistanceFeet:
275  return QObject::tr( "ft", "distance" );
276 
277  case DistanceYards:
278  return QObject::tr( "yd", "distance" );
279 
280  case DistanceMiles:
281  return QObject::tr( "mi", "distance" );
282 
283  case DistanceDegrees:
284  return QObject::tr( "deg", "distance" );
285 
286  case DistanceCentimeters:
287  return QObject::tr( "cm", "distance" );
288 
289  case DistanceMillimeters:
290  return QObject::tr( "mm", "distance" );
291 
292  case DistanceUnknownUnit:
293  return QString();
294 
296  return QObject::tr( "NM", "distance" );
297  }
298  return QString();
299 }
300 
301 /***************************************************************************
302  * This class is considered CRITICAL and any change MUST be accompanied with
303  * full unit tests in test_qgsunittypes.py.
304  * See details in QEP #17
305  ****************************************************************************/
306 
308 {
309  QString normalized = string.trimmed().toLower();
310 
311  if ( ok )
312  *ok = true;
313 
314  if ( normalized == toString( DistanceMeters ) )
315  return DistanceMeters;
316  if ( normalized == toString( DistanceKilometers ) )
317  return DistanceKilometers;
318  if ( normalized == toString( DistanceFeet ) )
319  return DistanceFeet;
320  if ( normalized == toString( DistanceYards ) )
321  return DistanceYards;
322  if ( normalized == toString( DistanceMiles ) )
323  return DistanceMiles;
324  if ( normalized == toString( DistanceDegrees ) )
325  return DistanceDegrees;
326  if ( normalized == toString( DistanceCentimeters ) )
327  return DistanceCentimeters;
328  if ( normalized == toString( DistanceMillimeters ) )
329  return DistanceMillimeters;
330  if ( normalized == toString( DistanceNauticalMiles ) )
331  return DistanceNauticalMiles;
332  if ( normalized == toString( DistanceUnknownUnit ) )
333  return DistanceUnknownUnit;
334 
335  if ( ok )
336  *ok = false;
337 
338  return DistanceUnknownUnit;
339 }
340 
341 /***************************************************************************
342  * This class is considered CRITICAL and any change MUST be accompanied with
343  * full unit tests in test_qgsunittypes.py.
344  * See details in QEP #17
345  ****************************************************************************/
346 
348 {
349 #define DEGREE_TO_METER 111319.49079327358
350 #define FEET_TO_METER 0.3048
351 #define NMILE_TO_METER 1852.0
352 #define KILOMETERS_TO_METER 1000.0
353 #define CENTIMETERS_TO_METER 0.01
354 #define MILLIMETERS_TO_METER 0.001
355 #define YARDS_TO_METER 0.9144
356 #define YARDS_TO_FEET 3.0
357 #define MILES_TO_METER 1609.344
358 
359  // Calculate the conversion factor between the specified units
360  switch ( fromUnit )
361  {
362  case DistanceMeters:
363  {
364  switch ( toUnit )
365  {
366  case DistanceMeters:
367  return 1.0;
368  case DistanceKilometers:
369  return 1.0 / KILOMETERS_TO_METER;
370  case DistanceMillimeters:
371  return 1.0 / MILLIMETERS_TO_METER;
372  case DistanceCentimeters:
373  return 1.0 / CENTIMETERS_TO_METER;
374  case DistanceFeet:
375  return 1.0 / FEET_TO_METER;
376  case DistanceYards:
377  return 1.0 / YARDS_TO_METER;
378  case DistanceMiles:
379  return 1.0 / MILES_TO_METER;
380  case DistanceDegrees:
381  return 1.0 / DEGREE_TO_METER;
383  return 1.0 / NMILE_TO_METER;
384  case DistanceUnknownUnit:
385  break;
386  }
387 
388  break;
389  }
390  case DistanceKilometers:
391  {
392  switch ( toUnit )
393  {
394  case DistanceMeters:
395  return KILOMETERS_TO_METER;
396  case DistanceKilometers:
397  return 1.0;
398  case DistanceCentimeters:
400  case DistanceMillimeters:
402  case DistanceFeet:
404  case DistanceYards:
406  case DistanceMiles:
408  case DistanceDegrees:
412  case DistanceUnknownUnit:
413  break;
414  }
415 
416  break;
417  }
418  case DistanceFeet:
419  {
420  switch ( toUnit )
421  {
422  case DistanceMeters:
423  return FEET_TO_METER;
424  case DistanceKilometers:
426  case DistanceCentimeters:
428  case DistanceMillimeters:
430  case DistanceFeet:
431  return 1.0;
432  case DistanceYards:
433  return 1.0 / YARDS_TO_FEET;
434  case DistanceMiles:
435  return FEET_TO_METER / MILES_TO_METER;
436  case DistanceDegrees:
439  return FEET_TO_METER / NMILE_TO_METER;
440  case DistanceUnknownUnit:
441  break;
442  }
443 
444  break;
445  }
446  case DistanceYards:
447  {
448  switch ( toUnit )
449  {
450  case DistanceMeters:
451  return YARDS_TO_METER;
452  case DistanceKilometers:
454  case DistanceCentimeters:
456  case DistanceMillimeters:
458  case DistanceFeet:
459  return YARDS_TO_FEET;
460  case DistanceYards:
461  return 1.0;
462  case DistanceMiles:
464  case DistanceDegrees:
468  case DistanceUnknownUnit:
469  break;
470  }
471 
472  break;
473  }
474  case DistanceMiles:
475  {
476  switch ( toUnit )
477  {
478  case DistanceMeters:
479  return MILES_TO_METER;
480  case DistanceKilometers:
482  case DistanceCentimeters:
484  case DistanceMillimeters:
486  case DistanceFeet:
487  return MILES_TO_METER / FEET_TO_METER;
488  case DistanceYards:
490  case DistanceMiles:
491  return 1.0;
492  case DistanceDegrees:
496  case DistanceUnknownUnit:
497  break;
498  }
499 
500  break;
501  }
502  case DistanceDegrees:
503  {
504  switch ( toUnit )
505  {
506  case DistanceMeters:
507  return DEGREE_TO_METER;
508  case DistanceKilometers:
510  case DistanceCentimeters:
512  case DistanceMillimeters:
514  case DistanceFeet:
516  case DistanceYards:
518  case DistanceMiles:
520  case DistanceDegrees:
521  return 1.0;
524  case DistanceUnknownUnit:
525  break;
526  }
527 
528  break;
529  }
531  {
532  switch ( toUnit )
533  {
534  case DistanceMeters:
535  return NMILE_TO_METER;
536  case DistanceKilometers:
538  case DistanceCentimeters:
540  case DistanceMillimeters:
542  case DistanceFeet:
543  return NMILE_TO_METER / FEET_TO_METER;
544  case DistanceYards:
546  case DistanceMiles:
548  case DistanceDegrees:
551  return 1.0;
552  case DistanceUnknownUnit:
553  break;
554  }
555 
556  break;
557  }
558  case DistanceCentimeters:
559  {
560  switch ( toUnit )
561  {
562  case DistanceMeters:
563  return CENTIMETERS_TO_METER;
564  case DistanceKilometers:
566  case DistanceCentimeters:
567  return 1.0;
568  case DistanceMillimeters:
570  case DistanceFeet:
572  case DistanceYards:
574  case DistanceMiles:
576  case DistanceDegrees:
580  case DistanceUnknownUnit:
581  break;
582  }
583 
584  break;
585  }
586  case DistanceMillimeters:
587  {
588  switch ( toUnit )
589  {
590  case DistanceMeters:
591  return MILLIMETERS_TO_METER;
592  case DistanceKilometers:
594  case DistanceCentimeters:
596  case DistanceMillimeters:
597  return 1.0;
598  case DistanceFeet:
600  case DistanceYards:
602  case DistanceMiles:
604  case DistanceDegrees:
608  case DistanceUnknownUnit:
609  break;
610  }
611 
612  break;
613  }
614  case DistanceUnknownUnit:
615  break;
616  }
617  return 1.0;
618 }
619 
621 {
622  switch ( unit )
623  {
624  case AreaSquareMeters:
625  return QStringLiteral( "m2" );
627  return QStringLiteral( "km2" );
628  case AreaSquareFeet:
629  return QStringLiteral( "ft2" );
630  case AreaSquareYards:
631  return QStringLiteral( "y2" );
632  case AreaSquareMiles:
633  return QStringLiteral( "mi2" );
634  case AreaHectares:
635  return QStringLiteral( "ha" );
636  case AreaAcres:
637  return QStringLiteral( "ac" );
639  return QStringLiteral( "nm2" );
640  case AreaSquareDegrees:
641  return QStringLiteral( "deg2" );
643  return QStringLiteral( "cm2" );
645  return QStringLiteral( "mm2" );
646  case AreaUnknownUnit:
647  return QStringLiteral( "<unknown>" );
648  }
649  return QString();
650 }
651 
652 QgsUnitTypes::AreaUnit QgsUnitTypes::decodeAreaUnit( const QString &string, bool *ok )
653 {
654  QString normalized = string.trimmed().toLower();
655 
656  if ( ok )
657  *ok = true;
658 
659  if ( normalized == encodeUnit( AreaSquareMeters ) )
660  return AreaSquareMeters;
661  if ( normalized == encodeUnit( AreaSquareKilometers ) )
662  return AreaSquareKilometers;
663  if ( normalized == encodeUnit( AreaSquareFeet ) )
664  return AreaSquareFeet;
665  if ( normalized == encodeUnit( AreaSquareYards ) )
666  return AreaSquareYards;
667  if ( normalized == encodeUnit( AreaSquareMiles ) )
668  return AreaSquareMiles;
669  if ( normalized == encodeUnit( AreaHectares ) )
670  return AreaHectares;
671  if ( normalized == encodeUnit( AreaAcres ) )
672  return AreaAcres;
673  if ( normalized == encodeUnit( AreaSquareNauticalMiles ) )
675  if ( normalized == encodeUnit( AreaSquareDegrees ) )
676  return AreaSquareDegrees;
677  if ( normalized == encodeUnit( AreaSquareCentimeters ) )
678  return AreaSquareCentimeters;
679  if ( normalized == encodeUnit( AreaSquareMillimeters ) )
680  return AreaSquareMillimeters;
681  if ( normalized == encodeUnit( AreaUnknownUnit ) )
682  return AreaUnknownUnit;
683 
684  if ( ok )
685  *ok = false;
686 
687  return AreaUnknownUnit;
688 }
689 
691 {
692  switch ( unit )
693  {
694  case AreaSquareMeters:
695  return QObject::tr( "square meters", "area" );
697  return QObject::tr( "square kilometers", "area" );
698  case AreaSquareFeet:
699  return QObject::tr( "square feet", "area" );
700  case AreaSquareYards:
701  return QObject::tr( "square yards", "area" );
702  case AreaSquareMiles:
703  return QObject::tr( "square miles", "area" );
704  case AreaHectares:
705  return QObject::tr( "hectares", "area" );
706  case AreaAcres:
707  return QObject::tr( "acres", "area" );
709  return QObject::tr( "square nautical miles", "area" );
710  case AreaSquareDegrees:
711  return QObject::tr( "square degrees", "area" );
713  return QObject::tr( "square millimeters", "area" );
715  return QObject::tr( "square centimeters", "area" );
716  case AreaUnknownUnit:
717  return QObject::tr( "<unknown>", "area" );
718  }
719  return QString();
720 }
721 
723 {
724  switch ( unit )
725  {
726  case AreaSquareMeters:
727  return QObject::tr( "m²", "area" );
729  return QObject::tr( "km²", "area" );
730  case AreaSquareFeet:
731  return QObject::tr( "ft²", "area" );
732  case AreaSquareYards:
733  return QObject::tr( "yd²", "area" );
734  case AreaSquareMiles:
735  return QObject::tr( "mi²", "area" );
736  case AreaHectares:
737  return QObject::tr( "ha", "area" );
738  case AreaAcres:
739  return QObject::tr( "ac", "area" );
741  return QObject::tr( "NM²", "area" );
742  case AreaSquareDegrees:
743  return QObject::tr( "deg²", "area" );
745  return QObject::tr( "cm²", "area" );
747  return QObject::tr( "mm²", "area" );
748  case AreaUnknownUnit:
749  return QString();
750  }
751  return QString();
752 }
753 
754 QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString &string, bool *ok )
755 {
756  QString normalized = string.trimmed().toLower();
757 
758  if ( ok )
759  *ok = true;
760 
761  if ( normalized == toString( AreaSquareMeters ) )
762  return AreaSquareMeters;
763  if ( normalized == toString( AreaSquareKilometers ) )
764  return AreaSquareKilometers;
765  if ( normalized == toString( AreaSquareFeet ) )
766  return AreaSquareFeet;
767  if ( normalized == toString( AreaSquareYards ) )
768  return AreaSquareYards;
769  if ( normalized == toString( AreaSquareMiles ) )
770  return AreaSquareMiles;
771  if ( normalized == toString( AreaHectares ) )
772  return AreaHectares;
773  if ( normalized == toString( AreaAcres ) )
774  return AreaAcres;
775  if ( normalized == toString( AreaSquareNauticalMiles ) )
777  if ( normalized == toString( AreaSquareDegrees ) )
778  return AreaSquareDegrees;
779  if ( normalized == toString( AreaSquareMillimeters ) )
780  return AreaSquareMillimeters;
781  if ( normalized == toString( AreaSquareCentimeters ) )
782  return AreaSquareCentimeters;
783  if ( normalized == toString( AreaUnknownUnit ) )
784  return AreaUnknownUnit;
785  if ( ok )
786  *ok = false;
787 
788  return AreaUnknownUnit;
789 }
790 
792 {
793 #define KM2_TO_M2 1000000.0
794 #define CM2_TO_M2 0.0001
795 #define MM2_TO_M2 0.000001
796 #define FT2_TO_M2 0.09290304
797 #define YD2_TO_M2 0.83612736
798 #define MI2_TO_M2 2589988.110336
799 #define HA_TO_M2 10000.0
800 #define AC_TO_FT2 43560.0
801 #define DEG2_TO_M2 12392029030.5
802 #define NM2_TO_M2 3429904.0
803 
804  // Calculate the conversion factor between the specified units
805  switch ( fromUnit )
806  {
807  case AreaSquareMeters:
808  {
809  switch ( toUnit )
810  {
811  case AreaSquareMeters:
812  return 1.0;
814  return 1.0 / KM2_TO_M2;
815  case AreaSquareFeet:
816  return 1.0 / FT2_TO_M2;
817  case AreaSquareYards:
818  return 1.0 / YD2_TO_M2;
819  case AreaSquareMiles:
820  return 1.0 / MI2_TO_M2;
821  case AreaHectares:
822  return 1.0 / HA_TO_M2;
823  case AreaAcres:
824  return 1.0 / AC_TO_FT2 / FT2_TO_M2;
826  return 1.0 / NM2_TO_M2;
827  case AreaSquareDegrees:
828  return 1.0 / DEG2_TO_M2;
830  return 1.0 / CM2_TO_M2;
832  return 1.0 / MM2_TO_M2;
833  case AreaUnknownUnit:
834  break;
835  }
836 
837  break;
838  }
840  {
841  switch ( toUnit )
842  {
843  case AreaSquareMeters:
844  return KM2_TO_M2;
846  return 1.0;
847  case AreaSquareFeet:
848  return KM2_TO_M2 / FT2_TO_M2;
849  case AreaSquareYards:
850  return KM2_TO_M2 / YD2_TO_M2;
851  case AreaSquareMiles:
852  return KM2_TO_M2 / MI2_TO_M2;
853  case AreaHectares:
854  return KM2_TO_M2 / HA_TO_M2;
855  case AreaAcres:
856  return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
858  return KM2_TO_M2 / NM2_TO_M2;
859  case AreaSquareDegrees:
860  return KM2_TO_M2 / DEG2_TO_M2;
862  return KM2_TO_M2 / CM2_TO_M2;
864  return KM2_TO_M2 / MM2_TO_M2;
865  case AreaUnknownUnit:
866  break;
867  }
868 
869  break;
870  }
871  case AreaSquareFeet:
872  {
873  switch ( toUnit )
874  {
875  case AreaSquareMeters:
876  return FT2_TO_M2;
878  return FT2_TO_M2 / KM2_TO_M2;
879  case AreaSquareFeet:
880  return 1.0;
881  case AreaSquareYards:
882  return FT2_TO_M2 / YD2_TO_M2;
883  case AreaSquareMiles:
884  return FT2_TO_M2 / MI2_TO_M2;
885  case AreaHectares:
886  return FT2_TO_M2 / HA_TO_M2;
887  case AreaAcres:
888  return 1.0 / AC_TO_FT2;
890  return FT2_TO_M2 / NM2_TO_M2;
891  case AreaSquareDegrees:
892  return FT2_TO_M2 / DEG2_TO_M2;
894  return FT2_TO_M2 / CM2_TO_M2;
896  return FT2_TO_M2 / MM2_TO_M2;
897  case AreaUnknownUnit:
898  break;
899  }
900 
901  break;
902  }
903 
904  case AreaSquareYards:
905  {
906  switch ( toUnit )
907  {
908  case AreaSquareMeters:
909  return YD2_TO_M2;
911  return YD2_TO_M2 / KM2_TO_M2;
912  case AreaSquareFeet:
913  return YD2_TO_M2 / FT2_TO_M2;
914  case AreaSquareYards:
915  return 1.0;
916  case AreaSquareMiles:
917  return YD2_TO_M2 / MI2_TO_M2;
918  case AreaHectares:
919  return YD2_TO_M2 / HA_TO_M2;
920  case AreaAcres:
921  return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
923  return YD2_TO_M2 / NM2_TO_M2;
924  case AreaSquareDegrees:
925  return YD2_TO_M2 / DEG2_TO_M2;
927  return YD2_TO_M2 / CM2_TO_M2;
929  return YD2_TO_M2 / MM2_TO_M2;
930  case AreaUnknownUnit:
931  break;
932  }
933  break;
934  }
935 
936  case AreaSquareMiles:
937  {
938  switch ( toUnit )
939  {
940  case AreaSquareMeters:
941  return MI2_TO_M2;
943  return MI2_TO_M2 / KM2_TO_M2;
944  case AreaSquareFeet:
945  return MI2_TO_M2 / FT2_TO_M2;
946  case AreaSquareYards:
947  return MI2_TO_M2 / YD2_TO_M2;
948  case AreaSquareMiles:
949  return 1.0;
950  case AreaHectares:
951  return MI2_TO_M2 / HA_TO_M2;
952  case AreaAcres:
953  return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
955  return MI2_TO_M2 / NM2_TO_M2;
956  case AreaSquareDegrees:
957  return MI2_TO_M2 / DEG2_TO_M2;
959  return MI2_TO_M2 / CM2_TO_M2;
961  return MI2_TO_M2 / MM2_TO_M2;
962  case AreaUnknownUnit:
963  break;
964  }
965 
966  break;
967  }
968 
969  case AreaHectares:
970  {
971  switch ( toUnit )
972  {
973  case AreaSquareMeters:
974  return HA_TO_M2;
976  return HA_TO_M2 / KM2_TO_M2;
977  case AreaSquareFeet:
978  return HA_TO_M2 / FT2_TO_M2;
979  case AreaSquareYards:
980  return HA_TO_M2 / YD2_TO_M2;
981  case AreaSquareMiles:
982  return HA_TO_M2 / MI2_TO_M2;
983  case AreaHectares:
984  return 1.0;
985  case AreaAcres:
986  return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
988  return HA_TO_M2 / NM2_TO_M2;
989  case AreaSquareDegrees:
990  return HA_TO_M2 / DEG2_TO_M2;
992  return HA_TO_M2 / CM2_TO_M2;
994  return HA_TO_M2 / MM2_TO_M2;
995  case AreaUnknownUnit:
996  break;
997  }
998 
999  break;
1000  }
1001 
1002  case AreaAcres:
1003  {
1004  switch ( toUnit )
1005  {
1006  case AreaSquareMeters:
1007  return AC_TO_FT2 * FT2_TO_M2;
1008  case AreaSquareKilometers:
1009  return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
1010  case AreaSquareFeet:
1011  return AC_TO_FT2;
1012  case AreaSquareYards:
1013  return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
1014  case AreaSquareMiles:
1015  return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
1016  case AreaHectares:
1017  return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
1018  case AreaAcres:
1019  return 1.0;
1021  return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
1022  case AreaSquareDegrees:
1023  return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
1024  case AreaSquareCentimeters:
1025  return AC_TO_FT2 * FT2_TO_M2 / CM2_TO_M2;
1026  case AreaSquareMillimeters:
1027  return AC_TO_FT2 * FT2_TO_M2 / MM2_TO_M2;
1028  case AreaUnknownUnit:
1029  break;
1030  }
1031 
1032  break;
1033  }
1034 
1036  {
1037  switch ( toUnit )
1038  {
1039  case AreaSquareMeters:
1040  return NM2_TO_M2;
1041  case AreaSquareKilometers:
1042  return NM2_TO_M2 / KM2_TO_M2;
1043  case AreaSquareFeet:
1044  return NM2_TO_M2 / FT2_TO_M2;
1045  case AreaSquareYards:
1046  return NM2_TO_M2 / YD2_TO_M2;
1047  case AreaSquareMiles:
1048  return NM2_TO_M2 / MI2_TO_M2;
1049  case AreaHectares:
1050  return NM2_TO_M2 / HA_TO_M2;
1051  case AreaAcres:
1052  return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1054  return 1.0;
1055  case AreaSquareDegrees:
1056  return NM2_TO_M2 / DEG2_TO_M2;
1057  case AreaSquareCentimeters:
1058  return NM2_TO_M2 / CM2_TO_M2;
1059  case AreaSquareMillimeters:
1060  return NM2_TO_M2 / MM2_TO_M2;
1061  case AreaUnknownUnit:
1062  break;
1063  }
1064 
1065  break;
1066  }
1067 
1068  case AreaSquareDegrees:
1069  {
1070  switch ( toUnit )
1071  {
1072  case AreaSquareMeters:
1073  return DEG2_TO_M2;
1074  case AreaSquareKilometers:
1075  return DEG2_TO_M2 / KM2_TO_M2;
1076  case AreaSquareFeet:
1077  return DEG2_TO_M2 / FT2_TO_M2;
1078  case AreaSquareYards:
1079  return DEG2_TO_M2 / YD2_TO_M2;
1080  case AreaSquareMiles:
1081  return DEG2_TO_M2 / MI2_TO_M2;
1082  case AreaHectares:
1083  return DEG2_TO_M2 / HA_TO_M2;
1084  case AreaAcres:
1085  return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1087  return DEG2_TO_M2 / NM2_TO_M2;
1088  case AreaSquareDegrees:
1089  return 1.0;
1090  case AreaSquareCentimeters:
1091  return DEG2_TO_M2 / CM2_TO_M2;
1092  case AreaSquareMillimeters:
1093  return DEG2_TO_M2 / MM2_TO_M2;
1094  case AreaUnknownUnit:
1095  break;
1096  }
1097 
1098  break;
1099  }
1100 
1101  case AreaSquareMillimeters:
1102  {
1103  switch ( toUnit )
1104  {
1105  case AreaSquareMeters:
1106  return MM2_TO_M2;
1107  case AreaSquareKilometers:
1108  return MM2_TO_M2 / KM2_TO_M2;
1109  case AreaSquareFeet:
1110  return MM2_TO_M2 / FT2_TO_M2;
1111  case AreaSquareYards:
1112  return MM2_TO_M2 / YD2_TO_M2;
1113  case AreaSquareMiles:
1114  return MM2_TO_M2 / MI2_TO_M2;
1115  case AreaHectares:
1116  return MM2_TO_M2 / HA_TO_M2;
1117  case AreaAcres:
1118  return MM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1120  return MM2_TO_M2 / NM2_TO_M2;
1121  case AreaSquareDegrees:
1122  return MM2_TO_M2 / DEG2_TO_M2;
1123  case AreaSquareCentimeters:
1124  return MM2_TO_M2 / CM2_TO_M2;
1125  case AreaSquareMillimeters:
1126  return 1.0;
1127  case AreaUnknownUnit:
1128  break;
1129  }
1130 
1131  break;
1132  }
1133  case AreaSquareCentimeters:
1134  {
1135  switch ( toUnit )
1136  {
1137  case AreaSquareMeters:
1138  return CM2_TO_M2;
1139  case AreaSquareKilometers:
1140  return CM2_TO_M2 / KM2_TO_M2;
1141  case AreaSquareFeet:
1142  return CM2_TO_M2 / FT2_TO_M2;
1143  case AreaSquareYards:
1144  return CM2_TO_M2 / YD2_TO_M2;
1145  case AreaSquareMiles:
1146  return CM2_TO_M2 / MI2_TO_M2;
1147  case AreaHectares:
1148  return CM2_TO_M2 / HA_TO_M2;
1149  case AreaAcres:
1150  return CM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1152  return CM2_TO_M2 / NM2_TO_M2;
1153  case AreaSquareDegrees:
1154  return CM2_TO_M2 / DEG2_TO_M2;
1155  case AreaSquareCentimeters:
1156  return 1.0;
1157  case AreaSquareMillimeters:
1158  return CM2_TO_M2 / MM2_TO_M2;
1159  case AreaUnknownUnit:
1160  break;
1161  }
1162 
1163  break;
1164  }
1165  case AreaUnknownUnit:
1166  break;
1167  }
1168  return 1.0;
1169 }
1170 
1172 {
1173  switch ( distanceUnit )
1174  {
1175  case DistanceMeters:
1176  return AreaSquareMeters;
1177 
1178  case DistanceKilometers:
1179  return AreaSquareKilometers;
1180 
1181  case DistanceCentimeters:
1182  return AreaSquareCentimeters;
1183 
1184  case DistanceMillimeters:
1185  return AreaSquareMillimeters;
1186 
1187  case DistanceFeet:
1188  return AreaSquareFeet;
1189 
1190  case DistanceYards:
1191  return AreaSquareYards;
1192 
1193  case DistanceMiles:
1194  return AreaSquareMiles;
1195 
1196  case DistanceDegrees:
1197  return AreaSquareDegrees;
1198 
1199  case DistanceUnknownUnit:
1200  return AreaUnknownUnit;
1201 
1202  case DistanceNauticalMiles:
1203  return AreaSquareNauticalMiles;
1204  }
1205 
1206  return AreaUnknownUnit;
1207 }
1208 
1210 {
1211  switch ( areaUnit )
1212  {
1213  case AreaSquareMeters:
1214  case AreaHectares:
1215  return DistanceMeters;
1216 
1217  case AreaSquareKilometers:
1218  return DistanceKilometers;
1219 
1220  case AreaSquareCentimeters:
1221  return DistanceCentimeters;
1222 
1223  case AreaSquareMillimeters:
1224  return DistanceMillimeters;
1225 
1226  case AreaSquareFeet:
1227  return DistanceFeet;
1228 
1229  case AreaSquareYards:
1230  case AreaAcres:
1231  return DistanceYards;
1232 
1233  case AreaSquareMiles:
1234  return DistanceMiles;
1235 
1236  case AreaSquareDegrees:
1237  return DistanceDegrees;
1238 
1239  case AreaUnknownUnit:
1240  return DistanceUnknownUnit;
1241 
1243  return DistanceNauticalMiles;
1244  }
1245 
1246  return DistanceUnknownUnit;
1247 }
1248 
1250 {
1251  QString normalized = string.trimmed().toLower();
1252 
1253  if ( ok )
1254  *ok = true;
1255 
1256  if ( normalized == encodeUnit( VolumeCubicMeters ) )
1257  return VolumeCubicMeters;
1258  if ( normalized == encodeUnit( VolumeCubicFeet ) )
1259  return VolumeCubicFeet;
1260  if ( normalized == encodeUnit( VolumeCubicYards ) )
1261  return VolumeCubicYards;
1262  if ( normalized == encodeUnit( VolumeBarrel ) )
1263  return VolumeBarrel;
1264  if ( normalized == encodeUnit( VolumeCubicDecimeter ) )
1265  return VolumeCubicDecimeter;
1266  if ( normalized == encodeUnit( VolumeLiters ) )
1267  return VolumeLiters;
1268  if ( normalized == encodeUnit( VolumeGallonUS ) )
1269  return VolumeGallonUS;
1270  if ( normalized == encodeUnit( VolumeCubicInch ) )
1271  return VolumeCubicInch;
1272  if ( normalized == encodeUnit( VolumeCubicCentimeter ) )
1273  return VolumeCubicCentimeter;
1274  if ( normalized == encodeUnit( VolumeCubicDegrees ) )
1275  return VolumeCubicDegrees;
1276  if ( normalized == encodeUnit( VolumeUnknownUnit ) )
1277  return VolumeUnknownUnit;
1278 
1279  if ( ok )
1280  *ok = false;
1281 
1282  return VolumeUnknownUnit;
1283 }
1284 
1286 {
1287  switch ( unit )
1288  {
1289  case VolumeCubicMeters:
1290  return QObject::tr( "cubic meters", "volume" );
1291  case VolumeCubicFeet:
1292  return QObject::tr( "cubic feet", "volume" );
1293  case VolumeCubicYards:
1294  return QObject::tr( "cubic yards", "volume" );
1295  case VolumeBarrel:
1296  return QObject::tr( "barrels", "volume" );
1297  case VolumeCubicDecimeter:
1298  return QObject::tr( "cubic decimeters", "volume" );
1299  case VolumeLiters:
1300  return QObject::tr( "liters", "volume" );
1301  case VolumeGallonUS:
1302  return QObject::tr( "gallons", "volume" );
1303  case VolumeCubicInch:
1304  return QObject::tr( "cubic inches", "volume" );
1305  case VolumeCubicCentimeter:
1306  return QObject::tr( "cubic centimeters", "volume" );
1307  case VolumeCubicDegrees:
1308  return QObject::tr( "cubic degrees", "volume" );
1309  case VolumeUnknownUnit:
1310  return QObject::tr( "<unknown>", "volume" );
1311  }
1312  return QString();
1313 }
1314 
1316 {
1317  switch ( unit )
1318  {
1319  case VolumeCubicMeters:
1320  return QObject::tr( "m³", "volume" );
1321  case VolumeCubicFeet:
1322  return QObject::tr( "ft³", "volume" );
1323  case VolumeCubicYards:
1324  return QObject::tr( "yds³", "volume" );
1325  case VolumeBarrel:
1326  return QObject::tr( "bbl", "volume" );
1327  case VolumeCubicDecimeter:
1328  return QObject::tr( "dm³", "volume" );
1329  case VolumeLiters:
1330  return QObject::tr( "l", "volume" );
1331  case VolumeGallonUS:
1332  return QObject::tr( "gal", "volume" );
1333  case VolumeCubicInch:
1334  return QObject::tr( "in³", "volume" );
1335  case VolumeCubicCentimeter:
1336  return QObject::tr( "cm³", "volume" );
1337  case VolumeCubicDegrees:
1338  return QObject::tr( "deg³", "volume" );
1339  case VolumeUnknownUnit:
1340  return QObject::tr( "<unknown>", "volume" );
1341  }
1342  return QString();
1343 
1344 }
1345 
1347 {
1348  QString normalized = string.trimmed().toLower();
1349 
1350  if ( ok )
1351  *ok = true;
1352 
1353  if ( normalized == toString( VolumeCubicMeters ) )
1354  return VolumeCubicMeters;
1355  if ( normalized == toString( VolumeCubicFeet ) )
1356  return VolumeCubicFeet;
1357  if ( normalized == toString( VolumeCubicYards ) )
1358  return VolumeCubicYards;
1359  if ( normalized == toString( VolumeBarrel ) )
1360  return VolumeBarrel;
1361  if ( normalized == toString( VolumeCubicDecimeter ) )
1362  return VolumeCubicDecimeter;
1363  if ( normalized == toString( VolumeLiters ) )
1364  return VolumeLiters;
1365  if ( normalized == toString( VolumeGallonUS ) )
1366  return VolumeGallonUS;
1367  if ( normalized == toString( VolumeCubicInch ) )
1368  return VolumeCubicInch;
1369  if ( normalized == toString( VolumeCubicCentimeter ) )
1370  return VolumeCubicCentimeter;
1371  if ( normalized == toString( VolumeCubicDegrees ) )
1372  return VolumeCubicDegrees;
1373  if ( normalized == toString( VolumeUnknownUnit ) )
1374  return VolumeUnknownUnit;
1375 
1376  if ( ok )
1377  *ok = false;
1378 
1379  return VolumeUnknownUnit;
1380 }
1381 
1382 #define DEG2_TO_M3 1379474361572186.2
1384 {
1385  switch ( fromUnit )
1386  {
1387  case VolumeCubicMeters:
1388  {
1389  switch ( toUnit )
1390  {
1391  case VolumeCubicMeters:
1392  return 1.0;
1393  case VolumeCubicFeet:
1394  return 35.314666572222;
1395  case VolumeCubicYards:
1396  return 1.307950613786;
1397  case VolumeBarrel:
1398  return 6.2898107438466;
1399  case VolumeCubicDecimeter:
1400  return 1000;
1401  case VolumeLiters:
1402  return 1000;
1403  case VolumeGallonUS:
1404  return 264.17205124156;
1405  case VolumeCubicInch:
1406  return 61023.7438368;
1407  case VolumeCubicCentimeter:
1408  return 1000000;
1409  case VolumeCubicDegrees:
1410  return 1 / DEG2_TO_M3; // basically meaningless!
1411  case VolumeUnknownUnit:
1412  return 1.0;
1413  }
1414  break;
1415  }
1416  case VolumeCubicFeet:
1417  {
1418  switch ( toUnit )
1419  {
1420  case VolumeCubicMeters:
1421  return 0.028316846592;
1422  case VolumeCubicFeet:
1423  return 1.0;
1424  case VolumeCubicYards:
1425  return 0.037037037;
1426  case VolumeBarrel:
1427  return 0.178107622;
1428  case VolumeCubicDecimeter:
1429  return 28.31685;
1430  case VolumeLiters:
1431  return 28.31685;
1432  case VolumeGallonUS:
1433  return 7.480519954;
1434  case VolumeCubicInch:
1435  return 1728.000629765;
1436  case VolumeCubicCentimeter:
1437  return 28316.85;
1438  case VolumeCubicDegrees:
1439  return 0.028316846592 / DEG2_TO_M3; // basically meaningless!
1440  case VolumeUnknownUnit:
1441  return 1.0;
1442  }
1443  break;
1444  }
1445  case VolumeCubicYards:
1446  {
1447  switch ( toUnit )
1448  {
1449  case VolumeCubicMeters:
1450  return 0.764554900;
1451  case VolumeCubicFeet:
1452  return 26.999998234;
1453  case VolumeCubicYards:
1454  return 1.0;
1455  case VolumeBarrel:
1456  return 4.808905491;
1457  case VolumeCubicDecimeter:
1458  return 764.5549;
1459  case VolumeLiters:
1460  return 764.5549;
1461  case VolumeGallonUS:
1462  return 201.974025549;
1463  case VolumeCubicInch:
1464  return 46656.013952472;
1465  case VolumeCubicCentimeter:
1466  return 764554.9;
1467  case VolumeCubicDegrees:
1468  return 0.764554900 / DEG2_TO_M3; // basically meaningless!
1469  case VolumeUnknownUnit:
1470  return 1.0;
1471  }
1472  break;
1473  }
1474  case VolumeBarrel:
1475  {
1476  switch ( toUnit )
1477  {
1478  case VolumeCubicMeters:
1479  return 0.158987300;
1480  case VolumeCubicFeet:
1481  return 5.614582837;
1482  case VolumeCubicYards:
1483  return 0.207947526;
1484  case VolumeBarrel:
1485  return 1.0;
1486  case VolumeCubicDecimeter:
1487  return 158.9873;
1488  case VolumeLiters:
1489  return 158.9873;
1490  case VolumeGallonUS:
1491  return 41.999998943;
1492  case VolumeCubicInch:
1493  return 9702.002677722;
1494  case VolumeCubicCentimeter:
1495  return 158987.3;
1496  case VolumeCubicDegrees:
1497  return 0.158987300 / DEG2_TO_M3; // basically meaningless!
1498  case VolumeUnknownUnit:
1499  return 1.0;
1500  }
1501  break;
1502  }
1503  case VolumeCubicDecimeter:
1504  case VolumeLiters:
1505  {
1506  switch ( toUnit )
1507  {
1508  case VolumeCubicMeters:
1509  return 0.001;
1510  case VolumeCubicFeet:
1511  return 0.035314662;
1512  case VolumeCubicYards:
1513  return 0.001307951;
1514  case VolumeBarrel:
1515  return 0.006289811;
1516  case VolumeCubicDecimeter:
1517  case VolumeLiters:
1518  return 1.0;
1519  case VolumeGallonUS:
1520  return 0.264172037;
1521  case VolumeCubicInch:
1522  return 61.023758990;
1523  case VolumeCubicCentimeter:
1524  return 1000;
1525  case VolumeCubicDegrees:
1526  return 0.001 / DEG2_TO_M3; // basically meaningless!
1527  case VolumeUnknownUnit:
1528  return 1.0;
1529  }
1530  break;
1531  }
1532  case VolumeGallonUS:
1533  {
1534  switch ( toUnit )
1535  {
1536  case VolumeCubicMeters:
1537  return 0.003785412;
1538  case VolumeCubicFeet:
1539  return 0.133680547;
1540  case VolumeCubicYards:
1541  return 0.004951132;
1542  case VolumeBarrel:
1543  return 0.023809524;
1544  case VolumeCubicDecimeter:
1545  case VolumeLiters:
1546  return 3.785412000;
1547  case VolumeGallonUS:
1548  return 1.0;
1549  case VolumeCubicInch:
1550  return 231.000069567;
1551  case VolumeCubicCentimeter:
1552  return 3785.412;
1553  case VolumeCubicDegrees:
1554  return 0.003785412 / DEG2_TO_M3; // basically meaningless!
1555  case VolumeUnknownUnit:
1556  return 1.0;
1557  }
1558  break;
1559  }
1560  case VolumeCubicInch:
1561  {
1562  switch ( toUnit )
1563  {
1564  case VolumeCubicMeters:
1565  return 0.000016387;
1566  case VolumeCubicFeet:
1567  return 0.000578703;
1568  case VolumeCubicYards:
1569  return 0.000021433;
1570  case VolumeBarrel:
1571  return 0.000103072;
1572  case VolumeCubicDecimeter:
1573  case VolumeLiters:
1574  return 0.016387060;
1575  case VolumeGallonUS:
1576  return 0.004329003;
1577  case VolumeCubicInch:
1578  return 1.0;
1579  case VolumeCubicCentimeter:
1580  return 16.387060000;
1581  case VolumeCubicDegrees:
1582  return 0.000016387 / DEG2_TO_M3; // basically meaningless!
1583  case VolumeUnknownUnit:
1584  return 1.0;
1585  }
1586  break;
1587  }
1588  case VolumeCubicCentimeter:
1589  {
1590  switch ( toUnit )
1591  {
1592  case VolumeCubicMeters:
1593  return 0.000001;
1594  case VolumeCubicFeet:
1595  return 0.000035315;
1596  case VolumeCubicYards:
1597  return 0.000001308;
1598  case VolumeBarrel:
1599  return 0.000006290;
1600  case VolumeCubicDecimeter:
1601  case VolumeLiters:
1602  return 0.001;
1603  case VolumeGallonUS:
1604  return 0.000264172 ;
1605  case VolumeCubicInch:
1606  return 0.061023759;
1607  case VolumeCubicCentimeter:
1608  return 1.0;
1609  case VolumeCubicDegrees:
1610  return 0.000001 / DEG2_TO_M3; // basically meaningless!
1611  case VolumeUnknownUnit:
1612  return 1.0;
1613  }
1614  break;
1615  }
1616  case VolumeCubicDegrees:
1617  if ( toUnit == VolumeUnknownUnit || toUnit == VolumeCubicDegrees )
1618  return 1.0;
1619  else
1621 
1622  case VolumeUnknownUnit:
1623  {
1624  return 1.0;
1625  }
1626  }
1627  return 1.0;
1628 }
1629 
1631 {
1632  switch ( distanceUnit )
1633  {
1634  case DistanceMeters:
1635  return VolumeCubicMeters;
1636 
1637  case DistanceKilometers:
1638  return VolumeCubicMeters;
1639 
1640  case DistanceCentimeters:
1641  return VolumeCubicCentimeter;
1642 
1643  case DistanceMillimeters:
1644  return VolumeCubicCentimeter;
1645 
1646  case DistanceFeet:
1647  return VolumeCubicFeet;
1648 
1649  case DistanceYards:
1650  return VolumeCubicYards;
1651 
1652  case DistanceMiles:
1653  return VolumeCubicFeet;
1654 
1655  case DistanceDegrees:
1656  return VolumeCubicDegrees;
1657 
1658  case DistanceUnknownUnit:
1659  return VolumeUnknownUnit;
1660 
1661  case DistanceNauticalMiles:
1662  return VolumeCubicFeet;
1663  }
1664 
1665  return VolumeUnknownUnit;
1666 }
1667 
1669 {
1670  switch ( volumeUnit )
1671  {
1672  case VolumeCubicMeters:
1673  return DistanceMeters;
1674  case VolumeCubicFeet:
1675  return DistanceFeet;
1676  case VolumeCubicYards:
1677  return DistanceYards;
1678  case VolumeBarrel:
1679  return DistanceFeet;
1680  case VolumeCubicDecimeter:
1681  return DistanceCentimeters;
1682  case VolumeLiters:
1683  return DistanceMeters;
1684  case VolumeGallonUS:
1685  return DistanceFeet;
1686  case VolumeCubicInch:
1687  return DistanceFeet;
1688  case VolumeCubicCentimeter:
1689  return DistanceCentimeters;
1690  case VolumeCubicDegrees:
1691  return DistanceDegrees;
1692  case VolumeUnknownUnit:
1693  return DistanceUnknownUnit;
1694  }
1695  return DistanceUnknownUnit;
1696 }
1697 
1699 {
1700  switch ( unit )
1701  {
1702  case VolumeCubicMeters:
1703  case VolumeCubicFeet:
1704  case VolumeCubicYards:
1705  case VolumeBarrel:
1706  case VolumeCubicDecimeter:
1707  case VolumeLiters:
1708  case VolumeGallonUS:
1709  case VolumeCubicInch:
1710  case VolumeCubicCentimeter:
1711  return Standard;
1712  case VolumeCubicDegrees:
1713  return Geographic;
1714  case VolumeUnknownUnit:
1715  return UnknownType;
1716  }
1717  return UnknownType;
1718 }
1719 
1721 {
1722  switch ( unit )
1723  {
1724  case VolumeCubicMeters:
1725  return QStringLiteral( "m3" );
1726  case VolumeCubicFeet:
1727  return QStringLiteral( "ft3" );
1728  case VolumeCubicYards:
1729  return QStringLiteral( "yd3" );
1730  case VolumeBarrel:
1731  return QStringLiteral( "bbl" );
1732  case VolumeCubicDecimeter:
1733  return QStringLiteral( "dm3" );
1734  case VolumeLiters:
1735  return QStringLiteral( "l" );
1736  case VolumeGallonUS:
1737  return QStringLiteral( "gal" );
1738  case VolumeCubicInch:
1739  return QStringLiteral( "in3" );
1740  case VolumeCubicCentimeter:
1741  return QStringLiteral( "cm3" );
1742  case VolumeCubicDegrees:
1743  return QStringLiteral( "deg3" );
1744  case VolumeUnknownUnit:
1745  return QStringLiteral( "<unknown>" );
1746  }
1747  return QString();
1748 }
1749 
1751 {
1752  switch ( unit )
1753  {
1754  case AngleDegrees:
1755  return QStringLiteral( "degrees" );
1756  case AngleRadians:
1757  return QStringLiteral( "radians" );
1758  case AngleGon:
1759  return QStringLiteral( "gon" );
1760  case AngleMinutesOfArc:
1761  return QStringLiteral( "moa" );
1762  case AngleSecondsOfArc:
1763  return QStringLiteral( "soa" );
1764  case AngleTurn:
1765  return QStringLiteral( "tr" );
1766  case AngleMilliradiansSI:
1767  return QStringLiteral( "milliradians" );
1768  case AngleMilNATO:
1769  return QStringLiteral( "mil" );
1770  case AngleUnknownUnit:
1771  return QStringLiteral( "<unknown>" );
1772  }
1773  return QString();
1774 }
1775 
1776 QgsUnitTypes::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
1777 {
1778  QString normalized = string.trimmed().toLower();
1779 
1780  if ( ok )
1781  *ok = true;
1782 
1783  if ( normalized == encodeUnit( AngleDegrees ) )
1784  return AngleDegrees;
1785  if ( normalized == encodeUnit( AngleRadians ) )
1786  return AngleRadians;
1787  if ( normalized == encodeUnit( AngleGon ) )
1788  return AngleGon;
1789  if ( normalized == encodeUnit( AngleMinutesOfArc ) )
1790  return AngleMinutesOfArc;
1791  if ( normalized == encodeUnit( AngleSecondsOfArc ) )
1792  return AngleSecondsOfArc;
1793  if ( normalized == encodeUnit( AngleTurn ) )
1794  return AngleTurn;
1795  if ( normalized == encodeUnit( AngleMilliradiansSI ) )
1796  return AngleMilliradiansSI;
1797  if ( normalized == encodeUnit( AngleMilNATO ) )
1798  return AngleMilNATO;
1799  if ( normalized == encodeUnit( AngleUnknownUnit ) )
1800  return AngleUnknownUnit;
1801  if ( ok )
1802  *ok = false;
1803 
1804  return AngleUnknownUnit;
1805 }
1806 
1808 {
1809  switch ( unit )
1810  {
1811  case AngleDegrees:
1812  return QObject::tr( "degrees", "angle" );
1813  case AngleRadians:
1814  return QObject::tr( "radians", "angle" );
1815  case AngleGon:
1816  return QObject::tr( "gon", "angle" );
1817  case AngleMinutesOfArc:
1818  return QObject::tr( "minutes of arc", "angle" );
1819  case AngleSecondsOfArc:
1820  return QObject::tr( "seconds of arc", "angle" );
1821  case AngleTurn:
1822  return QObject::tr( "turns", "angle" );
1823  case AngleMilliradiansSI:
1824  return QObject::tr( "milliradians", "angle" );
1825  case AngleMilNATO:
1826  return QObject::tr( "mil", "angle" );
1827  case AngleUnknownUnit:
1828  return QObject::tr( "<unknown>", "angle" );
1829  }
1830  return QString();
1831 }
1832 
1834 {
1835  // Calculate the conversion factor between the specified units
1836  switch ( fromUnit )
1837  {
1838  case AngleDegrees:
1839  {
1840  switch ( toUnit )
1841  {
1842  case AngleDegrees:
1843  return 1.0;
1844  case AngleRadians:
1845  return M_PI / 180.0;
1846  case AngleGon:
1847  return 400.0 / 360.0;
1848  case AngleMinutesOfArc:
1849  return 60;
1850  case AngleSecondsOfArc:
1851  return 3600;
1852  case AngleTurn:
1853  return 1.0 / 360.0;
1854  case AngleMilliradiansSI:
1855  return M_PI / 180.0 * 1000;
1856  case AngleMilNATO:
1857  return 3200.0 / 180;
1858  case AngleUnknownUnit:
1859  break;
1860  }
1861  break;
1862  }
1863  case AngleRadians:
1864  {
1865  switch ( toUnit )
1866  {
1867  case AngleDegrees:
1868  return 180.0 / M_PI;
1869  case AngleRadians:
1870  return 1.0;
1871  case AngleGon:
1872  return 200.0 / M_PI;
1873  case AngleMinutesOfArc:
1874  return 60 * 180.0 / M_PI;
1875  case AngleSecondsOfArc:
1876  return 3600 * 180.0 / M_PI;
1877  case AngleTurn:
1878  return 0.5 / M_PI;
1879  case AngleMilliradiansSI:
1880  return 1000;
1881  case AngleMilNATO:
1882  return 3200.0 / M_PI;
1883  case AngleUnknownUnit:
1884  break;
1885  }
1886  break;
1887  }
1888  case AngleGon:
1889  {
1890  switch ( toUnit )
1891  {
1892  case AngleDegrees:
1893  return 360.0 / 400.0;
1894  case AngleRadians:
1895  return M_PI / 200.0;
1896  case AngleGon:
1897  return 1.0;
1898  case AngleMinutesOfArc:
1899  return 60 * 360.0 / 400.0;
1900  case AngleSecondsOfArc:
1901  return 3600 * 360.0 / 400.0;
1902  case AngleTurn:
1903  return 1.0 / 400.0;
1904  case AngleMilliradiansSI:
1905  return M_PI / 200.0 * 1000;
1906  case AngleMilNATO:
1907  return 3200.0 / 200.0;
1908  case AngleUnknownUnit:
1909  break;
1910  }
1911  break;
1912  }
1913  case AngleMinutesOfArc:
1914  {
1915  switch ( toUnit )
1916  {
1917  case AngleDegrees:
1918  return 1 / 60.0;
1919  case AngleRadians:
1920  return M_PI / 180.0 / 60.0;
1921  case AngleGon:
1922  return 400.0 / 360.0 / 60.0;
1923  case AngleMinutesOfArc:
1924  return 1.0;
1925  case AngleSecondsOfArc:
1926  return 60.0;
1927  case AngleTurn:
1928  return 1.0 / 360.0 / 60.0;
1929  case AngleMilliradiansSI:
1930  return M_PI / 180.0 / 60.0 * 1000;
1931  case AngleMilNATO:
1932  return 3200.0 / 180.0 / 60.0;
1933  case AngleUnknownUnit:
1934  break;
1935  }
1936  break;
1937  }
1938  case AngleSecondsOfArc:
1939  {
1940  switch ( toUnit )
1941  {
1942  case AngleDegrees:
1943  return 1 / 3600.0;
1944  case AngleRadians:
1945  return M_PI / 180.0 / 3600.0;
1946  case AngleGon:
1947  return 400.0 / 360.0 / 3600.0;
1948  case AngleMinutesOfArc:
1949  return 1.0 / 60.0;
1950  case AngleSecondsOfArc:
1951  return 1.0;
1952  case AngleTurn:
1953  return 1.0 / 360.0 / 3600.0;
1954  case AngleMilliradiansSI:
1955  return M_PI / 180.0 / 3600.0 * 1000;
1956  case AngleMilNATO:
1957  return 3200.0 / 180.0 / 3600.0;
1958  case AngleUnknownUnit:
1959  break;
1960  }
1961  break;
1962  }
1963  case AngleTurn:
1964  {
1965  switch ( toUnit )
1966  {
1967  case AngleDegrees:
1968  return 360.0;
1969  case AngleRadians:
1970  return 2 * M_PI;
1971  case AngleGon:
1972  return 400.0;
1973  case AngleMinutesOfArc:
1974  return 360.0 * 60.0;
1975  case AngleSecondsOfArc:
1976  return 360.0 * 3600.0;
1977  case AngleTurn:
1978  return 1.0;
1979  case AngleMilliradiansSI:
1980  return 2 * M_PI * 1000;
1981  case AngleMilNATO:
1982  return 2 * 3200;
1983  case AngleUnknownUnit:
1984  break;
1985  }
1986  break;
1987  }
1988  case AngleMilliradiansSI:
1989  {
1990  switch ( toUnit )
1991  {
1992  case AngleDegrees:
1993  return 180.0 / M_PI / 1000;
1994  case AngleRadians:
1995  return 0.001;
1996  case AngleGon:
1997  return 200.0 / M_PI / 1000;
1998  case AngleMinutesOfArc:
1999  return 180.0 * 60.0 / M_PI / 1000;
2000  case AngleSecondsOfArc:
2001  return 180.0 * 3600.0 / M_PI / 1000;
2002  case AngleTurn:
2003  return M_PI / 2 / 1000;
2004  case AngleMilliradiansSI:
2005  return 1.0;
2006  case AngleMilNATO:
2007  return 3200.0 / 1000.0 / M_PI;
2008  case AngleUnknownUnit:
2009  break;
2010  }
2011  break;
2012  }
2013 
2014  case AngleMilNATO:
2015  {
2016  switch ( toUnit )
2017  {
2018  case AngleDegrees:
2019  return 180.0 / 3200;
2020  case AngleRadians:
2021  return M_PI / 3200;
2022  case AngleGon:
2023  return 200.0 / 3200;
2024  case AngleMinutesOfArc:
2025  return 60 * 180.0 / 3200;
2026  case AngleSecondsOfArc:
2027  return 3600.0 * 180 / 3200;
2028  case AngleTurn:
2029  return 1.0 / ( 2 * 32000 );
2030  case AngleMilliradiansSI:
2031  return 1000.0 * M_PI / 3200.0;
2032  case AngleMilNATO:
2033  return 1.0;
2034  case AngleUnknownUnit:
2035  break;
2036  }
2037  break;
2038  }
2039 
2040  case AngleUnknownUnit:
2041  break;
2042  }
2043  return 1.0;
2044 }
2045 
2046 QString QgsUnitTypes::formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit )
2047 {
2048  QString unitLabel;
2049 
2050  switch ( unit )
2051  {
2052  case AngleDegrees:
2053  unitLabel = QObject::tr( "°", "angle" );
2054  break;
2055  case AngleRadians:
2056  unitLabel = QObject::tr( " rad", "angle" );
2057  break;
2058  case AngleGon:
2059  unitLabel = QObject::tr( " gon", "angle" );
2060  break;
2061  case AngleMinutesOfArc:
2062  unitLabel = QObject::tr( "′", "angle minutes" );
2063  break;
2064  case AngleSecondsOfArc:
2065  unitLabel = QObject::tr( "″", "angle seconds" );
2066  break;
2067  case AngleTurn:
2068  unitLabel = QObject::tr( " tr", "angle turn" );
2069  break;
2070  case AngleMilliradiansSI:
2071  unitLabel = QObject::tr( " millirad", "angular mil SI" );
2072  break;
2073  case AngleMilNATO:
2074  unitLabel = QObject::tr( " mil", "angular mil NATO" );
2075  break;
2076  case AngleUnknownUnit:
2077  break;
2078  }
2079 
2080  return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
2081 }
2082 
2083 
2084 QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit )
2085 {
2086  DistanceValue result;
2087 
2088  switch ( unit )
2089  {
2090  case DistanceMeters:
2091  if ( keepBaseUnit )
2092  {
2093  result.value = qgsRound( distance, decimals );
2095  }
2096  else if ( std::fabs( distance ) > 1000.0 )
2097  {
2098  result.value = qgsRound( distance / 1000, decimals );
2100  }
2101  else if ( std::fabs( distance ) < 0.01 )
2102  {
2103  result.value = qgsRound( distance * 1000, decimals );
2105  }
2106  else if ( std::fabs( distance ) < 0.1 )
2107  {
2108 
2109  result.value = qgsRound( distance * 100, decimals );
2111  }
2112  else
2113  {
2114  result.value = qgsRound( distance, decimals );
2116  }
2117  break;
2118 
2119  case DistanceKilometers:
2120  if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
2121  {
2122  result.value = qgsRound( distance, decimals );
2124  }
2125  else
2126  {
2127  result.value = qgsRound( distance * 1000, decimals );
2129  }
2130  break;
2131 
2132  case DistanceFeet:
2133  if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
2134  {
2135  result.value = qgsRound( distance, decimals );
2137  }
2138  else
2139  {
2140  result.value = qgsRound( distance / 5280.0, decimals );
2142  }
2143  break;
2144 
2145  case DistanceYards:
2146  if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
2147  {
2148  result.value = qgsRound( distance, decimals );
2150  }
2151  else
2152  {
2153  result.value = qgsRound( distance / 1760.0, decimals );
2155  }
2156  break;
2157 
2158  case DistanceMiles:
2159  if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
2160  {
2161  result.value = qgsRound( distance, decimals );
2163  }
2164  else
2165  {
2166  result.value = qgsRound( distance * 5280.0, decimals );
2168  }
2169  break;
2170 
2171  case DistanceNauticalMiles:
2172  result.value = qgsRound( distance, decimals );
2174  break;
2175 
2176  case DistanceDegrees:
2177  result.value = qgsRound( distance, decimals );
2179  break;
2180 
2181  case DistanceUnknownUnit:
2182  result.value = qgsRound( distance, decimals );
2184  break;
2185 
2186  default:
2187  result.value = qgsRound( distance, decimals );
2188  result.unit = unit;
2189  break;
2190  }
2191 
2192  return result;
2193 }
2194 
2195 QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit )
2196 {
2197  AreaValue result;
2198  result.value = -1.0;
2199  result.unit = AreaUnknownUnit;
2200 
2201  // If we are not forced to keep the base units, switch to meter calculation
2202  if ( unit == AreaSquareMillimeters )
2203  {
2204  if ( keepBaseUnit )
2205  {
2206  result.value = qgsRound( area, decimals );
2208  }
2209  else
2210  {
2211  area /= 1000000.0;
2213  }
2214  }
2215  else if ( unit == AreaSquareCentimeters )
2216  {
2217  if ( keepBaseUnit )
2218  {
2219  result.value = qgsRound( area, decimals );
2221  }
2222  else
2223  {
2224  area /= 10000.0;
2226  }
2227  }
2228 
2229  switch ( unit )
2230  {
2231  case AreaSquareCentimeters:
2232  // handled in the if above
2233  break;
2234  case AreaSquareMillimeters:
2235  // handled in the if above
2236  break;
2237  case AreaSquareMeters:
2238  {
2239  if ( keepBaseUnit )
2240  {
2241  result.value = qgsRound( area, decimals );
2243  }
2245  {
2248  }
2250  {
2253  }
2254  else
2255  {
2256  result.value = qgsRound( area, decimals );
2258  }
2259  break;
2260  }
2261 
2262  case AreaSquareKilometers:
2263  {
2264  result.value = qgsRound( area, decimals );
2266  break;
2267  }
2268 
2269  case AreaSquareFeet:
2270  {
2271  if ( keepBaseUnit )
2272  {
2273  result.value = qgsRound( area, decimals );
2275  }
2277  {
2280  }
2281  else
2282  {
2283  result.value = qgsRound( area, decimals );
2285  }
2286  break;
2287  }
2288 
2289  case AreaSquareYards:
2290  {
2291  if ( keepBaseUnit )
2292  {
2293  result.value = qgsRound( area, decimals );
2295  }
2297  {
2300  }
2301  else
2302  {
2303  result.value = qgsRound( area, decimals );
2305  }
2306  break;
2307  }
2308 
2309  case AreaSquareMiles:
2310  {
2311  result.value = qgsRound( area, decimals );
2313  break;
2314  }
2315 
2316  case AreaHectares:
2317  {
2318  if ( keepBaseUnit )
2319  {
2320  result.value = qgsRound( area, decimals );
2322  }
2324  {
2327  }
2328  else
2329  {
2330  result.value = qgsRound( area, decimals );
2332  }
2333  break;
2334  }
2335 
2336  case AreaAcres:
2337  {
2338  if ( keepBaseUnit )
2339  {
2340  result.value = qgsRound( area, decimals );
2341  result.unit = QgsUnitTypes::AreaAcres;
2342  }
2344  {
2347  }
2348  else
2349  {
2350  result.value = qgsRound( area, decimals );
2351  result.unit = QgsUnitTypes::AreaAcres;
2352  }
2353  break;
2354  }
2355 
2357  {
2358  result.value = qgsRound( area, decimals );
2360  break;
2361  }
2362 
2363  case AreaSquareDegrees:
2364  {
2365  result.value = qgsRound( area, decimals );
2367  break;
2368  }
2369 
2370  case AreaUnknownUnit:
2371  {
2372  result.value = qgsRound( area, decimals );
2374  break;
2375  }
2376  }
2377  return result;
2378 }
2379 
2380 
2381 QString QgsUnitTypes::formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit )
2382 {
2383  DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
2384 
2385  QString unitText;
2386 
2387  if ( dist.unit != DistanceUnknownUnit )
2388  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
2389 
2390  return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
2391 }
2392 
2393 QString QgsUnitTypes::formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit )
2394 {
2395  AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
2396 
2397  QString unitText;
2398 
2399  if ( areaValue.unit != AreaUnknownUnit )
2400  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
2401 
2402  return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
2403 }
2404 
2406 {
2407  switch ( unit )
2408  {
2409  case RenderMillimeters:
2410  return QStringLiteral( "MM" );
2412  return QStringLiteral( "RenderMetersInMapUnits" );
2413  case RenderMapUnits:
2414  return QStringLiteral( "MapUnit" );
2415  case RenderPixels:
2416  return QStringLiteral( "Pixel" );
2417  case RenderPercentage:
2418  return QStringLiteral( "Percentage" );
2419  case RenderPoints:
2420  return QStringLiteral( "Point" );
2421  case RenderInches:
2422  return QStringLiteral( "Inch" );
2423  case RenderUnknownUnit:
2424  return QString();
2425  }
2426  return QString();
2427 }
2428 
2430 {
2431  QString normalized = string.trimmed().toLower();
2432 
2433  if ( ok )
2434  *ok = true;
2435 
2436  if ( normalized == encodeUnit( RenderMillimeters ).toLower() )
2437  return RenderMillimeters;
2438  if ( normalized == encodeUnit( RenderMetersInMapUnits ).toLower() )
2439  return RenderMetersInMapUnits;
2440  if ( normalized == QLatin1String( "meters" ) )
2441  return RenderMetersInMapUnits;
2442  if ( normalized == encodeUnit( RenderMapUnits ).toLower() )
2443  return RenderMapUnits;
2444  if ( normalized == QLatin1String( "mapunits" ) )
2445  return RenderMapUnits;
2446  if ( normalized == encodeUnit( RenderPixels ).toLower() )
2447  return RenderPixels;
2448  if ( normalized == encodeUnit( RenderPercentage ).toLower() )
2449  return RenderPercentage;
2450  if ( normalized == QLatin1String( "percent" ) )
2451  return RenderPercentage;
2452  if ( normalized == encodeUnit( RenderPoints ).toLower() )
2453  return RenderPoints;
2454  if ( normalized == QLatin1String( "points" ) )
2455  return RenderPoints;
2456  if ( normalized == encodeUnit( RenderInches ).toLower() )
2457  return RenderInches;
2458 
2459  if ( ok )
2460  *ok = false;
2461 
2462  // millimeters are default
2463  return RenderMillimeters;
2464 }
2465 
2467 {
2468  switch ( unit )
2469  {
2470  case RenderMillimeters:
2471  return QObject::tr( "millimeters", "render" );
2472 
2474  return QObject::tr( "meters at scale", "render" );
2475 
2476  case RenderMapUnits:
2477  return QObject::tr( "map units", "render" );
2478 
2479  case RenderPixels:
2480  return QObject::tr( "pixels", "render" );
2481 
2482  case RenderPercentage:
2483  return QObject::tr( "percent", "render" );
2484 
2485  case RenderPoints:
2486  return QObject::tr( "points", "render" );
2487 
2488  case RenderInches:
2489  return QObject::tr( "inches", "render" );
2490 
2491  case RenderUnknownUnit:
2492  return QObject::tr( "<unknown>", "render" );
2493 
2494  }
2495  return QString();
2496 }
2497 
2498 
2499 
2501 {
2502  switch ( unit )
2503  {
2504  case LayoutCentimeters:
2505  return QStringLiteral( "cm" );
2506  case LayoutMeters:
2507  return QStringLiteral( "m" );
2508  case LayoutInches:
2509  return QStringLiteral( "in" );
2510  case LayoutFeet:
2511  return QStringLiteral( "ft" );
2512  case LayoutPoints:
2513  return QStringLiteral( "pt" );
2514  case LayoutPicas:
2515  return QStringLiteral( "pi" );
2516  case LayoutPixels:
2517  return QStringLiteral( "px" );
2518  case LayoutMillimeters:
2519  return QStringLiteral( "mm" );
2520  }
2521  return QString();
2522 }
2523 
2525 {
2526  QString normalized = string.trimmed().toLower();
2527 
2528  if ( ok )
2529  *ok = true;
2530 
2531  if ( normalized == encodeUnit( LayoutMillimeters ).toLower() )
2532  return LayoutMillimeters;
2533  if ( normalized == encodeUnit( LayoutCentimeters ).toLower() )
2534  return LayoutCentimeters;
2535  if ( normalized == encodeUnit( LayoutMeters ).toLower() )
2536  return LayoutMeters;
2537  if ( normalized == encodeUnit( LayoutInches ).toLower() )
2538  return LayoutInches;
2539  if ( normalized == encodeUnit( LayoutFeet ).toLower() )
2540  return LayoutFeet;
2541  if ( normalized == encodeUnit( LayoutPoints ).toLower() )
2542  return LayoutPoints;
2543  if ( normalized == encodeUnit( LayoutPicas ).toLower() )
2544  return LayoutPicas;
2545  if ( normalized == encodeUnit( LayoutPixels ).toLower() )
2546  return LayoutPixels;
2547 
2548  if ( ok )
2549  *ok = false;
2550 
2551  // millimeters are default
2552  return LayoutMillimeters;
2553 }
2554 
2556 {
2557  switch ( units )
2558  {
2559  case LayoutPixels:
2560  return LayoutScreenUnits;
2561  case LayoutMillimeters:
2562  case LayoutCentimeters:
2563  case LayoutMeters:
2564  case LayoutInches:
2565  case LayoutFeet:
2566  case LayoutPoints:
2567  case LayoutPicas:
2568  return LayoutPaperUnits;
2569  }
2570 
2571  // avoid warnings
2572  return LayoutPaperUnits;
2573 }
2574 
2576 {
2577  switch ( unit )
2578  {
2579  case LayoutPixels:
2580  return QObject::tr( "px" );
2581  case LayoutMillimeters:
2582  return QObject::tr( "mm" );
2583  case LayoutCentimeters:
2584  return QObject::tr( "cm" );
2585  case LayoutMeters:
2586  return QObject::tr( "m" );
2587  case LayoutInches:
2588  return QObject::tr( "in", "unit inch" );
2589  case LayoutFeet:
2590  return QObject::tr( "ft" );
2591  case LayoutPoints:
2592  return QObject::tr( "pt" );
2593  case LayoutPicas:
2594  return QObject::tr( "pica" );
2595  }
2596  return QString(); // no warnings
2597 }
2598 
2600 {
2601  switch ( unit )
2602  {
2603  case LayoutPixels:
2604  return QObject::tr( "pixels" );
2605  case LayoutMillimeters:
2606  return QObject::tr( "millimeters" );
2607  case LayoutCentimeters:
2608  return QObject::tr( "centimeters" );
2609  case LayoutMeters:
2610  return QObject::tr( "meters" );
2611  case LayoutInches:
2612  return QObject::tr( "inches" );
2613  case LayoutFeet:
2614  return QObject::tr( "feet" );
2615  case LayoutPoints:
2616  return QObject::tr( "points" );
2617  case LayoutPicas:
2618  return QObject::tr( "picas" );
2619  }
2620  return QString(); // no warnings
2621 }
#define HA_TO_M2
VolumeUnit
Units of volume.
Definition: qgsunittypes.h:113
Meters value as Map units.
Definition: qgsunittypes.h:154
static Q_INVOKABLE AreaUnit decodeAreaUnit(const QString &string, bool *ok=nullptr)
Decodes an areal unit from a string.
double value
The value part of the distance.
Definition: qgsunittypes.h:209
static Q_INVOKABLE QgsUnitTypes::UnitType decodeUnitType(const QString &string, bool *ok=nullptr)
Decodes a unit type from a string.
#define DEGREE_TO_METER
QgsUnitTypes::DistanceUnit unit
The value part of the distance.
Definition: qgsunittypes.h:195
double value
The value part of the distance.
Definition: qgsunittypes.h:190
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
static Q_INVOKABLE QgsUnitTypes::DistanceValue scaledDistance(double distance, QgsUnitTypes::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 QgsUnitTypes::AngleUnit decodeAngleUnit(const QString &string, bool *ok=nullptr)
Decodes an angular unit from a string.
static Q_INVOKABLE QString formatAngle(double angle, int decimals, QgsUnitTypes::AngleUnit unit)
Returns an angle formatted as a friendly string.
static Q_INVOKABLE AreaUnit stringToAreaUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to an areal unit.
#define MI2_TO_M2
LayoutUnitType
Types of layout units.
Definition: qgsunittypes.h:173
DistanceUnitType
Types of distance units.
Definition: qgsunittypes.h:84
Unknown areal unit.
Definition: qgsunittypes.h:105
#define NMILE_TO_METER
Mixed or unknown units.
Definition: qgsunittypes.h:153
Square kilometers.
Definition: qgsunittypes.h:133
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:150
Typographic points.
Definition: qgsunittypes.h:166
#define KM2_TO_M2
static Q_INVOKABLE QgsUnitTypes::DistanceUnit areaToDistanceUnit(QgsUnitTypes::AreaUnit areaUnit)
Converts an area unit to its corresponding distance unit, e.g., square meters to meters.
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
Unit is a paper based measurement unit.
Definition: qgsunittypes.h:175
#define KILOMETERS_TO_METER
#define FEET_TO_METER
AngleUnit
Units of angles.
Definition: qgsunittypes.h:130
#define AC_TO_FT2
static Q_INVOKABLE QString formatArea(double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit=false)
Returns an area formatted as a friendly string.
Unknown unit type.
Definition: qgsunittypes.h:62
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
#define YARDS_TO_METER
#define DEG2_TO_M2
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
#define FT2_TO_M2
#define DEG2_TO_M3
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:74
#define CENTIMETERS_TO_METER
Cubic degrees, for planar geographic CRS volume measurements.
Definition: qgsunittypes.h:124
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
#define NM2_TO_M2
Square degrees, for planar geographic CRS area measurements.
Definition: qgsunittypes.h:102
A combination of distance value and unit.
Definition: qgsunittypes.h:184
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::VolumeUnit distanceToVolumeUnit(QgsUnitTypes::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding volume unit, e.g., meters to cubic meters.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66
static Q_INVOKABLE DistanceUnitType unitType(QgsUnitTypes::DistanceUnit unit)
Returns the type for a distance unit.
static Q_INVOKABLE QgsUnitTypes::DistanceUnit volumeToDistanceUnit(QgsUnitTypes::VolumeUnit volumeUnit)
Converts a volume unit to its corresponding distance unit, e.g., cubic meters to meters.
Unknown distance unit.
Definition: qgsunittypes.h:77
static Q_INVOKABLE QgsUnitTypes::AreaValue scaledArea(double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit=false)
Will convert an area with a given unit to an area value which is nice to display. ...
QgsUnitTypes::AreaUnit unit
The value part of the distance.
Definition: qgsunittypes.h:214
static Q_INVOKABLE QgsUnitTypes::DistanceUnit stringToDistanceUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a distance unit.
Unknown angle unit.
Definition: qgsunittypes.h:140
Unknown unit type.
Definition: qgsunittypes.h:88
Unit is a geographic (e.g., degree based) unit.
Definition: qgsunittypes.h:87
Points (e.g., for font sizes)
Definition: qgsunittypes.h:151
static Q_INVOKABLE QString encodeUnitType(QgsUnitTypes::UnitType type)
Encodes a unit type to a string.
static Q_INVOKABLE QString formatDistance(double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit=false)
Returns an distance formatted as a friendly string.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places...
Definition: qgis.h:328
#define MM2_TO_M2
Turn/revolutions.
Definition: qgsunittypes.h:137
#define MILLIMETERS_TO_METER
A combination of area value and unit.
Definition: qgsunittypes.h:203
#define MILES_TO_METER
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:159
Angular milliradians (SI definition, 1/1000 of radian)
Definition: qgsunittypes.h:138
Angular mil (NATO definition, 6400 mil = 2PI radians)
Definition: qgsunittypes.h:139
Unknown volume unit.
Definition: qgsunittypes.h:125
Unit is a screen based measurement unit.
Definition: qgsunittypes.h:176
UnitType
Unit types.
Definition: qgsunittypes.h:57
Terrestrial miles.
Definition: qgsunittypes.h:73
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE VolumeUnit decodeVolumeUnit(const QString &string, bool *ok=nullptr)
Decodes a volume unit from a string.
#define YARDS_TO_FEET
static Q_INVOKABLE QgsUnitTypes::AreaUnit distanceToAreaUnit(QgsUnitTypes::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding area unit, e.g., meters to square meters.
AreaUnit
Units of area.
Definition: qgsunittypes.h:92
static Q_INVOKABLE VolumeUnit stringToVolumeUnit(const QString &string, bool *ok=nullptr)
Converts a translatedstring to a volume unit.
Unit is a standard measurement unit.
Definition: qgsunittypes.h:86
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:145
Typographic picas.
Definition: qgsunittypes.h:167
#define YD2_TO_M2
#define CM2_TO_M2