QGIS API Documentation  2.14.0-Essen
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 <QCoreApplication>
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 QGis::Meters:
31  case QGis::Feet:
33  return Standard;
34 
35  case QGis::Degrees:
36  return Geographic;
37 
38  case QGis::UnknownUnit:
39  return UnknownType;
40  }
41  return UnknownType;
42 }
43 
45 {
46  switch ( unit )
47  {
48  case SquareMeters:
49  case SquareKilometers:
50  case SquareFeet:
51  case SquareYards:
52  case SquareMiles:
53  case Hectares:
54  case Acres:
56  return Standard;
57 
58  case SquareDegrees:
59  return Geographic;
60 
61  case UnknownAreaUnit:
62  return UnknownType;
63  }
64 
65  return UnknownType;
66 }
67 
69 {
70  switch ( unit )
71  {
72  case QGis::Meters:
73  return "meters";
74 
75  case QGis::Feet:
76  return "feet";
77 
78  case QGis::Degrees:
79  return "degrees";
80 
81  case QGis::UnknownUnit:
82  return "<unknown>";
83 
85  return "nautical miles";
86  }
87  return QString();
88 }
89 
90 /***************************************************************************
91  * This class is considered CRITICAL and any change MUST be accompanied with
92  * full unit tests in test_qgsunittypes.py.
93  * See details in QEP #17
94  ****************************************************************************/
95 
97 {
98  QString normalized = string.trimmed().toLower();
99 
100  if ( ok )
101  *ok = true;
102 
103  if ( normalized == encodeUnit( QGis::Meters ) )
104  return QGis::Meters;
105  if ( normalized == encodeUnit( QGis::Feet ) )
106  return QGis::Feet;
107  if ( normalized == encodeUnit( QGis::Degrees ) )
108  return QGis::Degrees;
109  if ( normalized == encodeUnit( QGis::NauticalMiles ) )
110  return QGis::NauticalMiles;
111  if ( normalized == encodeUnit( QGis::UnknownUnit ) )
112  return QGis::UnknownUnit;
113 
114  if ( ok )
115  *ok = false;
116 
117  return QGis::UnknownUnit;
118 }
119 
121 {
122  switch ( unit )
123  {
124  case QGis::Meters:
125  return QCoreApplication::translate( "QGis::UnitType", "meters" );
126  case QGis::Feet:
127  return QCoreApplication::translate( "QGis::UnitType", "feet" );
128 
129  case QGis::Degrees:
130  return QCoreApplication::translate( "QGis::UnitType", "degrees" );
131 
132  case QGis::UnknownUnit:
133  return QCoreApplication::translate( "QGis::UnitType", "<unknown>" );
134 
135  case QGis::NauticalMiles:
136  return QCoreApplication::translate( "QGis::UnitType", "nautical miles" );
137  }
138  return QString();
139 }
140 
141 /***************************************************************************
142  * This class is considered CRITICAL and any change MUST be accompanied with
143  * full unit tests in test_qgsunittypes.py.
144  * See details in QEP #17
145  ****************************************************************************/
146 
148 {
149  QString normalized = string.trimmed().toLower();
150 
151  if ( ok )
152  *ok = true;
153 
154  if ( normalized == toString( QGis::Meters ) )
155  return QGis::Meters;
156  if ( normalized == toString( QGis::Feet ) )
157  return QGis::Feet;
158  if ( normalized == toString( QGis::Degrees ) )
159  return QGis::Degrees;
160  if ( normalized == toString( QGis::NauticalMiles ) )
161  return QGis::NauticalMiles;
162  if ( normalized == toString( QGis::UnknownUnit ) )
163  return QGis::UnknownUnit;
164 
165  if ( ok )
166  *ok = false;
167 
168  return QGis::UnknownUnit;
169 }
170 
171 /***************************************************************************
172  * This class is considered CRITICAL and any change MUST be accompanied with
173  * full unit tests in test_qgsunittypes.py.
174  * See details in QEP #17
175  ****************************************************************************/
176 
178 {
179 #define DEGREE_TO_METER 111319.49079327358
180 #define FEET_TO_METER 0.3048
181 #define NMILE_TO_METER 1852.0
182 
183  // Unify degree units
184  // remove for QGIS 3.0, as extra degree types will be removed
185  if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
186  fromUnit = QGis::Degrees;
187  if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
188  toUnit = QGis::Degrees;
189 
190  // Calculate the conversion factor between the specified units
191  if ( fromUnit != toUnit )
192  {
193  switch ( fromUnit )
194  {
195  case QGis::Meters:
196  {
197  switch ( toUnit )
198  {
199  case QGis::Meters:
200  return 1.0;
201  case QGis::Feet:
202  return 1.0 / FEET_TO_METER;
203  case QGis::Degrees:
204  return 1.0 / DEGREE_TO_METER;
205  case QGis::NauticalMiles:
206  return 1.0 / NMILE_TO_METER;
207  case QGis::UnknownUnit:
208  break;
209  }
210 
211  break;
212  }
213  case QGis::Feet:
214  {
215  switch ( toUnit )
216  {
217  case QGis::Meters:
218  return FEET_TO_METER;
219  case QGis::Feet:
220  return 1.0;
221  case QGis::Degrees:
223  case QGis::NauticalMiles:
224  return FEET_TO_METER / NMILE_TO_METER;
225  case QGis::UnknownUnit:
226  break;
227  }
228 
229  break;
230  }
231  case QGis::Degrees:
232  {
233  switch ( toUnit )
234  {
235  case QGis::Meters:
236  return DEGREE_TO_METER;
237  case QGis::Feet:
239  case QGis::Degrees:
240  return 1.0;
241  case QGis::NauticalMiles:
243  case QGis::UnknownUnit:
244  break;
245  }
246 
247  break;
248  }
249  case QGis::NauticalMiles:
250  {
251  switch ( toUnit )
252  {
253  case QGis::Meters:
254  return NMILE_TO_METER;
255  case QGis::Feet:
256  return NMILE_TO_METER / FEET_TO_METER;
257  case QGis::Degrees:
259  case QGis::NauticalMiles:
260  return 1.0;
261  case QGis::UnknownUnit:
262  break;
263  }
264 
265  break;
266  }
267  case QGis::UnknownUnit:
268  break;
269  }
270  }
271  return 1.0;
272 }
273 
275 {
276  switch ( unit )
277  {
278  case SquareMeters:
279  return "m2";
280  case SquareKilometers:
281  return "km2";
282  case SquareFeet:
283  return "ft2";
284  case SquareYards:
285  return "y2";
286  case SquareMiles:
287  return "mi2";
288  case Hectares:
289  return "ha";
290  case Acres:
291  return "ac";
292  case SquareNauticalMiles:
293  return "nm2";
294  case SquareDegrees:
295  return "deg2";
296  case UnknownAreaUnit:
297  return "<unknown>";
298  }
299  return QString();
300 }
301 
303 {
304  QString normalized = string.trimmed().toLower();
305 
306  if ( ok )
307  *ok = true;
308 
309  if ( normalized == encodeUnit( SquareMeters ) )
310  return SquareMeters;
311  if ( normalized == encodeUnit( SquareKilometers ) )
312  return SquareKilometers;
313  if ( normalized == encodeUnit( SquareFeet ) )
314  return SquareFeet;
315  if ( normalized == encodeUnit( SquareYards ) )
316  return SquareYards;
317  if ( normalized == encodeUnit( SquareMiles ) )
318  return SquareMiles;
319  if ( normalized == encodeUnit( Hectares ) )
320  return Hectares;
321  if ( normalized == encodeUnit( Acres ) )
322  return Acres;
323  if ( normalized == encodeUnit( SquareNauticalMiles ) )
324  return SquareNauticalMiles;
325  if ( normalized == encodeUnit( SquareDegrees ) )
326  return SquareDegrees;
327  if ( normalized == encodeUnit( UnknownAreaUnit ) )
328  return UnknownAreaUnit;
329 
330  if ( ok )
331  *ok = false;
332 
333  return UnknownAreaUnit;
334 }
335 
337 {
338  switch ( unit )
339  {
340  case SquareMeters:
341  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square meters" );
342  case SquareKilometers:
343  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square kilometers" );
344  case SquareFeet:
345  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square feet" );
346  case SquareYards:
347  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square yards" );
348  case SquareMiles:
349  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square miles" );
350  case Hectares:
351  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "hectares" );
352  case Acres:
353  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "acres" );
354  case SquareNauticalMiles:
355  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square nautical miles" );
356  case SquareDegrees:
357  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "square degrees" );
358  case UnknownAreaUnit:
359  return QCoreApplication::translate( "QgsUnitTypes::AreaUnit", "<unknown>" );
360  }
361  return QString();
362 }
363 
365 {
366  QString normalized = string.trimmed().toLower();
367 
368  if ( ok )
369  *ok = true;
370 
371  if ( normalized == toString( SquareMeters ) )
372  return SquareMeters;
373  if ( normalized == toString( SquareKilometers ) )
374  return SquareKilometers;
375  if ( normalized == toString( SquareFeet ) )
376  return SquareFeet;
377  if ( normalized == toString( SquareYards ) )
378  return SquareYards;
379  if ( normalized == toString( SquareMiles ) )
380  return SquareMiles;
381  if ( normalized == toString( Hectares ) )
382  return Hectares;
383  if ( normalized == toString( Acres ) )
384  return Acres;
385  if ( normalized == toString( SquareNauticalMiles ) )
386  return SquareNauticalMiles;
387  if ( normalized == toString( SquareDegrees ) )
388  return SquareDegrees;
389  if ( normalized == toString( UnknownAreaUnit ) )
390  return UnknownAreaUnit;
391  if ( ok )
392  *ok = false;
393 
394  return UnknownAreaUnit;
395 }
396 
398 {
399 #define KM2_TO_M2 1000000.0
400 #define FT2_TO_M2 0.09290304
401 #define YD2_TO_M2 0.83612736
402 #define MI2_TO_M2 2589988.110336
403 #define HA_TO_M2 10000.0
404 #define AC_TO_FT2 43560.0
405 #define DEG2_TO_M2 12392029030.5
406 #define NM2_TO_M2 3429904.0
407 
408  // Calculate the conversion factor between the specified units
409  if ( fromUnit != toUnit )
410  {
411  switch ( fromUnit )
412  {
413  case SquareMeters:
414  {
415  switch ( toUnit )
416  {
417  case SquareMeters:
418  return 1.0;
419  case SquareKilometers:
420  return 1.0 / KM2_TO_M2;
421  case SquareFeet:
422  return 1.0 / FT2_TO_M2;
423  case SquareYards:
424  return 1.0 / YD2_TO_M2;
425  case SquareMiles:
426  return 1.0 / MI2_TO_M2;
427  case Hectares:
428  return 1.0 / HA_TO_M2;
429  case Acres:
430  return 1.0 / AC_TO_FT2 / FT2_TO_M2;
431  case SquareNauticalMiles:
432  return 1.0 / NM2_TO_M2;
433  case SquareDegrees:
434  return 1.0 / DEG2_TO_M2;
435  case UnknownAreaUnit:
436  break;
437  }
438 
439  break;
440  }
441  case SquareKilometers:
442  {
443  switch ( toUnit )
444  {
445  case SquareMeters:
446  return KM2_TO_M2;
447  case SquareKilometers:
448  return 1.0;
449  case SquareFeet:
450  return KM2_TO_M2 / FT2_TO_M2 ;
451  case SquareYards:
452  return KM2_TO_M2 / YD2_TO_M2;
453  case SquareMiles:
454  return KM2_TO_M2 / MI2_TO_M2;
455  case Hectares:
456  return KM2_TO_M2 / HA_TO_M2;
457  case Acres:
458  return KM2_TO_M2 / AC_TO_FT2 / FT2_TO_M2;
459  case SquareNauticalMiles:
460  return KM2_TO_M2 / NM2_TO_M2;
461  case SquareDegrees:
462  return KM2_TO_M2 / DEG2_TO_M2;
463  case UnknownAreaUnit:
464  break;
465  }
466 
467  break;
468  }
469  case SquareFeet:
470  {
471  switch ( toUnit )
472  {
473  case SquareMeters:
474  return FT2_TO_M2;
475  case SquareKilometers:
476  return FT2_TO_M2 / KM2_TO_M2;
477  case SquareFeet:
478  return 1.0;
479  case SquareYards:
480  return FT2_TO_M2 / YD2_TO_M2;
481  case SquareMiles:
482  return FT2_TO_M2 / MI2_TO_M2;
483  case Hectares:
484  return FT2_TO_M2 / HA_TO_M2;
485  case Acres:
486  return 1.0 / AC_TO_FT2;
487  case SquareNauticalMiles:
488  return FT2_TO_M2 / NM2_TO_M2;
489  case SquareDegrees:
490  return FT2_TO_M2 / DEG2_TO_M2;
491  case UnknownAreaUnit:
492  break;
493  }
494 
495  break;
496  }
497 
498  case SquareYards:
499  {
500  switch ( toUnit )
501  {
502  case SquareMeters:
503  return YD2_TO_M2;
504  case SquareKilometers:
505  return YD2_TO_M2 / KM2_TO_M2;
506  case SquareFeet:
507  return YD2_TO_M2 / FT2_TO_M2;
508  case SquareYards:
509  return 1.0;
510  case SquareMiles:
511  return YD2_TO_M2 / MI2_TO_M2;
512  case Hectares:
513  return YD2_TO_M2 / HA_TO_M2;
514  case Acres:
515  return YD2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
516  case SquareNauticalMiles:
517  return YD2_TO_M2 / NM2_TO_M2;
518  case SquareDegrees:
519  return YD2_TO_M2 / DEG2_TO_M2;
520  case UnknownAreaUnit:
521  break;
522  }
523  break;
524  }
525 
526  case SquareMiles:
527  {
528  switch ( toUnit )
529  {
530  case SquareMeters:
531  return MI2_TO_M2;
532  case SquareKilometers:
533  return MI2_TO_M2 / KM2_TO_M2;
534  case SquareFeet:
535  return MI2_TO_M2 / FT2_TO_M2;
536  case SquareYards:
537  return MI2_TO_M2 / YD2_TO_M2;
538  case SquareMiles:
539  return 1.0;
540  case Hectares:
541  return MI2_TO_M2 / HA_TO_M2;
542  case Acres:
543  return MI2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
544  case SquareNauticalMiles:
545  return MI2_TO_M2 / NM2_TO_M2;
546  case SquareDegrees:
547  return MI2_TO_M2 / DEG2_TO_M2;
548  case UnknownAreaUnit:
549  break;
550  }
551 
552  break;
553  }
554 
555  case Hectares:
556  {
557  switch ( toUnit )
558  {
559  case SquareMeters:
560  return HA_TO_M2;
561  case SquareKilometers:
562  return HA_TO_M2 / KM2_TO_M2;
563  case SquareFeet:
564  return HA_TO_M2 / FT2_TO_M2;
565  case SquareYards:
566  return HA_TO_M2 / YD2_TO_M2;
567  case SquareMiles:
568  return HA_TO_M2 / MI2_TO_M2;
569  case Hectares:
570  return 1.0;
571  case Acres:
572  return HA_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
573  case SquareNauticalMiles:
574  return HA_TO_M2 / NM2_TO_M2;
575  case SquareDegrees:
576  return HA_TO_M2 / DEG2_TO_M2;
577  case UnknownAreaUnit:
578  break;
579  }
580 
581  break;
582  }
583 
584  case Acres:
585  {
586  switch ( toUnit )
587  {
588  case SquareMeters:
589  return AC_TO_FT2 * FT2_TO_M2;
590  case SquareKilometers:
591  return AC_TO_FT2 * FT2_TO_M2 / KM2_TO_M2;
592  case SquareFeet:
593  return AC_TO_FT2;
594  case SquareYards:
595  return AC_TO_FT2 * FT2_TO_M2 / YD2_TO_M2;
596  case SquareMiles:
597  return AC_TO_FT2 * FT2_TO_M2 / MI2_TO_M2;
598  case Hectares:
599  return AC_TO_FT2 * FT2_TO_M2 / HA_TO_M2;
600  case Acres:
601  return 1.0;
602  case SquareNauticalMiles:
603  return AC_TO_FT2 * FT2_TO_M2 / NM2_TO_M2;
604  case SquareDegrees:
605  return AC_TO_FT2 * FT2_TO_M2 / DEG2_TO_M2;
606  case UnknownAreaUnit:
607  break;
608  }
609 
610  break;
611  }
612 
613  case SquareNauticalMiles:
614  {
615  switch ( toUnit )
616  {
617  case SquareMeters:
618  return NM2_TO_M2;
619  case SquareKilometers:
620  return NM2_TO_M2 / KM2_TO_M2;
621  case SquareFeet:
622  return NM2_TO_M2 / FT2_TO_M2;
623  case SquareYards:
624  return NM2_TO_M2 / YD2_TO_M2;
625  case SquareMiles:
626  return NM2_TO_M2 / MI2_TO_M2;
627  case Hectares:
628  return NM2_TO_M2 / HA_TO_M2;
629  case Acres:
630  return NM2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
631  case SquareNauticalMiles:
632  return 1.0;
633  case SquareDegrees:
634  return NM2_TO_M2 / DEG2_TO_M2;
635  case UnknownAreaUnit:
636  break;
637  }
638 
639  break;
640  }
641 
642  case SquareDegrees:
643  {
644  switch ( toUnit )
645  {
646  case SquareMeters:
647  return DEG2_TO_M2;
648  case SquareKilometers:
649  return DEG2_TO_M2 / KM2_TO_M2;
650  case SquareFeet:
651  return DEG2_TO_M2 / FT2_TO_M2;
652  case SquareYards:
653  return DEG2_TO_M2 / YD2_TO_M2;
654  case SquareMiles:
655  return DEG2_TO_M2 / MI2_TO_M2;
656  case Hectares:
657  return DEG2_TO_M2 / HA_TO_M2;
658  case Acres:
659  return DEG2_TO_M2 / FT2_TO_M2 / AC_TO_FT2;
660  case SquareNauticalMiles:
661  return DEG2_TO_M2 / NM2_TO_M2;
662  case SquareDegrees:
663  return 1.0;
664  case UnknownAreaUnit:
665  break;
666  }
667 
668  break;
669  }
670 
671  case UnknownAreaUnit:
672  break;
673  }
674  }
675  return 1.0;
676 }
677 
679 {
680  switch ( distanceUnit )
681  {
682  case QGis::Meters:
683  return SquareMeters;
684 
685  case QGis::Feet:
686  return SquareFeet;
687 
688  case QGis::Degrees:
689  return SquareDegrees;
690 
691  case QGis::UnknownUnit:
692  return UnknownAreaUnit;
693 
694  case QGis::NauticalMiles:
695  return SquareNauticalMiles;
696  }
697 
698  return UnknownAreaUnit;
699 }
700 
702 {
703  switch ( unit )
704  {
705  case AngleDegrees:
706  return "degrees";
707  case Radians:
708  return "radians";
709  case Gon:
710  return "gon";
711  case MinutesOfArc:
712  return "moa";
713  case SecondsOfArc:
714  return "soa";
715  case Turn:
716  return "tr";
717  case UnknownAngleUnit:
718  return "<unknown>";
719  }
720  return QString();
721 }
722 
724 {
725  QString normalized = string.trimmed().toLower();
726 
727  if ( ok )
728  *ok = true;
729 
730  if ( normalized == encodeUnit( AngleDegrees ) )
731  return AngleDegrees;
732  if ( normalized == encodeUnit( Radians ) )
733  return Radians;
734  if ( normalized == encodeUnit( Gon ) )
735  return Gon;
736  if ( normalized == encodeUnit( MinutesOfArc ) )
737  return MinutesOfArc;
738  if ( normalized == encodeUnit( SecondsOfArc ) )
739  return SecondsOfArc;
740  if ( normalized == encodeUnit( Turn ) )
741  return Turn;
742  if ( normalized == encodeUnit( UnknownAngleUnit ) )
743  return UnknownAngleUnit;
744  if ( ok )
745  *ok = false;
746 
747  return UnknownAngleUnit;
748 }
749 
751 {
752  switch ( unit )
753  {
754  case AngleDegrees:
755  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "degrees" );
756  case Radians:
757  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "radians" );
758  case Gon:
759  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "gon" );
760  case MinutesOfArc:
761  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "minutes of arc" );
762  case SecondsOfArc:
763  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "seconds of arc" );
764  case Turn:
765  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "turns" );
766  case UnknownAngleUnit:
767  return QCoreApplication::translate( "QgsUnitTypes::AngleUnit", "<unknown>" );
768  }
769  return QString();
770 }
771 
773 {
774  // Calculate the conversion factor between the specified units
775  if ( fromUnit != toUnit )
776  {
777  switch ( fromUnit )
778  {
779  case AngleDegrees:
780  {
781  switch ( toUnit )
782  {
783  case AngleDegrees:
784  return 1.0;
785  case Radians:
786  return M_PI / 180.0;
787  case Gon:
788  return 400.0 / 360.0;
789  case MinutesOfArc:
790  return 60;
791  case SecondsOfArc:
792  return 3600;
793  case Turn:
794  return 1.0 / 360.0;
795  case UnknownAngleUnit:
796  break;
797  }
798  break;
799  }
800  case Radians:
801  {
802  switch ( toUnit )
803  {
804  case AngleDegrees:
805  return 180.0 / M_PI;
806  case Radians:
807  return 1.0;
808  case Gon:
809  return 200.0 / M_PI;
810  case MinutesOfArc:
811  return 60 * 180.0 / M_PI;
812  case SecondsOfArc:
813  return 3600 * 180.0 / M_PI;
814  case Turn:
815  return 0.5 / M_PI;
816  case UnknownAngleUnit:
817  break;
818  }
819  break;
820  }
821  case Gon:
822  {
823  switch ( toUnit )
824  {
825  case AngleDegrees:
826  return 360.0 / 400.0;
827  case Radians:
828  return M_PI / 200.0;
829  case Gon:
830  return 1.0;
831  case MinutesOfArc:
832  return 60 * 360.0 / 400.0;
833  case SecondsOfArc:
834  return 3600 * 360.0 / 400.0;
835  case Turn:
836  return 1.0 / 400.0;
837  case UnknownAngleUnit:
838  break;
839  }
840  break;
841  }
842  case MinutesOfArc:
843  {
844  switch ( toUnit )
845  {
846  case AngleDegrees:
847  return 1 / 60.0;
848  case Radians:
849  return M_PI / 180.0 / 60.0;
850  case Gon:
851  return 400.0 / 360.0 / 60.0;
852  case MinutesOfArc:
853  return 1.0;
854  case SecondsOfArc:
855  return 60.0;
856  case Turn:
857  return 1.0 / 360.0 / 60.0;
858  case UnknownAngleUnit:
859  break;
860  }
861  break;
862  }
863  case SecondsOfArc:
864  {
865  switch ( toUnit )
866  {
867  case AngleDegrees:
868  return 1 / 3600.0;
869  case Radians:
870  return M_PI / 180.0 / 3600.0;
871  case Gon:
872  return 400.0 / 360.0 / 3600.0;
873  case MinutesOfArc:
874  return 1.0 / 60.0;
875  case SecondsOfArc:
876  return 1.0;
877  case Turn:
878  return 1.0 / 360.0 / 3600.0;
879  case UnknownAngleUnit:
880  break;
881  }
882  break;
883  }
884  case Turn:
885  {
886  switch ( toUnit )
887  {
888  case AngleDegrees:
889  return 360.0;
890  case Radians:
891  return 2 * M_PI;
892  case Gon:
893  return 400.0;
894  case MinutesOfArc:
895  return 360.0 * 60.0;
896  case SecondsOfArc:
897  return 360.0 * 3600.0;
898  case Turn:
899  return 1.0;
900  case UnknownAngleUnit:
901  break;
902  }
903  break;
904  }
905  case UnknownAngleUnit:
906  break;
907  }
908  }
909  return 1.0;
910 }
911 
913 {
914  QString unitLabel;
915 
916  switch ( unit )
917  {
918  case AngleDegrees:
919  unitLabel = QObject::trUtf8( "°" );
920  break;
921  case Radians:
922  unitLabel = QObject::trUtf8( " rad" );
923  break;
924  case Gon:
925  unitLabel = QObject::trUtf8( " gon" );
926  break;
927  case MinutesOfArc:
928  unitLabel = QObject::trUtf8( "′" );
929  break;
930  case SecondsOfArc:
931  unitLabel = QObject::trUtf8( "″" );
932  break;
933  case Turn:
934  unitLabel = QObject::trUtf8( " tr" );
935  break;
936  case UnknownAngleUnit:
937  break;
938  }
939 
940  return QString( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
941 }
942 
943 // enable for QGIS 3.0
944 #if 0
945 
947 {
948  switch ( unit )
949  {
950  case QgsSymbolV2::MM:
951  return "MM";
953  return "MapUnit";
954  case QgsSymbolV2::Pixel:
955  return "Pixel";
957  return "Percentage";
958  default:
959  return "MM";
960  }
961 }
962 
963 QgsSymbolV2::OutputUnit QgsUnitTypes::decodeSymbolUnit( const QString& string, bool* ok )
964 {
965  QString normalized = string.trimmed().toLower();
966 
967  if ( ok )
968  *ok = true;
969 
970  if ( normalized == encodeUnit( QgsSymbolV2::MM ).toLower() )
971  return QgsSymbolV2::MM;
972  if ( normalized == encodeUnit( QgsSymbolV2::MapUnit ).toLower() )
973  return QgsSymbolV2::MapUnit;
974  if ( normalized == encodeUnit( QgsSymbolV2::Pixel ).toLower() )
975  return QgsSymbolV2::Pixel;
976  if ( normalized == encodeUnit( QgsSymbolV2::Percentage ).toLower() )
978 
979  if ( ok )
980  *ok = false;
981 
982  // millimeters are default
983  return QgsSymbolV2::MM;
984 }
985 
986 #endif
#define HA_TO_M2
static QString formatAngle(double angle, int decimals, AngleUnit unit)
Returns an angle formatted as a friendly string.
#define DEGREE_TO_METER
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
static DistanceUnitType unitType(QGis::UnitType unit)
Returns the type for a distance unit.
static AreaUnit decodeAreaUnit(const QString &string, bool *ok=0)
Decodes an areal unit from a string.
#define MI2_TO_M2
static QGis::UnitType decodeDistanceUnit(const QString &string, bool *ok=0)
Decodes a distance unit from a string.
The output shall be in pixels.
Definition: qgssymbolv2.h:67
DistanceUnitType
Types of distance units.
Definition: qgsunittypes.h:41
#define NMILE_TO_METER
#define KM2_TO_M2
QString trUtf8(const char *sourceText, const char *disambiguation, int n)
#define FEET_TO_METER
AngleUnit
Units of angles.
Definition: qgsunittypes.h:64
#define AC_TO_FT2
static QString encodeUnit(QGis::UnitType unit)
Encodes a distance unit to a string.
static QString toString(QGis::UnitType unit)
Returns a translated string representing a distance unit.
The output shall be in millimeters.
Definition: qgssymbolv2.h:64
static QGis::UnitType stringToDistanceUnit(const QString &string, bool *ok=0)
Converts a translated string to a distance unit.
#define DEG2_TO_M2
The ouput shall be a percentage of another measurement (eg canvas size, feature size) ...
Definition: qgssymbolv2.h:68
#define FT2_TO_M2
QString trimmed() const
The output shall be in map unitx.
Definition: qgssymbolv2.h:65
#define M_PI
#define NM2_TO_M2
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified distance units.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QString toLower() const
static AngleUnit decodeAngleUnit(const QString &string, bool *ok=0)
Decodes an angular unit from a string.
static AreaUnit distanceToAreaUnit(QGis::UnitType distanceUnit)
Converts a distance unit to its corresponding area unit, eg meters to square meters.
QString translate(const char *context, const char *sourceText, const char *disambiguation, Encoding encoding)
static AreaUnit stringToAreaUnit(const QString &string, bool *ok=0)
Converts a translated string to an areal unit.
UnitType
Map units that qgis supports.
Definition: qgis.h:155
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
AreaUnit
Units of area.
Definition: qgsunittypes.h:49
#define YD2_TO_M2