QGIS API Documentation  2.5.0-Master
 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 : sherman@mrcc.com
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 
124 {
125 #define DEGREE_TO_METER 111319.49079327358
126 #define FEET_TO_METER 0.3048
127 #define NMILE_TO_METER 1852.0
128 
129  // Unify degree units
130  if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
131  fromUnit = QGis::Degrees;
132  if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
133  toUnit = QGis::Degrees;
134 
135  // Calculate the conversion factor between the specified units
136  if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
137  {
138  switch ( fromUnit )
139  {
140  case QGis::Meters:
141  {
142  if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
143  if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
144  if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
145  break;
146  }
147  case QGis::Feet:
148  {
149  if ( toUnit == QGis::Meters ) return FEET_TO_METER;
150  if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
151  if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
152  break;
153  }
154  case QGis::Degrees:
155  {
156  if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
157  if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
158  if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
159  break;
160  }
161  case QGis::NauticalMiles:
162  {
163  if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
164  if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
165  if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
166  break;
167  }
168  case QGis::UnknownUnit:
169  break;
170  }
171  }
172  return 1.0;
173 }
174 
175 void *qgsMalloc( size_t size )
176 {
177  if ( size == 0 || long( size ) < 0 )
178  {
179  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
180  return NULL;
181  }
182  void *p = malloc( size );
183  if ( p == NULL )
184  {
185  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
186  }
187  return p;
188 }
189 
190 void *qgsCalloc( size_t nmemb, size_t size )
191 {
192  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
193  {
194  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
195  return NULL;
196  }
197  void *p = qgsMalloc( nmemb * size );
198  if ( p != NULL )
199  {
200  memset( p, 0, nmemb * size );
201  }
202  return p;
203 }
204 
205 void qgsFree( void *ptr )
206 {
207  free( ptr );
208 }
209 
210 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
211 {
212  switch ( lhs.type() )
213  {
214  case QVariant::Int:
215  return lhs.toInt() < rhs.toInt();
216  case QVariant::UInt:
217  return lhs.toUInt() < rhs.toUInt();
218  case QVariant::LongLong:
219  return lhs.toLongLong() < rhs.toLongLong();
220  case QVariant::ULongLong:
221  return lhs.toULongLong() < rhs.toULongLong();
222  case QVariant::Double:
223  return lhs.toDouble() < rhs.toDouble();
224  case QVariant::Char:
225  return lhs.toChar() < rhs.toChar();
226  case QVariant::Date:
227  return lhs.toDate() < rhs.toDate();
228  case QVariant::Time:
229  return lhs.toTime() < rhs.toTime();
230  case QVariant::DateTime:
231  return lhs.toDateTime() < rhs.toDateTime();
232  default:
233  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
234  }
235 }
236 
237 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
238 {
239  return ! qgsVariantLessThan( lhs, rhs );
240 }
241 
242 QString qgsVsiPrefix( QString path )
243 {
244  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
245  path.endsWith( ".zip", Qt::CaseInsensitive ) )
246  return "/vsizip/";
247  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
248  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
249  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
250  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
251  return "/vsitar/";
252  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
253  path.endsWith( ".gz", Qt::CaseInsensitive ) )
254  return "/vsigzip/";
255  else
256  return "";
257 }
static const char * QGIS_VERSION
Definition: qgis.h:40
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:285
static UnitType fromLiteral(QString literal, QGis::UnitType defaultType=UnknownUnit)
Converts from the canonical name to the type value.
Definition: qgis.cpp:101
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:281
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:175
const CORE_EXPORT QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:54
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
#define FEET_TO_METER
void * qgsCalloc(size_t nmemb, size_t size)
Allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the alloc...
Definition: qgis.cpp:190
#define DEGREE_TO_METER
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:242
static const double DEFAULT_IDENTIFY_RADIUS
Old search radius in % of canvas width.
Definition: qgis.h:269
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:237
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:210
#define NMILE_TO_METER
static const char * QGIS_DEV_VERSION
Definition: qgis.h:46
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:276
const CORE_EXPORT QString PROJECT_SCALES
Definition: qgis.cpp:67
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:273
static const char * QGIS_RELEASE_NAME
Definition: qgis.h:44
static QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:118
const CORE_EXPORT QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:73
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:123
UnitType
Map units that qgis supports.
Definition: qgis.h:229
const CORE_EXPORT QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:51
static QString toLiteral(QGis::UnitType unit)
Provides the canonical name of the type value.
Definition: qgis.cpp:113
double size
Definition: qgssvgcache.cpp:77
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:205
static const int QGIS_VERSION_INT
Definition: qgis.h:42
static const char * qgisUnitTypes[]
Definition: qgis.h:293
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:289
const CORE_EXPORT QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:71