QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgis.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgis.cpp
3 
4  -------------------
5  begin : 2007
6  copyright : (C) 2007 by Gary E. Sherman
7  email : [email protected]
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 #include "qgis.h"
19 #ifndef QGSVERSION
20 #include "qgsversion.h"
21 #endif
22 #include <QCoreApplication>
23 #include <QColor>
24 #include <QDate>
25 #include <QTime>
26 #include <QDateTime>
27 #include "qgsconfig.h"
28 #include "qgslogger.h"
29 
30 #include <ogr_api.h>
31 
32 // Version constants
33 //
34 
35 // Version string
36 const char* QGis::QGIS_VERSION = VERSION;
37 
38 // development version
39 const char* QGis::QGIS_DEV_VERSION = QGSVERSION;
40 
41 // Version number used for comparing versions using the
42 // "Check QGIS Version" function
43 const int QGis::QGIS_VERSION_INT = VERSION_INT;
44 
45 // Release name
46 const char* QGis::QGIS_RELEASE_NAME = RELEASE_NAME;
47 
48 #if GDAL_VERSION_NUM >= 1800
49 const CORE_EXPORT QString GEOPROJ4 = "+proj=longlat +datum=WGS84 +no_defs";
50 #else
51 const CORE_EXPORT QString GEOPROJ4 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
52 #endif
53 
54 const CORE_EXPORT QString GEOWKT =
55  "GEOGCS[\"WGS 84\", "
56  " DATUM[\"WGS_1984\", "
57  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
58  " AUTHORITY[\"EPSG\",7030]], "
59  " TOWGS84[0,0,0,0,0,0,0], "
60  " AUTHORITY[\"EPSG\",6326]], "
61  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
62  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
63  " AXIS[\"Lat\",NORTH], "
64  " AXIS[\"Long\",EAST], "
65  " AUTHORITY[\"EPSG\",4326]]";
66 
67 const CORE_EXPORT QString PROJECT_SCALES =
68  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
69  "1:10000,1:5000,1:2500,1:1000,1:500";
70 
71 const CORE_EXPORT QString GEO_EPSG_CRS_AUTHID = "EPSG:4326";
72 
73 const CORE_EXPORT QString GEO_NONE = "NONE";
74 
75 const double QGis::DEFAULT_IDENTIFY_RADIUS = 0.5;
76 const double QGis::DEFAULT_SEARCH_RADIUS_MM = 2.;
77 
79 const float QGis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
80 
81 const QColor QGis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
82 
84 
86 
87 // description strings for units
88 // Order must match enum indices
89 const char* QGis::qgisUnitTypes[] =
90 {
91  QT_TRANSLATE_NOOP( "QGis::UnitType", "meters" ),
92  QT_TRANSLATE_NOOP( "QGis::UnitType", "feet" ),
93  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
94  QT_TRANSLATE_NOOP( "QGis::UnitType", "<unknown>" ),
95  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
96  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
97  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
98  QT_TRANSLATE_NOOP( "QGis::UnitType", "nautical miles" )
99 };
100 
101 QGis::UnitType QGis::fromLiteral( QString literal, QGis::UnitType defaultType )
102 {
103  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
104  {
105  if ( literal == qgisUnitTypes[ i ] )
106  {
107  return static_cast<UnitType>( i );
108  }
109  }
110  return defaultType;
111 }
112 
114 {
115  return QString( qgisUnitTypes[ static_cast<int>( unit )] );
116 }
117 
118 QString QGis::tr( QGis::UnitType unit )
119 {
120  return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
121 }
122 
123 QGis::UnitType QGis::fromTr( QString literal, QGis::UnitType defaultType )
124 {
125  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
126  {
127  if ( literal == QGis::tr( static_cast<UnitType>( i ) ) )
128  {
129  return static_cast<UnitType>( i );
130  }
131  }
132  return defaultType;
133 }
134 
136 {
137 #define DEGREE_TO_METER 111319.49079327358
138 #define FEET_TO_METER 0.3048
139 #define NMILE_TO_METER 1852.0
140 
141  // Unify degree units
142  if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
143  fromUnit = QGis::Degrees;
144  if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
145  toUnit = QGis::Degrees;
146 
147  // Calculate the conversion factor between the specified units
148  if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
149  {
150  switch ( fromUnit )
151  {
152  case QGis::Meters:
153  {
154  if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
155  if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
156  if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
157  break;
158  }
159  case QGis::Feet:
160  {
161  if ( toUnit == QGis::Meters ) return FEET_TO_METER;
162  if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
163  if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
164  break;
165  }
166  case QGis::Degrees:
167  {
168  if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
169  if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
170  if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
171  break;
172  }
173  case QGis::NauticalMiles:
174  {
175  if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
176  if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
177  if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
178  break;
179  }
180  case QGis::UnknownUnit:
181  break;
182  }
183  }
184  return 1.0;
185 }
186 
187 void *qgsMalloc( size_t size )
188 {
189  if ( size == 0 || long( size ) < 0 )
190  {
191  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
192  return NULL;
193  }
194  void *p = malloc( size );
195  if ( p == NULL )
196  {
197  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
198  }
199  return p;
200 }
201 
202 void *qgsCalloc( size_t nmemb, size_t size )
203 {
204  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
205  {
206  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
207  return NULL;
208  }
209  void *p = qgsMalloc( nmemb * size );
210  if ( p != NULL )
211  {
212  memset( p, 0, nmemb * size );
213  }
214  return p;
215 }
216 
217 void qgsFree( void *ptr )
218 {
219  free( ptr );
220 }
221 
222 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
223 {
224  switch ( lhs.type() )
225  {
226  case QVariant::Int:
227  return lhs.toInt() < rhs.toInt();
228  case QVariant::UInt:
229  return lhs.toUInt() < rhs.toUInt();
230  case QVariant::LongLong:
231  return lhs.toLongLong() < rhs.toLongLong();
232  case QVariant::ULongLong:
233  return lhs.toULongLong() < rhs.toULongLong();
234  case QVariant::Double:
235  return lhs.toDouble() < rhs.toDouble();
236  case QVariant::Char:
237  return lhs.toChar() < rhs.toChar();
238  case QVariant::Date:
239  return lhs.toDate() < rhs.toDate();
240  case QVariant::Time:
241  return lhs.toTime() < rhs.toTime();
242  case QVariant::DateTime:
243  return lhs.toDateTime() < rhs.toDateTime();
244  default:
245  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
246  }
247 }
248 
249 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
250 {
251  return ! qgsVariantLessThan( lhs, rhs );
252 }
253 
254 QString qgsVsiPrefix( QString path )
255 {
256  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
257  path.endsWith( ".zip", Qt::CaseInsensitive ) )
258  return "/vsizip/";
259  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
260  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
261  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
262  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
263  return "/vsitar/";
264  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
265  path.endsWith( ".gz", Qt::CaseInsensitive ) )
266  return "/vsigzip/";
267  else
268  return "";
269 }