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