QGIS API Documentation  3.9.0-Master (224899f119)
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 ( unit )
29  {
30  case DistanceMeters:
31  case DistanceFeet:
33  case DistanceYards:
34  case DistanceMiles:
35  case DistanceKilometers:
38  return Standard;
39 
40  case DistanceDegrees:
41  return Geographic;
42 
44  return UnknownType;
45  }
46  return UnknownType;
47 }
48 
50 {
51  switch ( unit )
52  {
53  case AreaSquareMeters:
55  case AreaSquareFeet:
56  case AreaSquareYards:
57  case AreaSquareMiles:
58  case AreaHectares:
59  case AreaAcres:
63  return Standard;
64 
65  case AreaSquareDegrees:
66  return Geographic;
67 
68  case AreaUnknownUnit:
69  return UnknownType;
70  }
71 
72  return UnknownType;
73 }
74 
76 {
77  switch ( unit )
78  {
79  case DistanceMeters:
80  return QStringLiteral( "meters" );
81 
82  case DistanceKilometers:
83  return QStringLiteral( "km" );
84 
85  case DistanceFeet:
86  return QStringLiteral( "feet" );
87 
88  case DistanceYards:
89  return QStringLiteral( "yd" );
90 
91  case DistanceMiles:
92  return QStringLiteral( "mi" );
93 
94  case DistanceDegrees:
95  return QStringLiteral( "degrees" );
96 
98  return QStringLiteral( "<unknown>" );
99 
101  return QStringLiteral( "nautical miles" );
102 
103  case DistanceCentimeters:
104  return QStringLiteral( "cm" );
105 
106  case DistanceMillimeters:
107  return QStringLiteral( "mm" );
108  }
109  return QString();
110 }
111 
112 /***************************************************************************
113  * This class is considered CRITICAL and any change MUST be accompanied with
114  * full unit tests in test_qgsunittypes.py.
115  * See details in QEP #17
116  ****************************************************************************/
117 
119 {
120  QString normalized = string.trimmed().toLower();
121 
122  if ( ok )
123  *ok = true;
124 
125  if ( normalized == encodeUnit( DistanceMeters ) )
126  return DistanceMeters;
127  if ( normalized == encodeUnit( DistanceFeet ) )
128  return DistanceFeet;
129  if ( normalized == encodeUnit( DistanceDegrees ) )
130  return DistanceDegrees;
131  if ( normalized == encodeUnit( DistanceNauticalMiles ) )
132  return DistanceNauticalMiles;
133  if ( normalized == encodeUnit( DistanceKilometers ) )
134  return DistanceKilometers;
135  if ( normalized == encodeUnit( DistanceYards ) )
136  return DistanceYards;
137  if ( normalized == encodeUnit( DistanceMiles ) )
138  return DistanceMiles;
139  if ( normalized == encodeUnit( DistanceCentimeters ) )
140  return DistanceCentimeters;
141  if ( normalized == encodeUnit( DistanceMillimeters ) )
142  return DistanceMillimeters;
143  if ( normalized == encodeUnit( DistanceUnknownUnit ) )
144  return DistanceUnknownUnit;
145 
146  if ( ok )
147  *ok = false;
148 
149  return DistanceUnknownUnit;
150 }
151 
153 {
154  switch ( unit )
155  {
156  case DistanceMeters:
157  return QObject::tr( "meters", "distance" );
158 
159  case DistanceKilometers:
160  return QObject::tr( "kilometers", "distance" );
161 
162  case DistanceFeet:
163  return QObject::tr( "feet", "distance" );
164 
165  case DistanceYards:
166  return QObject::tr( "yards", "distance" );
167 
168  case DistanceMiles:
169  return QObject::tr( "miles", "distance" );
170 
171  case DistanceDegrees:
172  return QObject::tr( "degrees", "distance" );
173 
174  case DistanceCentimeters:
175  return QObject::tr( "centimeters", "distance" );
176 
177  case DistanceMillimeters:
178  return QObject::tr( "millimeters", "distance" );
179 
180  case DistanceUnknownUnit:
181  return QObject::tr( "<unknown>", "distance" );
182 
184  return QObject::tr( "nautical miles", "distance" );
185  }
186  return QString();
187 }
188 
190 {
191  switch ( unit )
192  {
193  case RenderMillimeters:
194  return QObject::tr( "mm", "render" );
195 
196  case RenderMapUnits:
197  return QObject::tr( "map units", "render" );
198 
199  case RenderPixels:
200  return QObject::tr( "px", "render" );
201 
202  case RenderPercentage:
203  return QObject::tr( "%", "render" );
204 
205  case RenderPoints:
206  return QObject::tr( "pt", "render" );
207 
208  case RenderInches:
209  return QObject::tr( "in", "render" );
210 
211  case RenderUnknownUnit:
212  return QObject::tr( "unknown", "render" );
213 
215  return QObject::tr( "m", "render" );
216 
217  }
218 
219  return QString();
220 }
221 
223 {
224  switch ( unit )
225  {
226  case DistanceMeters:
227  return QObject::tr( "m", "distance" );
228 
229  case DistanceKilometers:
230  return QObject::tr( "km", "distance" );
231 
232  case DistanceFeet:
233  return QObject::tr( "ft", "distance" );
234 
235  case DistanceYards:
236  return QObject::tr( "yd", "distance" );
237 
238  case DistanceMiles:
239  return QObject::tr( "mi", "distance" );
240 
241  case DistanceDegrees:
242  return QObject::tr( "deg", "distance" );
243 
244  case DistanceCentimeters:
245  return QObject::tr( "cm", "distance" );
246 
247  case DistanceMillimeters:
248  return QObject::tr( "mm", "distance" );
249 
250  case DistanceUnknownUnit:
251  return QString();
252 
254  return QObject::tr( "NM", "distance" );
255  }
256  return QString();
257 }
258 
259 /***************************************************************************
260  * This class is considered CRITICAL and any change MUST be accompanied with
261  * full unit tests in test_qgsunittypes.py.
262  * See details in QEP #17
263  ****************************************************************************/
264 
266 {
267  QString normalized = string.trimmed().toLower();
268 
269  if ( ok )
270  *ok = true;
271 
272  if ( normalized == toString( DistanceMeters ) )
273  return DistanceMeters;
274  if ( normalized == toString( DistanceKilometers ) )
275  return DistanceKilometers;
276  if ( normalized == toString( DistanceFeet ) )
277  return DistanceFeet;
278  if ( normalized == toString( DistanceYards ) )
279  return DistanceYards;
280  if ( normalized == toString( DistanceMiles ) )
281  return DistanceMiles;
282  if ( normalized == toString( DistanceDegrees ) )
283  return DistanceDegrees;
284  if ( normalized == toString( DistanceCentimeters ) )
285  return DistanceCentimeters;
286  if ( normalized == toString( DistanceMillimeters ) )
287  return DistanceMillimeters;
288  if ( normalized == toString( DistanceNauticalMiles ) )
289  return DistanceNauticalMiles;
290  if ( normalized == toString( DistanceUnknownUnit ) )
291  return DistanceUnknownUnit;
292 
293  if ( ok )
294  *ok = false;
295 
296  return DistanceUnknownUnit;
297 }
298 
299 /***************************************************************************
300  * This class is considered CRITICAL and any change MUST be accompanied with
301  * full unit tests in test_qgsunittypes.py.
302  * See details in QEP #17
303  ****************************************************************************/
304 
306 {
307 #define DEGREE_TO_METER 111319.49079327358
308 #define FEET_TO_METER 0.3048
309 #define NMILE_TO_METER 1852.0
310 #define KILOMETERS_TO_METER 1000.0
311 #define CENTIMETERS_TO_METER 0.01
312 #define MILLIMETERS_TO_METER 0.001
313 #define YARDS_TO_METER 0.9144
314 #define YARDS_TO_FEET 3.0
315 #define MILES_TO_METER 1609.344
316 
317  // Calculate the conversion factor between the specified units
318  switch ( fromUnit )
319  {
320  case DistanceMeters:
321  {
322  switch ( toUnit )
323  {
324  case DistanceMeters:
325  return 1.0;
326  case DistanceKilometers:
327  return 1.0 / KILOMETERS_TO_METER;
328  case DistanceMillimeters:
329  return 1.0 / MILLIMETERS_TO_METER;
330  case DistanceCentimeters:
331  return 1.0 / CENTIMETERS_TO_METER;
332  case DistanceFeet:
333  return 1.0 / FEET_TO_METER;
334  case DistanceYards:
335  return 1.0 / YARDS_TO_METER;
336  case DistanceMiles:
337  return 1.0 / MILES_TO_METER;
338  case DistanceDegrees:
339  return 1.0 / DEGREE_TO_METER;
341  return 1.0 / NMILE_TO_METER;
342  case DistanceUnknownUnit:
343  break;
344  }
345 
346  break;
347  }
348  case DistanceKilometers:
349  {
350  switch ( toUnit )
351  {
352  case DistanceMeters:
353  return KILOMETERS_TO_METER;
354  case DistanceKilometers:
355  return 1.0;
356  case DistanceCentimeters:
358  case DistanceMillimeters:
360  case DistanceFeet:
362  case DistanceYards:
364  case DistanceMiles:
366  case DistanceDegrees:
370  case DistanceUnknownUnit:
371  break;
372  }
373 
374  break;
375  }
376  case DistanceFeet:
377  {
378  switch ( toUnit )
379  {
380  case DistanceMeters:
381  return FEET_TO_METER;
382  case DistanceKilometers:
384  case DistanceCentimeters:
386  case DistanceMillimeters:
388  case DistanceFeet:
389  return 1.0;
390  case DistanceYards:
391  return 1.0 / YARDS_TO_FEET;
392  case DistanceMiles:
393  return FEET_TO_METER / MILES_TO_METER;
394  case DistanceDegrees:
397  return FEET_TO_METER / NMILE_TO_METER;
398  case DistanceUnknownUnit:
399  break;
400  }
401 
402  break;
403  }
404  case DistanceYards:
405  {
406  switch ( toUnit )
407  {
408  case DistanceMeters:
409  return YARDS_TO_METER;
410  case DistanceKilometers:
412  case DistanceCentimeters:
414  case DistanceMillimeters:
416  case DistanceFeet:
417  return YARDS_TO_FEET;
418  case DistanceYards:
419  return 1.0;
420  case DistanceMiles:
422  case DistanceDegrees:
426  case DistanceUnknownUnit:
427  break;
428  }
429 
430  break;
431  }
432  case DistanceMiles:
433  {
434  switch ( toUnit )
435  {
436  case DistanceMeters:
437  return MILES_TO_METER;
438  case DistanceKilometers:
440  case DistanceCentimeters:
442  case DistanceMillimeters:
444  case DistanceFeet:
445  return MILES_TO_METER / FEET_TO_METER;
446  case DistanceYards:
448  case DistanceMiles:
449  return 1.0;
450  case DistanceDegrees:
454  case DistanceUnknownUnit:
455  break;
456  }
457 
458  break;
459  }
460  case DistanceDegrees:
461  {
462  switch ( toUnit )
463  {
464  case DistanceMeters:
465  return DEGREE_TO_METER;
466  case DistanceKilometers:
468  case DistanceCentimeters:
470  case DistanceMillimeters:
472  case DistanceFeet:
474  case DistanceYards:
476  case DistanceMiles:
478  case DistanceDegrees:
479  return 1.0;
482  case DistanceUnknownUnit:
483  break;
484  }
485 
486  break;
487  }
489  {
490  switch ( toUnit )
491  {
492  case DistanceMeters:
493  return NMILE_TO_METER;
494  case DistanceKilometers:
496  case DistanceCentimeters:
498  case DistanceMillimeters:
500  case DistanceFeet:
501  return NMILE_TO_METER / FEET_TO_METER;
502  case DistanceYards:
504  case DistanceMiles:
506  case DistanceDegrees:
509  return 1.0;
510  case DistanceUnknownUnit:
511  break;
512  }
513 
514  break;
515  }
516  case DistanceCentimeters:
517  {
518  switch ( toUnit )
519  {
520  case DistanceMeters:
521  return CENTIMETERS_TO_METER;
522  case DistanceKilometers:
524  case DistanceCentimeters:
525  return 1.0;
526  case DistanceMillimeters:
528  case DistanceFeet:
530  case DistanceYards:
532  case DistanceMiles:
534  case DistanceDegrees:
538  case DistanceUnknownUnit:
539  break;
540  }
541 
542  break;
543  }
544  case DistanceMillimeters:
545  {
546  switch ( toUnit )
547  {
548  case DistanceMeters:
549  return MILLIMETERS_TO_METER;
550  case DistanceKilometers:
552  case DistanceCentimeters:
554  case DistanceMillimeters:
555  return 1.0;
556  case DistanceFeet:
558  case DistanceYards:
560  case DistanceMiles:
562  case DistanceDegrees:
566  case DistanceUnknownUnit:
567  break;
568  }
569 
570  break;
571  }
572  case DistanceUnknownUnit:
573  break;
574  }
575  return 1.0;
576 }
577 
579 {
580  switch ( unit )
581  {
582  case AreaSquareMeters:
583  return QStringLiteral( "m2" );
585  return QStringLiteral( "km2" );
586  case AreaSquareFeet:
587  return QStringLiteral( "ft2" );
588  case AreaSquareYards:
589  return QStringLiteral( "y2" );
590  case AreaSquareMiles:
591  return QStringLiteral( "mi2" );
592  case AreaHectares:
593  return QStringLiteral( "ha" );
594  case AreaAcres:
595  return QStringLiteral( "ac" );
597  return QStringLiteral( "nm2" );
598  case AreaSquareDegrees:
599  return QStringLiteral( "deg2" );
601  return QStringLiteral( "cm2" );
603  return QStringLiteral( "mm2" );
604  case AreaUnknownUnit:
605  return QStringLiteral( "<unknown>" );
606  }
607  return QString();
608 }
609 
610 QgsUnitTypes::AreaUnit QgsUnitTypes::decodeAreaUnit( const QString &string, bool *ok )
611 {
612  QString normalized = string.trimmed().toLower();
613 
614  if ( ok )
615  *ok = true;
616 
617  if ( normalized == encodeUnit( AreaSquareMeters ) )
618  return AreaSquareMeters;
619  if ( normalized == encodeUnit( AreaSquareKilometers ) )
620  return AreaSquareKilometers;
621  if ( normalized == encodeUnit( AreaSquareFeet ) )
622  return AreaSquareFeet;
623  if ( normalized == encodeUnit( AreaSquareYards ) )
624  return AreaSquareYards;
625  if ( normalized == encodeUnit( AreaSquareMiles ) )
626  return AreaSquareMiles;
627  if ( normalized == encodeUnit( AreaHectares ) )
628  return AreaHectares;
629  if ( normalized == encodeUnit( AreaAcres ) )
630  return AreaAcres;
631  if ( normalized == encodeUnit( AreaSquareNauticalMiles ) )
633  if ( normalized == encodeUnit( AreaSquareDegrees ) )
634  return AreaSquareDegrees;
635  if ( normalized == encodeUnit( AreaSquareCentimeters ) )
636  return AreaSquareCentimeters;
637  if ( normalized == encodeUnit( AreaSquareMillimeters ) )
638  return AreaSquareMillimeters;
639  if ( normalized == encodeUnit( AreaUnknownUnit ) )
640  return AreaUnknownUnit;
641 
642  if ( ok )
643  *ok = false;
644 
645  return AreaUnknownUnit;
646 }
647 
649 {
650  switch ( unit )
651  {
652  case AreaSquareMeters:
653  return QObject::tr( "square meters", "area" );
655  return QObject::tr( "square kilometers", "area" );
656  case AreaSquareFeet:
657  return QObject::tr( "square feet", "area" );
658  case AreaSquareYards:
659  return QObject::tr( "square yards", "area" );
660  case AreaSquareMiles:
661  return QObject::tr( "square miles", "area" );
662  case AreaHectares:
663  return QObject::tr( "hectares", "area" );
664  case AreaAcres:
665  return QObject::tr( "acres", "area" );
667  return QObject::tr( "square nautical miles", "area" );
668  case AreaSquareDegrees:
669  return QObject::tr( "square degrees", "area" );
671  return QObject::tr( "square millimeters", "area" );
673  return QObject::tr( "square centimeters", "area" );
674  case AreaUnknownUnit:
675  return QObject::tr( "<unknown>", "area" );
676  }
677  return QString();
678 }
679 
681 {
682  switch ( unit )
683  {
684  case AreaSquareMeters:
685  return QObject::tr( "m²", "area" );
687  return QObject::tr( "km²", "area" );
688  case AreaSquareFeet:
689  return QObject::tr( "ft²", "area" );
690  case AreaSquareYards:
691  return QObject::tr( "yd²", "area" );
692  case AreaSquareMiles:
693  return QObject::tr( "mi²", "area" );
694  case AreaHectares:
695  return QObject::tr( "ha", "area" );
696  case AreaAcres:
697  return QObject::tr( "ac", "area" );
699  return QObject::tr( "NM²", "area" );
700  case AreaSquareDegrees:
701  return QObject::tr( "deg²", "area" );
703  return QObject::tr( "cm²", "area" );
705  return QObject::tr( "mm²", "area" );
706  case AreaUnknownUnit:
707  return QString();
708  }
709  return QString();
710 }
711 
712 QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString &string, bool *ok )
713 {
714  QString normalized = string.trimmed().toLower();
715 
716  if ( ok )
717  *ok = true;
718 
719  if ( normalized == toString( AreaSquareMeters ) )
720  return AreaSquareMeters;
721  if ( normalized == toString( AreaSquareKilometers ) )
722  return AreaSquareKilometers;
723  if ( normalized == toString( AreaSquareFeet ) )
724  return AreaSquareFeet;
725  if ( normalized == toString( AreaSquareYards ) )
726  return AreaSquareYards;
727  if ( normalized == toString( AreaSquareMiles ) )
728  return AreaSquareMiles;
729  if ( normalized == toString( AreaHectares ) )
730  return AreaHectares;
731  if ( normalized == toString( AreaAcres ) )
732  return AreaAcres;
733  if ( normalized == toString( AreaSquareNauticalMiles ) )
735  if ( normalized == toString( AreaSquareDegrees ) )
736  return AreaSquareDegrees;
737  if ( normalized == toString( AreaSquareMillimeters ) )
738  return AreaSquareMillimeters;
739  if ( normalized == toString( AreaSquareCentimeters ) )
740  return AreaSquareCentimeters;
741  if ( normalized == toString( AreaUnknownUnit ) )
742  return AreaUnknownUnit;
743  if ( ok )
744  *ok = false;
745 
746  return AreaUnknownUnit;
747 }
748 
750 {
751 #define KM2_TO_M2 1000000.0
752 #define CM2_TO_M2 0.0001
753 #define MM2_TO_M2 0.000001
754 #define FT2_TO_M2 0.09290304
755 #define YD2_TO_M2 0.83612736
756 #define MI2_TO_M2 2589988.110336
757 #define HA_TO_M2 10000.0
758 #define AC_TO_FT2 43560.0
759 #define DEG2_TO_M2 12392029030.5
760 #define NM2_TO_M2 3429904.0
761 
762  // Calculate the conversion factor between the specified units
763  switch ( fromUnit )
764  {
765  case AreaSquareMeters:
766  {
767  switch ( toUnit )
768  {
769  case AreaSquareMeters:
770  return 1.0;
772  return 1.0 / KM2_TO_M2;
773  case AreaSquareFeet:
774  return 1.0 / FT2_TO_M2;
775  case AreaSquareYards:
776  return 1.0 / YD2_TO_M2;
777  case AreaSquareMiles:
778  return 1.0 / MI2_TO_M2;
779  case AreaHectares:
780  return 1.0 / HA_TO_M2;
781  case AreaAcres:
782  return 1.0 / AC_TO_FT2 / FT2_TO_M2;
784  return 1.0 / NM2_TO_M2;
785  case AreaSquareDegrees:
786  return 1.0 / DEG2_TO_M2;
788  return 1.0 / CM2_TO_M2;
790  return 1.0 / MM2_TO_M2;
791  case AreaUnknownUnit:
792  break;
793  }
794 
795  break;
796  }
798  {
799  switch ( toUnit )
800  {
801  case AreaSquareMeters:
802  return KM2_TO_M2;
804  return 1.0;
805  case AreaSquareFeet:
806  return KM2_TO_M2 / FT2_TO_M2;
807  case AreaSquareYards:
808  return KM2_TO_M2 / YD2_TO_M2;
809  case AreaSquareMiles:
810  return KM2_TO_M2 / MI2_TO_M2;
811  case AreaHectares:
812  return KM2_TO_M2 / HA_TO_M2;
813  case AreaAcres:
814  return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
816  return KM2_TO_M2 / NM2_TO_M2;
817  case AreaSquareDegrees:
818  return KM2_TO_M2 / DEG2_TO_M2;
820  return KM2_TO_M2 / CM2_TO_M2;
822  return KM2_TO_M2 / MM2_TO_M2;
823  case AreaUnknownUnit:
824  break;
825  }
826 
827  break;
828  }
829  case AreaSquareFeet:
830  {
831  switch ( toUnit )
832  {
833  case AreaSquareMeters:
834  return FT2_TO_M2;
836  return FT2_TO_M2 / KM2_TO_M2;
837  case AreaSquareFeet:
838  return 1.0;
839  case AreaSquareYards:
840  return FT2_TO_M2 / YD2_TO_M2;
841  case AreaSquareMiles:
842  return FT2_TO_M2 / MI2_TO_M2;
843  case AreaHectares:
844  return FT2_TO_M2 / HA_TO_M2;
845  case AreaAcres:
846  return 1.0 / AC_TO_FT2;
848  return FT2_TO_M2 / NM2_TO_M2;
849  case AreaSquareDegrees:
850  return FT2_TO_M2 / DEG2_TO_M2;
852  return FT2_TO_M2 / CM2_TO_M2;
854  return FT2_TO_M2 / MM2_TO_M2;
855  case AreaUnknownUnit:
856  break;
857  }
858 
859  break;
860  }
861 
862  case AreaSquareYards:
863  {
864  switch ( toUnit )
865  {
866  case AreaSquareMeters:
867  return YD2_TO_M2;
869  return YD2_TO_M2 / KM2_TO_M2;
870  case AreaSquareFeet:
871  return YD2_TO_M2 / FT2_TO_M2;
872  case AreaSquareYards:
873  return 1.0;
874  case AreaSquareMiles:
875  return YD2_TO_M2 / MI2_TO_M2;
876  case AreaHectares:
877  return YD2_TO_M2 / HA_TO_M2;
878  case AreaAcres:
879  return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
881  return YD2_TO_M2 / NM2_TO_M2;
882  case AreaSquareDegrees:
883  return YD2_TO_M2 / DEG2_TO_M2;
885  return YD2_TO_M2 / CM2_TO_M2;
887  return YD2_TO_M2 / MM2_TO_M2;
888  case AreaUnknownUnit:
889  break;
890  }
891  break;
892  }
893 
894  case AreaSquareMiles:
895  {
896  switch ( toUnit )
897  {
898  case AreaSquareMeters:
899  return MI2_TO_M2;
901  return MI2_TO_M2 / KM2_TO_M2;
902  case AreaSquareFeet:
903  return MI2_TO_M2 / FT2_TO_M2;
904  case AreaSquareYards:
905  return MI2_TO_M2 / YD2_TO_M2;
906  case AreaSquareMiles:
907  return 1.0;
908  case AreaHectares:
909  return MI2_TO_M2 / HA_TO_M2;
910  case AreaAcres:
911  return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
913  return MI2_TO_M2 / NM2_TO_M2;
914  case AreaSquareDegrees:
915  return MI2_TO_M2 / DEG2_TO_M2;
917  return MI2_TO_M2 / CM2_TO_M2;
919  return MI2_TO_M2 / MM2_TO_M2;
920  case AreaUnknownUnit:
921  break;
922  }
923 
924  break;
925  }
926 
927  case AreaHectares:
928  {
929  switch ( toUnit )
930  {
931  case AreaSquareMeters:
932  return HA_TO_M2;
934  return HA_TO_M2 / KM2_TO_M2;
935  case AreaSquareFeet:
936  return HA_TO_M2 / FT2_TO_M2;
937  case AreaSquareYards:
938  return HA_TO_M2 / YD2_TO_M2;
939  case AreaSquareMiles:
940  return HA_TO_M2 / MI2_TO_M2;
941  case AreaHectares:
942  return 1.0;
943  case AreaAcres:
944  return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
946  return HA_TO_M2 / NM2_TO_M2;
947  case AreaSquareDegrees:
948  return HA_TO_M2 / DEG2_TO_M2;
950  return HA_TO_M2 / CM2_TO_M2;
952  return HA_TO_M2 / MM2_TO_M2;
953  case AreaUnknownUnit:
954  break;
955  }
956 
957  break;
958  }
959 
960  case AreaAcres:
961  {
962  switch ( toUnit )
963  {
964  case AreaSquareMeters:
965  return AC_TO_FT2 * FT2_TO_M2;
967  return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
968  case AreaSquareFeet:
969  return AC_TO_FT2;
970  case AreaSquareYards:
971  return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
972  case AreaSquareMiles:
973  return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
974  case AreaHectares:
975  return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
976  case AreaAcres:
977  return 1.0;
979  return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
980  case AreaSquareDegrees:
981  return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
983  return AC_TO_FT2 * FT2_TO_M2 / CM2_TO_M2;
985  return AC_TO_FT2 * FT2_TO_M2 / MM2_TO_M2;
986  case AreaUnknownUnit:
987  break;
988  }
989 
990  break;
991  }
992 
994  {
995  switch ( toUnit )
996  {
997  case AreaSquareMeters:
998  return NM2_TO_M2;
1000  return NM2_TO_M2 / KM2_TO_M2;
1001  case AreaSquareFeet:
1002  return NM2_TO_M2 / FT2_TO_M2;
1003  case AreaSquareYards:
1004  return NM2_TO_M2 / YD2_TO_M2;
1005  case AreaSquareMiles:
1006  return NM2_TO_M2 / MI2_TO_M2;
1007  case AreaHectares:
1008  return NM2_TO_M2 / HA_TO_M2;
1009  case AreaAcres:
1010  return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1012  return 1.0;
1013  case AreaSquareDegrees:
1014  return NM2_TO_M2 / DEG2_TO_M2;
1015  case AreaSquareCentimeters:
1016  return NM2_TO_M2 / CM2_TO_M2;
1017  case AreaSquareMillimeters:
1018  return NM2_TO_M2 / MM2_TO_M2;
1019  case AreaUnknownUnit:
1020  break;
1021  }
1022 
1023  break;
1024  }
1025 
1026  case AreaSquareDegrees:
1027  {
1028  switch ( toUnit )
1029  {
1030  case AreaSquareMeters:
1031  return DEG2_TO_M2;
1032  case AreaSquareKilometers:
1033  return DEG2_TO_M2 / KM2_TO_M2;
1034  case AreaSquareFeet:
1035  return DEG2_TO_M2 / FT2_TO_M2;
1036  case AreaSquareYards:
1037  return DEG2_TO_M2 / YD2_TO_M2;
1038  case AreaSquareMiles:
1039  return DEG2_TO_M2 / MI2_TO_M2;
1040  case AreaHectares:
1041  return DEG2_TO_M2 / HA_TO_M2;
1042  case AreaAcres:
1043  return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1045  return DEG2_TO_M2 / NM2_TO_M2;
1046  case AreaSquareDegrees:
1047  return 1.0;
1048  case AreaSquareCentimeters:
1049  return DEG2_TO_M2 / CM2_TO_M2;
1050  case AreaSquareMillimeters:
1051  return DEG2_TO_M2 / MM2_TO_M2;
1052  case AreaUnknownUnit:
1053  break;
1054  }
1055 
1056  break;
1057  }
1058 
1059  case AreaSquareMillimeters:
1060  {
1061  switch ( toUnit )
1062  {
1063  case AreaSquareMeters:
1064  return MM2_TO_M2;
1065  case AreaSquareKilometers:
1066  return MM2_TO_M2 / KM2_TO_M2;
1067  case AreaSquareFeet:
1068  return MM2_TO_M2 / FT2_TO_M2;
1069  case AreaSquareYards:
1070  return MM2_TO_M2 / YD2_TO_M2;
1071  case AreaSquareMiles:
1072  return MM2_TO_M2 / MI2_TO_M2;
1073  case AreaHectares:
1074  return MM2_TO_M2 / HA_TO_M2;
1075  case AreaAcres:
1076  return MM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1078  return MM2_TO_M2 / NM2_TO_M2;
1079  case AreaSquareDegrees:
1080  return MM2_TO_M2 / DEG2_TO_M2;
1081  case AreaSquareCentimeters:
1082  return MM2_TO_M2 / CM2_TO_M2;
1083  case AreaSquareMillimeters:
1084  return 1.0;
1085  case AreaUnknownUnit:
1086  break;
1087  }
1088 
1089  break;
1090  }
1091  case AreaSquareCentimeters:
1092  {
1093  switch ( toUnit )
1094  {
1095  case AreaSquareMeters:
1096  return CM2_TO_M2;
1097  case AreaSquareKilometers:
1098  return CM2_TO_M2 / KM2_TO_M2;
1099  case AreaSquareFeet:
1100  return CM2_TO_M2 / FT2_TO_M2;
1101  case AreaSquareYards:
1102  return CM2_TO_M2 / YD2_TO_M2;
1103  case AreaSquareMiles:
1104  return CM2_TO_M2 / MI2_TO_M2;
1105  case AreaHectares:
1106  return CM2_TO_M2 / HA_TO_M2;
1107  case AreaAcres:
1108  return CM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1110  return CM2_TO_M2 / NM2_TO_M2;
1111  case AreaSquareDegrees:
1112  return CM2_TO_M2 / DEG2_TO_M2;
1113  case AreaSquareCentimeters:
1114  return 1.0;
1115  case AreaSquareMillimeters:
1116  return CM2_TO_M2 / MM2_TO_M2;
1117  case AreaUnknownUnit:
1118  break;
1119  }
1120 
1121  break;
1122  }
1123  case AreaUnknownUnit:
1124  break;
1125  }
1126  return 1.0;
1127 }
1128 
1130 {
1131  switch ( distanceUnit )
1132  {
1133  case DistanceMeters:
1134  return AreaSquareMeters;
1135 
1136  case DistanceKilometers:
1137  return AreaSquareKilometers;
1138 
1139  case DistanceCentimeters:
1140  return AreaSquareCentimeters;
1141 
1142  case DistanceMillimeters:
1143  return AreaSquareMillimeters;
1144 
1145  case DistanceFeet:
1146  return AreaSquareFeet;
1147 
1148  case DistanceYards:
1149  return AreaSquareYards;
1150 
1151  case DistanceMiles:
1152  return AreaSquareMiles;
1153 
1154  case DistanceDegrees:
1155  return AreaSquareDegrees;
1156 
1157  case DistanceUnknownUnit:
1158  return AreaUnknownUnit;
1159 
1160  case DistanceNauticalMiles:
1161  return AreaSquareNauticalMiles;
1162  }
1163 
1164  return AreaUnknownUnit;
1165 }
1166 
1168 {
1169  switch ( areaUnit )
1170  {
1171  case AreaSquareMeters:
1172  case AreaHectares:
1173  return DistanceMeters;
1174 
1175  case AreaSquareKilometers:
1176  return DistanceKilometers;
1177 
1178  case AreaSquareCentimeters:
1179  return DistanceCentimeters;
1180 
1181  case AreaSquareMillimeters:
1182  return DistanceMillimeters;
1183 
1184  case AreaSquareFeet:
1185  return DistanceFeet;
1186 
1187  case AreaSquareYards:
1188  case AreaAcres:
1189  return DistanceYards;
1190 
1191  case AreaSquareMiles:
1192  return DistanceMiles;
1193 
1194  case AreaSquareDegrees:
1195  return DistanceDegrees;
1196 
1197  case AreaUnknownUnit:
1198  return DistanceUnknownUnit;
1199 
1201  return DistanceNauticalMiles;
1202  }
1203 
1204  return DistanceUnknownUnit;
1205 }
1206 
1208 {
1209  switch ( unit )
1210  {
1211  case AngleDegrees:
1212  return QStringLiteral( "degrees" );
1213  case AngleRadians:
1214  return QStringLiteral( "radians" );
1215  case AngleGon:
1216  return QStringLiteral( "gon" );
1217  case AngleMinutesOfArc:
1218  return QStringLiteral( "moa" );
1219  case AngleSecondsOfArc:
1220  return QStringLiteral( "soa" );
1221  case AngleTurn:
1222  return QStringLiteral( "tr" );
1223  case AngleMilliradiansSI:
1224  return QStringLiteral( "milliradians" );
1225  case AngleMilNATO:
1226  return QStringLiteral( "mil" );
1227  case AngleUnknownUnit:
1228  return QStringLiteral( "<unknown>" );
1229  }
1230  return QString();
1231 }
1232 
1233 QgsUnitTypes::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
1234 {
1235  QString normalized = string.trimmed().toLower();
1236 
1237  if ( ok )
1238  *ok = true;
1239 
1240  if ( normalized == encodeUnit( AngleDegrees ) )
1241  return AngleDegrees;
1242  if ( normalized == encodeUnit( AngleRadians ) )
1243  return AngleRadians;
1244  if ( normalized == encodeUnit( AngleGon ) )
1245  return AngleGon;
1246  if ( normalized == encodeUnit( AngleMinutesOfArc ) )
1247  return AngleMinutesOfArc;
1248  if ( normalized == encodeUnit( AngleSecondsOfArc ) )
1249  return AngleSecondsOfArc;
1250  if ( normalized == encodeUnit( AngleTurn ) )
1251  return AngleTurn;
1252  if ( normalized == encodeUnit( AngleMilliradiansSI ) )
1253  return AngleMilliradiansSI;
1254  if ( normalized == encodeUnit( AngleMilNATO ) )
1255  return AngleMilNATO;
1256  if ( normalized == encodeUnit( AngleUnknownUnit ) )
1257  return AngleUnknownUnit;
1258  if ( ok )
1259  *ok = false;
1260 
1261  return AngleUnknownUnit;
1262 }
1263 
1265 {
1266  switch ( unit )
1267  {
1268  case AngleDegrees:
1269  return QObject::tr( "degrees", "angle" );
1270  case AngleRadians:
1271  return QObject::tr( "radians", "angle" );
1272  case AngleGon:
1273  return QObject::tr( "gon", "angle" );
1274  case AngleMinutesOfArc:
1275  return QObject::tr( "minutes of arc", "angle" );
1276  case AngleSecondsOfArc:
1277  return QObject::tr( "seconds of arc", "angle" );
1278  case AngleTurn:
1279  return QObject::tr( "turns", "angle" );
1280  case AngleMilliradiansSI:
1281  return QObject::tr( "milliradians", "angle" );
1282  case AngleMilNATO:
1283  return QObject::tr( "mil", "angle" );
1284  case AngleUnknownUnit:
1285  return QObject::tr( "<unknown>", "angle" );
1286  }
1287  return QString();
1288 }
1289 
1291 {
1292  // Calculate the conversion factor between the specified units
1293  switch ( fromUnit )
1294  {
1295  case AngleDegrees:
1296  {
1297  switch ( toUnit )
1298  {
1299  case AngleDegrees:
1300  return 1.0;
1301  case AngleRadians:
1302  return M_PI / 180.0;
1303  case AngleGon:
1304  return 400.0 / 360.0;
1305  case AngleMinutesOfArc:
1306  return 60;
1307  case AngleSecondsOfArc:
1308  return 3600;
1309  case AngleTurn:
1310  return 1.0 / 360.0;
1311  case AngleMilliradiansSI:
1312  return M_PI / 180.0 * 1000;
1313  case AngleMilNATO:
1314  return 3200.0 / 180;
1315  case AngleUnknownUnit:
1316  break;
1317  }
1318  break;
1319  }
1320  case AngleRadians:
1321  {
1322  switch ( toUnit )
1323  {
1324  case AngleDegrees:
1325  return 180.0 / M_PI;
1326  case AngleRadians:
1327  return 1.0;
1328  case AngleGon:
1329  return 200.0 / M_PI;
1330  case AngleMinutesOfArc:
1331  return 60 * 180.0 / M_PI;
1332  case AngleSecondsOfArc:
1333  return 3600 * 180.0 / M_PI;
1334  case AngleTurn:
1335  return 0.5 / M_PI;
1336  case AngleMilliradiansSI:
1337  return 1000;
1338  case AngleMilNATO:
1339  return 3200.0 / M_PI;
1340  case AngleUnknownUnit:
1341  break;
1342  }
1343  break;
1344  }
1345  case AngleGon:
1346  {
1347  switch ( toUnit )
1348  {
1349  case AngleDegrees:
1350  return 360.0 / 400.0;
1351  case AngleRadians:
1352  return M_PI / 200.0;
1353  case AngleGon:
1354  return 1.0;
1355  case AngleMinutesOfArc:
1356  return 60 * 360.0 / 400.0;
1357  case AngleSecondsOfArc:
1358  return 3600 * 360.0 / 400.0;
1359  case AngleTurn:
1360  return 1.0 / 400.0;
1361  case AngleMilliradiansSI:
1362  return M_PI / 200.0 * 1000;
1363  case AngleMilNATO:
1364  return 3200.0 / 200.0;
1365  case AngleUnknownUnit:
1366  break;
1367  }
1368  break;
1369  }
1370  case AngleMinutesOfArc:
1371  {
1372  switch ( toUnit )
1373  {
1374  case AngleDegrees:
1375  return 1 / 60.0;
1376  case AngleRadians:
1377  return M_PI / 180.0 / 60.0;
1378  case AngleGon:
1379  return 400.0 / 360.0 / 60.0;
1380  case AngleMinutesOfArc:
1381  return 1.0;
1382  case AngleSecondsOfArc:
1383  return 60.0;
1384  case AngleTurn:
1385  return 1.0 / 360.0 / 60.0;
1386  case AngleMilliradiansSI:
1387  return M_PI / 180.0 / 60.0 * 1000;
1388  case AngleMilNATO:
1389  return 3200.0 / 180.0 / 60.0;
1390  case AngleUnknownUnit:
1391  break;
1392  }
1393  break;
1394  }
1395  case AngleSecondsOfArc:
1396  {
1397  switch ( toUnit )
1398  {
1399  case AngleDegrees:
1400  return 1 / 3600.0;
1401  case AngleRadians:
1402  return M_PI / 180.0 / 3600.0;
1403  case AngleGon:
1404  return 400.0 / 360.0 / 3600.0;
1405  case AngleMinutesOfArc:
1406  return 1.0 / 60.0;
1407  case AngleSecondsOfArc:
1408  return 1.0;
1409  case AngleTurn:
1410  return 1.0 / 360.0 / 3600.0;
1411  case AngleMilliradiansSI:
1412  return M_PI / 180.0 / 3600.0 * 1000;
1413  case AngleMilNATO:
1414  return 3200.0 / 180.0 / 3600.0;
1415  case AngleUnknownUnit:
1416  break;
1417  }
1418  break;
1419  }
1420  case AngleTurn:
1421  {
1422  switch ( toUnit )
1423  {
1424  case AngleDegrees:
1425  return 360.0;
1426  case AngleRadians:
1427  return 2 * M_PI;
1428  case AngleGon:
1429  return 400.0;
1430  case AngleMinutesOfArc:
1431  return 360.0 * 60.0;
1432  case AngleSecondsOfArc:
1433  return 360.0 * 3600.0;
1434  case AngleTurn:
1435  return 1.0;
1436  case AngleMilliradiansSI:
1437  return 2 * M_PI * 1000;
1438  case AngleMilNATO:
1439  return 2 * 3200;
1440  case AngleUnknownUnit:
1441  break;
1442  }
1443  break;
1444  }
1445  case AngleMilliradiansSI:
1446  {
1447  switch ( toUnit )
1448  {
1449  case AngleDegrees:
1450  return 180.0 / M_PI / 1000;
1451  case AngleRadians:
1452  return 0.001;
1453  case AngleGon:
1454  return 200.0 / M_PI / 1000;
1455  case AngleMinutesOfArc:
1456  return 180.0 * 60.0 / M_PI / 1000;
1457  case AngleSecondsOfArc:
1458  return 180.0 * 3600.0 / M_PI / 1000;
1459  case AngleTurn:
1460  return M_PI / 2 / 1000;
1461  case AngleMilliradiansSI:
1462  return 1.0;
1463  case AngleMilNATO:
1464  return 3200.0 / 1000.0 / M_PI;
1465  case AngleUnknownUnit:
1466  break;
1467  }
1468  break;
1469  }
1470 
1471  case AngleMilNATO:
1472  {
1473  switch ( toUnit )
1474  {
1475  case AngleDegrees:
1476  return 180.0 / 3200;
1477  case AngleRadians:
1478  return M_PI / 3200;
1479  case AngleGon:
1480  return 200.0 / 3200;
1481  case AngleMinutesOfArc:
1482  return 60 * 180.0 / 3200;
1483  case AngleSecondsOfArc:
1484  return 3600.0 * 180 / 3200;
1485  case AngleTurn:
1486  return 1.0 / ( 2 * 32000 );
1487  case AngleMilliradiansSI:
1488  return 1000.0 * M_PI / 3200.0;
1489  case AngleMilNATO:
1490  return 1.0;
1491  case AngleUnknownUnit:
1492  break;
1493  }
1494  break;
1495  }
1496 
1497  case AngleUnknownUnit:
1498  break;
1499  }
1500  return 1.0;
1501 }
1502 
1503 QString QgsUnitTypes::formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit )
1504 {
1505  QString unitLabel;
1506 
1507  switch ( unit )
1508  {
1509  case AngleDegrees:
1510  unitLabel = QObject::tr( "°", "angle" );
1511  break;
1512  case AngleRadians:
1513  unitLabel = QObject::tr( " rad", "angle" );
1514  break;
1515  case AngleGon:
1516  unitLabel = QObject::tr( " gon", "angle" );
1517  break;
1518  case AngleMinutesOfArc:
1519  unitLabel = QObject::tr( "′", "angle minutes" );
1520  break;
1521  case AngleSecondsOfArc:
1522  unitLabel = QObject::tr( "″", "angle seconds" );
1523  break;
1524  case AngleTurn:
1525  unitLabel = QObject::tr( " tr", "angle turn" );
1526  break;
1527  case AngleMilliradiansSI:
1528  unitLabel = QObject::tr( " millirad", "angular mil SI" );
1529  break;
1530  case AngleMilNATO:
1531  unitLabel = QObject::tr( " mil", "angular mil NATO" );
1532  break;
1533  case AngleUnknownUnit:
1534  break;
1535  }
1536 
1537  return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
1538 }
1539 
1540 
1541 QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit )
1542 {
1543  DistanceValue result;
1544 
1545  switch ( unit )
1546  {
1547  case DistanceMeters:
1548  if ( keepBaseUnit )
1549  {
1550  result.value = qgsRound( distance, decimals );
1552  }
1553  else if ( std::fabs( distance ) > 1000.0 )
1554  {
1555  result.value = qgsRound( distance / 1000, decimals );
1557  }
1558  else if ( std::fabs( distance ) < 0.01 )
1559  {
1560  result.value = qgsRound( distance * 1000, decimals );
1562  }
1563  else if ( std::fabs( distance ) < 0.1 )
1564  {
1565 
1566  result.value = qgsRound( distance * 100, decimals );
1568  }
1569  else
1570  {
1571  result.value = qgsRound( distance, decimals );
1573  }
1574  break;
1575 
1576  case DistanceKilometers:
1577  if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
1578  {
1579  result.value = qgsRound( distance, decimals );
1581  }
1582  else
1583  {
1584  result.value = qgsRound( distance * 1000, decimals );
1586  }
1587  break;
1588 
1589  case DistanceFeet:
1590  if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
1591  {
1592  result.value = qgsRound( distance, decimals );
1594  }
1595  else
1596  {
1597  result.value = qgsRound( distance / 5280.0, decimals );
1599  }
1600  break;
1601 
1602  case DistanceYards:
1603  if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
1604  {
1605  result.value = qgsRound( distance, decimals );
1607  }
1608  else
1609  {
1610  result.value = qgsRound( distance / 1760.0, decimals );
1612  }
1613  break;
1614 
1615  case DistanceMiles:
1616  if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
1617  {
1618  result.value = qgsRound( distance, decimals );
1620  }
1621  else
1622  {
1623  result.value = qgsRound( distance * 5280.0, decimals );
1625  }
1626  break;
1627 
1628  case DistanceNauticalMiles:
1629  result.value = qgsRound( distance, decimals );
1631  break;
1632 
1633  case DistanceDegrees:
1634  result.value = qgsRound( distance, decimals );
1636  break;
1637 
1638  case DistanceUnknownUnit:
1639  result.value = qgsRound( distance, decimals );
1641  break;
1642 
1643  default:
1644  result.value = qgsRound( distance, decimals );
1645  result.unit = unit;
1646  break;
1647  }
1648 
1649  return result;
1650 }
1651 
1652 QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit )
1653 {
1654  AreaValue result;
1655  result.value = -1.0;
1656  result.unit = AreaUnknownUnit;
1657 
1658  // If we are not forced to keep the base units, switch to meter calculation
1659  if ( unit == AreaSquareMillimeters )
1660  {
1661  if ( keepBaseUnit )
1662  {
1663  result.value = qgsRound( area, decimals );
1665  }
1666  else
1667  {
1668  area /= 1000000.0;
1670  }
1671  }
1672  else if ( unit == AreaSquareCentimeters )
1673  {
1674  if ( keepBaseUnit )
1675  {
1676  result.value = qgsRound( area, decimals );
1678  }
1679  else
1680  {
1681  area /= 10000.0;
1683  }
1684  }
1685 
1686  switch ( unit )
1687  {
1688  case AreaSquareCentimeters:
1689  // handled in the if above
1690  break;
1691  case AreaSquareMillimeters:
1692  // handled in the if above
1693  break;
1694  case AreaSquareMeters:
1695  {
1696  if ( keepBaseUnit )
1697  {
1698  result.value = qgsRound( area, decimals );
1700  }
1702  {
1705  }
1707  {
1710  }
1711  else
1712  {
1713  result.value = qgsRound( area, decimals );
1715  }
1716  break;
1717  }
1718 
1719  case AreaSquareKilometers:
1720  {
1721  result.value = qgsRound( area, decimals );
1723  break;
1724  }
1725 
1726  case AreaSquareFeet:
1727  {
1728  if ( keepBaseUnit )
1729  {
1730  result.value = qgsRound( area, decimals );
1732  }
1734  {
1737  }
1738  else
1739  {
1740  result.value = qgsRound( area, decimals );
1742  }
1743  break;
1744  }
1745 
1746  case AreaSquareYards:
1747  {
1748  if ( keepBaseUnit )
1749  {
1750  result.value = qgsRound( area, decimals );
1752  }
1754  {
1757  }
1758  else
1759  {
1760  result.value = qgsRound( area, decimals );
1762  }
1763  break;
1764  }
1765 
1766  case AreaSquareMiles:
1767  {
1768  result.value = qgsRound( area, decimals );
1770  break;
1771  }
1772 
1773  case AreaHectares:
1774  {
1775  if ( keepBaseUnit )
1776  {
1777  result.value = qgsRound( area, decimals );
1779  }
1781  {
1784  }
1785  else
1786  {
1787  result.value = qgsRound( area, decimals );
1789  }
1790  break;
1791  }
1792 
1793  case AreaAcres:
1794  {
1795  if ( keepBaseUnit )
1796  {
1797  result.value = qgsRound( area, decimals );
1798  result.unit = QgsUnitTypes::AreaAcres;
1799  }
1801  {
1804  }
1805  else
1806  {
1807  result.value = qgsRound( area, decimals );
1808  result.unit = QgsUnitTypes::AreaAcres;
1809  }
1810  break;
1811  }
1812 
1814  {
1815  result.value = qgsRound( area, decimals );
1817  break;
1818  }
1819 
1820  case AreaSquareDegrees:
1821  {
1822  result.value = qgsRound( area, decimals );
1824  break;
1825  }
1826 
1827  case AreaUnknownUnit:
1828  {
1829  result.value = qgsRound( area, decimals );
1831  break;
1832  }
1833  }
1834  return result;
1835 }
1836 
1837 
1838 QString QgsUnitTypes::formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit )
1839 {
1840  DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
1841 
1842  QString unitText;
1843 
1844  if ( dist.unit != DistanceUnknownUnit )
1845  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
1846 
1847  return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
1848 }
1849 
1850 QString QgsUnitTypes::formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit )
1851 {
1852  AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
1853 
1854  QString unitText;
1855 
1856  if ( areaValue.unit != AreaUnknownUnit )
1857  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
1858 
1859  return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
1860 }
1861 
1863 {
1864  switch ( unit )
1865  {
1866  case RenderMillimeters:
1867  return QStringLiteral( "MM" );
1869  return QStringLiteral( "RenderMetersInMapUnits" );
1870  case RenderMapUnits:
1871  return QStringLiteral( "MapUnit" );
1872  case RenderPixels:
1873  return QStringLiteral( "Pixel" );
1874  case RenderPercentage:
1875  return QStringLiteral( "Percentage" );
1876  case RenderPoints:
1877  return QStringLiteral( "Point" );
1878  case RenderInches:
1879  return QStringLiteral( "Inch" );
1880  case RenderUnknownUnit:
1881  return QString();
1882  }
1883  return QString();
1884 }
1885 
1887 {
1888  QString normalized = string.trimmed().toLower();
1889 
1890  if ( ok )
1891  *ok = true;
1892 
1893  if ( normalized == encodeUnit( RenderMillimeters ).toLower() )
1894  return RenderMillimeters;
1895  if ( normalized == encodeUnit( RenderMetersInMapUnits ).toLower() )
1896  return RenderMetersInMapUnits;
1897  if ( normalized == QLatin1String( "meters" ) )
1898  return RenderMetersInMapUnits;
1899  if ( normalized == encodeUnit( RenderMapUnits ).toLower() )
1900  return RenderMapUnits;
1901  if ( normalized == QLatin1String( "mapunits" ) )
1902  return RenderMapUnits;
1903  if ( normalized == encodeUnit( RenderPixels ).toLower() )
1904  return RenderPixels;
1905  if ( normalized == encodeUnit( RenderPercentage ).toLower() )
1906  return RenderPercentage;
1907  if ( normalized == QLatin1String( "percent" ) )
1908  return RenderPercentage;
1909  if ( normalized == encodeUnit( RenderPoints ).toLower() )
1910  return RenderPoints;
1911  if ( normalized == QLatin1String( "points" ) )
1912  return RenderPoints;
1913  if ( normalized == encodeUnit( RenderInches ).toLower() )
1914  return RenderInches;
1915 
1916  if ( ok )
1917  *ok = false;
1918 
1919  // millimeters are default
1920  return RenderMillimeters;
1921 }
1922 
1924 {
1925  switch ( unit )
1926  {
1927  case RenderMillimeters:
1928  return QObject::tr( "millimeters", "render" );
1929 
1931  return QObject::tr( "meters at scale", "render" );
1932 
1933  case RenderMapUnits:
1934  return QObject::tr( "map units", "render" );
1935 
1936  case RenderPixels:
1937  return QObject::tr( "pixels", "render" );
1938 
1939  case RenderPercentage:
1940  return QObject::tr( "percent", "render" );
1941 
1942  case RenderPoints:
1943  return QObject::tr( "points", "render" );
1944 
1945  case RenderInches:
1946  return QObject::tr( "inches", "render" );
1947 
1948  case RenderUnknownUnit:
1949  return QObject::tr( "<unknown>", "render" );
1950 
1951  }
1952  return QString();
1953 }
1954 
1955 
1956 
1958 {
1959  switch ( unit )
1960  {
1961  case LayoutCentimeters:
1962  return QStringLiteral( "cm" );
1963  case LayoutMeters:
1964  return QStringLiteral( "m" );
1965  case LayoutInches:
1966  return QStringLiteral( "in" );
1967  case LayoutFeet:
1968  return QStringLiteral( "ft" );
1969  case LayoutPoints:
1970  return QStringLiteral( "pt" );
1971  case LayoutPicas:
1972  return QStringLiteral( "pi" );
1973  case LayoutPixels:
1974  return QStringLiteral( "px" );
1975  case LayoutMillimeters:
1976  return QStringLiteral( "mm" );
1977  }
1978  return QString();
1979 }
1980 
1982 {
1983  QString normalized = string.trimmed().toLower();
1984 
1985  if ( ok )
1986  *ok = true;
1987 
1988  if ( normalized == encodeUnit( LayoutMillimeters ).toLower() )
1989  return LayoutMillimeters;
1990  if ( normalized == encodeUnit( LayoutCentimeters ).toLower() )
1991  return LayoutCentimeters;
1992  if ( normalized == encodeUnit( LayoutMeters ).toLower() )
1993  return LayoutMeters;
1994  if ( normalized == encodeUnit( LayoutInches ).toLower() )
1995  return LayoutInches;
1996  if ( normalized == encodeUnit( LayoutFeet ).toLower() )
1997  return LayoutFeet;
1998  if ( normalized == encodeUnit( LayoutPoints ).toLower() )
1999  return LayoutPoints;
2000  if ( normalized == encodeUnit( LayoutPicas ).toLower() )
2001  return LayoutPicas;
2002  if ( normalized == encodeUnit( LayoutPixels ).toLower() )
2003  return LayoutPixels;
2004 
2005  if ( ok )
2006  *ok = false;
2007 
2008  // millimeters are default
2009  return LayoutMillimeters;
2010 }
2011 
2013 {
2014  switch ( units )
2015  {
2016  case LayoutPixels:
2017  return LayoutScreenUnits;
2018  case LayoutMillimeters:
2019  case LayoutCentimeters:
2020  case LayoutMeters:
2021  case LayoutInches:
2022  case LayoutFeet:
2023  case LayoutPoints:
2024  case LayoutPicas:
2025  return LayoutPaperUnits;
2026  }
2027 
2028  // avoid warnings
2029  return LayoutPaperUnits;
2030 }
2031 
2033 {
2034  switch ( unit )
2035  {
2036  case LayoutPixels:
2037  return QObject::tr( "px" );
2038  case LayoutMillimeters:
2039  return QObject::tr( "mm" );
2040  case LayoutCentimeters:
2041  return QObject::tr( "cm" );
2042  case LayoutMeters:
2043  return QObject::tr( "m" );
2044  case LayoutInches:
2045  return QObject::tr( "in", "unit inch" );
2046  case LayoutFeet:
2047  return QObject::tr( "ft" );
2048  case LayoutPoints:
2049  return QObject::tr( "pt" );
2050  case LayoutPicas:
2051  return QObject::tr( "pica" );
2052  }
2053  return QString(); // no warnings
2054 }
2055 
2057 {
2058  switch ( unit )
2059  {
2060  case LayoutPixels:
2061  return QObject::tr( "pixels" );
2062  case LayoutMillimeters:
2063  return QObject::tr( "millimeters" );
2064  case LayoutCentimeters:
2065  return QObject::tr( "centimeters" );
2066  case LayoutMeters:
2067  return QObject::tr( "meters" );
2068  case LayoutInches:
2069  return QObject::tr( "inches" );
2070  case LayoutFeet:
2071  return QObject::tr( "feet" );
2072  case LayoutPoints:
2073  return QObject::tr( "points" );
2074  case LayoutPicas:
2075  return QObject::tr( "picas" );
2076  }
2077  return QString(); // no warnings
2078 }
#define HA_TO_M2
Meters value as Map units.
Definition: qgsunittypes.h:122
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:177
#define DEGREE_TO_METER
QgsUnitTypes::DistanceUnit unit
The value part of the distance.
Definition: qgsunittypes.h:163
double value
The value part of the distance.
Definition: qgsunittypes.h:158
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:141
DistanceUnitType
Types of distance units.
Definition: qgsunittypes.h:72
Unknown areal unit.
Definition: qgsunittypes.h:93
#define NMILE_TO_METER
Mixed or unknown units.
Definition: qgsunittypes.h:121
Square kilometers.
Definition: qgsunittypes.h:101
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:118
Typographic points.
Definition: qgsunittypes.h:134
#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:143
#define KILOMETERS_TO_METER
#define FEET_TO_METER
AngleUnit
Units of angles.
Definition: qgsunittypes.h:98
#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.
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
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:62
#define CENTIMETERS_TO_METER
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:90
A combination of distance value and unit.
Definition: qgsunittypes.h:152
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
static Q_INVOKABLE DistanceUnitType unitType(QgsUnitTypes::DistanceUnit unit)
Returns the type for a distance unit.
Unknown distance unit.
Definition: qgsunittypes.h:65
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:182
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:108
Unknown unit type.
Definition: qgsunittypes.h:76
Unit is a geographic (e.g., degree based) unit.
Definition: qgsunittypes.h:75
Points (e.g., for font sizes)
Definition: qgsunittypes.h:119
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:313
#define MM2_TO_M2
Turn/revolutions.
Definition: qgsunittypes.h:105
#define MILLIMETERS_TO_METER
A combination of area value and unit.
Definition: qgsunittypes.h:171
#define MILES_TO_METER
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:127
Angular milliradians (SI definition, 1/1000 of radian)
Definition: qgsunittypes.h:106
Angular mil (NATO definition, 6400 mil = 2PI radians)
Definition: qgsunittypes.h:107
Unit is a screen based measurement unit.
Definition: qgsunittypes.h:144
Terrestrial miles.
Definition: qgsunittypes.h:61
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
#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:80
Unit is a standard measurement unit.
Definition: qgsunittypes.h:74
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:113
Typographic picas.
Definition: qgsunittypes.h:135
#define YD2_TO_M2
#define CM2_TO_M2