QGIS API Documentation  2.99.0-Master (c558d51)
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 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 QString Qgis::QGIS_RELEASE_NAME( QStringLiteral( RELEASE_NAME ) );
49 
50 #if GDAL_VERSION_NUM >= 1800
51 const QString GEOPROJ4 = QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" );
52 #else
53 const QString GEOPROJ4 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
54 #endif
55 
56 const QString GEOWKT =
57  "GEOGCS[\"WGS 84\", "
58  " DATUM[\"WGS_1984\", "
59  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
60  " AUTHORITY[\"EPSG\",7030]], "
61  " TOWGS84[0,0,0,0,0,0,0], "
62  " AUTHORITY[\"EPSG\",6326]], "
63  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
64  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
65  " AXIS[\"Lat\",NORTH], "
66  " AXIS[\"Long\",EAST], "
67  " AUTHORITY[\"EPSG\",4326]]";
68 
69 const QString PROJECT_SCALES =
70  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
71  "1:10000,1:5000,1:2500,1:1000,1:500";
72 
73 const QString GEO_EPSG_CRS_AUTHID = QStringLiteral( "EPSG:4326" );
74 
75 const QString GEO_NONE = QStringLiteral( "NONE" );
76 
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 double Qgis::SCALE_PRECISION = 0.9999999999;
89 
90 
91 double qgsPermissiveToDouble( QString string, bool &ok )
92 {
93  //remove any thousands separators
94  string.remove( QLocale::system().groupSeparator() );
95  return QLocale::system().toDouble( string, &ok );
96 }
97 
98 int qgsPermissiveToInt( QString string, bool &ok )
99 {
100  //remove any thousands separators
101  string.remove( QLocale::system().groupSeparator() );
102  return QLocale::system().toInt( string, &ok );
103 }
104 
105 void *qgsMalloc( size_t size )
106 {
107  if ( size == 0 || long( size ) < 0 )
108  {
109  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
110  return nullptr;
111  }
112  void *p = malloc( size );
113  if ( !p )
114  {
115  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
116  }
117  return p;
118 }
119 
120 void *qgsCalloc( size_t nmemb, size_t size )
121 {
122  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
123  {
124  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
125  return nullptr;
126  }
127  void *p = qgsMalloc( nmemb * size );
128  if ( p )
129  {
130  memset( p, 0, nmemb * size );
131  }
132  return p;
133 }
134 
135 void qgsFree( void *ptr )
136 {
137  free( ptr );
138 }
139 
140 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
141 {
142  // invalid < NULL < any value
143  if ( !lhs.isValid() )
144  return rhs.isValid();
145  else if ( lhs.isNull() )
146  return rhs.isValid() && !rhs.isNull();
147  else if ( !rhs.isValid() || rhs.isNull() )
148  return false;
149 
150  switch ( lhs.type() )
151  {
152  case QVariant::Int:
153  return lhs.toInt() < rhs.toInt();
154  case QVariant::UInt:
155  return lhs.toUInt() < rhs.toUInt();
156  case QVariant::LongLong:
157  return lhs.toLongLong() < rhs.toLongLong();
158  case QVariant::ULongLong:
159  return lhs.toULongLong() < rhs.toULongLong();
160  case QVariant::Double:
161  return lhs.toDouble() < rhs.toDouble();
162  case QVariant::Char:
163  return lhs.toChar() < rhs.toChar();
164  case QVariant::Date:
165  return lhs.toDate() < rhs.toDate();
166  case QVariant::Time:
167  return lhs.toTime() < rhs.toTime();
168  case QVariant::DateTime:
169  return lhs.toDateTime() < rhs.toDateTime();
170  case QVariant::Bool:
171  return lhs.toBool() < rhs.toBool();
172 
173  case QVariant::List:
174  {
175  const QList<QVariant> &lhsl = lhs.toList();
176  const QList<QVariant> &rhsl = rhs.toList();
177 
178  int i, n = qMin( lhsl.size(), rhsl.size() );
179  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
180  ;
181 
182  if ( i == n )
183  return lhsl.size() < rhsl.size();
184  else
185  return qgsVariantLessThan( lhsl[i], rhsl[i] );
186  }
187 
188  case QVariant::StringList:
189  {
190  const QStringList &lhsl = lhs.toStringList();
191  const QStringList &rhsl = rhs.toStringList();
192 
193  int i, n = qMin( lhsl.size(), rhsl.size() );
194  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
195  ;
196 
197  if ( i == n )
198  return lhsl.size() < rhsl.size();
199  else
200  return lhsl[i] < rhsl[i];
201  }
202 
203  default:
204  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
205  }
206 }
207 
208 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
209 {
210  return ! qgsVariantLessThan( lhs, rhs );
211 }
212 
213 QString qgsVsiPrefix( const QString& path )
214 {
215  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) ||
216  path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
217  return QStringLiteral( "/vsizip/" );
218  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
219  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
220  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
221  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
222  return QStringLiteral( "/vsitar/" );
223  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
224  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
225  return QStringLiteral( "/vsigzip/" );
226  else
227  return QLatin1String( "" );
228 }
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:108
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:52
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:105
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
double qgsPermissiveToDouble(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:91
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:120
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:100
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:208
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:48
int qgsPermissiveToInt(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:98
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:140
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:75
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:53
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:92
static double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:114
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:73
const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:56
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:95
const QString PROJECT_SCALES
Definition: qgis.cpp:69
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:213
static QString QGIS_VERSION
Version string.
Definition: qgis.h:46
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:104
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:135
static QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:50