QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgsunittypes.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsunittypes.cpp
3 --------------
4 begin : February 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsunittypes.h"
18#include "qgis.h"
19
20/***************************************************************************
21 * This class is considered CRITICAL and any change MUST be accompanied with
22 * full unit tests in test_qgsunittypes.py.
23 * See details in QEP #17
24 ****************************************************************************/
25
27{
28 switch ( type )
29 {
31 return QStringLiteral( "distance" );
32
34 return QStringLiteral( "area" );
35
37 return QStringLiteral( "volume" );
38
40 return QStringLiteral( "temporal" );
41
43 return QStringLiteral( "<unknown>" );
44
45 }
46 return QString();
47}
48
49Qgis::UnitType QgsUnitTypes::decodeUnitType( const QString &string, bool *ok )
50{
51 const QString normalized = string.trimmed().toLower();
52
53 if ( ok )
54 *ok = true;
55
56 if ( normalized == encodeUnitType( Qgis::UnitType::Distance ) )
58 if ( normalized == encodeUnitType( Qgis::UnitType::Area ) )
60 if ( normalized == encodeUnitType( Qgis::UnitType::Volume ) )
62 if ( normalized == encodeUnitType( Qgis::UnitType::Temporal ) )
64 if ( normalized == encodeUnitType( Qgis::UnitType::Unknown ) )
66
67 if ( ok )
68 *ok = false;
69
71}
72
96
123
125{
126 switch ( unit )
127 {
129 return QStringLiteral( "meters" );
130
132 return QStringLiteral( "km" );
133
135 return QStringLiteral( "feet" );
136
138 return QStringLiteral( "yd" );
139
141 return QStringLiteral( "mi" );
142
144 return QStringLiteral( "degrees" );
145
147 return QStringLiteral( "<unknown>" );
148
150 return QStringLiteral( "nautical miles" );
151
153 return QStringLiteral( "cm" );
154
156 return QStringLiteral( "mm" );
157
159 return QStringLiteral( "in" );
160 }
161 return QString();
162}
163
164/***************************************************************************
165 * This class is considered CRITICAL and any change MUST be accompanied with
166 * full unit tests in test_qgsunittypes.py.
167 * See details in QEP #17
168 ****************************************************************************/
169
170Qgis::DistanceUnit QgsUnitTypes::decodeDistanceUnit( const QString &string, bool *ok )
171{
172 const QString normalized = string.trimmed().toLower();
173
174 if ( ok )
175 *ok = true;
176
177 if ( normalized == encodeUnit( Qgis::DistanceUnit::Meters ) )
179 if ( normalized == encodeUnit( Qgis::DistanceUnit::Feet ) )
181 if ( normalized == encodeUnit( Qgis::DistanceUnit::Degrees ) )
183 if ( normalized == encodeUnit( Qgis::DistanceUnit::NauticalMiles ) )
185 if ( normalized == encodeUnit( Qgis::DistanceUnit::Kilometers ) )
187 if ( normalized == encodeUnit( Qgis::DistanceUnit::Yards ) )
189 if ( normalized == encodeUnit( Qgis::DistanceUnit::Miles ) )
191 if ( normalized == encodeUnit( Qgis::DistanceUnit::Centimeters ) )
193 if ( normalized == encodeUnit( Qgis::DistanceUnit::Millimeters ) )
195 if ( normalized == encodeUnit( Qgis::DistanceUnit::Inches ) )
197 if ( normalized == encodeUnit( Qgis::DistanceUnit::Unknown ) )
199
200 if ( ok )
201 *ok = false;
202
204}
205
207{
208 switch ( unit )
209 {
211 return QObject::tr( "meters", "distance" );
212
214 return QObject::tr( "kilometers", "distance" );
215
217 return QObject::tr( "feet", "distance" );
218
220 return QObject::tr( "yards", "distance" );
221
223 return QObject::tr( "miles", "distance" );
224
226 return QObject::tr( "degrees", "distance" );
227
229 return QObject::tr( "centimeters", "distance" );
230
232 return QObject::tr( "millimeters", "distance" );
233
235 return QObject::tr( "inches", "distance" );
236
238 return QObject::tr( "<unknown>", "distance" );
239
241 return QObject::tr( "nautical miles", "distance" );
242 }
243 return QString();
244}
245
247{
248 switch ( unit )
249 {
251 return QObject::tr( "mm", "render" );
252
254 return QObject::tr( "map units", "render" );
255
257 return QObject::tr( "px", "render" );
258
260 return QObject::tr( "%", "render" );
261
263 return QObject::tr( "pt", "render" );
264
266 return QObject::tr( "in", "render" );
267
269 return QObject::tr( "unknown", "render" );
270
272 return QObject::tr( "m", "render" );
273
274 }
275
276 return QString();
277}
278
280{
281 switch ( unit )
282 {
284 return QObject::tr( "m", "distance" );
285
287 return QObject::tr( "km", "distance" );
288
290 return QObject::tr( "ft", "distance" );
291
293 return QObject::tr( "yd", "distance" );
294
296 return QObject::tr( "mi", "distance" );
297
299 return QObject::tr( "deg", "distance" );
300
302 return QObject::tr( "cm", "distance" );
303
305 return QObject::tr( "mm", "distance" );
306
308 return QObject::tr( "in", "distance" );
309
311 return QString();
312
314 return QObject::tr( "NM", "distance" );
315 }
316 return QString();
317}
318
319/***************************************************************************
320 * This class is considered CRITICAL and any change MUST be accompanied with
321 * full unit tests in test_qgsunittypes.py.
322 * See details in QEP #17
323 ****************************************************************************/
324
326{
327 const QString normalized = string.trimmed().toLower();
328
329 if ( ok )
330 *ok = true;
331
332 if ( normalized == toString( Qgis::DistanceUnit::Meters ) )
334 if ( normalized == toString( Qgis::DistanceUnit::Kilometers ) )
336 if ( normalized == toString( Qgis::DistanceUnit::Feet ) )
338 if ( normalized == toString( Qgis::DistanceUnit::Yards ) )
340 if ( normalized == toString( Qgis::DistanceUnit::Miles ) )
342 if ( normalized == toString( Qgis::DistanceUnit::Degrees ) )
344 if ( normalized == toString( Qgis::DistanceUnit::Centimeters ) )
346 if ( normalized == toString( Qgis::DistanceUnit::Millimeters ) )
348 if ( normalized == toString( Qgis::DistanceUnit::NauticalMiles ) )
350 if ( normalized == toString( Qgis::DistanceUnit::Inches ) )
352 if ( normalized == toString( Qgis::DistanceUnit::Unknown ) )
354
355 if ( ok )
356 *ok = false;
357
359}
360
361/***************************************************************************
362 * This class is considered CRITICAL and any change MUST be accompanied with
363 * full unit tests in test_qgsunittypes.py.
364 * See details in QEP #17
365 ****************************************************************************/
366
368{
369#define DEGREE_TO_METER 111319.49079327358
370#define FEET_TO_METER 0.3048
371#define NMILE_TO_METER 1852.0
372#define KILOMETERS_TO_METER 1000.0
373#define CENTIMETERS_TO_METER 0.01
374#define MILLIMETERS_TO_METER 0.001
375#define INCHES_TO_METER 0.0254
376#define FEET_TO_INCHES 12
377#define YARDS_TO_METER 0.9144
378#define YARDS_TO_FEET 3.0
379#define MILES_TO_METER 1609.344
380
381 // Calculate the conversion factor between the specified units
382 switch ( fromUnit )
383 {
385 {
386 switch ( toUnit )
387 {
389 return 1.0;
391 return 1.0 / KILOMETERS_TO_METER;
393 return 1.0 / MILLIMETERS_TO_METER;
395 return 1.0 / CENTIMETERS_TO_METER;
397 return 1.0 / INCHES_TO_METER;
399 return 1.0 / FEET_TO_METER;
401 return 1.0 / YARDS_TO_METER;
403 return 1.0 / MILES_TO_METER;
405 return 1.0 / DEGREE_TO_METER;
407 return 1.0 / NMILE_TO_METER;
409 break;
410 }
411
412 break;
413 }
415 {
416 switch ( toUnit )
417 {
419 return KILOMETERS_TO_METER;
421 return 1.0;
439 break;
440 }
441
442 break;
443 }
445 {
446 switch ( toUnit )
447 {
449 return FEET_TO_METER;
457 return FEET_TO_INCHES;
459 return 1.0;
461 return 1.0 / YARDS_TO_FEET;
469 break;
470 }
471
472 break;
473 }
475 {
476 switch ( toUnit )
477 {
479 return YARDS_TO_METER;
487 return YARDS_TO_FEET;
491 return 1.0;
499 break;
500 }
501
502 break;
503 }
505 {
506 switch ( toUnit )
507 {
509 return MILES_TO_METER;
523 return 1.0;
529 break;
530 }
531
532 break;
533 }
535 {
536 switch ( toUnit )
537 {
539 return DEGREE_TO_METER;
555 return 1.0;
559 break;
560 }
561
562 break;
563 }
565 {
566 switch ( toUnit )
567 {
569 return NMILE_TO_METER;
587 return 1.0;
589 break;
590 }
591
592 break;
593 }
595 {
596 switch ( toUnit )
597 {
603 return 1.0;
619 break;
620 }
621
622 break;
623 }
625 {
626 switch ( toUnit )
627 {
635 return 1.0;
649 break;
650 }
651
652 break;
653 }
654
656 {
657 switch ( toUnit )
658 {
660 return INCHES_TO_METER;
668 return 1.0 / FEET_TO_INCHES;
670 return 1;
672 return 1.0 / ( YARDS_TO_FEET * FEET_TO_INCHES );
680 break;
681 }
682
683 break;
684 }
686 break;
687 }
688 return 1.0;
689}
690
692{
693 switch ( unit )
694 {
696 return QStringLiteral( "m2" );
698 return QStringLiteral( "km2" );
700 return QStringLiteral( "ft2" );
702 return QStringLiteral( "y2" );
704 return QStringLiteral( "mi2" );
706 return QStringLiteral( "ha" );
708 return QStringLiteral( "ac" );
710 return QStringLiteral( "nm2" );
712 return QStringLiteral( "deg2" );
714 return QStringLiteral( "cm2" );
716 return QStringLiteral( "mm2" );
718 return QStringLiteral( "in2" );
720 return QStringLiteral( "<unknown>" );
721 }
722 return QString();
723}
724
725Qgis::AreaUnit QgsUnitTypes::decodeAreaUnit( const QString &string, bool *ok )
726{
727 const QString normalized = string.trimmed().toLower();
728
729 if ( ok )
730 *ok = true;
731
732 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMeters ) )
734 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareKilometers ) )
736 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareFeet ) )
738 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareYards ) )
740 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMiles ) )
742 if ( normalized == encodeUnit( Qgis::AreaUnit::Hectares ) )
744 if ( normalized == encodeUnit( Qgis::AreaUnit::Acres ) )
746 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareNauticalMiles ) )
748 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareDegrees ) )
750 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareCentimeters ) )
752 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareMillimeters ) )
754 if ( normalized == encodeUnit( Qgis::AreaUnit::SquareInches ) )
756 if ( normalized == encodeUnit( Qgis::AreaUnit::Unknown ) )
758
759 if ( ok )
760 *ok = false;
761
763}
764
766{
767 switch ( unit )
768 {
770 return QObject::tr( "square meters", "area" );
772 return QObject::tr( "square kilometers", "area" );
774 return QObject::tr( "square feet", "area" );
776 return QObject::tr( "square yards", "area" );
778 return QObject::tr( "square miles", "area" );
780 return QObject::tr( "hectares", "area" );
782 return QObject::tr( "acres", "area" );
784 return QObject::tr( "square nautical miles", "area" );
786 return QObject::tr( "square degrees", "area" );
788 return QObject::tr( "square millimeters", "area" );
790 return QObject::tr( "square centimeters", "area" );
792 return QObject::tr( "square inches", "area" );
794 return QObject::tr( "<unknown>", "area" );
795 }
796 return QString();
797}
798
800{
801 switch ( unit )
802 {
804 return QObject::tr( "m²", "area" );
806 return QObject::tr( "km²", "area" );
808 return QObject::tr( "ft²", "area" );
810 return QObject::tr( "yd²", "area" );
812 return QObject::tr( "mi²", "area" );
814 return QObject::tr( "ha", "area" );
816 return QObject::tr( "ac", "area" );
818 return QObject::tr( "NM²", "area" );
820 return QObject::tr( "deg²", "area" );
822 return QObject::tr( "cm²", "area" );
824 return QObject::tr( "mm²", "area" );
826 return QObject::tr( "in²", "area" );
828 return QString();
829 }
830 return QString();
831}
832
833Qgis::AreaUnit QgsUnitTypes::stringToAreaUnit( const QString &string, bool *ok )
834{
835 const QString normalized = string.trimmed().toLower();
836
837 if ( ok )
838 *ok = true;
839
840 if ( normalized == toString( Qgis::AreaUnit::SquareMeters ) )
842 if ( normalized == toString( Qgis::AreaUnit::SquareKilometers ) )
844 if ( normalized == toString( Qgis::AreaUnit::SquareFeet ) )
846 if ( normalized == toString( Qgis::AreaUnit::SquareYards ) )
848 if ( normalized == toString( Qgis::AreaUnit::SquareMiles ) )
850 if ( normalized == toString( Qgis::AreaUnit::Hectares ) )
852 if ( normalized == toString( Qgis::AreaUnit::Acres ) )
854 if ( normalized == toString( Qgis::AreaUnit::SquareNauticalMiles ) )
856 if ( normalized == toString( Qgis::AreaUnit::SquareDegrees ) )
858 if ( normalized == toString( Qgis::AreaUnit::SquareMillimeters ) )
860 if ( normalized == toString( Qgis::AreaUnit::SquareCentimeters ) )
862 if ( normalized == toString( Qgis::AreaUnit::SquareInches ) )
864 if ( normalized == toString( Qgis::AreaUnit::Unknown ) )
866 if ( ok )
867 *ok = false;
868
870}
871
873{
874#define KM2_TO_M2 1000000.0
875#define CM2_TO_M2 0.0001
876#define MM2_TO_M2 0.000001
877#define FT2_TO_M2 0.09290304
878#define IN2_TO_M2 0.00064516
879#define YD2_TO_M2 0.83612736
880#define MI2_TO_M2 2589988.110336
881#define HA_TO_M2 10000.0
882#define AC_TO_FT2 43560.0
883#define DEG2_TO_M2 12392029030.5
884#define NM2_TO_M2 3429904.0
885
886 // Calculate the conversion factor between the specified units
887 switch ( fromUnit )
888 {
890 {
891 switch ( toUnit )
892 {
894 return 1.0;
896 return 1.0 / KM2_TO_M2;
898 return 1.0 / FT2_TO_M2;
900 return 1.0 / YD2_TO_M2;
902 return 1.0 / MI2_TO_M2;
904 return 1.0 / HA_TO_M2;
906 return 1.0 / AC_TO_FT2 / FT2_TO_M2;
908 return 1.0 / NM2_TO_M2;
910 return 1.0 / DEG2_TO_M2;
912 return 1.0 / CM2_TO_M2;
914 return 1.0 / MM2_TO_M2;
916 return 1.0 / IN2_TO_M2;
918 break;
919 }
920
921 break;
922 }
924 {
925 switch ( toUnit )
926 {
928 return KM2_TO_M2;
930 return 1.0;
932 return KM2_TO_M2 / FT2_TO_M2;
934 return KM2_TO_M2 / YD2_TO_M2;
936 return KM2_TO_M2 / MI2_TO_M2;
938 return KM2_TO_M2 / HA_TO_M2;
940 return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
942 return KM2_TO_M2 / NM2_TO_M2;
944 return KM2_TO_M2 / DEG2_TO_M2;
946 return KM2_TO_M2 / CM2_TO_M2;
948 return KM2_TO_M2 / MM2_TO_M2;
950 return KM2_TO_M2 / IN2_TO_M2;
952 break;
953 }
954
955 break;
956 }
958 {
959 switch ( toUnit )
960 {
962 return FT2_TO_M2;
964 return FT2_TO_M2 / KM2_TO_M2;
966 return 1.0;
968 return FT2_TO_M2 / YD2_TO_M2;
970 return FT2_TO_M2 / MI2_TO_M2;
972 return FT2_TO_M2 / HA_TO_M2;
974 return 1.0 / AC_TO_FT2;
976 return FT2_TO_M2 / NM2_TO_M2;
978 return FT2_TO_M2 / DEG2_TO_M2;
980 return FT2_TO_M2 / CM2_TO_M2;
982 return FT2_TO_M2 / MM2_TO_M2;
984 return FT2_TO_M2 / IN2_TO_M2;
986 break;
987 }
988
989 break;
990 }
991
993 {
994 switch ( toUnit )
995 {
997 return YD2_TO_M2;
999 return YD2_TO_M2 / KM2_TO_M2;
1001 return YD2_TO_M2 / FT2_TO_M2;
1003 return 1.0;
1005 return YD2_TO_M2 / MI2_TO_M2;
1007 return YD2_TO_M2 / HA_TO_M2;
1009 return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1011 return YD2_TO_M2 / NM2_TO_M2;
1013 return YD2_TO_M2 / DEG2_TO_M2;
1015 return YD2_TO_M2 / CM2_TO_M2;
1017 return YD2_TO_M2 / MM2_TO_M2;
1019 return YD2_TO_M2 / IN2_TO_M2;
1021 break;
1022 }
1023 break;
1024 }
1025
1027 {
1028 switch ( toUnit )
1029 {
1031 return MI2_TO_M2;
1033 return MI2_TO_M2 / KM2_TO_M2;
1035 return MI2_TO_M2 / FT2_TO_M2;
1037 return MI2_TO_M2 / YD2_TO_M2;
1039 return 1.0;
1041 return MI2_TO_M2 / HA_TO_M2;
1043 return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1045 return MI2_TO_M2 / NM2_TO_M2;
1047 return MI2_TO_M2 / DEG2_TO_M2;
1049 return MI2_TO_M2 / CM2_TO_M2;
1051 return MI2_TO_M2 / MM2_TO_M2;
1053 return MI2_TO_M2 / IN2_TO_M2;
1055 break;
1056 }
1057
1058 break;
1059 }
1060
1062 {
1063 switch ( toUnit )
1064 {
1066 return HA_TO_M2;
1068 return HA_TO_M2 / KM2_TO_M2;
1070 return HA_TO_M2 / FT2_TO_M2;
1072 return HA_TO_M2 / YD2_TO_M2;
1074 return HA_TO_M2 / MI2_TO_M2;
1076 return 1.0;
1078 return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1080 return HA_TO_M2 / NM2_TO_M2;
1082 return HA_TO_M2 / DEG2_TO_M2;
1084 return HA_TO_M2 / CM2_TO_M2;
1086 return HA_TO_M2 / MM2_TO_M2;
1088 return HA_TO_M2 / IN2_TO_M2;
1090 break;
1091 }
1092
1093 break;
1094 }
1095
1097 {
1098 switch ( toUnit )
1099 {
1101 return AC_TO_FT2 * FT2_TO_M2;
1103 return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
1105 return AC_TO_FT2;
1107 return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
1109 return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
1111 return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
1113 return 1.0;
1115 return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
1117 return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
1119 return AC_TO_FT2 * FT2_TO_M2 / CM2_TO_M2;
1121 return AC_TO_FT2 * FT2_TO_M2 / MM2_TO_M2;
1123 return AC_TO_FT2 * FT2_TO_M2 / IN2_TO_M2;
1125 break;
1126 }
1127
1128 break;
1129 }
1130
1132 {
1133 switch ( toUnit )
1134 {
1136 return NM2_TO_M2;
1138 return NM2_TO_M2 / KM2_TO_M2;
1140 return NM2_TO_M2 / FT2_TO_M2;
1142 return NM2_TO_M2 / YD2_TO_M2;
1144 return NM2_TO_M2 / MI2_TO_M2;
1146 return NM2_TO_M2 / HA_TO_M2;
1148 return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1150 return 1.0;
1152 return NM2_TO_M2 / DEG2_TO_M2;
1154 return NM2_TO_M2 / CM2_TO_M2;
1156 return NM2_TO_M2 / MM2_TO_M2;
1158 return NM2_TO_M2 / IN2_TO_M2;
1160 break;
1161 }
1162
1163 break;
1164 }
1165
1167 {
1168 switch ( toUnit )
1169 {
1171 return DEG2_TO_M2;
1173 return DEG2_TO_M2 / KM2_TO_M2;
1175 return DEG2_TO_M2 / FT2_TO_M2;
1177 return DEG2_TO_M2 / YD2_TO_M2;
1179 return DEG2_TO_M2 / MI2_TO_M2;
1181 return DEG2_TO_M2 / HA_TO_M2;
1183 return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
1185 return DEG2_TO_M2 / NM2_TO_M2;
1187 return 1.0;
1189 return DEG2_TO_M2 / CM2_TO_M2;
1191 return DEG2_TO_M2 / MM2_TO_M2;
1193 return DEG2_TO_M2 / IN2_TO_M2;
1195 break;
1196 }
1197
1198 break;
1199 }
1200
1202 {
1203 switch ( toUnit )
1204 {
1206 return MM2_TO_M2;
1208 return MM2_TO_M2 / KM2_TO_M2;
1210 return MM2_TO_M2 / FT2_TO_M2;
1212 return MM2_TO_M2 / YD2_TO_M2;
1214 return MM2_TO_M2 / MI2_TO_M2;
1216 return MM2_TO_M2 / HA_TO_M2;
1218 return MM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1220 return MM2_TO_M2 / NM2_TO_M2;
1222 return MM2_TO_M2 / DEG2_TO_M2;
1224 return MM2_TO_M2 / CM2_TO_M2;
1226 return 1.0;
1228 return MM2_TO_M2 / IN2_TO_M2;
1230 break;
1231 }
1232
1233 break;
1234 }
1236 {
1237 switch ( toUnit )
1238 {
1240 return CM2_TO_M2;
1242 return CM2_TO_M2 / KM2_TO_M2;
1244 return CM2_TO_M2 / FT2_TO_M2;
1246 return CM2_TO_M2 / YD2_TO_M2;
1248 return CM2_TO_M2 / MI2_TO_M2;
1250 return CM2_TO_M2 / HA_TO_M2;
1252 return CM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1254 return CM2_TO_M2 / NM2_TO_M2;
1256 return CM2_TO_M2 / DEG2_TO_M2;
1258 return 1.0;
1260 return CM2_TO_M2 / MM2_TO_M2;
1262 return CM2_TO_M2 / IN2_TO_M2;
1264 break;
1265 }
1266
1267 break;
1268 }
1270 {
1271 switch ( toUnit )
1272 {
1274 return IN2_TO_M2;
1276 return IN2_TO_M2 / KM2_TO_M2;
1278 return IN2_TO_M2 / FT2_TO_M2;
1280 return IN2_TO_M2 / YD2_TO_M2;
1282 return IN2_TO_M2 / MI2_TO_M2;
1284 return IN2_TO_M2 / HA_TO_M2;
1286 return IN2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
1288 return IN2_TO_M2 / NM2_TO_M2;
1290 return IN2_TO_M2 / DEG2_TO_M2;
1292 return IN2_TO_M2 / CM2_TO_M2;
1294 return IN2_TO_M2 / MM2_TO_M2;
1296 return 1;
1298 break;
1299 }
1300
1301 break;
1302 }
1304 break;
1305 }
1306 return 1.0;
1307}
1308
1349
1392
1394{
1395 switch ( unit )
1396 {
1398 return QStringLiteral( "s" );
1400 return QStringLiteral( "ms" );
1402 return QStringLiteral( "min" );
1404 return QStringLiteral( "h" );
1406 return QStringLiteral( "d" );
1408 return QStringLiteral( "wk" );
1410 return QStringLiteral( "mon" );
1412 return QStringLiteral( "y" );
1414 return QStringLiteral( "dec" );
1416 return QStringLiteral( "c" );
1418 return QStringLiteral( "xxx" );
1420 return QStringLiteral( "<unknown>" );
1421 }
1422 return QString();
1423}
1424
1426{
1427 const QString normalized = string.trimmed().toLower();
1428
1429 if ( ok )
1430 *ok = true;
1431
1432 if ( normalized == encodeUnit( Qgis::TemporalUnit::Seconds ) )
1434 if ( normalized == encodeUnit( Qgis::TemporalUnit::Milliseconds ) )
1436 if ( normalized == encodeUnit( Qgis::TemporalUnit::Minutes ) )
1438 if ( normalized == encodeUnit( Qgis::TemporalUnit::Hours ) )
1440 if ( normalized == encodeUnit( Qgis::TemporalUnit::Days ) )
1442 if ( normalized == encodeUnit( Qgis::TemporalUnit::Weeks ) )
1444 if ( normalized == encodeUnit( Qgis::TemporalUnit::Months ) )
1446 if ( normalized == encodeUnit( Qgis::TemporalUnit::Years ) )
1448 if ( normalized == encodeUnit( Qgis::TemporalUnit::Decades ) )
1450 if ( normalized == encodeUnit( Qgis::TemporalUnit::Centuries ) )
1452 if ( normalized == encodeUnit( Qgis::TemporalUnit::IrregularStep ) )
1454 if ( normalized == encodeUnit( Qgis::TemporalUnit::Unknown ) )
1456
1457 if ( ok )
1458 *ok = false;
1459
1461}
1462
1464{
1465 switch ( unit )
1466 {
1468 return QObject::tr( "seconds", "temporal" );
1470 return QObject::tr( "milliseconds", "temporal" );
1472 return QObject::tr( "minutes", "temporal" );
1474 return QObject::tr( "hours", "temporal" );
1476 return QObject::tr( "days", "temporal" );
1478 return QObject::tr( "weeks", "temporal" );
1480 return QObject::tr( "months", "temporal" );
1482 return QObject::tr( "years", "temporal" );
1484 return QObject::tr( "decades", "temporal" );
1486 return QObject::tr( "centuries", "temporal" );
1488 return QObject::tr( "steps", "temporal" );
1490 return QObject::tr( "<unknown>", "temporal" );
1491 }
1492 return QString();
1493}
1494
1496{
1497 switch ( unit )
1498 {
1500 return QObject::tr( "s", "temporal" );
1502 return QObject::tr( "ms", "temporal" );
1504 return QObject::tr( "min", "temporal" );
1506 return QObject::tr( "h", "temporal" );
1508 return QObject::tr( "d", "temporal" );
1510 return QObject::tr( "wk", "temporal" );
1512 return QObject::tr( "mon", "temporal" );
1514 return QObject::tr( "y", "temporal" );
1516 return QObject::tr( "dec", "temporal" );
1518 return QObject::tr( "cen", "temporal" );
1520 return QObject::tr( "steps", "temporal" );
1522 return QObject::tr( "<unknown>", "temporal" );
1523 }
1524 return QString();
1525}
1526
1528{
1529 const QString normalized = string.trimmed().toLower();
1530
1531 if ( ok )
1532 *ok = true;
1533
1534 if ( normalized == toString( Qgis::TemporalUnit::Seconds ) )
1536 if ( normalized == toString( Qgis::TemporalUnit::Milliseconds ) )
1538 if ( normalized == toString( Qgis::TemporalUnit::Minutes ) )
1540 if ( normalized == toString( Qgis::TemporalUnit::Hours ) )
1542 if ( normalized == toString( Qgis::TemporalUnit::Days ) )
1544 if ( normalized == toString( Qgis::TemporalUnit::Weeks ) )
1546 if ( normalized == toString( Qgis::TemporalUnit::Months ) )
1548 if ( normalized == toString( Qgis::TemporalUnit::Years ) )
1550 if ( normalized == toString( Qgis::TemporalUnit::Decades ) )
1552 if ( normalized == toString( Qgis::TemporalUnit::Centuries ) )
1554 if ( normalized == toString( Qgis::TemporalUnit::IrregularStep ) )
1556 if ( normalized == toString( Qgis::TemporalUnit::Unknown ) )
1558
1559 if ( ok )
1560 *ok = false;
1561
1563}
1564
1566{
1567 switch ( fromUnit )
1568 {
1570 {
1571 switch ( toUnit )
1572 {
1574 return 1.0;
1576 return 1000.0;
1578 return 1 / 60.0;
1580 return 1 / 3600.0;
1582 return 1 / 86400.0;
1584 return 1 / 604800.0;
1586 return 1 / 2592000.0;
1588 return 1 / 31557600.0;
1590 return 1 / 315576000.0;
1592 return 1 / 3155760000.0;
1595 return 1.0;
1596 }
1597 break;
1598 }
1600 {
1601 switch ( toUnit )
1602 {
1604 return 1 / 1000.0;
1606 return 1.0;
1608 return 1 / 60000.0;
1610 return 1 / 3600000.0;
1612 return 1 / 86400000.0;
1614 return 1 / 60480000.0;
1616 return 1 / 259200000.0;
1618 return 1 / 3155760000.0;
1620 return 1 / 31557600000.0;
1622 return 1 / 315576000000.0;
1625 return 1.0;
1626 }
1627 break;
1628 }
1630 {
1631 switch ( toUnit )
1632 {
1634 return 60.0;
1636 return 60000.0;
1638 return 1;
1640 return 1 / 60.0;
1642 return 1 / 1440.0;
1644 return 1 / 10080.0;
1646 return 1 / 43200.0;
1648 return 1 / 525960.0;
1650 return 1 / 5259600.0;
1652 return 1 / 52596000.0;
1655 return 1.0;
1656 }
1657 break;
1658 }
1660 {
1661 switch ( toUnit )
1662 {
1664 return 3600.0;
1666 return 3600000.0;
1668 return 60;
1670 return 1;
1672 return 1 / 24.0;
1674 return 1 / 168.0;
1676 return 1 / 720.0;
1678 return 1 / 8766.0;
1680 return 1 / 87660.0;
1682 return 1 / 876600.0;
1685 return 1.0;
1686 }
1687 break;
1688 }
1690 {
1691 switch ( toUnit )
1692 {
1694 return 86400.0;
1696 return 86400000.0;
1698 return 1440;
1700 return 24;
1702 return 1;
1704 return 1 / 7.0;
1706 return 1 / 30.0;
1708 return 1 / 365.25;
1710 return 1 / 3652.5;
1712 return 1 / 36525.0;
1715 return 1.0;
1716 }
1717 break;
1718 }
1720 {
1721 switch ( toUnit )
1722 {
1724 return 604800.0;
1726 return 604800000.0;
1728 return 10080;
1730 return 168;
1732 return 7;
1734 return 1;
1736 return 7 / 30.0;
1738 return 7 / 365.25;
1740 return 7 / 3652.5;
1742 return 7 / 36525.0;
1745 return 1.0;
1746 }
1747 break;
1748 }
1750 {
1751 switch ( toUnit )
1752 {
1754 return 2592000.0;
1756 return 2592000000.0;
1758 return 43200;
1760 return 720;
1762 return 30;
1764 return 30 / 7.0;
1766 return 1;
1768 return 30 / 365.25;
1770 return 30 / 3652.5;
1772 return 30 / 36525.0;
1775 return 1.0;
1776 }
1777 break;
1778 }
1780 {
1781 switch ( toUnit )
1782 {
1784 return 31557600.0;
1786 return 31557600000.0;
1788 return 525960.0;
1790 return 8766.0;
1792 return 365.25;
1794 return 365.25 / 7.0;
1796 return 365.25 / 30.0;
1798 return 1;
1800 return 0.1;
1802 return 0.01;
1805 return 1.0;
1806 }
1807 break;
1808 }
1810 {
1811 switch ( toUnit )
1812 {
1814 return 315576000.0;
1816 return 315576000000.0;
1818 return 5259600.0;
1820 return 87660.0;
1822 return 3652.5;
1824 return 3652.5 / 7.0;
1826 return 3652.5 / 30.0;
1828 return 10;
1830 return 1;
1832 return 0.1;
1835 return 1.0;
1836 }
1837 break;
1838 }
1839
1841 {
1842 switch ( toUnit )
1843 {
1845 return 3155760000.0;
1847 return 3155760000000.0;
1849 return 52596000.0;
1851 return 876600.0;
1853 return 36525;
1855 return 36525 / 7.0;
1857 return 36525 / 30.0;
1859 return 100;
1861 return 10;
1863 return 1;
1866 return 1.0;
1867 }
1868 break;
1869 }
1870
1873 {
1874 return 1.0;
1875 }
1876 }
1877 return 1.0;
1878}
1879
1880Qgis::VolumeUnit QgsUnitTypes::decodeVolumeUnit( const QString &string, bool *ok )
1881{
1882 const QString normalized = string.trimmed().toLower();
1883
1884 if ( ok )
1885 *ok = true;
1886
1887 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicMeters ) )
1889 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicFeet ) )
1891 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicYards ) )
1893 if ( normalized == encodeUnit( Qgis::VolumeUnit::Barrel ) )
1895 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicDecimeter ) )
1897 if ( normalized == encodeUnit( Qgis::VolumeUnit::Liters ) )
1899 if ( normalized == encodeUnit( Qgis::VolumeUnit::GallonUS ) )
1901 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicInch ) )
1903 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicCentimeter ) )
1905 if ( normalized == encodeUnit( Qgis::VolumeUnit::CubicDegrees ) )
1907 if ( normalized == encodeUnit( Qgis::VolumeUnit::Unknown ) )
1909
1910 if ( ok )
1911 *ok = false;
1912
1914}
1915
1917{
1918 switch ( unit )
1919 {
1921 return QObject::tr( "cubic meters", "volume" );
1923 return QObject::tr( "cubic feet", "volume" );
1925 return QObject::tr( "cubic yards", "volume" );
1927 return QObject::tr( "barrels", "volume" );
1929 return QObject::tr( "cubic decimeters", "volume" );
1931 return QObject::tr( "liters", "volume" );
1933 return QObject::tr( "gallons", "volume" );
1935 return QObject::tr( "cubic inches", "volume" );
1937 return QObject::tr( "cubic centimeters", "volume" );
1939 return QObject::tr( "cubic degrees", "volume" );
1941 return QObject::tr( "<unknown>", "volume" );
1942 }
1943 return QString();
1944}
1945
1947{
1948 switch ( unit )
1949 {
1951 return QObject::tr( "m³", "volume" );
1953 return QObject::tr( "ft³", "volume" );
1955 return QObject::tr( "yds³", "volume" );
1957 return QObject::tr( "bbl", "volume" );
1959 return QObject::tr( "dm³", "volume" );
1961 return QObject::tr( "l", "volume" );
1963 return QObject::tr( "gal", "volume" );
1965 return QObject::tr( "in³", "volume" );
1967 return QObject::tr( "cm³", "volume" );
1969 return QObject::tr( "deg³", "volume" );
1971 return QObject::tr( "<unknown>", "volume" );
1972 }
1973 return QString();
1974
1975}
1976
1977Qgis::VolumeUnit QgsUnitTypes::stringToVolumeUnit( const QString &string, bool *ok )
1978{
1979 const QString normalized = string.trimmed().toLower();
1980
1981 if ( ok )
1982 *ok = true;
1983
1984 if ( normalized == toString( Qgis::VolumeUnit::CubicMeters ) )
1986 if ( normalized == toString( Qgis::VolumeUnit::CubicFeet ) )
1988 if ( normalized == toString( Qgis::VolumeUnit::CubicYards ) )
1990 if ( normalized == toString( Qgis::VolumeUnit::Barrel ) )
1992 if ( normalized == toString( Qgis::VolumeUnit::CubicDecimeter ) )
1994 if ( normalized == toString( Qgis::VolumeUnit::Liters ) )
1996 if ( normalized == toString( Qgis::VolumeUnit::GallonUS ) )
1998 if ( normalized == toString( Qgis::VolumeUnit::CubicInch ) )
2000 if ( normalized == toString( Qgis::VolumeUnit::CubicCentimeter ) )
2002 if ( normalized == toString( Qgis::VolumeUnit::CubicDegrees ) )
2004 if ( normalized == toString( Qgis::VolumeUnit::Unknown ) )
2006
2007 if ( ok )
2008 *ok = false;
2009
2011}
2012
2013#define DEG2_TO_M3 1379474361572186.2
2015{
2016 switch ( fromUnit )
2017 {
2019 {
2020 switch ( toUnit )
2021 {
2023 return 1.0;
2025 return 35.314666572222;
2027 return 1.307950613786;
2029 return 6.2898107438466;
2031 return 1000;
2033 return 1000;
2035 return 264.17205124156;
2037 return 61023.7438368;
2039 return 1000000;
2041 return 1 / DEG2_TO_M3; // basically meaningless!
2043 return 1.0;
2044 }
2045 break;
2046 }
2048 {
2049 switch ( toUnit )
2050 {
2052 return 0.028316846592;
2054 return 1.0;
2056 return 0.037037037;
2058 return 0.178107622;
2060 return 28.31685;
2062 return 28.31685;
2064 return 7.480519954;
2066 return 1728.000629765;
2068 return 28316.85;
2070 return 0.028316846592 / DEG2_TO_M3; // basically meaningless!
2072 return 1.0;
2073 }
2074 break;
2075 }
2077 {
2078 switch ( toUnit )
2079 {
2081 return 0.764554900;
2083 return 26.999998234;
2085 return 1.0;
2087 return 4.808905491;
2089 return 764.5549;
2091 return 764.5549;
2093 return 201.974025549;
2095 return 46656.013952472;
2097 return 764554.9;
2099 return 0.764554900 / DEG2_TO_M3; // basically meaningless!
2101 return 1.0;
2102 }
2103 break;
2104 }
2106 {
2107 switch ( toUnit )
2108 {
2110 return 0.158987300;
2112 return 5.614582837;
2114 return 0.207947526;
2116 return 1.0;
2118 return 158.9873;
2120 return 158.9873;
2122 return 41.999998943;
2124 return 9702.002677722;
2126 return 158987.3;
2128 return 0.158987300 / DEG2_TO_M3; // basically meaningless!
2130 return 1.0;
2131 }
2132 break;
2133 }
2136 {
2137 switch ( toUnit )
2138 {
2140 return 0.001;
2142 return 0.035314662;
2144 return 0.001307951;
2146 return 0.006289811;
2149 return 1.0;
2151 return 0.264172037;
2153 return 61.023758990;
2155 return 1000;
2157 return 0.001 / DEG2_TO_M3; // basically meaningless!
2159 return 1.0;
2160 }
2161 break;
2162 }
2164 {
2165 switch ( toUnit )
2166 {
2168 return 0.003785412;
2170 return 0.133680547;
2172 return 0.004951132;
2174 return 0.023809524;
2177 return 3.785412000;
2179 return 1.0;
2181 return 231.000069567;
2183 return 3785.412;
2185 return 0.003785412 / DEG2_TO_M3; // basically meaningless!
2187 return 1.0;
2188 }
2189 break;
2190 }
2192 {
2193 switch ( toUnit )
2194 {
2196 return 0.000016387;
2198 return 0.000578703;
2200 return 0.000021433;
2202 return 0.000103072;
2205 return 0.016387060;
2207 return 0.004329003;
2209 return 1.0;
2211 return 16.387060000;
2213 return 0.000016387 / DEG2_TO_M3; // basically meaningless!
2215 return 1.0;
2216 }
2217 break;
2218 }
2220 {
2221 switch ( toUnit )
2222 {
2224 return 0.000001;
2226 return 0.000035315;
2228 return 0.000001308;
2230 return 0.000006290;
2233 return 0.001;
2235 return 0.000264172 ;
2237 return 0.061023759;
2239 return 1.0;
2241 return 0.000001 / DEG2_TO_M3; // basically meaningless!
2243 return 1.0;
2244 }
2245 break;
2246 }
2248 if ( toUnit == Qgis::VolumeUnit::Unknown || toUnit == Qgis::VolumeUnit::CubicDegrees )
2249 return 1.0;
2250 else
2252
2254 {
2255 return 1.0;
2256 }
2257 }
2258 return 1.0;
2259}
2260
2301
2331
2353
2355{
2356 switch ( unit )
2357 {
2359 return QStringLiteral( "m3" );
2361 return QStringLiteral( "ft3" );
2363 return QStringLiteral( "yd3" );
2365 return QStringLiteral( "bbl" );
2367 return QStringLiteral( "dm3" );
2369 return QStringLiteral( "l" );
2371 return QStringLiteral( "gal" );
2373 return QStringLiteral( "in3" );
2375 return QStringLiteral( "cm3" );
2377 return QStringLiteral( "deg3" );
2379 return QStringLiteral( "<unknown>" );
2380 }
2381 return QString();
2382}
2383
2385{
2386 switch ( unit )
2387 {
2389 return QStringLiteral( "degrees" );
2391 return QStringLiteral( "radians" );
2393 return QStringLiteral( "gon" );
2395 return QStringLiteral( "moa" );
2397 return QStringLiteral( "soa" );
2399 return QStringLiteral( "tr" );
2401 return QStringLiteral( "milliradians" );
2403 return QStringLiteral( "mil" );
2405 return QStringLiteral( "<unknown>" );
2406 }
2407 return QString();
2408}
2409
2410Qgis::AngleUnit QgsUnitTypes::decodeAngleUnit( const QString &string, bool *ok )
2411{
2412 const QString normalized = string.trimmed().toLower();
2413
2414 if ( ok )
2415 *ok = true;
2416
2417 if ( normalized == encodeUnit( Qgis::AngleUnit::Degrees ) )
2419 if ( normalized == encodeUnit( Qgis::AngleUnit::Radians ) )
2421 if ( normalized == encodeUnit( Qgis::AngleUnit::Gon ) )
2422 return Qgis::AngleUnit::Gon;
2423 if ( normalized == encodeUnit( Qgis::AngleUnit::MinutesOfArc ) )
2425 if ( normalized == encodeUnit( Qgis::AngleUnit::SecondsOfArc ) )
2427 if ( normalized == encodeUnit( Qgis::AngleUnit::Turn ) )
2428 return Qgis::AngleUnit::Turn;
2429 if ( normalized == encodeUnit( Qgis::AngleUnit::MilliradiansSI ) )
2431 if ( normalized == encodeUnit( Qgis::AngleUnit::MilNATO ) )
2433 if ( normalized == encodeUnit( Qgis::AngleUnit::Unknown ) )
2435 if ( ok )
2436 *ok = false;
2437
2439}
2440
2442{
2443 switch ( unit )
2444 {
2446 return QObject::tr( "degrees", "angle" );
2448 return QObject::tr( "radians", "angle" );
2450 return QObject::tr( "gon", "angle" );
2452 return QObject::tr( "minutes of arc", "angle" );
2454 return QObject::tr( "seconds of arc", "angle" );
2456 return QObject::tr( "turns", "angle" );
2458 return QObject::tr( "milliradians", "angle" );
2460 return QObject::tr( "mil", "angle" );
2462 return QObject::tr( "<unknown>", "angle" );
2463 }
2464 return QString();
2465}
2466
2468{
2469 // Calculate the conversion factor between the specified units
2470 switch ( fromUnit )
2471 {
2473 {
2474 switch ( toUnit )
2475 {
2477 return 1.0;
2479 return M_PI / 180.0;
2481 return 400.0 / 360.0;
2483 return 60;
2485 return 3600;
2487 return 1.0 / 360.0;
2489 return M_PI / 180.0 * 1000;
2491 return 3200.0 / 180;
2493 break;
2494 }
2495 break;
2496 }
2498 {
2499 switch ( toUnit )
2500 {
2502 return 180.0 / M_PI;
2504 return 1.0;
2506 return 200.0 / M_PI;
2508 return 60 * 180.0 / M_PI;
2510 return 3600 * 180.0 / M_PI;
2512 return 0.5 / M_PI;
2514 return 1000;
2516 return 3200.0 / M_PI;
2518 break;
2519 }
2520 break;
2521 }
2523 {
2524 switch ( toUnit )
2525 {
2527 return 360.0 / 400.0;
2529 return M_PI / 200.0;
2531 return 1.0;
2533 return 60 * 360.0 / 400.0;
2535 return 3600 * 360.0 / 400.0;
2537 return 1.0 / 400.0;
2539 return M_PI / 200.0 * 1000;
2541 return 3200.0 / 200.0;
2543 break;
2544 }
2545 break;
2546 }
2548 {
2549 switch ( toUnit )
2550 {
2552 return 1 / 60.0;
2554 return M_PI / 180.0 / 60.0;
2556 return 400.0 / 360.0 / 60.0;
2558 return 1.0;
2560 return 60.0;
2562 return 1.0 / 360.0 / 60.0;
2564 return M_PI / 180.0 / 60.0 * 1000;
2566 return 3200.0 / 180.0 / 60.0;
2568 break;
2569 }
2570 break;
2571 }
2573 {
2574 switch ( toUnit )
2575 {
2577 return 1 / 3600.0;
2579 return M_PI / 180.0 / 3600.0;
2581 return 400.0 / 360.0 / 3600.0;
2583 return 1.0 / 60.0;
2585 return 1.0;
2587 return 1.0 / 360.0 / 3600.0;
2589 return M_PI / 180.0 / 3600.0 * 1000;
2591 return 3200.0 / 180.0 / 3600.0;
2593 break;
2594 }
2595 break;
2596 }
2598 {
2599 switch ( toUnit )
2600 {
2602 return 360.0;
2604 return 2 * M_PI;
2606 return 400.0;
2608 return 360.0 * 60.0;
2610 return 360.0 * 3600.0;
2612 return 1.0;
2614 return 2 * M_PI * 1000;
2616 return 2 * 3200;
2618 break;
2619 }
2620 break;
2621 }
2623 {
2624 switch ( toUnit )
2625 {
2627 return 180.0 / M_PI / 1000;
2629 return 0.001;
2631 return 200.0 / M_PI / 1000;
2633 return 180.0 * 60.0 / M_PI / 1000;
2635 return 180.0 * 3600.0 / M_PI / 1000;
2637 return M_PI / 2 / 1000;
2639 return 1.0;
2641 return 3200.0 / 1000.0 / M_PI;
2643 break;
2644 }
2645 break;
2646 }
2647
2649 {
2650 switch ( toUnit )
2651 {
2653 return 180.0 / 3200;
2655 return M_PI / 3200;
2657 return 200.0 / 3200;
2659 return 60 * 180.0 / 3200;
2661 return 3600.0 * 180 / 3200;
2663 return 1.0 / ( 2 * 32000 );
2665 return 1000.0 * M_PI / 3200.0;
2667 return 1.0;
2669 break;
2670 }
2671 break;
2672 }
2673
2675 break;
2676 }
2677 return 1.0;
2678}
2679
2680QString QgsUnitTypes::formatAngle( double angle, int decimals, Qgis::AngleUnit unit )
2681{
2682 QString unitLabel;
2683 int decimalPlaces = 2;
2684
2685 switch ( unit )
2686 {
2688 unitLabel = QObject::tr( "°", "angle" );
2689 decimalPlaces = 0;
2690 break;
2692 unitLabel = QObject::tr( " rad", "angle" );
2693 decimalPlaces = 2;
2694 break;
2696 unitLabel = QObject::tr( " gon", "angle" );
2697 decimalPlaces = 0;
2698 break;
2700 unitLabel = QObject::tr( "′", "angle minutes" );
2701 decimalPlaces = 0;
2702 break;
2704 unitLabel = QObject::tr( "″", "angle seconds" );
2705 decimalPlaces = 0;
2706 break;
2708 unitLabel = QObject::tr( " tr", "angle turn" );
2709 decimalPlaces = 3;
2710 break;
2712 unitLabel = QObject::tr( " millirad", "angular mil SI" );
2713 decimalPlaces = 0;
2714 break;
2716 unitLabel = QObject::tr( " mil", "angular mil NATO" );
2717 decimalPlaces = 0;
2718 break;
2720 break;
2721 }
2722
2723 if ( decimals >= 0 )
2724 decimalPlaces = decimals;
2725
2726 return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimalPlaces ).arg( unitLabel );
2727}
2728
2729QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, Qgis::DistanceUnit unit, int decimals, bool keepBaseUnit )
2730{
2731 DistanceValue result;
2732
2733 switch ( unit )
2734 {
2736 if ( keepBaseUnit )
2737 {
2738 result.value = qgsRound( distance, decimals );
2740 }
2741 else if ( std::fabs( distance ) > 1000.0 )
2742 {
2743 result.value = qgsRound( distance / 1000, decimals );
2745 }
2746 else if ( std::fabs( distance ) < 0.01 )
2747 {
2748 result.value = qgsRound( distance * 1000, decimals );
2750 }
2751 else if ( std::fabs( distance ) < 0.1 )
2752 {
2753
2754 result.value = qgsRound( distance * 100, decimals );
2756 }
2757 else
2758 {
2759 result.value = qgsRound( distance, decimals );
2761 }
2762 break;
2763
2765 if ( keepBaseUnit || std::fabs( distance ) >= 1.0 )
2766 {
2767 result.value = qgsRound( distance, decimals );
2769 }
2770 else
2771 {
2772 result.value = qgsRound( distance * 1000, decimals );
2774 }
2775 break;
2776
2778 if ( std::fabs( distance ) <= 5280.0 || keepBaseUnit )
2779 {
2780 result.value = qgsRound( distance, decimals );
2782 }
2783 else
2784 {
2785 result.value = qgsRound( distance / 5280.0, decimals );
2787 }
2788 break;
2789
2791 if ( std::fabs( distance ) <= 1760.0 || keepBaseUnit )
2792 {
2793 result.value = qgsRound( distance, decimals );
2795 }
2796 else
2797 {
2798 result.value = qgsRound( distance / 1760.0, decimals );
2800 }
2801 break;
2802
2804 if ( std::fabs( distance ) >= 1.0 || keepBaseUnit )
2805 {
2806 result.value = qgsRound( distance, decimals );
2808 }
2809 else
2810 {
2811 result.value = qgsRound( distance * 5280.0, decimals );
2813 }
2814 break;
2815
2817 result.value = qgsRound( distance, decimals );
2819 break;
2820
2822 result.value = qgsRound( distance, decimals );
2824 break;
2825
2827 result.value = qgsRound( distance, decimals );
2829 break;
2830
2831 default:
2832 result.value = qgsRound( distance, decimals );
2833 result.unit = unit;
2834 break;
2835 }
2836
2837 return result;
2838}
2839
2840QgsUnitTypes::AreaValue QgsUnitTypes::scaledArea( double area, Qgis::AreaUnit unit, int decimals, bool keepBaseUnit )
2841{
2843 result.value = -1.0;
2845
2846 // If we are not forced to keep the base units, switch to meter calculation
2848 {
2849 if ( keepBaseUnit )
2850 {
2851 result.value = qgsRound( area, decimals );
2853 }
2854 else
2855 {
2856 area /= 1000000.0;
2858 }
2859 }
2860 else if ( unit == Qgis::AreaUnit::SquareCentimeters )
2861 {
2862 if ( keepBaseUnit )
2863 {
2864 result.value = qgsRound( area, decimals );
2866 }
2867 else
2868 {
2869 area /= 10000.0;
2871 }
2872 }
2873
2874 switch ( unit )
2875 {
2877 // handled in the if above
2878 break;
2880 // handled in the if above
2881 break;
2883 {
2884 if ( keepBaseUnit )
2885 {
2886 result.value = qgsRound( area, decimals );
2888 }
2890 {
2893 }
2895 {
2898 }
2899 else
2900 {
2901 result.value = qgsRound( area, decimals );
2903 }
2904 break;
2905 }
2906
2908 {
2909 result.value = qgsRound( area, decimals );
2911 break;
2912 }
2913
2915 {
2916 result.value = qgsRound( area, decimals );
2918 break;
2919 }
2920
2922 {
2923 if ( keepBaseUnit )
2924 {
2925 result.value = qgsRound( area, decimals );
2927 }
2929 {
2932 }
2933 else
2934 {
2935 result.value = qgsRound( area, decimals );
2937 }
2938 break;
2939 }
2940
2942 {
2943 if ( keepBaseUnit )
2944 {
2945 result.value = qgsRound( area, decimals );
2947 }
2949 {
2952 }
2953 else
2954 {
2955 result.value = qgsRound( area, decimals );
2957 }
2958 break;
2959 }
2960
2962 {
2963 result.value = qgsRound( area, decimals );
2965 break;
2966 }
2967
2969 {
2970 if ( keepBaseUnit )
2971 {
2972 result.value = qgsRound( area, decimals );
2974 }
2976 {
2979 }
2980 else
2981 {
2982 result.value = qgsRound( area, decimals );
2984 }
2985 break;
2986 }
2987
2989 {
2990 if ( keepBaseUnit )
2991 {
2992 result.value = qgsRound( area, decimals );
2993 result.unit = Qgis::AreaUnit::Acres;
2994 }
2996 {
2999 }
3000 else
3001 {
3002 result.value = qgsRound( area, decimals );
3003 result.unit = Qgis::AreaUnit::Acres;
3004 }
3005 break;
3006 }
3007
3009 {
3010 result.value = qgsRound( area, decimals );
3012 break;
3013 }
3014
3016 {
3017 result.value = qgsRound( area, decimals );
3019 break;
3020 }
3021
3023 {
3024 result.value = qgsRound( area, decimals );
3026 break;
3027 }
3028 }
3029 return result;
3030}
3031
3032
3033QString QgsUnitTypes::formatDistance( double distance, int decimals, Qgis::DistanceUnit unit, bool keepBaseUnit )
3034{
3035 const DistanceValue dist = scaledDistance( distance, unit, decimals, keepBaseUnit );
3036
3037 QString unitText;
3038
3039 if ( dist.unit != Qgis::DistanceUnit::Unknown )
3040 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( dist.unit );
3041
3042 if ( qgsDoubleNear( dist.value, 0 ) )
3043 {
3044 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( unit );
3045 return QStringLiteral( "%L1%2" ).arg( distance, 0, 'e', decimals ).arg( unitText );
3046 }
3047 else
3048 {
3049 return QStringLiteral( "%L1%2" ).arg( dist.value, 0, 'f', decimals ).arg( unitText );
3050 }
3051}
3052
3053QString QgsUnitTypes::formatArea( double area, int decimals, Qgis::AreaUnit unit, bool keepBaseUnit )
3054{
3055 const QgsUnitTypes::AreaValue areaValue = scaledArea( area, unit, decimals, keepBaseUnit );
3056
3057 QString unitText;
3058
3059 if ( areaValue.unit != Qgis::AreaUnit::Unknown )
3060 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( areaValue.unit );
3061
3062 if ( qgsDoubleNear( areaValue.value, 0 ) )
3063 {
3064 unitText = QChar( ' ' ) + QgsUnitTypes::toAbbreviatedString( unit );
3065 return QStringLiteral( "%L1%2" ).arg( area, 0, 'e', decimals ).arg( unitText );
3066 }
3067 else
3068 {
3069 return QStringLiteral( "%L1%2" ).arg( areaValue.value, 0, 'f', decimals ).arg( unitText );
3070 }
3071}
3072
3074{
3075 switch ( unit )
3076 {
3078 return QStringLiteral( "MM" );
3080 return QStringLiteral( "RenderMetersInMapUnits" );
3082 return QStringLiteral( "MapUnit" );
3084 return QStringLiteral( "Pixel" );
3086 return QStringLiteral( "Percentage" );
3088 return QStringLiteral( "Point" );
3090 return QStringLiteral( "Inch" );
3092 return QString();
3093 }
3094 return QString();
3095}
3096
3097Qgis::RenderUnit QgsUnitTypes::decodeRenderUnit( const QString &string, bool *ok )
3098{
3099 const QString normalized = string.trimmed().toLower();
3100
3101 if ( ok )
3102 *ok = true;
3103
3104 if ( normalized == encodeUnit( Qgis::RenderUnit::Millimeters ).toLower() )
3106 if ( normalized == encodeUnit( Qgis::RenderUnit::MetersInMapUnits ).toLower() )
3108 if ( normalized == QLatin1String( "meters" ) )
3110 if ( normalized == encodeUnit( Qgis::RenderUnit::MapUnits ).toLower() )
3112 if ( normalized == QLatin1String( "mapunits" ) )
3114 if ( normalized == encodeUnit( Qgis::RenderUnit::Pixels ).toLower() )
3116 if ( normalized == encodeUnit( Qgis::RenderUnit::Percentage ).toLower() )
3118 if ( normalized == QLatin1String( "percent" ) )
3120 if ( normalized == encodeUnit( Qgis::RenderUnit::Points ).toLower() )
3122 if ( normalized == QLatin1String( "points" ) )
3124 if ( normalized == encodeUnit( Qgis::RenderUnit::Inches ).toLower() )
3126
3127 if ( ok )
3128 *ok = false;
3129
3130 // millimeters are default
3132}
3133
3135{
3136 switch ( unit )
3137 {
3139 return QObject::tr( "millimeters", "render" );
3140
3142 return QObject::tr( "meters at scale", "render" );
3143
3145 return QObject::tr( "map units", "render" );
3146
3148 return QObject::tr( "pixels", "render" );
3149
3151 return QObject::tr( "percent", "render" );
3152
3154 return QObject::tr( "points", "render" );
3155
3157 return QObject::tr( "inches", "render" );
3158
3160 return QObject::tr( "<unknown>", "render" );
3161
3162 }
3163 return QString();
3164}
3165
3166
3167
3169{
3170 switch ( unit )
3171 {
3173 return QStringLiteral( "cm" );
3175 return QStringLiteral( "m" );
3177 return QStringLiteral( "in" );
3179 return QStringLiteral( "ft" );
3181 return QStringLiteral( "pt" );
3183 return QStringLiteral( "pi" );
3185 return QStringLiteral( "px" );
3187 return QStringLiteral( "mm" );
3188 }
3189 return QString();
3190}
3191
3192Qgis::LayoutUnit QgsUnitTypes::decodeLayoutUnit( const QString &string, bool *ok )
3193{
3194 const QString normalized = string.trimmed().toLower();
3195
3196 if ( ok )
3197 *ok = true;
3198
3199 if ( normalized == encodeUnit( Qgis::LayoutUnit::Millimeters ).toLower() )
3201 if ( normalized == encodeUnit( Qgis::LayoutUnit::Centimeters ).toLower() )
3203 if ( normalized == encodeUnit( Qgis::LayoutUnit::Meters ).toLower() )
3205 if ( normalized == encodeUnit( Qgis::LayoutUnit::Inches ).toLower() )
3207 if ( normalized == encodeUnit( Qgis::LayoutUnit::Feet ).toLower() )
3209 if ( normalized == encodeUnit( Qgis::LayoutUnit::Points ).toLower() )
3211 if ( normalized == encodeUnit( Qgis::LayoutUnit::Picas ).toLower() )
3213 if ( normalized == encodeUnit( Qgis::LayoutUnit::Pixels ).toLower() )
3215
3216 if ( ok )
3217 *ok = false;
3218
3219 // millimeters are default
3221}
3222
3242
3244{
3245 switch ( unit )
3246 {
3248 return QObject::tr( "px" );
3250 return QObject::tr( "mm" );
3252 return QObject::tr( "cm" );
3254 return QObject::tr( "m" );
3256 return QObject::tr( "in", "unit inch" );
3258 return QObject::tr( "ft" );
3260 return QObject::tr( "pt" );
3262 return QObject::tr( "pica" );
3263 }
3264 return QString(); // no warnings
3265}
3266
3268{
3269 switch ( unit )
3270 {
3272 return QObject::tr( "pixels" );
3274 return QObject::tr( "millimeters" );
3276 return QObject::tr( "centimeters" );
3278 return QObject::tr( "meters" );
3280 return QObject::tr( "inches" );
3282 return QObject::tr( "feet" );
3284 return QObject::tr( "points" );
3286 return QObject::tr( "picas" );
3287 }
3288 return QString(); // no warnings
3289}
DistanceUnitType
Types of distance units.
Definition qgis.h:4085
@ Geographic
Unit is a geographic (e.g., degree based) unit.
@ Unknown
Unknown unit type.
@ Standard
Unit is a standard measurement unit.
UnitType
Unit types.
Definition qgis.h:4045
@ Distance
Distance unit.
@ Temporal
Temporal unit.
@ Unknown
Unknown unit type.
@ Volume
Volume unit.
@ Area
Area unit.
LayoutUnit
Layout measurement units.
Definition qgis.h:4213
@ Centimeters
Centimeters.
@ Millimeters
Millimeters.
@ Points
Typographic points.
@ Picas
Typographic picas.
AngleUnit
Units of angles.
Definition qgis.h:4148
@ SecondsOfArc
Seconds of arc.
@ Radians
Square kilometers.
@ Turn
Turn/revolutions.
@ MinutesOfArc
Minutes of arc.
@ Unknown
Unknown angle unit.
@ MilliradiansSI
Angular milliradians (SI definition, 1/1000 of radian)
@ Degrees
Degrees.
@ Gon
Gon/gradian.
@ MilNATO
Angular mil (NATO definition, 6400 mil = 2PI radians)
DistanceUnit
Units of distance.
Definition qgis.h:4062
@ Feet
Imperial feet.
@ Centimeters
Centimeters.
@ Millimeters
Millimeters.
@ Miles
Terrestrial miles.
@ Unknown
Unknown distance unit.
@ Yards
Imperial yards.
@ Degrees
Degrees, for planar geographic CRS distance measurements.
@ Inches
Inches (since QGIS 3.32)
@ NauticalMiles
Nautical miles.
@ Kilometers
Kilometers.
AreaUnit
Units of area.
Definition qgis.h:4100
@ SquareFeet
Square feet.
@ SquareCentimeters
Square centimeters.
@ SquareInches
Square inches (since QGIS 3.32)
@ SquareNauticalMiles
Square nautical miles.
@ SquareMillimeters
Square millimeters.
@ SquareYards
Square yards.
@ Hectares
Hectares.
@ SquareKilometers
Square kilometers.
@ SquareMeters
Square meters.
@ Unknown
Unknown areal unit.
@ SquareDegrees
Square degrees, for planar geographic CRS area measurements.
@ SquareMiles
Square miles.
TemporalUnit
Temporal units.
Definition qgis.h:4169
@ IrregularStep
Special 'irregular step' time unit, used for temporal data which uses irregular, non-real-world unit ...
@ Milliseconds
Milliseconds.
@ Unknown
Unknown time unit.
@ Centuries
Centuries.
RenderUnit
Rendering size units.
Definition qgis.h:4193
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size)
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ Unknown
Mixed or unknown units.
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
LayoutUnitType
Types of layout units.
Definition qgis.h:4233
@ PaperUnits
Unit is a paper based measurement unit.
@ ScreenUnits
Unit is a screen based measurement unit.
VolumeUnit
Units of volume.
Definition qgis.h:4125
@ CubicMeters
Cubic meters.
@ Barrel
Barrels.
@ CubicYards
Cubic yards.
@ CubicFeet
Cubic feet.
@ CubicDegrees
Cubic degrees, for planar geographic CRS volume measurements.
@ CubicDecimeter
Cubic decimeters.
@ Unknown
Unknown volume unit.
@ CubicInch
Cubic inches.
@ GallonUS
US Gallons.
@ CubicCentimeter
Cubic Centimeters.
static Q_INVOKABLE Qgis::VolumeUnit decodeVolumeUnit(const QString &string, bool *ok=nullptr)
Decodes a volume unit from a string.
static Q_INVOKABLE Qgis::DistanceUnit stringToDistanceUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a distance unit.
static Q_INVOKABLE QgsUnitTypes::AreaValue scaledArea(double area, Qgis::AreaUnit unit, int decimals, bool keepBaseUnit=false)
Will convert an area with a given unit to an area value which is nice to display.
static Q_INVOKABLE QgsUnitTypes::DistanceValue scaledDistance(double distance, Qgis::DistanceUnit unit, int decimals, bool keepBaseUnit=false)
Will convert a distance with a given unit to a distance value which is nice to display.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
static Q_INVOKABLE QString formatArea(double area, int decimals, Qgis::AreaUnit unit, bool keepBaseUnit=false)
Returns an area formatted as a friendly string.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE Qgis::VolumeUnit stringToVolumeUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a volume unit.
static Q_INVOKABLE QString formatDistance(double distance, int decimals, Qgis::DistanceUnit unit, bool keepBaseUnit=false)
Returns an distance formatted as a friendly string.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE Qgis::DistanceUnitType unitType(Qgis::DistanceUnit unit)
Returns the type for a distance unit.
static Q_INVOKABLE Qgis::UnitType decodeUnitType(const QString &string, bool *ok=nullptr)
Decodes a unit type from a string.
static Q_INVOKABLE Qgis::AreaUnit decodeAreaUnit(const QString &string, bool *ok=nullptr)
Decodes an areal unit from a string.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE Qgis::TemporalUnit stringToTemporalUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to a temporal unit.
static Q_INVOKABLE Qgis::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
static Q_INVOKABLE Qgis::AreaUnit stringToAreaUnit(const QString &string, bool *ok=nullptr)
Converts a translated string to an areal unit.
static Q_INVOKABLE Qgis::AreaUnit distanceToAreaUnit(Qgis::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding area unit, e.g., meters to square meters.
static Q_INVOKABLE Qgis::TemporalUnit decodeTemporalUnit(const QString &string, bool *ok=nullptr)
Decodes a temporal unit from a string.
static Q_INVOKABLE Qgis::AngleUnit decodeAngleUnit(const QString &string, bool *ok=nullptr)
Decodes an angular unit from a string.
static Q_INVOKABLE Qgis::DistanceUnit areaToDistanceUnit(Qgis::AreaUnit areaUnit)
Converts an area unit to its corresponding distance unit, e.g., square meters to meters.
static Q_INVOKABLE Qgis::DistanceUnit volumeToDistanceUnit(Qgis::VolumeUnit volumeUnit)
Converts a volume unit to its corresponding distance unit, e.g., cubic meters to meters.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QString encodeUnitType(Qgis::UnitType type)
Encodes a unit type to a string.
static Q_INVOKABLE QString formatAngle(double angle, int decimals, Qgis::AngleUnit unit)
Returns an angle formatted as a friendly string.
static Q_INVOKABLE Qgis::VolumeUnit distanceToVolumeUnit(Qgis::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding volume unit, e.g., meters to cubic meters.
static Q_INVOKABLE Qgis::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
Definition qgis.h:5185
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5144
#define DEGREE_TO_METER
#define MILLIMETERS_TO_METER
#define KM2_TO_M2
#define CENTIMETERS_TO_METER
#define DEG2_TO_M3
#define MI2_TO_M2
#define AC_TO_FT2
#define NMILE_TO_METER
#define KILOMETERS_TO_METER
#define FEET_TO_INCHES
#define YARDS_TO_FEET
#define FEET_TO_METER
#define YD2_TO_M2
#define NM2_TO_M2
#define IN2_TO_M2
#define MM2_TO_M2
#define DEG2_TO_M2
#define CM2_TO_M2
#define FT2_TO_M2
#define MILES_TO_METER
#define YARDS_TO_METER
#define INCHES_TO_METER
#define HA_TO_M2
A combination of area value and unit.
double value
The value part of the distance.
Qgis::AreaUnit unit
The value part of the distance.
A combination of distance value and unit.
double value
The value part of the distance.
Qgis::DistanceUnit unit
The value part of the distance.