QGIS API Documentation  3.21.0-Master (402cf1129e)
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 <QLocale>
27 #include <QDateTime>
28 #include "qgsconfig.h"
29 #include "qgslogger.h"
30 #include "qgswkbtypes.h"
31 
32 #include <gdal.h>
33 #include <geos_c.h>
34 #include <ogr_api.h>
35 
36 #define xstr(x) str(x)
37 #define str(x) #x
38 
39 // Version constants
40 //
41 
42 // development version
43 const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
44 
45 const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
46 
47 const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
48 
49 const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
50 
51 const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
52 
53 const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
54 
55 const double Qgis::SCALE_PRECISION = 0.9999999999;
56 
57 const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
58 
59 const double Qgis::DEFAULT_M_COORDINATE = 0.0;
60 
61 const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
62 
64 
65 #ifdef Q_OS_WIN
66 const double Qgis::UI_SCALE_FACTOR = 1.5;
67 #else
68 const double Qgis::UI_SCALE_FACTOR = 1;
69 #endif
70 
71 double qgsPermissiveToDouble( QString string, bool &ok )
72 {
73  //remove any thousands separators
74  string.remove( QLocale().groupSeparator() );
75  return QLocale().toDouble( string, &ok );
76 }
77 
78 int qgsPermissiveToInt( QString string, bool &ok )
79 {
80  //remove any thousands separators
81  string.remove( QLocale().groupSeparator() );
82  return QLocale().toInt( string, &ok );
83 }
84 
85 qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
86 {
87  //remove any thousands separators
88  string.remove( QLocale().groupSeparator() );
89  return QLocale().toLongLong( string, &ok );
90 }
91 
92 void *qgsMalloc( size_t size )
93 {
94  if ( size == 0 || long( size ) < 0 )
95  {
96  QgsDebugMsg( QStringLiteral( "Negative or zero size %1." ).arg( size ) );
97  return nullptr;
98  }
99  void *p = malloc( size );
100  if ( !p )
101  {
102  QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
103  }
104  return p;
105 }
106 
107 void *qgsCalloc( size_t nmemb, size_t size )
108 {
109  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
110  {
111  QgsDebugMsg( QStringLiteral( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
112  return nullptr;
113  }
114  void *p = qgsMalloc( nmemb * size );
115  if ( p )
116  {
117  memset( p, 0, nmemb * size );
118  }
119  return p;
120 }
121 
122 void qgsFree( void *ptr )
123 {
124  free( ptr );
125 }
126 
127 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
128 {
129  // invalid < NULL < any value
130  if ( !lhs.isValid() )
131  return rhs.isValid();
132  else if ( lhs.isNull() )
133  return rhs.isValid() && !rhs.isNull();
134  else if ( !rhs.isValid() || rhs.isNull() )
135  return false;
136 
137  switch ( lhs.type() )
138  {
139  case QVariant::Int:
140  return lhs.toInt() < rhs.toInt();
141  case QVariant::UInt:
142  return lhs.toUInt() < rhs.toUInt();
143  case QVariant::LongLong:
144  return lhs.toLongLong() < rhs.toLongLong();
145  case QVariant::ULongLong:
146  return lhs.toULongLong() < rhs.toULongLong();
147  case QVariant::Double:
148  return lhs.toDouble() < rhs.toDouble();
149  case QVariant::Char:
150  return lhs.toChar() < rhs.toChar();
151  case QVariant::Date:
152  return lhs.toDate() < rhs.toDate();
153  case QVariant::Time:
154  return lhs.toTime() < rhs.toTime();
155  case QVariant::DateTime:
156  return lhs.toDateTime() < rhs.toDateTime();
157  case QVariant::Bool:
158  return lhs.toBool() < rhs.toBool();
159 
160  case QVariant::List:
161  {
162  const QList<QVariant> &lhsl = lhs.toList();
163  const QList<QVariant> &rhsl = rhs.toList();
164 
165  int i, n = std::min( lhsl.size(), rhsl.size() );
166  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && qgsVariantEqual( lhsl[i], rhsl[i] ); i++ )
167  ;
168 
169  if ( i == n )
170  return lhsl.size() < rhsl.size();
171  else
172  return qgsVariantLessThan( lhsl[i], rhsl[i] );
173  }
174 
175  case QVariant::StringList:
176  {
177  const QStringList &lhsl = lhs.toStringList();
178  const QStringList &rhsl = rhs.toStringList();
179 
180  int i, n = std::min( lhsl.size(), rhsl.size() );
181  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
182  ;
183 
184  if ( i == n )
185  return lhsl.size() < rhsl.size();
186  else
187  return lhsl[i] < rhsl[i];
188  }
189 
190  default:
191  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
192  }
193 }
194 
195 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
196 {
197  return ! qgsVariantLessThan( lhs, rhs );
198 }
199 
200 QString qgsVsiPrefix( const QString &path )
201 {
202  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) )
203  return QStringLiteral( "/vsizip/" );
204  else if ( path.endsWith( QLatin1String( ".shp.zip" ), Qt::CaseInsensitive ) )
205  {
206  // GDAL 3.1 Shapefile driver directly handles .shp.zip files
207  if ( GDALIdentifyDriverEx( path.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr ) )
208  return QString();
209  return QStringLiteral( "/vsizip/" );
210  }
211  else if ( path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
212  return QStringLiteral( "/vsizip/" );
213  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
214  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
215  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
216  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
217  return QStringLiteral( "/vsitar/" );
218  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
219  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
220  return QStringLiteral( "/vsigzip/" );
221  else
222  return QString();
223 }
224 
225 uint qHash( const QVariant &variant )
226 {
227  if ( !variant.isValid() || variant.isNull() )
228  return std::numeric_limits<uint>::max();
229 
230  switch ( variant.type() )
231  {
232  case QVariant::Int:
233  return qHash( variant.toInt() );
234  case QVariant::UInt:
235  return qHash( variant.toUInt() );
236  case QVariant::Bool:
237  return qHash( variant.toBool() );
238  case QVariant::Double:
239  return qHash( variant.toDouble() );
240  case QVariant::LongLong:
241  return qHash( variant.toLongLong() );
242  case QVariant::ULongLong:
243  return qHash( variant.toULongLong() );
244  case QVariant::String:
245  return qHash( variant.toString() );
246  case QVariant::Char:
247  return qHash( variant.toChar() );
248  case QVariant::List:
249  return qHash( variant.toList() );
250  case QVariant::StringList:
251  return qHash( variant.toStringList() );
252  case QVariant::ByteArray:
253  return qHash( variant.toByteArray() );
254  case QVariant::Date:
255  return qHash( variant.toDate() );
256  case QVariant::Time:
257  return qHash( variant.toTime() );
258  case QVariant::DateTime:
259  return qHash( variant.toDateTime() );
260  case QVariant::Url:
261  case QVariant::Locale:
262  case QVariant::RegularExpression:
263 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
264  case QVariant::RegExp:
265 #endif
266  return qHash( variant.toString() );
267  default:
268  break;
269  }
270 
271  return std::numeric_limits<uint>::max();
272 }
273 
274 bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
275 {
276  return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
277 }
278 
280 {
281  return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
282  "1:10000,1:5000,1:2500,1:1000,1:500" );
283 }
284 
285 QString Qgis::version()
286 {
287  return QString::fromUtf8( VERSION );
288 }
289 
291 {
292  // Version number used for comparing versions using the
293  // "Check QGIS Version" function
294  return VERSION_INT;
295 }
296 
298 {
299  return QString::fromUtf8( RELEASE_NAME );
300 }
301 
303 {
304  return QString::fromUtf8( QGIS_DEV_VERSION );
305 }
306 
308 {
309  return GEOSversion();
310 }
311 
313 {
314  static const int version = QStringLiteral( "%1%2%3" )
315  .arg( GEOS_VERSION_MAJOR, 2, 10, QChar( '0' ) )
316  .arg( GEOS_VERSION_MINOR, 2, 10, QChar( '0' ) )
317  .arg( geosVersionPatch(), 2, 10, QChar( '0' ) ).toInt()
318  ;
319  return version;
320 }
321 
323 {
324  return GEOS_VERSION_MAJOR;
325 }
326 
328 {
329  return GEOS_VERSION_MINOR;
330 }
331 
333 {
334  static const int version = atoi( xstr( GEOS_VERSION_PATCH ) );
335  return version;
336 }
337 
338 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
339 template<>
340 bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
341 {
342  // qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
343  // this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
344  return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
345 }
346 #endif
347 
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:551
static QString version()
Version string.
Definition: qgis.cpp:285
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value.
Definition: qgis.h:566
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:89
static QString geosVersion()
GEOS string version linked.
Definition: qgis.cpp:307
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:586
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:592
static const double DEFAULT_M_COORDINATE
Default M coordinate value.
Definition: qgis.h:573
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:545
static int geosVersionPatch()
GEOS Patch version number linked.
Definition: qgis.cpp:332
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:539
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:559
static QString devVersion()
The development version.
Definition: qgis.cpp:302
static QString releaseName()
Release name.
Definition: qgis.cpp:297
static QString defaultProjectScales()
A string with default project scales.
Definition: qgis.cpp:279
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:531
static int geosVersionMajor()
GEOS Major version number linked.
Definition: qgis.cpp:322
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:528
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.cpp:290
static int geosVersionMinor()
GEOS Minor version number linked.
Definition: qgis.cpp:327
static int geosVersionInt()
GEOS version number linked.
Definition: qgis.cpp:312
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:580
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
@ Pixels
Pixels unit of tolerance.
Definition: qgstolerance.h:46
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
Definition: qgis.cpp:85
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, two NULL values are always treated a...
Definition: qgis.cpp:274
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:225
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:71
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:78
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:122
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:200
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:127
#define xstr(x)
Definition: qgis.cpp:36
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:107
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:92
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:195
#define QgsDebugMsg(str)
Definition: qgslogger.h:38