QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 <QLocale>
27 #include <QDateTime>
28 #include "qgsconfig.h"
29 #include "qgslogger.h"
30 
31 #include <ogr_api.h>
32 
33 // Version constants
34 //
35 
36 // Version string
37 const char* QGis::QGIS_VERSION = VERSION;
38 
39 // development version
40 const char* QGis::QGIS_DEV_VERSION = QGSVERSION;
41 
42 // Version number used for comparing versions using the
43 // "Check QGIS Version" function
44 const int QGis::QGIS_VERSION_INT = VERSION_INT;
45 
46 // Release name
47 const char* QGis::QGIS_RELEASE_NAME = RELEASE_NAME;
48 
49 #if GDAL_VERSION_NUM >= 1800
50 const QString GEOPROJ4 = "+proj=longlat +datum=WGS84 +no_defs";
51 #else
52 const QString GEOPROJ4 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
53 #endif
54 
55 const QString GEOWKT =
56  "GEOGCS[\"WGS 84\", "
57  " DATUM[\"WGS_1984\", "
58  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
59  " AUTHORITY[\"EPSG\",7030]], "
60  " TOWGS84[0,0,0,0,0,0,0], "
61  " AUTHORITY[\"EPSG\",6326]], "
62  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
63  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
64  " AXIS[\"Lat\",NORTH], "
65  " AXIS[\"Long\",EAST], "
66  " AUTHORITY[\"EPSG\",4326]]";
67 
69  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
70  "1:10000,1:5000,1:2500,1:1000,1:500";
71 
72 const QString GEO_EPSG_CRS_AUTHID = "EPSG:4326";
73 
74 const QString GEO_NONE = "NONE";
75 
76 const double QGis::DEFAULT_IDENTIFY_RADIUS = 0.5;
77 const double QGis::DEFAULT_SEARCH_RADIUS_MM = 2.;
78 
80 const float QGis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
81 
82 const QColor QGis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
83 
85 
87 
88 // description strings for units
89 // Order must match enum indices
90 const char* QGis::qgisUnitTypes[] =
91 {
92  QT_TRANSLATE_NOOP( "QGis::UnitType", "meters" ),
93  QT_TRANSLATE_NOOP( "QGis::UnitType", "feet" ),
94  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
95  QT_TRANSLATE_NOOP( "QGis::UnitType", "<unknown>" ),
96  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
97  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
98  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
99  QT_TRANSLATE_NOOP( "QGis::UnitType", "nautical miles" )
100 };
101 
103 {
104  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
105  {
106  if ( literal == qgisUnitTypes[ i ] )
107  {
108  return static_cast<UnitType>( i );
109  }
110  }
111  return defaultType;
112 }
113 
115 {
116  return QString( qgisUnitTypes[ static_cast<int>( unit )] );
117 }
118 
120 {
121  return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
122 }
123 
125 {
126  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
127  {
128  if ( literal == QGis::tr( static_cast<UnitType>( i ) ) )
129  {
130  return static_cast<UnitType>( i );
131  }
132  }
133  return defaultType;
134 }
135 
137 {
138 #define DEGREE_TO_METER 111319.49079327358
139 #define FEET_TO_METER 0.3048
140 #define NMILE_TO_METER 1852.0
141 
142  // Unify degree units
143  if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
144  fromUnit = QGis::Degrees;
145  if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
146  toUnit = QGis::Degrees;
147 
148  // Calculate the conversion factor between the specified units
149  if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
150  {
151  switch ( fromUnit )
152  {
153  case QGis::Meters:
154  {
155  if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
156  if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
157  if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
158  break;
159  }
160  case QGis::Feet:
161  {
162  if ( toUnit == QGis::Meters ) return FEET_TO_METER;
163  if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
164  if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
165  break;
166  }
167  case QGis::Degrees:
168  {
169  if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
170  if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
171  if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
172  break;
173  }
174  case QGis::NauticalMiles:
175  {
176  if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
177  if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
178  if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
179  break;
180  }
181  case QGis::UnknownUnit:
182  break;
183  }
184  }
185  return 1.0;
186 }
187 
188 double QGis::permissiveToDouble( QString string, bool &ok )
189 {
190  //remove any thousands separators
191  string.remove( QLocale::system().groupSeparator() );
192  return QLocale::system().toDouble( string, &ok );
193 }
194 
195 int QGis::permissiveToInt( QString string, bool &ok )
196 {
197  //remove any thousands separators
198  string.remove( QLocale::system().groupSeparator() );
199  return QLocale::system().toInt( string, &ok );
200 }
201 
202 void *qgsMalloc( size_t size )
203 {
204  if ( size == 0 || long( size ) < 0 )
205  {
206  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
207  return NULL;
208  }
209  void *p = malloc( size );
210  if ( p == NULL )
211  {
212  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
213  }
214  return p;
215 }
216 
217 void *qgsCalloc( size_t nmemb, size_t size )
218 {
219  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
220  {
221  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
222  return NULL;
223  }
224  void *p = qgsMalloc( nmemb * size );
225  if ( p != NULL )
226  {
227  memset( p, 0, nmemb * size );
228  }
229  return p;
230 }
231 
232 void qgsFree( void *ptr )
233 {
234  free( ptr );
235 }
236 
237 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
238 {
239  switch ( lhs.type() )
240  {
241  case QVariant::Int:
242  return lhs.toInt() < rhs.toInt();
243  case QVariant::UInt:
244  return lhs.toUInt() < rhs.toUInt();
245  case QVariant::LongLong:
246  return lhs.toLongLong() < rhs.toLongLong();
247  case QVariant::ULongLong:
248  return lhs.toULongLong() < rhs.toULongLong();
249  case QVariant::Double:
250  return lhs.toDouble() < rhs.toDouble();
251  case QVariant::Char:
252  return lhs.toChar() < rhs.toChar();
253  case QVariant::Date:
254  return lhs.toDate() < rhs.toDate();
255  case QVariant::Time:
256  return lhs.toTime() < rhs.toTime();
257  case QVariant::DateTime:
258  return lhs.toDateTime() < rhs.toDateTime();
259  default:
260  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
261  }
262 }
263 
264 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
265 {
266  return ! qgsVariantLessThan( lhs, rhs );
267 }
268 
270 {
271  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
272  path.endsWith( ".zip", Qt::CaseInsensitive ) )
273  return "/vsizip/";
274  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
275  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
276  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
277  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
278  return "/vsitar/";
279  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
280  path.endsWith( ".gz", Qt::CaseInsensitive ) )
281  return "/vsigzip/";
282  else
283  return "";
284 }
static const char * QGIS_VERSION
Definition: qgis.h:40
qlonglong toLongLong(bool *ok) const
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:304
static UnitType fromLiteral(QString literal, QGis::UnitType defaultType=UnknownUnit)
Converts from the canonical name to the type value.
Definition: qgis.cpp:102
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:300
static int permissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, eg allowing for incorrect numbers of digits betw...
Definition: qgis.cpp:195
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:202
int localeAwareCompare(const QString &other) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
UnitType
Map units that qgis supports.
Definition: qgis.h:229
QDateTime toDateTime() const
#define FEET_TO_METER
static double permissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, eg allowing for incorrect numbers of digits betwee...
Definition: qgis.cpp:188
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:217
QTime toTime() const
#define DEGREE_TO_METER
QString qgsVsiPrefix(QString path)
Definition: qgis.cpp:269
static const double DEFAULT_IDENTIFY_RADIUS
Old search radius in % of canvas width.
Definition: qgis.h:288
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:264
qulonglong toULongLong(bool *ok) const
double toDouble(const QString &s, bool *ok) const
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:237
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:74
QLocale system()
#define NMILE_TO_METER
uint toUInt(bool *ok) const
int toInt(bool *ok) const
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:52
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:295
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:72
const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:55
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:292
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:119
QDate toDate() const
const QString PROJECT_SCALES
Definition: qgis.cpp:68
QString translate(const char *context, const char *sourceText, const char *disambiguation, Encoding encoding)
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:136
int toInt(const QString &s, bool *ok, int base) const
static UnitType fromTr(QString literal, QGis::UnitType defaultType=UnknownUnit)
Provides type value from translated version.
Definition: qgis.cpp:124
double toDouble(bool *ok) const
static QString toLiteral(QGis::UnitType unit)
Provides the canonical name of the type value.
Definition: qgis.cpp:114
Type type() const
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:232
static const int QGIS_VERSION_INT
Definition: qgis.h:42
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:308
QChar toChar() const