/
qgis.cpp
347 lines (297 loc) · 9.71 KB
/
qgis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***************************************************************************
qgis.cpp
-------------------
begin : 2007
copyright : (C) 2007 by Gary E. Sherman
email : sherman@mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgis.h"
#ifndef QGSVERSION
#include "qgsversion.h"
#endif
#include <QCoreApplication>
#include <QColor>
#include <QDate>
#include <QTime>
#include <QLocale>
#include <QDateTime>
#include "qgsconfig.h"
#include "qgslogger.h"
#include "qgswkbtypes.h"
#include <gdal.h>
#include <geos_c.h>
#include <ogr_api.h>
#define xstr(x) str(x)
#define str(x) #x
// Version constants
//
// development version
const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
const double Qgis::SCALE_PRECISION = 0.9999999999;
const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
const double Qgis::DEFAULT_M_COORDINATE = 0.0;
const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
const QgsTolerance::UnitType Qgis::DEFAULT_SNAP_UNITS = QgsTolerance::Pixels;
#ifdef Q_OS_WIN
const double Qgis::UI_SCALE_FACTOR = 1.5;
#else
const double Qgis::UI_SCALE_FACTOR = 1;
#endif
double qgsPermissiveToDouble( QString string, bool &ok )
{
//remove any thousands separators
string.remove( QLocale().groupSeparator() );
return QLocale().toDouble( string, &ok );
}
int qgsPermissiveToInt( QString string, bool &ok )
{
//remove any thousands separators
string.remove( QLocale().groupSeparator() );
return QLocale().toInt( string, &ok );
}
qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
{
//remove any thousands separators
string.remove( QLocale().groupSeparator() );
return QLocale().toLongLong( string, &ok );
}
void *qgsMalloc( size_t size )
{
if ( size == 0 || long( size ) < 0 )
{
QgsDebugMsg( QStringLiteral( "Negative or zero size %1." ).arg( size ) );
return nullptr;
}
void *p = malloc( size );
if ( !p )
{
QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
}
return p;
}
void *qgsCalloc( size_t nmemb, size_t size )
{
if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
{
QgsDebugMsg( QStringLiteral( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
return nullptr;
}
void *p = qgsMalloc( nmemb * size );
if ( p )
{
memset( p, 0, nmemb * size );
}
return p;
}
void qgsFree( void *ptr )
{
free( ptr );
}
bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
{
// invalid < NULL < any value
if ( !lhs.isValid() )
return rhs.isValid();
else if ( lhs.isNull() )
return rhs.isValid() && !rhs.isNull();
else if ( !rhs.isValid() || rhs.isNull() )
return false;
switch ( lhs.type() )
{
case QVariant::Int:
return lhs.toInt() < rhs.toInt();
case QVariant::UInt:
return lhs.toUInt() < rhs.toUInt();
case QVariant::LongLong:
return lhs.toLongLong() < rhs.toLongLong();
case QVariant::ULongLong:
return lhs.toULongLong() < rhs.toULongLong();
case QVariant::Double:
return lhs.toDouble() < rhs.toDouble();
case QVariant::Char:
return lhs.toChar() < rhs.toChar();
case QVariant::Date:
return lhs.toDate() < rhs.toDate();
case QVariant::Time:
return lhs.toTime() < rhs.toTime();
case QVariant::DateTime:
return lhs.toDateTime() < rhs.toDateTime();
case QVariant::Bool:
return lhs.toBool() < rhs.toBool();
case QVariant::List:
{
const QList<QVariant> &lhsl = lhs.toList();
const QList<QVariant> &rhsl = rhs.toList();
int i, n = std::min( lhsl.size(), rhsl.size() );
for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && qgsVariantEqual( lhsl[i], rhsl[i] ); i++ )
;
if ( i == n )
return lhsl.size() < rhsl.size();
else
return qgsVariantLessThan( lhsl[i], rhsl[i] );
}
case QVariant::StringList:
{
const QStringList &lhsl = lhs.toStringList();
const QStringList &rhsl = rhs.toStringList();
int i, n = std::min( lhsl.size(), rhsl.size() );
for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
;
if ( i == n )
return lhsl.size() < rhsl.size();
else
return lhsl[i] < rhsl[i];
}
default:
return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
}
}
bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
{
return ! qgsVariantLessThan( lhs, rhs );
}
QString qgsVsiPrefix( const QString &path )
{
if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) )
return QStringLiteral( "/vsizip/" );
else if ( path.endsWith( QLatin1String( ".shp.zip" ), Qt::CaseInsensitive ) )
{
// GDAL 3.1 Shapefile driver directly handles .shp.zip files
if ( GDALIdentifyDriverEx( path.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr ) )
return QString();
return QStringLiteral( "/vsizip/" );
}
else if ( path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
return QStringLiteral( "/vsizip/" );
else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
return QStringLiteral( "/vsitar/" );
else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
return QStringLiteral( "/vsigzip/" );
else
return QString();
}
uint qHash( const QVariant &variant )
{
if ( !variant.isValid() || variant.isNull() )
return std::numeric_limits<uint>::max();
switch ( variant.type() )
{
case QVariant::Int:
return qHash( variant.toInt() );
case QVariant::UInt:
return qHash( variant.toUInt() );
case QVariant::Bool:
return qHash( variant.toBool() );
case QVariant::Double:
return qHash( variant.toDouble() );
case QVariant::LongLong:
return qHash( variant.toLongLong() );
case QVariant::ULongLong:
return qHash( variant.toULongLong() );
case QVariant::String:
return qHash( variant.toString() );
case QVariant::Char:
return qHash( variant.toChar() );
case QVariant::List:
return qHash( variant.toList() );
case QVariant::StringList:
return qHash( variant.toStringList() );
case QVariant::ByteArray:
return qHash( variant.toByteArray() );
case QVariant::Date:
return qHash( variant.toDate() );
case QVariant::Time:
return qHash( variant.toTime() );
case QVariant::DateTime:
return qHash( variant.toDateTime() );
case QVariant::Url:
case QVariant::Locale:
case QVariant::RegularExpression:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
case QVariant::RegExp:
#endif
return qHash( variant.toString() );
default:
break;
}
return std::numeric_limits<uint>::max();
}
bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
{
return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
}
QString Qgis::defaultProjectScales()
{
return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
"1:10000,1:5000,1:2500,1:1000,1:500" );
}
QString Qgis::version()
{
return QString::fromUtf8( VERSION );
}
int Qgis::versionInt()
{
// Version number used for comparing versions using the
// "Check QGIS Version" function
return VERSION_INT;
}
QString Qgis::releaseName()
{
return QString::fromUtf8( RELEASE_NAME );
}
QString Qgis::devVersion()
{
return QString::fromUtf8( QGIS_DEV_VERSION );
}
QString Qgis::geosVersion()
{
return GEOSversion();
}
int Qgis::geosVersionInt()
{
static const int version = QStringLiteral( "%1%2%3" )
.arg( GEOS_VERSION_MAJOR, 2, 10, QChar( '0' ) )
.arg( GEOS_VERSION_MINOR, 2, 10, QChar( '0' ) )
.arg( geosVersionPatch(), 2, 10, QChar( '0' ) ).toInt()
;
return version;
}
int Qgis::geosVersionMajor()
{
return GEOS_VERSION_MAJOR;
}
int Qgis::geosVersionMinor()
{
return GEOS_VERSION_MINOR;
}
int Qgis::geosVersionPatch()
{
static const int version = atoi( xstr( GEOS_VERSION_PATCH ) );
return version;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
template<>
bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
{
// qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
// this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
}
#endif