QGIS API Documentation  3.6.0-Noosa (5873452)
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 <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 const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
88 
90 
91 #ifdef Q_OS_WIN
92 const double Qgis::UI_SCALE_FACTOR = 1.5;
93 #else
94 const double Qgis::UI_SCALE_FACTOR = 1;
95 #endif
96 
97 double qgsPermissiveToDouble( QString string, bool &ok )
98 {
99  //remove any thousands separators
100  string.remove( QLocale().groupSeparator() );
101  return QLocale().toDouble( string, &ok );
102 }
103 
104 int qgsPermissiveToInt( QString string, bool &ok )
105 {
106  //remove any thousands separators
107  string.remove( QLocale().groupSeparator() );
108  return QLocale().toInt( string, &ok );
109 }
110 
111 qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
112 {
113  //remove any thousands separators
114  string.remove( QLocale().groupSeparator() );
115  return QLocale().toLongLong( string, &ok );
116 }
117 
118 void *qgsMalloc( size_t size )
119 {
120  if ( size == 0 || long( size ) < 0 )
121  {
122  QgsDebugMsg( QStringLiteral( "Negative or zero size %1." ).arg( size ) );
123  return nullptr;
124  }
125  void *p = malloc( size );
126  if ( !p )
127  {
128  QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
129  }
130  return p;
131 }
132 
133 void *qgsCalloc( size_t nmemb, size_t size )
134 {
135  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
136  {
137  QgsDebugMsg( QStringLiteral( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
138  return nullptr;
139  }
140  void *p = qgsMalloc( nmemb * size );
141  if ( p )
142  {
143  memset( p, 0, nmemb * size );
144  }
145  return p;
146 }
147 
148 void qgsFree( void *ptr )
149 {
150  free( ptr );
151 }
152 
153 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
154 {
155  // invalid < NULL < any value
156  if ( !lhs.isValid() )
157  return rhs.isValid();
158  else if ( lhs.isNull() )
159  return rhs.isValid() && !rhs.isNull();
160  else if ( !rhs.isValid() || rhs.isNull() )
161  return false;
162 
163  switch ( lhs.type() )
164  {
165  case QVariant::Int:
166  return lhs.toInt() < rhs.toInt();
167  case QVariant::UInt:
168  return lhs.toUInt() < rhs.toUInt();
169  case QVariant::LongLong:
170  return lhs.toLongLong() < rhs.toLongLong();
171  case QVariant::ULongLong:
172  return lhs.toULongLong() < rhs.toULongLong();
173  case QVariant::Double:
174  return lhs.toDouble() < rhs.toDouble();
175  case QVariant::Char:
176  return lhs.toChar() < rhs.toChar();
177  case QVariant::Date:
178  return lhs.toDate() < rhs.toDate();
179  case QVariant::Time:
180  return lhs.toTime() < rhs.toTime();
181  case QVariant::DateTime:
182  return lhs.toDateTime() < rhs.toDateTime();
183  case QVariant::Bool:
184  return lhs.toBool() < rhs.toBool();
185 
186  case QVariant::List:
187  {
188  const QList<QVariant> &lhsl = lhs.toList();
189  const QList<QVariant> &rhsl = rhs.toList();
190 
191  int i, n = std::min( lhsl.size(), rhsl.size() );
192  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && qgsVariantEqual( lhsl[i], rhsl[i] ); i++ )
193  ;
194 
195  if ( i == n )
196  return lhsl.size() < rhsl.size();
197  else
198  return qgsVariantLessThan( lhsl[i], rhsl[i] );
199  }
200 
201  case QVariant::StringList:
202  {
203  const QStringList &lhsl = lhs.toStringList();
204  const QStringList &rhsl = rhs.toStringList();
205 
206  int i, n = std::min( lhsl.size(), rhsl.size() );
207  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
208  ;
209 
210  if ( i == n )
211  return lhsl.size() < rhsl.size();
212  else
213  return lhsl[i] < rhsl[i];
214  }
215 
216  default:
217  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
218  }
219 }
220 
221 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
222 {
223  return ! qgsVariantLessThan( lhs, rhs );
224 }
225 
226 QString qgsVsiPrefix( const QString &path )
227 {
228  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) ||
229  path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
230  return QStringLiteral( "/vsizip/" );
231  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
232  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
233  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
234  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
235  return QStringLiteral( "/vsitar/" );
236  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
237  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
238  return QStringLiteral( "/vsigzip/" );
239  else
240  return QString();
241 }
242 
243 uint qHash( const QVariant &variant )
244 {
245  if ( !variant.isValid() || variant.isNull() )
246  return std::numeric_limits<uint>::max();
247 
248  switch ( variant.type() )
249  {
250  case QVariant::Int:
251  return qHash( variant.toInt() );
252  case QVariant::UInt:
253  return qHash( variant.toUInt() );
254  case QVariant::Bool:
255  return qHash( variant.toBool() );
256  case QVariant::Double:
257  return qHash( variant.toDouble() );
258  case QVariant::LongLong:
259  return qHash( variant.toLongLong() );
260  case QVariant::ULongLong:
261  return qHash( variant.toULongLong() );
262  case QVariant::String:
263  return qHash( variant.toString() );
264  case QVariant::Char:
265  return qHash( variant.toChar() );
266  case QVariant::List:
267  return qHash( variant.toList() );
268  case QVariant::StringList:
269  return qHash( variant.toStringList() );
270  case QVariant::ByteArray:
271  return qHash( variant.toByteArray() );
272  case QVariant::Date:
273  return qHash( variant.toDate() );
274  case QVariant::Time:
275  return qHash( variant.toTime() );
276  case QVariant::DateTime:
277  return qHash( variant.toDateTime() );
278  case QVariant::Url:
279  case QVariant::Locale:
280  case QVariant::RegExp:
281  return qHash( variant.toString() );
282  default:
283  break;
284  }
285 
286  return std::numeric_limits<uint>::max();
287 }
288 
289 bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
290 {
291  return lhs.isNull() == rhs.isNull() && lhs == rhs;
292 }
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:51
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:57
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:118
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:139
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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:97
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:133
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:40
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:109
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:221
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:53
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:104
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:153
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:132
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:145
Pixels unit of tolerance.
Definition: qgstolerance.h:45
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:50
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:111
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:100
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:69
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:151
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:243
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:103
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:55
const QString PROJECT_SCALES
Definition: qgis.cpp:65
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:226
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:119
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, NULL values are treated as equal...
Definition: qgis.cpp:289
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:114
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:148
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:126