QGIS API Documentation  3.2.0-Bonn (bc43194)
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 DistanceMeters:
194  return QObject::tr( "m", "distance" );
195 
196  case DistanceKilometers:
197  return QObject::tr( "km", "distance" );
198 
199  case DistanceFeet:
200  return QObject::tr( "ft", "distance" );
201 
202  case DistanceYards:
203  return QObject::tr( "yd", "distance" );
204 
205  case DistanceMiles:
206  return QObject::tr( "mi", "distance" );
207 
208  case DistanceDegrees:
209  return QObject::tr( "deg", "distance" );
210 
211  case DistanceCentimeters:
212  return QObject::tr( "cm", "distance" );
213 
214  case DistanceMillimeters:
215  return QObject::tr( "mm", "distance" );
216 
217  case DistanceUnknownUnit:
218  return QString();
219 
221  return QObject::tr( "NM", "distance" );
222  }
223  return QString();
224 }
225 
226 /***************************************************************************
227  * This class is considered CRITICAL and any change MUST be accompanied with
228  * full unit tests in test_qgsunittypes.py.
229  * See details in QEP #17
230  ****************************************************************************/
231 
233 {
234  QString normalized = string.trimmed().toLower();
235 
236  if ( ok )
237  *ok = true;
238 
239  if ( normalized == toString( DistanceMeters ) )
240  return DistanceMeters;
241  if ( normalized == toString( DistanceKilometers ) )
242  return DistanceKilometers;
243  if ( normalized == toString( DistanceFeet ) )
244  return DistanceFeet;
245  if ( normalized == toString( DistanceYards ) )
246  return DistanceYards;
247  if ( normalized == toString( DistanceMiles ) )
248  return DistanceMiles;
249  if ( normalized == toString( DistanceDegrees ) )
250  return DistanceDegrees;
251  if ( normalized == toString( DistanceCentimeters ) )
252  return DistanceCentimeters;
253  if ( normalized == toString( DistanceMillimeters ) )
254  return DistanceMillimeters;
255  if ( normalized == toString( DistanceNauticalMiles ) )
256  return DistanceNauticalMiles;
257  if ( normalized == toString( DistanceUnknownUnit ) )
258  return DistanceUnknownUnit;
259 
260  if ( ok )
261  *ok = false;
262 
263  return DistanceUnknownUnit;
264 }
265 
266 /***************************************************************************
267  * This class is considered CRITICAL and any change MUST be accompanied with
268  * full unit tests in test_qgsunittypes.py.
269  * See details in QEP #17
270  ****************************************************************************/
271 
273 {
274 #define DEGREE_TO_METER 111319.49079327358
275 #define FEET_TO_METER 0.3048
276 #define NMILE_TO_METER 1852.0
277 #define KILOMETERS_TO_METER 1000.0
278 #define CENTIMETERS_TO_METER 0.01
279 #define MILLIMETERS_TO_METER 0.001
280 #define YARDS_TO_METER 0.9144
281 #define YARDS_TO_FEET 3.0
282 #define MILES_TO_METER 1609.344
283 
284  // Calculate the conversion factor between the specified units
285  switch ( fromUnit )
286  {
287  case DistanceMeters:
288  {
289  switch ( toUnit )
290  {
291  case DistanceMeters:
292  return 1.0;
293  case DistanceKilometers:
294  return 1.0 / KILOMETERS_TO_METER;
295  case DistanceMillimeters:
296  return 1.0 / MILLIMETERS_TO_METER;
297  case DistanceCentimeters:
298  return 1.0 / CENTIMETERS_TO_METER;
299  case DistanceFeet:
300  return 1.0 / FEET_TO_METER;
301  case DistanceYards:
302  return 1.0 / YARDS_TO_METER;
303  case DistanceMiles:
304  return 1.0 / MILES_TO_METER;
305  case DistanceDegrees:
306  return 1.0 / DEGREE_TO_METER;
308  return 1.0 / NMILE_TO_METER;
309  case DistanceUnknownUnit:
310  break;
311  }
312 
313  break;
314  }
315  case DistanceKilometers:
316  {
317  switch ( toUnit )
318  {
319  case DistanceMeters:
320  return KILOMETERS_TO_METER;
321  case DistanceKilometers:
322  return 1.0;
323  case DistanceCentimeters:
325  case DistanceMillimeters:
327  case DistanceFeet:
329  case DistanceYards:
331  case DistanceMiles:
333  case DistanceDegrees:
337  case DistanceUnknownUnit:
338  break;
339  }
340 
341  break;
342  }
343  case DistanceFeet:
344  {
345  switch ( toUnit )
346  {
347  case DistanceMeters:
348  return FEET_TO_METER;
349  case DistanceKilometers:
351  case DistanceCentimeters:
353  case DistanceMillimeters:
355  case DistanceFeet:
356  return 1.0;
357  case DistanceYards:
358  return 1.0 / YARDS_TO_FEET;
359  case DistanceMiles:
360  return FEET_TO_METER / MILES_TO_METER;
361  case DistanceDegrees:
364  return FEET_TO_METER / NMILE_TO_METER;
365  case DistanceUnknownUnit:
366  break;
367  }
368 
369  break;
370  }
371  case DistanceYards:
372  {
373  switch ( toUnit )
374  {
375  case DistanceMeters:
376  return YARDS_TO_METER;
377  case DistanceKilometers:
379  case DistanceCentimeters:
381  case DistanceMillimeters:
383  case DistanceFeet:
384  return YARDS_TO_FEET;
385  case DistanceYards:
386  return 1.0;
387  case DistanceMiles:
389  case DistanceDegrees:
393  case DistanceUnknownUnit:
394  break;
395  }
396 
397  break;
398  }
399  case DistanceMiles:
400  {
401  switch ( toUnit )
402  {
403  case DistanceMeters:
404  return MILES_TO_METER;
405  case DistanceKilometers:
407  case DistanceCentimeters:
409  case DistanceMillimeters:
411  case DistanceFeet:
412  return MILES_TO_METER / FEET_TO_METER;
413  case DistanceYards:
415  case DistanceMiles:
416  return 1.0;
417  case DistanceDegrees:
421  case DistanceUnknownUnit:
422  break;
423  }
424 
425  break;
426  }
427  case DistanceDegrees:
428  {
429  switch ( toUnit )
430  {
431  case DistanceMeters:
432  return DEGREE_TO_METER;
433  case DistanceKilometers:
435  case DistanceCentimeters:
437  case DistanceMillimeters:
439  case DistanceFeet:
441  case DistanceYards:
443  case DistanceMiles:
445  case DistanceDegrees:
446  return 1.0;
449  case DistanceUnknownUnit:
450  break;
451  }
452 
453  break;
454  }
456  {
457  switch ( toUnit )
458  {
459  case DistanceMeters:
460  return NMILE_TO_METER;
461  case DistanceKilometers:
463  case DistanceCentimeters:
465  case DistanceMillimeters:
467  case DistanceFeet:
468  return NMILE_TO_METER / FEET_TO_METER;
469  case DistanceYards:
471  case DistanceMiles:
473  case DistanceDegrees:
476  return 1.0;
477  case DistanceUnknownUnit:
478  break;
479  }
480 
481  break;
482  }
483  case DistanceCentimeters:
484  {
485  switch ( toUnit )
486  {
487  case DistanceMeters:
488  return CENTIMETERS_TO_METER;
489  case DistanceKilometers:
491  case DistanceCentimeters:
492  return 1.0;
493  case DistanceMillimeters:
495  case DistanceFeet:
497  case DistanceYards:
499  case DistanceMiles:
501  case DistanceDegrees:
505  case DistanceUnknownUnit:
506  break;
507  }
508 
509  break;
510  }
511  case DistanceMillimeters:
512  {
513  switch ( toUnit )
514  {
515  case DistanceMeters:
516  return MILLIMETERS_TO_METER;
517  case DistanceKilometers:
519  case DistanceCentimeters:
521  case DistanceMillimeters:
522  return 1.0;
523  case DistanceFeet:
525  case DistanceYards:
527  case DistanceMiles:
529  case DistanceDegrees:
533  case DistanceUnknownUnit:
534  break;
535  }
536 
537  break;
538  }
539  case DistanceUnknownUnit:
540  break;
541  }
542  return 1.0;
543 }
544 
546 {
547  switch ( unit )
548  {
549  case AreaSquareMeters:
550  return QStringLiteral( "m2" );
552  return QStringLiteral( "km2" );
553  case AreaSquareFeet:
554  return QStringLiteral( "ft2" );
555  case AreaSquareYards:
556  return QStringLiteral( "y2" );
557  case AreaSquareMiles:
558  return QStringLiteral( "mi2" );
559  case AreaHectares:
560  return QStringLiteral( "ha" );
561  case AreaAcres:
562  return QStringLiteral( "ac" );
564  return QStringLiteral( "nm2" );
565  case AreaSquareDegrees:
566  return QStringLiteral( "deg2" );
568  return QStringLiteral( "cm2" );
570  return QStringLiteral( "mm2" );
571  case AreaUnknownUnit:
572  return QStringLiteral( "<unknown>" );
573  }
574  return QString();
575 }
576 
577 QgsUnitTypes::AreaUnit QgsUnitTypes::decodeAreaUnit( const QString &string, bool *ok )
578 {
579  QString normalized = string.trimmed().toLower();
580 
581  if ( ok )
582  *ok = true;
583 
584  if ( normalized == encodeUnit( AreaSquareMeters ) )
585  return AreaSquareMeters;
586  if ( normalized == encodeUnit( AreaSquareKilometers ) )
587  return AreaSquareKilometers;
588  if ( normalized == encodeUnit( AreaSquareFeet ) )
589  return AreaSquareFeet;
590  if ( normalized == encodeUnit( AreaSquareYards ) )
591  return AreaSquareYards;
592  if ( normalized == encodeUnit( AreaSquareMiles ) )
593  return AreaSquareMiles;
594  if ( normalized == encodeUnit( AreaHectares ) )
595  return AreaHectares;
596  if ( normalized == encodeUnit( AreaAcres ) )
597  return AreaAcres;
598  if ( normalized == encodeUnit( AreaSquareNauticalMiles ) )
600  if ( normalized == encodeUnit( AreaSquareDegrees ) )
601  return AreaSquareDegrees;
602  if ( normalized == encodeUnit( AreaSquareCentimeters ) )
603  return AreaSquareCentimeters;
604  if ( normalized == encodeUnit( AreaSquareMillimeters ) )
605  return AreaSquareMillimeters;
606  if ( normalized == encodeUnit( AreaUnknownUnit ) )
607  return AreaUnknownUnit;
608 
609  if ( ok )
610  *ok = false;
611 
612  return AreaUnknownUnit;
613 }
614 
616 {
617  switch ( unit )
618  {
619  case AreaSquareMeters:
620  return QObject::tr( "square meters", "area" );
622  return QObject::tr( "square kilometers", "area" );
623  case AreaSquareFeet:
624  return QObject::tr( "square feet", "area" );
625  case AreaSquareYards:
626  return QObject::tr( "square yards", "area" );
627  case AreaSquareMiles:
628  return QObject::tr( "square miles", "area" );
629  case AreaHectares:
630  return QObject::tr( "hectares", "area" );
631  case AreaAcres:
632  return QObject::tr( "acres", "area" );
634  return QObject::tr( "square nautical miles", "area" );
635  case AreaSquareDegrees:
636  return QObject::tr( "square degrees", "area" );
638  return QObject::tr( "square millimeters", "area" );
640  return QObject::tr( "square centimeters", "area" );
641  case AreaUnknownUnit:
642  return QObject::tr( "<unknown>", "area" );
643  }
644  return QString();
645 }
646 
648 {
649  switch ( unit )
650  {
651  case AreaSquareMeters:
652  return QObject::tr( "m²", "area" );
654  return QObject::tr( "km²", "area" );
655  case AreaSquareFeet:
656  return QObject::tr( "ft²", "area" );
657  case AreaSquareYards:
658  return QObject::tr( "yd²", "area" );
659  case AreaSquareMiles:
660  return QObject::tr( "mi²", "area" );
661  case AreaHectares:
662  return QObject::tr( "ha", "area" );
663  case AreaAcres:
664  return QObject::tr( "ac²", "area" );
666  return QObject::tr( "NM²", "area" );
667  case AreaSquareDegrees:
668  return QObject::tr( "deg²", "area" );
670  return QObject::tr( "cm²", "area" );
672  return QObject::tr( "mm²", "area" );
673  case AreaUnknownUnit:
674  return QString();
675  }
676  return QString();
677 }
678 
679 QgsUnitTypes::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString &string, bool *ok )
680 {
681  QString normalized = string.trimmed().toLower();
682 
683  if ( ok )
684  *ok = true;
685 
686  if ( normalized == toString( AreaSquareMeters ) )
687  return AreaSquareMeters;
688  if ( normalized == toString( AreaSquareKilometers ) )
689  return AreaSquareKilometers;
690  if ( normalized == toString( AreaSquareFeet ) )
691  return AreaSquareFeet;
692  if ( normalized == toString( AreaSquareYards ) )
693  return AreaSquareYards;
694  if ( normalized == toString( AreaSquareMiles ) )
695  return AreaSquareMiles;
696  if ( normalized == toString( AreaHectares ) )
697  return AreaHectares;
698  if ( normalized == toString( AreaAcres ) )
699  return AreaAcres;
700  if ( normalized == toString( AreaSquareNauticalMiles ) )
702  if ( normalized == toString( AreaSquareDegrees ) )
703  return AreaSquareDegrees;
704  if ( normalized == toString( AreaSquareMillimeters ) )
705  return AreaSquareMillimeters;
706  if ( normalized == toString( AreaSquareCentimeters ) )
707  return AreaSquareCentimeters;
708  if ( normalized == toString( AreaUnknownUnit ) )
709  return AreaUnknownUnit;
710  if ( ok )
711  *ok = false;
712 
713  return AreaUnknownUnit;
714 }
715 
717 {
718 #define KM2_TO_M2 1000000.0
719 #define CM2_TO_M2 0.0001
720 #define MM2_TO_M2 0.000001
721 #define FT2_TO_M2 0.09290304
722 #define YD2_TO_M2 0.83612736
723 #define MI2_TO_M2 2589988.110336
724 #define HA_TO_M2 10000.0
725 #define AC_TO_FT2 43560.0
726 #define DEG2_TO_M2 12392029030.5
727 #define NM2_TO_M2 3429904.0
728 
729  // Calculate the conversion factor between the specified units
730  switch ( fromUnit )
731  {
732  case AreaSquareMeters:
733  {
734  switch ( toUnit )
735  {
736  case AreaSquareMeters:
737  return 1.0;
739  return 1.0 / KM2_TO_M2;
740  case AreaSquareFeet:
741  return 1.0 / FT2_TO_M2;
742  case AreaSquareYards:
743  return 1.0 / YD2_TO_M2;
744  case AreaSquareMiles:
745  return 1.0 / MI2_TO_M2;
746  case AreaHectares:
747  return 1.0 / HA_TO_M2;
748  case AreaAcres:
749  return 1.0 / AC_TO_FT2 / FT2_TO_M2;
751  return 1.0 / NM2_TO_M2;
752  case AreaSquareDegrees:
753  return 1.0 / DEG2_TO_M2;
755  return 1.0 / CM2_TO_M2;
757  return 1.0 / MM2_TO_M2;
758  case AreaUnknownUnit:
759  break;
760  }
761 
762  break;
763  }
765  {
766  switch ( toUnit )
767  {
768  case AreaSquareMeters:
769  return KM2_TO_M2;
771  return 1.0;
772  case AreaSquareFeet:
773  return KM2_TO_M2 / FT2_TO_M2;
774  case AreaSquareYards:
775  return KM2_TO_M2 / YD2_TO_M2;
776  case AreaSquareMiles:
777  return KM2_TO_M2 / MI2_TO_M2;
778  case AreaHectares:
779  return KM2_TO_M2 / HA_TO_M2;
780  case AreaAcres:
781  return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
783  return KM2_TO_M2 / NM2_TO_M2;
784  case AreaSquareDegrees:
785  return KM2_TO_M2 / DEG2_TO_M2;
787  return KM2_TO_M2 / CM2_TO_M2;
789  return KM2_TO_M2 / MM2_TO_M2;
790  case AreaUnknownUnit:
791  break;
792  }
793 
794  break;
795  }
796  case AreaSquareFeet:
797  {
798  switch ( toUnit )
799  {
800  case AreaSquareMeters:
801  return FT2_TO_M2;
803  return FT2_TO_M2 / KM2_TO_M2;
804  case AreaSquareFeet:
805  return 1.0;
806  case AreaSquareYards:
807  return FT2_TO_M2 / YD2_TO_M2;
808  case AreaSquareMiles:
809  return FT2_TO_M2 / MI2_TO_M2;
810  case AreaHectares:
811  return FT2_TO_M2 / HA_TO_M2;
812  case AreaAcres:
813  return 1.0 / AC_TO_FT2;
815  return FT2_TO_M2 / NM2_TO_M2;
816  case AreaSquareDegrees:
817  return FT2_TO_M2 / DEG2_TO_M2;
819  return FT2_TO_M2 / CM2_TO_M2;
821  return FT2_TO_M2 / MM2_TO_M2;
822  case AreaUnknownUnit:
823  break;
824  }
825 
826  break;
827  }
828 
829  case AreaSquareYards:
830  {
831  switch ( toUnit )
832  {
833  case AreaSquareMeters:
834  return YD2_TO_M2;
836  return YD2_TO_M2 / KM2_TO_M2;
837  case AreaSquareFeet:
838  return YD2_TO_M2 / FT2_TO_M2;
839  case AreaSquareYards:
840  return 1.0;
841  case AreaSquareMiles:
842  return YD2_TO_M2 / MI2_TO_M2;
843  case AreaHectares:
844  return YD2_TO_M2 / HA_TO_M2;
845  case AreaAcres:
846  return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
848  return YD2_TO_M2 / NM2_TO_M2;
849  case AreaSquareDegrees:
850  return YD2_TO_M2 / DEG2_TO_M2;
852  return YD2_TO_M2 / CM2_TO_M2;
854  return YD2_TO_M2 / MM2_TO_M2;
855  case AreaUnknownUnit:
856  break;
857  }
858  break;
859  }
860 
861  case AreaSquareMiles:
862  {
863  switch ( toUnit )
864  {
865  case AreaSquareMeters:
866  return MI2_TO_M2;
868  return MI2_TO_M2 / KM2_TO_M2;
869  case AreaSquareFeet:
870  return MI2_TO_M2 / FT2_TO_M2;
871  case AreaSquareYards:
872  return MI2_TO_M2 / YD2_TO_M2;
873  case AreaSquareMiles:
874  return 1.0;
875  case AreaHectares:
876  return MI2_TO_M2 / HA_TO_M2;
877  case AreaAcres:
878  return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
880  return MI2_TO_M2 / NM2_TO_M2;
881  case AreaSquareDegrees:
882  return MI2_TO_M2 / DEG2_TO_M2;
884  return MI2_TO_M2 / CM2_TO_M2;
886  return MI2_TO_M2 / MM2_TO_M2;
887  case AreaUnknownUnit:
888  break;
889  }
890 
891  break;
892  }
893 
894  case AreaHectares:
895  {
896  switch ( toUnit )
897  {
898  case AreaSquareMeters:
899  return HA_TO_M2;
901  return HA_TO_M2 / KM2_TO_M2;
902  case AreaSquareFeet:
903  return HA_TO_M2 / FT2_TO_M2;
904  case AreaSquareYards:
905  return HA_TO_M2 / YD2_TO_M2;
906  case AreaSquareMiles:
907  return HA_TO_M2 / MI2_TO_M2;
908  case AreaHectares:
909  return 1.0;
910  case AreaAcres:
911  return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
913  return HA_TO_M2 / NM2_TO_M2;
914  case AreaSquareDegrees:
915  return HA_TO_M2 / DEG2_TO_M2;
917  return HA_TO_M2 / CM2_TO_M2;
919  return HA_TO_M2 / MM2_TO_M2;
920  case AreaUnknownUnit:
921  break;
922  }
923 
924  break;
925  }
926 
927  case AreaAcres:
928  {
929  switch ( toUnit )
930  {
931  case AreaSquareMeters:
932  return AC_TO_FT2 * FT2_TO_M2;
934  return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
935  case AreaSquareFeet:
936  return AC_TO_FT2;
937  case AreaSquareYards:
938  return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
939  case AreaSquareMiles:
940  return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
941  case AreaHectares:
942  return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
943  case AreaAcres:
944  return 1.0;
946  return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
947  case AreaSquareDegrees:
948  return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
950  return AC_TO_FT2 * FT2_TO_M2 / CM2_TO_M2;
952  return AC_TO_FT2 * FT2_TO_M2 / MM2_TO_M2;
953  case AreaUnknownUnit:
954  break;
955  }
956 
957  break;
958  }
959 
961  {
962  switch ( toUnit )
963  {
964  case AreaSquareMeters:
965  return NM2_TO_M2;
967  return NM2_TO_M2 / KM2_TO_M2;
968  case AreaSquareFeet:
969  return NM2_TO_M2 / FT2_TO_M2;
970  case AreaSquareYards:
971  return NM2_TO_M2 / YD2_TO_M2;
972  case AreaSquareMiles:
973  return NM2_TO_M2 / MI2_TO_M2;
974  case AreaHectares:
975  return NM2_TO_M2 / HA_TO_M2;
976  case AreaAcres:
977  return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
979  return 1.0;
980  case AreaSquareDegrees:
981  return NM2_TO_M2 / DEG2_TO_M2;
983  return NM2_TO_M2 / CM2_TO_M2;
985  return NM2_TO_M2 / MM2_TO_M2;
986  case AreaUnknownUnit:
987  break;
988  }
989 
990  break;
991  }
992 
993  case AreaSquareDegrees:
994  {
995  switch ( toUnit )
996  {
997  case AreaSquareMeters:
998  return DEG2_TO_M2;
1000  return DEG2_TO_M2 / KM2_TO_M2;
1001  case AreaSquareFeet:
1002  return DEG2_TO_M2 / FT2_TO_M2;
1003  case AreaSquareYards:
1004  return DEG2_TO_M2 / YD2_TO_M2;
1005  case AreaSquareMiles:
1006  return DEG2_TO_M2 / MI2_TO_M2;
1007  case AreaHectares:
1008  return DEG2_TO_M2 / HA_TO_M2;
1009  case AreaAcres:
1010  return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1012  return DEG2_TO_M2 / NM2_TO_M2;
1013  case AreaSquareDegrees:
1014  return 1.0;
1015  case AreaSquareCentimeters:
1016  return DEG2_TO_M2 / CM2_TO_M2;
1017  case AreaSquareMillimeters:
1018  return DEG2_TO_M2 / MM2_TO_M2;
1019  case AreaUnknownUnit:
1020  break;
1021  }
1022 
1023  break;
1024  }
1025 
1026  case AreaSquareMillimeters:
1027  {
1028  switch ( toUnit )
1029  {
1030  case AreaSquareMeters:
1031  return MM2_TO_M2;
1032  case AreaSquareKilometers:
1033  return MM2_TO_M2 / KM2_TO_M2;
1034  case AreaSquareFeet:
1035  return MM2_TO_M2 / FT2_TO_M2;
1036  case AreaSquareYards:
1037  return MM2_TO_M2 / YD2_TO_M2;
1038  case AreaSquareMiles:
1039  return MM2_TO_M2 / MI2_TO_M2;
1040  case AreaHectares:
1041  return MM2_TO_M2 / HA_TO_M2;
1042  case AreaAcres:
1043  return MM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1045  return MM2_TO_M2 / NM2_TO_M2;
1046  case AreaSquareDegrees:
1047  return MM2_TO_M2 / DEG2_TO_M2;
1048  case AreaSquareCentimeters:
1049  return MM2_TO_M2 / CM2_TO_M2;
1050  case AreaSquareMillimeters:
1051  return 1.0;
1052  case AreaUnknownUnit:
1053  break;
1054  }
1055 
1056  break;
1057  }
1058  case AreaSquareCentimeters:
1059  {
1060  switch ( toUnit )
1061  {
1062  case AreaSquareMeters:
1063  return CM2_TO_M2;
1064  case AreaSquareKilometers:
1065  return CM2_TO_M2 / KM2_TO_M2;
1066  case AreaSquareFeet:
1067  return CM2_TO_M2 / FT2_TO_M2;
1068  case AreaSquareYards:
1069  return CM2_TO_M2 / YD2_TO_M2;
1070  case AreaSquareMiles:
1071  return CM2_TO_M2 / MI2_TO_M2;
1072  case AreaHectares:
1073  return CM2_TO_M2 / HA_TO_M2;
1074  case AreaAcres:
1075  return CM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1077  return CM2_TO_M2 / NM2_TO_M2;
1078  case AreaSquareDegrees:
1079  return CM2_TO_M2 / DEG2_TO_M2;
1080  case AreaSquareCentimeters:
1081  return 1.0;
1082  case AreaSquareMillimeters:
1083  return CM2_TO_M2 / MM2_TO_M2;
1084  case AreaUnknownUnit:
1085  break;
1086  }
1087 
1088  break;
1089  }
1090  case AreaUnknownUnit:
1091  break;
1092  }
1093  return 1.0;
1094 }
1095 
1097 {
1098  switch ( distanceUnit )
1099  {
1100  case DistanceMeters:
1101  return AreaSquareMeters;
1102 
1103  case DistanceKilometers:
1104  return AreaSquareKilometers;
1105 
1106  case DistanceCentimeters:
1107  return AreaSquareCentimeters;
1108 
1109  case DistanceMillimeters:
1110  return AreaSquareCentimeters;
1111 
1112  case DistanceFeet:
1113  return AreaSquareFeet;
1114 
1115  case DistanceYards:
1116  return AreaSquareYards;
1117 
1118  case DistanceMiles:
1119  return AreaSquareMiles;
1120 
1121  case DistanceDegrees:
1122  return AreaSquareDegrees;
1123 
1124  case DistanceUnknownUnit:
1125  return AreaUnknownUnit;
1126 
1127  case DistanceNauticalMiles:
1128  return AreaSquareNauticalMiles;
1129  }
1130 
1131  return AreaUnknownUnit;
1132 }
1133 
1135 {
1136  switch ( unit )
1137  {
1138  case AngleDegrees:
1139  return QStringLiteral( "degrees" );
1140  case AngleRadians:
1141  return QStringLiteral( "radians" );
1142  case AngleGon:
1143  return QStringLiteral( "gon" );
1144  case AngleMinutesOfArc:
1145  return QStringLiteral( "moa" );
1146  case AngleSecondsOfArc:
1147  return QStringLiteral( "soa" );
1148  case AngleTurn:
1149  return QStringLiteral( "tr" );
1150  case AngleUnknownUnit:
1151  return QStringLiteral( "<unknown>" );
1152  }
1153  return QString();
1154 }
1155 
1156 QgsUnitTypes::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
1157 {
1158  QString normalized = string.trimmed().toLower();
1159 
1160  if ( ok )
1161  *ok = true;
1162 
1163  if ( normalized == encodeUnit( AngleDegrees ) )
1164  return AngleDegrees;
1165  if ( normalized == encodeUnit( AngleRadians ) )
1166  return AngleRadians;
1167  if ( normalized == encodeUnit( AngleGon ) )
1168  return AngleGon;
1169  if ( normalized == encodeUnit( AngleMinutesOfArc ) )
1170  return AngleMinutesOfArc;
1171  if ( normalized == encodeUnit( AngleSecondsOfArc ) )
1172  return AngleSecondsOfArc;
1173  if ( normalized == encodeUnit( AngleTurn ) )
1174  return AngleTurn;
1175  if ( normalized == encodeUnit( AngleUnknownUnit ) )
1176  return AngleUnknownUnit;
1177  if ( ok )
1178  *ok = false;
1179 
1180  return AngleUnknownUnit;
1181 }
1182 
1184 {
1185  switch ( unit )
1186  {
1187  case AngleDegrees:
1188  return QObject::tr( "degrees", "angle" );
1189  case AngleRadians:
1190  return QObject::tr( "radians", "angle" );
1191  case AngleGon:
1192  return QObject::tr( "gon", "angle" );
1193  case AngleMinutesOfArc:
1194  return QObject::tr( "minutes of arc", "angle" );
1195  case AngleSecondsOfArc:
1196  return QObject::tr( "seconds of arc", "angle" );
1197  case AngleTurn:
1198  return QObject::tr( "turns", "angle" );
1199  case AngleUnknownUnit:
1200  return QObject::tr( "<unknown>", "angle" );
1201  }
1202  return QString();
1203 }
1204 
1206 {
1207  // Calculate the conversion factor between the specified units
1208  switch ( fromUnit )
1209  {
1210  case AngleDegrees:
1211  {
1212  switch ( toUnit )
1213  {
1214  case AngleDegrees:
1215  return 1.0;
1216  case AngleRadians:
1217  return M_PI / 180.0;
1218  case AngleGon:
1219  return 400.0 / 360.0;
1220  case AngleMinutesOfArc:
1221  return 60;
1222  case AngleSecondsOfArc:
1223  return 3600;
1224  case AngleTurn:
1225  return 1.0 / 360.0;
1226  case AngleUnknownUnit:
1227  break;
1228  }
1229  break;
1230  }
1231  case AngleRadians:
1232  {
1233  switch ( toUnit )
1234  {
1235  case AngleDegrees:
1236  return 180.0 / M_PI;
1237  case AngleRadians:
1238  return 1.0;
1239  case AngleGon:
1240  return 200.0 / M_PI;
1241  case AngleMinutesOfArc:
1242  return 60 * 180.0 / M_PI;
1243  case AngleSecondsOfArc:
1244  return 3600 * 180.0 / M_PI;
1245  case AngleTurn:
1246  return 0.5 / M_PI;
1247  case AngleUnknownUnit:
1248  break;
1249  }
1250  break;
1251  }
1252  case AngleGon:
1253  {
1254  switch ( toUnit )
1255  {
1256  case AngleDegrees:
1257  return 360.0 / 400.0;
1258  case AngleRadians:
1259  return M_PI / 200.0;
1260  case AngleGon:
1261  return 1.0;
1262  case AngleMinutesOfArc:
1263  return 60 * 360.0 / 400.0;
1264  case AngleSecondsOfArc:
1265  return 3600 * 360.0 / 400.0;
1266  case AngleTurn:
1267  return 1.0 / 400.0;
1268  case AngleUnknownUnit:
1269  break;
1270  }
1271  break;
1272  }
1273  case AngleMinutesOfArc:
1274  {
1275  switch ( toUnit )
1276  {
1277  case AngleDegrees:
1278  return 1 / 60.0;
1279  case AngleRadians:
1280  return M_PI / 180.0 / 60.0;
1281  case AngleGon:
1282  return 400.0 / 360.0 / 60.0;
1283  case AngleMinutesOfArc:
1284  return 1.0;
1285  case AngleSecondsOfArc:
1286  return 60.0;
1287  case AngleTurn:
1288  return 1.0 / 360.0 / 60.0;
1289  case AngleUnknownUnit:
1290  break;
1291  }
1292  break;
1293  }
1294  case AngleSecondsOfArc:
1295  {
1296  switch ( toUnit )
1297  {
1298  case AngleDegrees:
1299  return 1 / 3600.0;
1300  case AngleRadians:
1301  return M_PI / 180.0 / 3600.0;
1302  case AngleGon:
1303  return 400.0 / 360.0 / 3600.0;
1304  case AngleMinutesOfArc:
1305  return 1.0 / 60.0;
1306  case AngleSecondsOfArc:
1307  return 1.0;
1308  case AngleTurn:
1309  return 1.0 / 360.0 / 3600.0;
1310  case AngleUnknownUnit:
1311  break;
1312  }
1313  break;
1314  }
1315  case AngleTurn:
1316  {
1317  switch ( toUnit )
1318  {
1319  case AngleDegrees:
1320  return 360.0;
1321  case AngleRadians:
1322  return 2 * M_PI;
1323  case AngleGon:
1324  return 400.0;
1325  case AngleMinutesOfArc:
1326  return 360.0 * 60.0;
1327  case AngleSecondsOfArc:
1328  return 360.0 * 3600.0;
1329  case AngleTurn:
1330  return 1.0;
1331  case AngleUnknownUnit:
1332  break;
1333  }
1334  break;
1335  }
1336  case AngleUnknownUnit:
1337  break;
1338  }
1339  return 1.0;
1340 }
1341 
1342 QString QgsUnitTypes::formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit )
1343 {
1344  QString unitLabel;
1345 
1346  switch ( unit )
1347  {
1348  case AngleDegrees:
1349  unitLabel = QObject::tr( "°", "angle" );
1350  break;
1351  case AngleRadians:
1352  unitLabel = QObject::tr( " rad", "angle" );
1353  break;
1354  case AngleGon:
1355  unitLabel = QObject::tr( " gon", "angle" );
1356  break;
1357  case AngleMinutesOfArc:
1358  unitLabel = QObject::tr( "′", "angle minutes" );
1359  break;
1360  case AngleSecondsOfArc:
1361  unitLabel = QObject::tr( "″", "angle seconds" );
1362  break;
1363  case AngleTurn:
1364  unitLabel = QObject::tr( " tr", "angle turn" );
1365  break;
1366  case AngleUnknownUnit:
1367  break;
1368  }
1369 
1370  return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
1371 }
1372 
1373 
1374 QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit )
1375 {
1376  DistanceValue result;
1377 
1378  switch ( unit )
1379  {
1380  case DistanceMeters:
1381  if ( keepBaseUnit )
1382  {
1383  result.value = qgsRound( distance, decimals );
1385  }
1386  else if ( std::fabs( distance ) > 1000.0 )
1387  {
1388  result.value = qgsRound( distance / 1000, decimals );
1390  }
1391  else if ( std::fabs( distance ) < 0.01 )
1392  {
1393  result.value = qgsRound( distance * 1000, decimals );
1395  }
1396  else if ( std::fabs( distance ) < 0.1 )
1397  {
1398 
1399  result.value = qgsRound( distance * 100, decimals );
1401  }
1402  else
1403  {
1404  result.value = qgsRound( distance, decimals );
1406  }
1407  break;
1408 
1409  case DistanceKilometers:
1410  if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
1411  {
1412  result.value = qgsRound( distance, decimals );
1414  }
1415  else
1416  {
1417  result.value = qgsRound( distance * 1000, decimals );
1419  }
1420  break;
1421 
1422  case DistanceFeet:
1423  if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
1424  {
1425  result.value = qgsRound( distance, decimals );
1427  }
1428  else
1429  {
1430  result.value = qgsRound( distance / 5280.0, decimals );
1432  }
1433  break;
1434 
1435  case DistanceYards:
1436  if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
1437  {
1438  result.value = qgsRound( distance, decimals );
1440  }
1441  else
1442  {
1443  result.value = qgsRound( distance / 1760.0, decimals );
1445  }
1446  break;
1447 
1448  case DistanceMiles:
1449  if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
1450  {
1451  result.value = qgsRound( distance, decimals );
1453  }
1454  else
1455  {
1456  result.value = qgsRound( distance * 5280.0, decimals );
1458  }
1459  break;
1460 
1461  case DistanceNauticalMiles:
1462  result.value = qgsRound( distance, decimals );
1464  break;
1465 
1466  case DistanceDegrees:
1467  result.value = qgsRound( distance, decimals );
1469  break;
1470 
1471  case DistanceUnknownUnit:
1472  result.value = qgsRound( distance, decimals );
1474  break;
1475 
1476  default:
1477  result.value = qgsRound( distance, decimals );
1478  result.unit = unit;
1479  break;
1480  }
1481 
1482  return result;
1483 }
1484 
1485 QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit )
1486 {
1487  AreaValue result;
1488  result.value = -1.0;
1489  result.unit = AreaUnknownUnit;
1490 
1491  // If we are not forced to keep the base units, switch to meter calculation
1492  if ( unit == AreaSquareMillimeters )
1493  {
1494  if ( keepBaseUnit )
1495  {
1496  result.value = qgsRound( area, decimals );
1498  }
1499  else
1500  {
1501  area /= 1000000.0;
1503  }
1504  }
1505  else if ( unit == AreaSquareCentimeters )
1506  {
1507  if ( keepBaseUnit )
1508  {
1509  result.value = qgsRound( area, decimals );
1511  }
1512  else
1513  {
1514  area /= 10000.0;
1516  }
1517  }
1518 
1519  switch ( unit )
1520  {
1521  case AreaSquareCentimeters:
1522  // handled in the if above
1523  break;
1524  case AreaSquareMillimeters:
1525  // handled in the if above
1526  break;
1527  case AreaSquareMeters:
1528  {
1529  if ( keepBaseUnit )
1530  {
1531  result.value = qgsRound( area, decimals );
1533  }
1535  {
1538  }
1540  {
1543  }
1544  else
1545  {
1546  result.value = qgsRound( area, decimals );
1548  }
1549  break;
1550  }
1551 
1552  case AreaSquareKilometers:
1553  {
1554  result.value = qgsRound( area, decimals );
1556  break;
1557  }
1558 
1559  case AreaSquareFeet:
1560  {
1561  if ( keepBaseUnit )
1562  {
1563  result.value = qgsRound( area, decimals );
1565  }
1567  {
1570  }
1571  else
1572  {
1573  result.value = qgsRound( area, decimals );
1575  }
1576  break;
1577  }
1578 
1579  case AreaSquareYards:
1580  {
1581  if ( keepBaseUnit )
1582  {
1583  result.value = qgsRound( area, decimals );
1585  }
1587  {
1590  }
1591  else
1592  {
1593  result.value = qgsRound( area, decimals );
1595  }
1596  break;
1597  }
1598 
1599  case AreaSquareMiles:
1600  {
1601  result.value = qgsRound( area, decimals );
1603  break;
1604  }
1605 
1606  case AreaHectares:
1607  {
1608  if ( keepBaseUnit )
1609  {
1610  result.value = qgsRound( area, decimals );
1612  }
1614  {
1617  }
1618  else
1619  {
1620  result.value = qgsRound( area, decimals );
1622  }
1623  break;
1624  }
1625 
1626  case AreaAcres:
1627  {
1628  if ( keepBaseUnit )
1629  {
1630  result.value = qgsRound( area, decimals );
1631  result.unit = QgsUnitTypes::AreaAcres;
1632  }
1634  {
1637  }
1638  else
1639  {
1640  result.value = qgsRound( area, decimals );
1641  result.unit = QgsUnitTypes::AreaAcres;
1642  }
1643  break;
1644  }
1645 
1647  {
1648  result.value = qgsRound( area, decimals );
1650  break;
1651  }
1652 
1653  case AreaSquareDegrees:
1654  {
1655  result.value = qgsRound( area, decimals );
1657  break;
1658  }
1659 
1660  case AreaUnknownUnit:
1661  {
1662  result.value = qgsRound( area, decimals );
1664  break;
1665  }
1666  }
1667  return result;
1668 }
1669 
1670 
1671 QString QgsUnitTypes::formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit )
1672 {
1673  DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
1674 
1675  QString unitText;
1676 
1677  if ( dist.unit != DistanceUnknownUnit )
1678  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
1679 
1680  return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
1681 }
1682 
1683 QString QgsUnitTypes::formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit )
1684 {
1685  AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
1686 
1687  QString unitText;
1688 
1689  if ( areaValue.unit != AreaUnknownUnit )
1690  unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
1691 
1692  return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
1693 }
1694 
1696 {
1697  switch ( unit )
1698  {
1699  case RenderMillimeters:
1700  return QStringLiteral( "MM" );
1702  return QStringLiteral( "RenderMetersInMapUnits" );
1703  case RenderMapUnits:
1704  return QStringLiteral( "MapUnit" );
1705  case RenderPixels:
1706  return QStringLiteral( "Pixel" );
1707  case RenderPercentage:
1708  return QStringLiteral( "Percentage" );
1709  case RenderPoints:
1710  return QStringLiteral( "Point" );
1711  case RenderInches:
1712  return QStringLiteral( "Inch" );
1713  case RenderUnknownUnit:
1714  return QString();
1715  }
1716  return QString();
1717 }
1718 
1720 {
1721  QString normalized = string.trimmed().toLower();
1722 
1723  if ( ok )
1724  *ok = true;
1725 
1726  if ( normalized == encodeUnit( RenderMillimeters ).toLower() )
1727  return RenderMillimeters;
1728  if ( normalized == encodeUnit( RenderMetersInMapUnits ).toLower() )
1729  return RenderMetersInMapUnits;
1730  if ( normalized == QLatin1String( "meters" ) )
1731  return RenderMetersInMapUnits;
1732  if ( normalized == encodeUnit( RenderMapUnits ).toLower() )
1733  return RenderMapUnits;
1734  if ( normalized == QLatin1String( "mapunits" ) )
1735  return RenderMapUnits;
1736  if ( normalized == encodeUnit( RenderPixels ).toLower() )
1737  return RenderPixels;
1738  if ( normalized == encodeUnit( RenderPercentage ).toLower() )
1739  return RenderPercentage;
1740  if ( normalized == QLatin1String( "percent" ) )
1741  return RenderPercentage;
1742  if ( normalized == encodeUnit( RenderPoints ).toLower() )
1743  return RenderPoints;
1744  if ( normalized == QLatin1String( "points" ) )
1745  return RenderPoints;
1746  if ( normalized == encodeUnit( RenderInches ).toLower() )
1747  return RenderInches;
1748 
1749  if ( ok )
1750  *ok = false;
1751 
1752  // millimeters are default
1753  return RenderMillimeters;
1754 }
1755 
1757 {
1758  switch ( unit )
1759  {
1760  case RenderMillimeters:
1761  return QObject::tr( "millimeters", "render" );
1762 
1764  return QObject::tr( "meters at scale", "render" );
1765 
1766  case RenderMapUnits:
1767  return QObject::tr( "map units", "render" );
1768 
1769  case RenderPixels:
1770  return QObject::tr( "pixels", "render" );
1771 
1772  case RenderPercentage:
1773  return QObject::tr( "percent", "render" );
1774 
1775  case RenderPoints:
1776  return QObject::tr( "points", "render" );
1777 
1778  case RenderInches:
1779  return QObject::tr( "inches", "render" );
1780 
1781  case RenderUnknownUnit:
1782  return QObject::tr( "<unknown>", "render" );
1783 
1784  }
1785  return QString();
1786 }
1787 
1788 
1789 
1791 {
1792  switch ( unit )
1793  {
1794  case LayoutCentimeters:
1795  return QStringLiteral( "cm" );
1796  case LayoutMeters:
1797  return QStringLiteral( "m" );
1798  case LayoutInches:
1799  return QStringLiteral( "in" );
1800  case LayoutFeet:
1801  return QStringLiteral( "ft" );
1802  case LayoutPoints:
1803  return QStringLiteral( "pt" );
1804  case LayoutPicas:
1805  return QStringLiteral( "pi" );
1806  case LayoutPixels:
1807  return QStringLiteral( "px" );
1808  case LayoutMillimeters:
1809  return QStringLiteral( "mm" );
1810  }
1811  return QString();
1812 }
1813 
1815 {
1816  QString normalized = string.trimmed().toLower();
1817 
1818  if ( ok )
1819  *ok = true;
1820 
1821  if ( normalized == encodeUnit( LayoutMillimeters ).toLower() )
1822  return LayoutMillimeters;
1823  if ( normalized == encodeUnit( LayoutCentimeters ).toLower() )
1824  return LayoutCentimeters;
1825  if ( normalized == encodeUnit( LayoutMeters ).toLower() )
1826  return LayoutMeters;
1827  if ( normalized == encodeUnit( LayoutInches ).toLower() )
1828  return LayoutInches;
1829  if ( normalized == encodeUnit( LayoutFeet ).toLower() )
1830  return LayoutFeet;
1831  if ( normalized == encodeUnit( LayoutPoints ).toLower() )
1832  return LayoutPoints;
1833  if ( normalized == encodeUnit( LayoutPicas ).toLower() )
1834  return LayoutPicas;
1835  if ( normalized == encodeUnit( LayoutPixels ).toLower() )
1836  return LayoutPixels;
1837 
1838  if ( ok )
1839  *ok = false;
1840 
1841  // millimeters are default
1842  return LayoutMillimeters;
1843 }
1844 
1846 {
1847  switch ( units )
1848  {
1849  case LayoutPixels:
1850  return LayoutScreenUnits;
1851  case LayoutMillimeters:
1852  case LayoutCentimeters:
1853  case LayoutMeters:
1854  case LayoutInches:
1855  case LayoutFeet:
1856  case LayoutPoints:
1857  case LayoutPicas:
1858  return LayoutPaperUnits;
1859  }
1860 
1861  // avoid warnings
1862  return LayoutPaperUnits;
1863 }
1864 
1866 {
1867  switch ( unit )
1868  {
1869  case LayoutPixels:
1870  return QObject::tr( "px" );
1871  case LayoutMillimeters:
1872  return QObject::tr( "mm" );
1873  case LayoutCentimeters:
1874  return QObject::tr( "cm" );
1875  case LayoutMeters:
1876  return QObject::tr( "m" );
1877  case LayoutInches:
1878  return QObject::tr( "in", "unit inch" );
1879  case LayoutFeet:
1880  return QObject::tr( "ft" );
1881  case LayoutPoints:
1882  return QObject::tr( "pt" );
1883  case LayoutPicas:
1884  return QObject::tr( "pica" );
1885  }
1886  return QString(); // no warnings
1887 }
1888 
1890 {
1891  switch ( unit )
1892  {
1893  case LayoutPixels:
1894  return QObject::tr( "pixels" );
1895  case LayoutMillimeters:
1896  return QObject::tr( "millimeters" );
1897  case LayoutCentimeters:
1898  return QObject::tr( "centimeters" );
1899  case LayoutMeters:
1900  return QObject::tr( "meters" );
1901  case LayoutInches:
1902  return QObject::tr( "inches" );
1903  case LayoutFeet:
1904  return QObject::tr( "feet" );
1905  case LayoutPoints:
1906  return QObject::tr( "points" );
1907  case LayoutPicas:
1908  return QObject::tr( "picas" );
1909  }
1910  return QString(); // no warnings
1911 }
#define HA_TO_M2
Meters value as Map units.
Definition: qgsunittypes.h:109
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:164
#define DEGREE_TO_METER
QgsUnitTypes::DistanceUnit unit
The value part of the distance.
Definition: qgsunittypes.h:150
double value
The value part of the distance.
Definition: qgsunittypes.h:145
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:128
DistanceUnitType
Types of distance units.
Definition: qgsunittypes.h:61
Square millimeters.
Definition: qgsunittypes.h:82
#define NMILE_TO_METER
Square kilometers.
Definition: qgsunittypes.h:90
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:105
Typographic points.
Definition: qgsunittypes.h:121
#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
double qgsRound(double number, double places)
Returns a double number, rounded (as close as possible) to the specified number of places...
Definition: qgis.h:290
Unit is a paper based measurement unit.
Definition: qgsunittypes.h:130
#define KILOMETERS_TO_METER
#define FEET_TO_METER
AngleUnit
Units of angles.
Definition: qgsunittypes.h:87
#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
points (e.g., for font sizes)
Definition: qgsunittypes.h:107
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:51
#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:79
A combination of distance value and unit.
Definition: qgsunittypes.h:139
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
static Q_INVOKABLE DistanceUnitType unitType(QgsUnitTypes::DistanceUnit unit)
Returns the type for a distance unit.
Unknown distance unit.
Definition: qgsunittypes.h:54
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:169
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:95
Unknown unit type.
Definition: qgsunittypes.h:65
Unit is a geographic (e.g., degree based) unit.
Definition: qgsunittypes.h:64
static Q_INVOKABLE QString formatDistance(double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit=false)
Returns an distance formatted as a friendly string.
#define MM2_TO_M2
Turn/revolutions.
Definition: qgsunittypes.h:94
#define MILLIMETERS_TO_METER
A combination of area value and unit.
Definition: qgsunittypes.h:158
#define MILES_TO_METER
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:114
Unit is a screen based measurement unit.
Definition: qgsunittypes.h:131
Terrestrial miles.
Definition: qgsunittypes.h:50
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:69
Unit is a standard measurement unit.
Definition: qgsunittypes.h:63
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
Typographic picas.
Definition: qgsunittypes.h:122
#define YD2_TO_M2
#define CM2_TO_M2