QGIS API Documentation  2.15.0-Master (af20121)
qgis.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgis.h - QGIS namespace
3  -------------------
4  begin : Sat Jun 30 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGIS_H
19 #define QGIS_H
20 
21 #include <QEvent>
22 #include <QString>
23 #include <QRegExp>
24 #include <QMetaType>
25 #include <QVariant>
26 #include <QDateTime>
27 #include <QDate>
28 #include <QTime>
29 #include <QHash>
30 #include <stdlib.h>
31 #include <cfloat>
32 #include <cmath>
33 #include <qnumeric.h>
34 
35 #include <qgswkbtypes.h>
36 
40 class CORE_EXPORT QGis
41 {
42  public:
43  // Version constants
44  //
45  // Version string
46  static const char* QGIS_VERSION;
47  // Version number used for comparing versions using the "Check QGIS Version" function
48  static const int QGIS_VERSION_INT;
49  // Release name
50  static const char* QGIS_RELEASE_NAME;
51  // The development version
52  static const char* QGIS_DEV_VERSION;
53 
54  // Enumerations
55  //
56 
58  // Feature types
59  // @deprecated use QgsWKBTypes::Type
60  /* Q_DECL_DEPRECATED */
61  enum WkbType
62  {
63  WKBUnknown = 0,
64  WKBPoint = 1,
70  WKBNoGeometry = 100, //attributes only
71  WKBPoint25D = 0x80000001,
77  };
78 
80  // @deprecated use QgsWKBTypes::singleType
81  /* Q_DECL_DEPRECATED */
82  static WkbType singleType( WkbType type );
83 
85  // @deprecated use QgsWKBTypes::multiType
86  /* Q_DECL_DEPRECATED */
87  static WkbType multiType( WkbType type );
88 
90  // @deprecated use QgsWKBTypes::flatType
91  /* Q_DECL_DEPRECATED */
92  static WkbType flatType( WkbType type );
93 
95  // @deprecated use QgsWKBTypes::isSingleType
96  /* Q_DECL_DEPRECATED */
97  static bool isSingleType( WkbType type );
98 
100  // @deprecated use QgsWKBTypes::isMultiType
101  /* Q_DECL_DEPRECATED */
102  static bool isMultiType( WkbType type );
103 
104  // get dimension of points
105  // @deprecated use QgsWKBTypes::coordDimensions()
106  /* Q_DECL_DEPRECATED */
107  static int wkbDimensions( WkbType type );
108 
110  static QgsWKBTypes::Type fromOldWkbType( QGis::WkbType type );
111 
113  static QGis::WkbType fromNewWkbType( QgsWKBTypes::Type type );
114 
116  {
121  NoGeometry
122  };
123 
125  static const char *vectorGeometryType( GeometryType type );
126 
128  static const char *featureType( WkbType type );
129 
133  enum DataType
134  { UnknownDataType = 0, Byte = 1, UInt16 = 2, Int16 = 3, UInt32 = 4, Int32 = 5, Float32 = 6, Float64 = 7, CInt16 = 8, CInt32 = 9, CFloat32 = 10, CFloat64 = 11, ARGB32 = 12, ARGB32_Premultiplied = 13
151  };
152 
153 
158  //TODO QGIS 3.0 - clean up and move to QgsUnitTypes and rename to DistanceUnit
159  enum UnitType
160  {
161  Meters = 0,
162  Feet = 1,
163  Degrees = 2, //for 1.0 api backwards compatibility
164  NauticalMiles = 7,
165  Kilometers = 8,
166  Yards = 9,
167  Miles = 10,
169  UnknownUnit = 3,
171  // for [1.4;1.8] api compatibility
172  DecimalDegrees = 2, // was 2
173  DegreesMinutesSeconds = 2, // was 4
174  DegreesDecimalMinutes = 2, // was 5
175  };
176 
179  Q_DECL_DEPRECATED static QString toLiteral( QGis::UnitType unit );
180 
183  Q_DECL_DEPRECATED static UnitType fromLiteral( const QString& literal, QGis::UnitType defaultType = UnknownUnit );
184 
187  Q_DECL_DEPRECATED static QString tr( QGis::UnitType unit );
188 
191  Q_DECL_DEPRECATED static UnitType fromTr( const QString& literal, QGis::UnitType defaultType = UnknownUnit );
192 
195  Q_DECL_DEPRECATED static double fromUnitToUnitFactor( QGis::UnitType fromUnit, QGis::UnitType toUnit );
196 
205  static double permissiveToDouble( QString string, bool& ok );
206 
215  static int permissiveToInt( QString string, bool& ok );
216 
219  {
220  // These first two are useful for threads to alert their parent data providers
221 
223  ProviderExtentCalcEvent = ( QEvent::User + 1 ),
224 
226  ProviderCountCalcEvent
227  };
228 
231  Q_DECL_DEPRECATED static const double DEFAULT_IDENTIFY_RADIUS;
232 
235  static const double DEFAULT_SEARCH_RADIUS_MM;
236 
238  static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
239 
244 
248 
252 
257  static double SCALE_PRECISION;
258 
259  private:
260  // String representation of unit types (set in qgis.cpp)
261  static const char *qgisUnitTypes[];
262 
263 };
264 
265 // hack to workaround warnings when casting void pointers
266 // retrieved from QLibrary::resolve to function pointers.
267 // It's assumed that this works on all systems supporting
268 // QLibrary
269 #if QT_VERSION >= 0x050000
270 #define cast_to_fptr(f) f
271 #else
272 inline void ( *cast_to_fptr( void *p ) )()
273 {
274  union
275  {
276  void *p;
277  void ( *f )();
278  } u;
279 
280  u.p = p;
281  return u.f;
282 }
283 #endif
284 
291 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
292 template<class Object> class QgsSignalBlocker
293 {
294  public:
295 
299  explicit QgsSignalBlocker( Object* object )
300  : mObject( object )
301  , mPreviousState( object->blockSignals( true ) )
302  {}
303 
305  {
306  mObject->blockSignals( mPreviousState );
307  }
308 
310  Object* operator->() { return mObject; }
311 
312  private:
313 
314  Object* mObject;
315  bool mPreviousState;
316 
317 };
318 
331 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
332 template<class Object> inline QgsSignalBlocker<Object> whileBlocking( Object* object )
333 {
334  return QgsSignalBlocker<Object>( object );
335 }
336 
340 inline QString qgsDoubleToString( double a, int precision = 17 )
341 {
342  if ( precision )
343  return QString::number( a, 'f', precision ).remove( QRegExp( "\\.?0+$" ) );
344  else
345  return QString::number( a, 'f', precision );
346 }
347 
352 inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON )
353 {
354  const double diff = a - b;
355  return diff > -epsilon && diff <= epsilon;
356 }
357 
359 inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
360 {
361  // The most simple would be to print numbers as %.xe and compare as strings
362  // but that is probably too costly
363  // Then the fastest would be to set some bits directly, but little/big endian
364  // has to be considered (maybe TODO)
365  // Is there a better way?
366  int aexp, bexp;
367  double ar = frexp( a, &aexp );
368  double br = frexp( b, &bexp );
369 
370  return aexp == bexp &&
371  qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) );
372 }
373 
375 inline double qgsRound( double x )
376 {
377  return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 );
378 }
379 
380 // Add missing qHash implementation for QDate, QTime, QDateTime
381 // implementations taken from upstream Qt5 versions
382 #if QT_VERSION < 0x050000
383 
386 inline uint qHash( const QDateTime &key )
387 {
388  return qHash( key.toMSecsSinceEpoch() );
389 }
390 
393 inline uint qHash( const QDate &key )
394 {
395  return qHash( key.toJulianDay() );
396 }
397 
400 inline uint qHash( const QTime &key )
401 {
402  return QTime( 0, 0, 0, 0 ).msecsTo( key );
403 }
404 #endif
405 
410 CORE_EXPORT bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs );
411 
416 CORE_EXPORT bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs );
417 
418 CORE_EXPORT QString qgsVsiPrefix( const QString& path );
419 
424 void CORE_EXPORT *qgsMalloc( size_t size );
425 
432 void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size );
433 
437 void CORE_EXPORT qgsFree( void *ptr );
438 
442 extern CORE_EXPORT const QString GEOWKT;
443 extern CORE_EXPORT const QString PROJECT_SCALES;
444 
446 extern CORE_EXPORT const QString GEOPROJ4;
448 const long GEOSRID = 4326;
450 const long GEOCRS_ID = 3452;
452 const long GEO_EPSG_CRS_ID = 4326;
454 extern CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID;
456 const int PROJ_PREFIX_LEN = 6;
458 const int ELLPS_PREFIX_LEN = 7;
460 const int LAT_PREFIX_LEN = 7;
463 const int USER_CRS_START_ID = 100000;
464 
466 extern CORE_EXPORT const QString GEO_NONE;
467 
468 //
469 // Constants for point symbols
470 //
471 
473 const double MINIMUM_POINT_SIZE = 0.1;
475 const double DEFAULT_POINT_SIZE = 2.0;
476 const double DEFAULT_LINE_WIDTH = 0.26;
477 
479 const double DEFAULT_SEGMENT_EPSILON = 1e-8;
480 
482 
489 typedef unsigned long long qgssize;
490 
491 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
492 #define Q_NOWARN_DEPRECATED_PUSH \
493  _Pragma("GCC diagnostic push") \
494  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
495 #define Q_NOWARN_DEPRECATED_POP \
496  _Pragma("GCC diagnostic pop");
497 #elif defined(_MSC_VER)
498 #define Q_NOWARN_DEPRECATED_PUSH \
499  __pragma(warning(push)) \
500  __pragma(warning(disable:4996))
501 #define Q_NOWARN_DEPRECATED_POP \
502  __pragma(warning(pop))
503 #else
504 #define Q_NOWARN_DEPRECATED_PUSH
505 #define Q_NOWARN_DEPRECATED_POP
506 #endif
507 
508 #ifndef QGISEXTERN
509 #ifdef Q_OS_WIN
510 # define QGISEXTERN extern "C" __declspec( dllexport )
511 # ifdef _MSC_VER
512 // do not warn about C bindings returing QString
513 # pragma warning(disable:4190)
514 # endif
515 #else
516 # if defined(__GNUC__) || defined(__clang__)
517 # define QGISEXTERN extern "C" __attribute__ ((visibility ("default")))
518 # else
519 # define QGISEXTERN extern "C"
520 # endif
521 #endif
522 #endif
523 #endif
524 
525 #if defined(__clang__)
526 #define FALLTHROUGH [[clang::fallthrough]]
527 #else
528 #define FALLTHROUGH
529 #endif
static const char * QGIS_VERSION
Definition: qgis.h:46
CORE_EXPORT QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:336
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:247
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:243
qint64 toMSecsSinceEpoch() const
GeometryType
Definition: qgis.h:115
uint qHash(const QDateTime &key)
Hash implementation for QDateTime.
Definition: qgis.h:386
int msecsTo(const QTime &t) const
WkbType
Used for symbology operations.
Definition: qgis.h:61
static Q_DECL_DEPRECATED const double DEFAULT_IDENTIFY_RADIUS
Old search radius in % of canvas width.
Definition: qgis.h:231
The QGis class provides global constants for use throughout the application.
Definition: qgis.h:40
QString & remove(int position, int n)
void CORE_EXPORT * 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:243
QMap< QString, QString > QgsStringMap
Definition: qgis.h:481
~QgsSignalBlocker()
Definition: qgis.h:304
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:352
CORE_EXPORT 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:263
double qgsRound(double x)
A round function which returns a double to guard against overflows.
Definition: qgis.h:375
QgsSignalBlocker(Object *object)
Constructor for QgsSignalBlocker.
Definition: qgis.h:299
QString number(int n, int base)
CORE_EXPORT const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:76
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:479
static const char * QGIS_DEV_VERSION
Definition: qgis.h:52
int toJulianDay() const
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:340
const int ELLPS_PREFIX_LEN
The length of the string "+ellps=".
Definition: qgis.h:458
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:238
const long GEOCRS_ID
Magic number for a geographic coord sys in QGIS srs.db tbl_srs.srs_id.
Definition: qgis.h:450
CORE_EXPORT const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:54
RAII signal blocking class.
Definition: qgis.h:292
void CORE_EXPORT qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:258
UserEvent
User defined event types.
Definition: qgis.h:218
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Definition: qgis.h:489
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:235
static const char * QGIS_RELEASE_NAME
Definition: qgis.h:50
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:475
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/...
Definition: qgis.h:463
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:332
const long GEOSRID
Magic number for a geographic coord sys in POSTGIS SRID.
Definition: qgis.h:448
const double MINIMUM_POINT_SIZE
Magic number that determines the minimum allowable point size for point symbols.
Definition: qgis.h:473
bool qgsDoubleNearSig(double a, double b, int significantDigits=10)
Compare two doubles using specified number of significant digits.
Definition: qgis.h:359
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:476
CORE_EXPORT const QString PROJECT_SCALES
Definition: qgis.cpp:70
DataType
Raster data types.
Definition: qgis.h:133
const int LAT_PREFIX_LEN
The length of the string "+lat_1=".
Definition: qgis.h:460
UnitType
Map units that qgis supports.
Definition: qgis.h:159
void(*)() cast_to_fptr(void *p)
Definition: qgis.h:272
const int PROJ_PREFIX_LEN
The length of the string "+proj=".
Definition: qgis.h:456
CORE_EXPORT 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:331
static double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:257
static const int QGIS_VERSION_INT
Definition: qgis.h:48
void CORE_EXPORT * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:228
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:251
Object * operator->()
Returns pointer to blocked QObject.
Definition: qgis.h:310
const long GEO_EPSG_CRS_ID
Magic number for a geographic coord sys in EpsgCrsId ID format.
Definition: qgis.h:452
CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:74
CORE_EXPORT const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:57
#define tr(sourceText)