QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 AreaSquareCentimeters;
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 ( unit )
1170  {
1171  case AngleDegrees:
1172  return QStringLiteral( "degrees" );
1173  case AngleRadians:
1174  return QStringLiteral( "radians" );
1175  case AngleGon:
1176  return QStringLiteral( "gon" );
1177  case AngleMinutesOfArc:
1178  return QStringLiteral( "moa" );
1179  case AngleSecondsOfArc:
1180  return QStringLiteral( "soa" );
1181  case AngleTurn:
1182  return QStringLiteral( "tr" );
1183  case AngleUnknownUnit:
1184  return QStringLiteral( "<unknown>" );
1185  }
1186  return QString();
1187 }
1188 
1189 QgsUnitTypes::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
1190 {
1191  QString normalized = string.trimmed().toLower();
1192 
1193  if ( ok )
1194  *ok = true;
1195 
1196  if ( normalized == encodeUnit( AngleDegrees ) )
1197  return AngleDegrees;
1198  if ( normalized == encodeUnit( AngleRadians ) )
1199  return AngleRadians;
1200  if ( normalized == encodeUnit( AngleGon ) )
1201  return AngleGon;
1202  if ( normalized == encodeUnit( AngleMinutesOfArc ) )
1203  return AngleMinutesOfArc;
1204  if ( normalized == encodeUnit( AngleSecondsOfArc ) )
1205  return AngleSecondsOfArc;
1206  if ( normalized == encodeUnit( AngleTurn ) )
1207  return AngleTurn;
1208  if ( normalized == encodeUnit( AngleUnknownUnit ) )
1209  return AngleUnknownUnit;
1210  if ( ok )
1211  *ok = false;
1212 
1213  return AngleUnknownUnit;
1214 }
1215 
1217 {
1218  switch ( unit )
1219  {
1220  case AngleDegrees:
1221  return QObject::tr( "degrees", "angle" );
1222  case AngleRadians:
1223  return QObject::tr( "radians", "angle" );
1224  case AngleGon:
1225  return QObject::tr( "gon", "angle" );
1226  case AngleMinutesOfArc:
1227  return QObject::tr( "minutes of arc", "angle" );
1228  case AngleSecondsOfArc:
1229  return QObject::tr( "seconds of arc", "angle" );
1230  case AngleTurn:
1231  return QObject::tr( "turns", "angle" );
1232  case AngleUnknownUnit:
1233  return QObject::tr( "<unknown>", "angle" );
1234  }
1235  return QString();
1236 }
1237 
1239 {
1240  // Calculate the conversion factor between the specified units
1241  switch ( fromUnit )
1242  {
1243  case AngleDegrees:
1244  {
1245  switch ( toUnit )
1246  {
1247  case AngleDegrees:
1248  return 1.0;
1249  case AngleRadians:
1250  return M_PI / 180.0;
1251  case AngleGon:
1252  return 400.0 / 360.0;
1253  case AngleMinutesOfArc:
1254  return 60;
1255  case AngleSecondsOfArc:
1256  return 3600;
1257  case AngleTurn:
1258  return 1.0 / 360.0;
1259  case AngleUnknownUnit:
1260  break;
1261  }
1262  break;
1263  }
1264  case AngleRadians:
1265  {
1266  switch ( toUnit )
1267  {
1268  case AngleDegrees:
1269  return 180.0 / M_PI;
1270  case AngleRadians:
1271  return 1.0;
1272  case AngleGon:
1273  return 200.0 / M_PI;
1274  case AngleMinutesOfArc:
1275  return 60 * 180.0 / M_PI;
1276  case AngleSecondsOfArc:
1277  return 3600 * 180.0 / M_PI;
1278  case AngleTurn:
1279  return 0.5 / M_PI;
1280  case AngleUnknownUnit:
1281  break;
1282  }
1283  break;
1284  }
1285  case AngleGon:
1286  {
1287  switch ( toUnit )
1288  {
1289  case AngleDegrees:
1290  return 360.0 / 400.0;
1291  case AngleRadians:
1292  return M_PI / 200.0;
1293  case AngleGon:
1294  return 1.0;
1295  case AngleMinutesOfArc:
1296  return 60 * 360.0 / 400.0;
1297  case AngleSecondsOfArc:
1298  return 3600 * 360.0 / 400.0;
1299  case AngleTurn:
1300  return 1.0 / 400.0;
1301  case AngleUnknownUnit:
1302  break;
1303  }
1304  break;
1305  }
1306  case AngleMinutesOfArc:
1307  {
1308  switch ( toUnit )
1309  {
1310  case AngleDegrees:
1311  return 1 / 60.0;
1312  case AngleRadians:
1313  return M_PI / 180.0 / 60.0;
1314  case AngleGon:
1315  return 400.0 / 360.0 / 60.0;
1316  case AngleMinutesOfArc:
1317  return 1.0;
1318  case AngleSecondsOfArc:
1319  return 60.0;
1320  case AngleTurn:
1321  return 1.0 / 360.0 / 60.0;
1322  case AngleUnknownUnit:
1323  break;
1324  }
1325  break;
1326  }
1327  case AngleSecondsOfArc:
1328  {
1329  switch ( toUnit )
1330  {
1331  case AngleDegrees:
1332  return 1 / 3600.0;
1333  case AngleRadians:
1334  return M_PI / 180.0 / 3600.0;
1335  case AngleGon:
1336  return 400.0 / 360.0 / 3600.0;
1337  case AngleMinutesOfArc:
1338  return 1.0 / 60.0;
1339  case AngleSecondsOfArc:
1340  return 1.0;
1341  case AngleTurn:
1342  return 1.0 / 360.0 / 3600.0;
1343  case AngleUnknownUnit:
1344  break;
1345  }
1346  break;
1347  }
1348  case AngleTurn:
1349  {
1350  switch ( toUnit )
1351  {
1352  case AngleDegrees:
1353  return 360.0;
1354  case AngleRadians:
1355  return 2 * M_PI;
1356  case AngleGon:
1357  return 400.0;
1358  case AngleMinutesOfArc:
1359  return 360.0 * 60.0;
1360  case AngleSecondsOfArc:
1361  return 360.0 * 3600.0;
1362  case AngleTurn:
1363  return 1.0;
1364  case AngleUnknownUnit:
1365  break;
1366  }
1367  break;
1368  }
1369  case AngleUnknownUnit:
1370  break;
1371  }
1372  return 1.0;
1373 }
1374 
1375 QString QgsUnitTypes::formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit )
1376 {
1377  QString unitLabel;
1378 
1379  switch ( unit )
1380  {
1381  case AngleDegrees:
1382  unitLabel = QObject::tr( "°", "angle" );
1383  break;
1384  case AngleRadians:
1385  unitLabel = QObject::tr( " rad", "angle" );
1386  break;
1387  case AngleGon:
1388  unitLabel = QObject::tr( " gon", "angle" );
1389  break;
1390  case AngleMinutesOfArc:
1391  unitLabel = QObject::tr( "′", "angle minutes" );
1392  break;
1393  case AngleSecondsOfArc:
1394  unitLabel = QObject::tr( "″", "angle seconds" );
1395  break;
1396  case AngleTurn:
1397  unitLabel = QObject::tr( " tr", "angle turn" );
1398  break;
1399  case AngleUnknownUnit:
1400  break;
1401  }
1402 
1403  return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
1404 }
1405 
1406 
1407 QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit )
1408 {
1409  DistanceValue result;
1410 
1411  switch ( unit )
1412  {
1413  case DistanceMeters:
1414  if ( keepBaseUnit )
1415  {
1416  result.value = qgsRound( distance, decimals );
1418  }
1419  else if ( std::fabs( distance ) > 1000.0 )
1420  {
1421  result.value = qgsRound( distance / 1000, decimals );
1423  }
1424  else if ( std::fabs( distance ) < 0.01 )
1425  {
1426  result.value = qgsRound( distance * 1000, decimals );
1428  }
1429  else if ( std::fabs( distance ) < 0.1 )
1430  {
1431 
1432  result.value = qgsRound( distance * 100, decimals );
1434  }
1435  else
1436  {
1437  result.value = qgsRound( distance, decimals );
1439  }
1440  break;
1441 
1442  case DistanceKilometers:
1443  if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
1444  {
1445  result.value = qgsRound( distance, decimals );
1447  }
1448  else
1449  {
1450  result.value = qgsRound( distance * 1000, decimals );
1452  }
1453  break;
1454 
1455  case DistanceFeet:
1456  if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
1457  {
1458  result.value = qgsRound( distance, decimals );
1460  }
1461  else
1462  {
1463  result.value = qgsRound( distance / 5280.0, decimals );
1465  }
1466  break;
1467 
1468  case DistanceYards:
1469  if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
1470  {
1471  result.value = qgsRound( distance, decimals );
1473  }
1474  else
1475  {
1476  result.value = qgsRound( distance / 1760.0, decimals );
1478  }
1479  break;
1480 
1481  case DistanceMiles:
1482  if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
1483  {
1484  result.value = qgsRound( distance, decimals );
1486  }
1487  else
1488  {
1489  result.value = qgsRound( distance * 5280.0, decimals );
1491  }
1492  break;
1493 
1494  case DistanceNauticalMiles:
1495  result.value = qgsRound( distance, decimals );
1497  break;
1498 
1499  case DistanceDegrees:
1500  result.value = qgsRound( distance, decimals );
1502  break;
1503 
1504  case DistanceUnknownUnit:
1505  result.value = qgsRound( distance, decimals );
1507  break;
1508 
1509  default:
1510  result.value = qgsRound( distance, decimals );
1511  result.unit = unit;
1512  break;
1513  }
1514 
1515  return result;
1516 }
1517 
1518 QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit )
1519 {
1520  AreaValue result;
1521  result.value = -1.0;
1522  result.unit = AreaUnknownUnit;
1523 
1524  // If we are not forced to keep the base units, switch to meter calculation
1525  if ( unit == AreaSquareMillimeters )
1526  {
1527  if ( keepBaseUnit )
1528  {
1529  result.value = qgsRound( area, decimals );
1531  }
1532  else
1533  {
1534  area /= 1000000.0;
1536  }
1537  }
1538  else if ( unit == AreaSquareCentimeters )
1539  {
1540  if ( keepBaseUnit )
1541  {
1542  result.value = qgsRound( area, decimals );
1544  }
1545  else
1546  {
1547  area /= 10000.0;
1549  }
1550  }
1551 
1552  switch ( unit )
1553  {
1554  case AreaSquareCentimeters:
1555  // handled in the if above
1556  break;
1557  case AreaSquareMillimeters:
1558  // handled in the if above
1559  break;
1560  case AreaSquareMeters:
1561  {
1562  if ( keepBaseUnit )
1563  {
1564  result.value = qgsRound( area, decimals );
1566  }
1568  {
1571  }
1573  {
1576  }
1577  else
1578  {
1579  result.value = qgsRound( area, decimals );
1581  }
1582  break;
1583  }
1584 
1585  case AreaSquareKilometers:
1586  {
1587  result.value = qgsRound( area, decimals );
1589  break;
1590  }
1591 
1592  case AreaSquareFeet:
1593  {
1594  if ( keepBaseUnit )
1595  {
1596  result.value = qgsRound( area, decimals );
1598  }
1600  {
1603  }
1604  else
1605  {
1606  result.value = qgsRound( area, decimals );
1608  }
1609  break;
1610  }
1611 
1612  case AreaSquareYards:
1613  {
1614  if ( keepBaseUnit )
1615  {
1616  result.value = qgsRound( area, decimals );
1618  }
1620  {
1623  }
1624  else
1625  {
1626  result.value = qgsRound( area, decimals );
1628  }
1629  break;
1630  }
1631 
1632  case AreaSquareMiles:
1633  {
1634  result.value = qgsRound( area, decimals );
1636  break;
1637  }
1638 
1639  case AreaHectares:
1640  {
1641  if ( keepBaseUnit )
1642  {
1643  result.value = qgsRound( area, decimals );
1645  }
1647  {
1650  }
1651  else
1652  {
1653  result.value = qgsRound( area, decimals );
1655  }
1656  break;
1657  }
1658 
1659  case AreaAcres:
1660  {
1661  if ( keepBaseUnit )
1662  {
1663  result.value = qgsRound( area, decimals );
1664  result.unit = QgsUnitTypes::AreaAcres;
1665  }
1667  {
1670  }
1671  else
1672  {
1673  result.value = qgsRound( area, decimals );
1674  result.unit = QgsUnitTypes::AreaAcres;
1675  }
1676  break;
1677  }
1678 
1680  {
1681  result.value = qgsRound( area, decimals );
1683  break;
1684  }
1685 
1686  case AreaSquareDegrees:
1687  {
1688  result.value = qgsRound( area, decimals );
1690  break;
1691  }
1692 
1693  case AreaUnknownUnit:
1694  {
1695  result.value = qgsRound( area, decimals );
1697  break;
1698  }
1699  }
1700  return result;
1701 }
1702 
1703 
1704 QString QgsUnitTypes::formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit )
1705 {
1706  DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
1707 
1708  QString unitText;
1709 
1710  if ( dist.unit != DistanceUnknownUnit )
1711  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
1712 
1713  return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
1714 }
1715 
1716 QString QgsUnitTypes::formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit )
1717 {
1718  AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
1719 
1720  QString unitText;
1721 
1722  if ( areaValue.unit != AreaUnknownUnit )
1723  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
1724 
1725  return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
1726 }
1727 
1729 {
1730  switch ( unit )
1731  {
1732  case RenderMillimeters:
1733  return QStringLiteral( "MM" );
1735  return QStringLiteral( "RenderMetersInMapUnits" );
1736  case RenderMapUnits:
1737  return QStringLiteral( "MapUnit" );
1738  case RenderPixels:
1739  return QStringLiteral( "Pixel" );
1740  case RenderPercentage:
1741  return QStringLiteral( "Percentage" );
1742  case RenderPoints:
1743  return QStringLiteral( "Point" );
1744  case RenderInches:
1745  return QStringLiteral( "Inch" );
1746  case RenderUnknownUnit:
1747  return QString();
1748  }
1749  return QString();
1750 }
1751 
1753 {
1754  QString normalized = string.trimmed().toLower();
1755 
1756  if ( ok )
1757  *ok = true;
1758 
1759  if ( normalized == encodeUnit( RenderMillimeters ).toLower() )
1760  return RenderMillimeters;
1761  if ( normalized == encodeUnit( RenderMetersInMapUnits ).toLower() )
1762  return RenderMetersInMapUnits;
1763  if ( normalized == QLatin1String( "meters" ) )
1764  return RenderMetersInMapUnits;
1765  if ( normalized == encodeUnit( RenderMapUnits ).toLower() )
1766  return RenderMapUnits;
1767  if ( normalized == QLatin1String( "mapunits" ) )
1768  return RenderMapUnits;
1769  if ( normalized == encodeUnit( RenderPixels ).toLower() )
1770  return RenderPixels;
1771  if ( normalized == encodeUnit( RenderPercentage ).toLower() )
1772  return RenderPercentage;
1773  if ( normalized == QLatin1String( "percent" ) )
1774  return RenderPercentage;
1775  if ( normalized == encodeUnit( RenderPoints ).toLower() )
1776  return RenderPoints;
1777  if ( normalized == QLatin1String( "points" ) )
1778  return RenderPoints;
1779  if ( normalized == encodeUnit( RenderInches ).toLower() )
1780  return RenderInches;
1781 
1782  if ( ok )
1783  *ok = false;
1784 
1785  // millimeters are default
1786  return RenderMillimeters;
1787 }
1788 
1790 {
1791  switch ( unit )
1792  {
1793  case RenderMillimeters:
1794  return QObject::tr( "millimeters", "render" );
1795 
1797  return QObject::tr( "meters at scale", "render" );
1798 
1799  case RenderMapUnits:
1800  return QObject::tr( "map units", "render" );
1801 
1802  case RenderPixels:
1803  return QObject::tr( "pixels", "render" );
1804 
1805  case RenderPercentage:
1806  return QObject::tr( "percent", "render" );
1807 
1808  case RenderPoints:
1809  return QObject::tr( "points", "render" );
1810 
1811  case RenderInches:
1812  return QObject::tr( "inches", "render" );
1813 
1814  case RenderUnknownUnit:
1815  return QObject::tr( "<unknown>", "render" );
1816 
1817  }
1818  return QString();
1819 }
1820 
1821 
1822 
1824 {
1825  switch ( unit )
1826  {
1827  case LayoutCentimeters:
1828  return QStringLiteral( "cm" );
1829  case LayoutMeters:
1830  return QStringLiteral( "m" );
1831  case LayoutInches:
1832  return QStringLiteral( "in" );
1833  case LayoutFeet:
1834  return QStringLiteral( "ft" );
1835  case LayoutPoints:
1836  return QStringLiteral( "pt" );
1837  case LayoutPicas:
1838  return QStringLiteral( "pi" );
1839  case LayoutPixels:
1840  return QStringLiteral( "px" );
1841  case LayoutMillimeters:
1842  return QStringLiteral( "mm" );
1843  }
1844  return QString();
1845 }
1846 
1848 {
1849  QString normalized = string.trimmed().toLower();
1850 
1851  if ( ok )
1852  *ok = true;
1853 
1854  if ( normalized == encodeUnit( LayoutMillimeters ).toLower() )
1855  return LayoutMillimeters;
1856  if ( normalized == encodeUnit( LayoutCentimeters ).toLower() )
1857  return LayoutCentimeters;
1858  if ( normalized == encodeUnit( LayoutMeters ).toLower() )
1859  return LayoutMeters;
1860  if ( normalized == encodeUnit( LayoutInches ).toLower() )
1861  return LayoutInches;
1862  if ( normalized == encodeUnit( LayoutFeet ).toLower() )
1863  return LayoutFeet;
1864  if ( normalized == encodeUnit( LayoutPoints ).toLower() )
1865  return LayoutPoints;
1866  if ( normalized == encodeUnit( LayoutPicas ).toLower() )
1867  return LayoutPicas;
1868  if ( normalized == encodeUnit( LayoutPixels ).toLower() )
1869  return LayoutPixels;
1870 
1871  if ( ok )
1872  *ok = false;
1873 
1874  // millimeters are default
1875  return LayoutMillimeters;
1876 }
1877 
1879 {
1880  switch ( units )
1881  {
1882  case LayoutPixels:
1883  return LayoutScreenUnits;
1884  case LayoutMillimeters:
1885  case LayoutCentimeters:
1886  case LayoutMeters:
1887  case LayoutInches:
1888  case LayoutFeet:
1889  case LayoutPoints:
1890  case LayoutPicas:
1891  return LayoutPaperUnits;
1892  }
1893 
1894  // avoid warnings
1895  return LayoutPaperUnits;
1896 }
1897 
1899 {
1900  switch ( unit )
1901  {
1902  case LayoutPixels:
1903  return QObject::tr( "px" );
1904  case LayoutMillimeters:
1905  return QObject::tr( "mm" );
1906  case LayoutCentimeters:
1907  return QObject::tr( "cm" );
1908  case LayoutMeters:
1909  return QObject::tr( "m" );
1910  case LayoutInches:
1911  return QObject::tr( "in", "unit inch" );
1912  case LayoutFeet:
1913  return QObject::tr( "ft" );
1914  case LayoutPoints:
1915  return QObject::tr( "pt" );
1916  case LayoutPicas:
1917  return QObject::tr( "pica" );
1918  }
1919  return QString(); // no warnings
1920 }
1921 
1923 {
1924  switch ( unit )
1925  {
1926  case LayoutPixels:
1927  return QObject::tr( "pixels" );
1928  case LayoutMillimeters:
1929  return QObject::tr( "millimeters" );
1930  case LayoutCentimeters:
1931  return QObject::tr( "centimeters" );
1932  case LayoutMeters:
1933  return QObject::tr( "meters" );
1934  case LayoutInches:
1935  return QObject::tr( "inches" );
1936  case LayoutFeet:
1937  return QObject::tr( "feet" );
1938  case LayoutPoints:
1939  return QObject::tr( "points" );
1940  case LayoutPicas:
1941  return QObject::tr( "picas" );
1942  }
1943  return QString(); // no warnings
1944 }
#define HA_TO_M2
Meters value as Map units.
Definition: qgsunittypes.h:120
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:175
#define DEGREE_TO_METER
QgsUnitTypes::DistanceUnit unit
The value part of the distance.
Definition: qgsunittypes.h:161
double value
The value part of the distance.
Definition: qgsunittypes.h:156
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:139
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:119
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:116
Typographic points.
Definition: qgsunittypes.h:132
#define KM2_TO_M2
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:141
#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:150
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:180
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:106
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:117
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:304
#define MM2_TO_M2
Turn/revolutions.
Definition: qgsunittypes.h:105
#define MILLIMETERS_TO_METER
A combination of area value and unit.
Definition: qgsunittypes.h:169
#define MILES_TO_METER
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
Unit is a screen based measurement unit.
Definition: qgsunittypes.h:142
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:111
Typographic picas.
Definition: qgsunittypes.h:133
#define YD2_TO_M2
#define CM2_TO_M2