QGIS API Documentation  2.99.0-Master (19b062c)
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 #include "qgswkbtypes.h"
31 
32 #include <ogr_api.h>
33 
34 // Version constants
35 //
36 
37 // Version string
38 const QString Qgis::QGIS_VERSION( QStringLiteral( VERSION ) );
39 
40 // development version
41 const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
42 
43 // Version number used for comparing versions using the
44 // "Check QGIS Version" function
45 const int Qgis::QGIS_VERSION_INT = VERSION_INT;
46 
47 // Release name
48 const QString Qgis::QGIS_RELEASE_NAME( QStringLiteral( RELEASE_NAME ) );
49 
50 const QString GEOPROJ4 = QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" );
51 
52 const QString GEOWKT =
53  "GEOGCS[\"WGS 84\", "
54  " DATUM[\"WGS_1984\", "
55  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
56  " AUTHORITY[\"EPSG\",7030]], "
57  " TOWGS84[0,0,0,0,0,0,0], "
58  " AUTHORITY[\"EPSG\",6326]], "
59  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
60  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
61  " AXIS[\"Lat\",NORTH], "
62  " AXIS[\"Long\",EAST], "
63  " AUTHORITY[\"EPSG\",4326]]";
64 
65 const QString PROJECT_SCALES =
66  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
67  "1:10000,1:5000,1:2500,1:1000,1:500";
68 
69 const QString GEO_EPSG_CRS_AUTHID = QStringLiteral( "EPSG:4326" );
70 
71 const QString GEO_NONE = QStringLiteral( "NONE" );
72 
73 const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
74 
75 const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
76 
77 const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
78 
79 const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
80 
81 const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
82 
83 const double Qgis::SCALE_PRECISION = 0.9999999999;
84 
85 const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
86 
87 #ifdef Q_OS_WIN
88 const double Qgis::UI_SCALE_FACTOR = 1.5;
89 #else
90 const double Qgis::UI_SCALE_FACTOR = 1;
91 #endif
92 
93 double qgsPermissiveToDouble( QString string, bool &ok )
94 {
95  //remove any thousands separators
96  string.remove( QLocale::system().groupSeparator() );
97  return QLocale::system().toDouble( string, &ok );
98 }
99 
100 int qgsPermissiveToInt( QString string, bool &ok )
101 {
102  //remove any thousands separators
103  string.remove( QLocale::system().groupSeparator() );
104  return QLocale::system().toInt( string, &ok );
105 }
106 
107 void *qgsMalloc( size_t size )
108 {
109  if ( size == 0 || long( size ) < 0 )
110  {
111  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
112  return nullptr;
113  }
114  void *p = malloc( size );
115  if ( !p )
116  {
117  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
118  }
119  return p;
120 }
121 
122 void *qgsCalloc( size_t nmemb, size_t size )
123 {
124  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
125  {
126  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
127  return nullptr;
128  }
129  void *p = qgsMalloc( nmemb * size );
130  if ( p )
131  {
132  memset( p, 0, nmemb * size );
133  }
134  return p;
135 }
136 
137 void qgsFree( void *ptr )
138 {
139  free( ptr );
140 }
141 
142 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
143 {
144  // invalid < NULL < any value
145  if ( !lhs.isValid() )
146  return rhs.isValid();
147  else if ( lhs.isNull() )
148  return rhs.isValid() && !rhs.isNull();
149  else if ( !rhs.isValid() || rhs.isNull() )
150  return false;
151 
152  switch ( lhs.type() )
153  {
154  case QVariant::Int:
155  return lhs.toInt() < rhs.toInt();
156  case QVariant::UInt:
157  return lhs.toUInt() < rhs.toUInt();
158  case QVariant::LongLong:
159  return lhs.toLongLong() < rhs.toLongLong();
160  case QVariant::ULongLong:
161  return lhs.toULongLong() < rhs.toULongLong();
162  case QVariant::Double:
163  return lhs.toDouble() < rhs.toDouble();
164  case QVariant::Char:
165  return lhs.toChar() < rhs.toChar();
166  case QVariant::Date:
167  return lhs.toDate() < rhs.toDate();
168  case QVariant::Time:
169  return lhs.toTime() < rhs.toTime();
170  case QVariant::DateTime:
171  return lhs.toDateTime() < rhs.toDateTime();
172  case QVariant::Bool:
173  return lhs.toBool() < rhs.toBool();
174 
175  case QVariant::List:
176  {
177  const QList<QVariant> &lhsl = lhs.toList();
178  const QList<QVariant> &rhsl = rhs.toList();
179 
180  int i, n = std::min( lhsl.size(), rhsl.size() );
181  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
182  ;
183 
184  if ( i == n )
185  return lhsl.size() < rhsl.size();
186  else
187  return qgsVariantLessThan( lhsl[i], rhsl[i] );
188  }
189 
190  case QVariant::StringList:
191  {
192  const QStringList &lhsl = lhs.toStringList();
193  const QStringList &rhsl = rhs.toStringList();
194 
195  int i, n = std::min( lhsl.size(), rhsl.size() );
196  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
197  ;
198 
199  if ( i == n )
200  return lhsl.size() < rhsl.size();
201  else
202  return lhsl[i] < rhsl[i];
203  }
204 
205  default:
206  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
207  }
208 }
209 
210 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
211 {
212  return ! qgsVariantLessThan( lhs, rhs );
213 }
214 
215 QString qgsVsiPrefix( const QString &path )
216 {
217  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) ||
218  path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
219  return QStringLiteral( "/vsizip/" );
220  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
221  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
222  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
223  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
224  return QStringLiteral( "/vsitar/" );
225  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
226  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
227  return QStringLiteral( "/vsigzip/" );
228  else
229  return QLatin1String( "" );
230 }
231 
232 uint qHash( const QVariant &variant )
233 {
234  if ( !variant.isValid() || variant.isNull() )
235  return std::numeric_limits<uint>::max();
236 
237  switch ( variant.type() )
238  {
239  case QVariant::Int:
240  return qHash( variant.toInt() );
241  case QVariant::UInt:
242  return qHash( variant.toUInt() );
243  case QVariant::Bool:
244  return qHash( variant.toBool() );
245  case QVariant::Double:
246  return qHash( variant.toDouble() );
247  case QVariant::LongLong:
248  return qHash( variant.toLongLong() );
249  case QVariant::ULongLong:
250  return qHash( variant.toULongLong() );
251  case QVariant::String:
252  return qHash( variant.toString() );
253  case QVariant::Char:
254  return qHash( variant.toChar() );
255  case QVariant::List:
256 
257 #if QT_VERSION >= 0x050600
258  return qHash( variant.toList() );
259 #else
260  {
261  QVariantList list = variant.toList();
262  if ( list.isEmpty() )
263  return -1;
264  else
265  return qHash( list.at( 0 ) );
266  }
267 #endif
268  case QVariant::StringList:
269 #if QT_VERSION >= 0x050600
270  return qHash( variant.toStringList() );
271 #else
272  {
273  QStringList list = variant.toStringList();
274  if ( list.isEmpty() )
275  return -1;
276  else
277  return qHash( list.at( 0 ) );
278  }
279 #endif
280  case QVariant::ByteArray:
281  return qHash( variant.toByteArray() );
282  case QVariant::Date:
283  return qHash( variant.toDate() );
284  case QVariant::Time:
285  return qHash( variant.toTime() );
286  case QVariant::DateTime:
287  return qHash( variant.toDateTime() );
288  case QVariant::Url:
289  case QVariant::Locale:
290  case QVariant::RegExp:
291  return qHash( variant.toString() );
292  default:
293  break;
294  }
295 
296  return std::numeric_limits<uint>::max();
297 }
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:63
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:69
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:107
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:138
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition: qgis.cpp:93
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:122
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:108
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is greater than the second.
Definition: qgis.cpp:210
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:65
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
Definition: qgis.cpp:100
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition: qgis.cpp:142
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:71
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value for 2.5d geometry This value have to be assigned to the Z coordinate for t...
Definition: qgis.h:131
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:50
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:99
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:69
const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:52
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:232
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:102
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:67
const QString PROJECT_SCALES
Definition: qgis.cpp:65
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:215
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:118
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:113
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:137
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:125