QGIS API Documentation  2.99.0-Master (314842d)
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 "geometry/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 
76 const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
77 
78 const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
79 
80 const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
81 
82 const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
83 
84 const double Qgis::SCALE_PRECISION = 0.9999999999;
85 
86 const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
87 
88 double qgsPermissiveToDouble( QString string, bool &ok )
89 {
90  //remove any thousands separators
91  string.remove( QLocale::system().groupSeparator() );
92  return QLocale::system().toDouble( string, &ok );
93 }
94 
95 int qgsPermissiveToInt( QString string, bool &ok )
96 {
97  //remove any thousands separators
98  string.remove( QLocale::system().groupSeparator() );
99  return QLocale::system().toInt( string, &ok );
100 }
101 
102 void *qgsMalloc( size_t size )
103 {
104  if ( size == 0 || long( size ) < 0 )
105  {
106  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
107  return nullptr;
108  }
109  void *p = malloc( size );
110  if ( !p )
111  {
112  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
113  }
114  return p;
115 }
116 
117 void *qgsCalloc( size_t nmemb, size_t size )
118 {
119  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
120  {
121  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
122  return nullptr;
123  }
124  void *p = qgsMalloc( nmemb * size );
125  if ( p )
126  {
127  memset( p, 0, nmemb * size );
128  }
129  return p;
130 }
131 
132 void qgsFree( void *ptr )
133 {
134  free( ptr );
135 }
136 
137 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
138 {
139  // invalid < NULL < any value
140  if ( !lhs.isValid() )
141  return rhs.isValid();
142  else if ( lhs.isNull() )
143  return rhs.isValid() && !rhs.isNull();
144  else if ( !rhs.isValid() || rhs.isNull() )
145  return false;
146 
147  switch ( lhs.type() )
148  {
149  case QVariant::Int:
150  return lhs.toInt() < rhs.toInt();
151  case QVariant::UInt:
152  return lhs.toUInt() < rhs.toUInt();
153  case QVariant::LongLong:
154  return lhs.toLongLong() < rhs.toLongLong();
155  case QVariant::ULongLong:
156  return lhs.toULongLong() < rhs.toULongLong();
157  case QVariant::Double:
158  return lhs.toDouble() < rhs.toDouble();
159  case QVariant::Char:
160  return lhs.toChar() < rhs.toChar();
161  case QVariant::Date:
162  return lhs.toDate() < rhs.toDate();
163  case QVariant::Time:
164  return lhs.toTime() < rhs.toTime();
165  case QVariant::DateTime:
166  return lhs.toDateTime() < rhs.toDateTime();
167  case QVariant::Bool:
168  return lhs.toBool() < rhs.toBool();
169 
170  case QVariant::List:
171  {
172  const QList<QVariant> &lhsl = lhs.toList();
173  const QList<QVariant> &rhsl = rhs.toList();
174 
175  int i, n = qMin( lhsl.size(), rhsl.size() );
176  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
177  ;
178 
179  if ( i == n )
180  return lhsl.size() < rhsl.size();
181  else
182  return qgsVariantLessThan( lhsl[i], rhsl[i] );
183  }
184 
185  case QVariant::StringList:
186  {
187  const QStringList &lhsl = lhs.toStringList();
188  const QStringList &rhsl = rhs.toStringList();
189 
190  int i, n = qMin( lhsl.size(), rhsl.size() );
191  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
192  ;
193 
194  if ( i == n )
195  return lhsl.size() < rhsl.size();
196  else
197  return lhsl[i] < rhsl[i];
198  }
199 
200  default:
201  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
202  }
203 }
204 
205 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
206 {
207  return ! qgsVariantLessThan( lhs, rhs );
208 }
209 
210 QString qgsVsiPrefix( const QString &path )
211 {
212  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) ||
213  path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
214  return QStringLiteral( "/vsizip/" );
215  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
216  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
217  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
218  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
219  return QStringLiteral( "/vsitar/" );
220  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
221  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
222  return QStringLiteral( "/vsigzip/" );
223  else
224  return QLatin1String( "" );
225 }
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:47
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:53
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:102
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
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:88
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:117
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:101
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:205
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:49
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:95
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:137
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:120
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:93
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
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:96
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:51
const QString PROJECT_SCALES
Definition: qgis.cpp:65
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:210
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:109
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:105
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:132
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:115