QGIS API Documentation  2.15.0-Master (af20121)
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 #include "qgsunittypes.h"
32 
33 #include <ogr_api.h>
34 
35 // Version constants
36 //
37 
38 // Version string
39 const char* QGis::QGIS_VERSION = 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 char* QGis::QGIS_RELEASE_NAME = RELEASE_NAME;
50 
51 #if GDAL_VERSION_NUM >= 1800
52 const QString GEOPROJ4 = "+proj=longlat +datum=WGS84 +no_defs";
53 #else
54 const QString GEOPROJ4 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
55 #endif
56 
57 const QString GEOWKT =
58  "GEOGCS[\"WGS 84\", "
59  " DATUM[\"WGS_1984\", "
60  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
61  " AUTHORITY[\"EPSG\",7030]], "
62  " TOWGS84[0,0,0,0,0,0,0], "
63  " AUTHORITY[\"EPSG\",6326]], "
64  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
65  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
66  " AXIS[\"Lat\",NORTH], "
67  " AXIS[\"Long\",EAST], "
68  " AUTHORITY[\"EPSG\",4326]]";
69 
71  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
72  "1:10000,1:5000,1:2500,1:1000,1:500";
73 
74 const QString GEO_EPSG_CRS_AUTHID = "EPSG:4326";
75 
76 const QString GEO_NONE = "NONE";
77 
78 const double QGis::DEFAULT_IDENTIFY_RADIUS = 0.5;
79 const double QGis::DEFAULT_SEARCH_RADIUS_MM = 2.;
80 
82 const float QGis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
83 
84 const QColor QGis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
85 
87 
89 
90 double QGis::SCALE_PRECISION = 0.9999999999;
91 
92 // description strings for units
93 // Order must match enum indices
94 const char* QGis::qgisUnitTypes[] =
95 {
96  QT_TRANSLATE_NOOP( "QGis::UnitType", "meters" ),
97  QT_TRANSLATE_NOOP( "QGis::UnitType", "feet" ),
98  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
99  QT_TRANSLATE_NOOP( "QGis::UnitType", "<unknown>" ),
100  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
101  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
102  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
103  QT_TRANSLATE_NOOP( "QGis::UnitType", "nautical miles" )
104 };
105 
107 {
108  switch ( type )
109  {
110  case QGis::WKBPoint:
111  return QgsWKBTypes::Point;
112  case QGis::WKBLineString:
114  case QGis::WKBPolygon:
115  return QgsWKBTypes::Polygon;
116  case QGis::WKBMultiPoint:
122  case QGis::WKBNoGeometry:
124  case QGis::WKBPoint25D:
125  return QgsWKBTypes::PointZ;
128  case QGis::WKBPolygon25D:
129  return QgsWKBTypes::PolygonZ;
136  case QGis::WKBUnknown:
137  return QgsWKBTypes::Unknown;
138  default:
139  break;
140  }
141 
142  QgsDebugMsg( QString( "unexpected old wkbType=%1" ).arg( type ) );
143  return static_cast< QgsWKBTypes::Type >( type );
144 }
145 
147 {
148  switch ( type )
149  {
150  case QgsWKBTypes::Point:
151  return QGis::WKBPoint;
153  return QGis::WKBLineString;
155  return QGis::WKBPolygon;
157  return QGis::WKBMultiPoint;
161  return QGis::WKBMultiPolygon;
163  return QGis::WKBNoGeometry;
164  case QgsWKBTypes::PointZ:
165  return QGis::WKBPoint25D;
167  return QGis::WKBLineString25D;
169  return QGis::WKBPolygon25D;
171  return QGis::WKBMultiPoint25D;
176  default:
177  break;
178  }
179 
180  QgsDebugMsg( QString( "unexpected new wkbType=%1" ).arg( type ) );
181  return static_cast< QGis::WkbType >( type );
182 }
183 
184 
186 {
187  bool ok = false;
188  QGis::UnitType unit = QgsUnitTypes::decodeDistanceUnit( literal, &ok );
189  return ok ? unit : defaultType;
190 }
191 
193 {
194  return QgsUnitTypes::encodeUnit( unit );
195 }
196 
198 {
199  return QgsUnitTypes::toString( unit );
200 }
201 
202 QGis::UnitType QGis::fromTr( const QString& literal, QGis::UnitType defaultType )
203 {
204  bool ok = false;
205  QGis::UnitType unit = QgsUnitTypes::stringToDistanceUnit( literal, &ok );
206  return ok ? unit : defaultType;
207 }
208 
210 {
211  return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit );
212 }
213 
214 double QGis::permissiveToDouble( QString string, bool &ok )
215 {
216  //remove any thousands separators
217  string.remove( QLocale::system().groupSeparator() );
218  return QLocale::system().toDouble( string, &ok );
219 }
220 
221 int QGis::permissiveToInt( QString string, bool &ok )
222 {
223  //remove any thousands separators
224  string.remove( QLocale::system().groupSeparator() );
225  return QLocale::system().toInt( string, &ok );
226 }
227 
228 void *qgsMalloc( size_t size )
229 {
230  if ( size == 0 || long( size ) < 0 )
231  {
232  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
233  return nullptr;
234  }
235  void *p = malloc( size );
236  if ( !p )
237  {
238  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
239  }
240  return p;
241 }
242 
243 void *qgsCalloc( size_t nmemb, size_t size )
244 {
245  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
246  {
247  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
248  return nullptr;
249  }
250  void *p = qgsMalloc( nmemb * size );
251  if ( p )
252  {
253  memset( p, 0, nmemb * size );
254  }
255  return p;
256 }
257 
258 void qgsFree( void *ptr )
259 {
260  free( ptr );
261 }
262 
263 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
264 {
265  // invalid < NULL < any value
266  if ( !lhs.isValid() )
267  return rhs.isValid();
268  else if ( lhs.isNull() )
269  return rhs.isValid() && !rhs.isNull();
270  else if ( !rhs.isValid() || rhs.isNull() )
271  return false;
272 
273  switch ( lhs.type() )
274  {
275  case QVariant::Int:
276  return lhs.toInt() < rhs.toInt();
277  case QVariant::UInt:
278  return lhs.toUInt() < rhs.toUInt();
279  case QVariant::LongLong:
280  return lhs.toLongLong() < rhs.toLongLong();
281  case QVariant::ULongLong:
282  return lhs.toULongLong() < rhs.toULongLong();
283  case QVariant::Double:
284  return lhs.toDouble() < rhs.toDouble();
285  case QVariant::Char:
286  return lhs.toChar() < rhs.toChar();
287  case QVariant::Date:
288  return lhs.toDate() < rhs.toDate();
289  case QVariant::Time:
290  return lhs.toTime() < rhs.toTime();
291  case QVariant::DateTime:
292  return lhs.toDateTime() < rhs.toDateTime();
293  case QVariant::Bool:
294  return lhs.toBool() < rhs.toBool();
295 
296  case QVariant::List:
297  {
298  const QList<QVariant> &lhsl = lhs.toList();
299  const QList<QVariant> &rhsl = rhs.toList();
300 
301  int i, n = qMin( lhsl.size(), rhsl.size() );
302  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
303  ;
304 
305  if ( i == n )
306  return lhsl.size() < rhsl.size();
307  else
308  return qgsVariantLessThan( lhsl[i], rhsl[i] );
309  }
310 
311  case QVariant::StringList:
312  {
313  const QStringList &lhsl = lhs.toStringList();
314  const QStringList &rhsl = rhs.toStringList();
315 
316  int i, n = qMin( lhsl.size(), rhsl.size() );
317  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
318  ;
319 
320  if ( i == n )
321  return lhsl.size() < rhsl.size();
322  else
323  return lhsl[i] < rhsl[i];
324  }
325 
326  default:
327  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
328  }
329 }
330 
331 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
332 {
333  return ! qgsVariantLessThan( lhs, rhs );
334 }
335 
337 {
338  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
339  path.endsWith( ".zip", Qt::CaseInsensitive ) )
340  return "/vsizip/";
341  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
342  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
343  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
344  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
345  return "/vsitar/";
346  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
347  path.endsWith( ".gz", Qt::CaseInsensitive ) )
348  return "/vsigzip/";
349  else
350  return "";
351 }
352 
354 {
355  switch ( type )
356  {
357  case WKBMultiPoint:
358  return WKBPoint;
359  case WKBMultiLineString:
360  return WKBLineString;
361  case WKBMultiPolygon:
362  return WKBPolygon;
363  case WKBMultiPoint25D:
364  return WKBPoint25D;
366  return WKBLineString25D;
367  case WKBMultiPolygon25D:
368  return WKBPolygon25D;
369  default:
371  }
372 }
373 
375 {
376  switch ( type )
377  {
378  case WKBPoint:
379  return WKBMultiPoint;
380  case WKBLineString:
381  return WKBMultiLineString;
382  case WKBPolygon:
383  return WKBMultiPolygon;
384  case WKBPoint25D:
385  return WKBMultiPoint25D;
386  case WKBLineString25D:
387  return WKBMultiLineString25D;
388  case WKBPolygon25D:
389  return WKBMultiPolygon25D;
390  default:
392  }
393 }
394 
396 {
397  switch ( type )
398  {
399  case WKBPoint25D:
400  return WKBPoint;
401  case WKBLineString25D:
402  return WKBLineString;
403  case WKBPolygon25D:
404  return WKBPolygon;
405  case WKBMultiPoint25D:
406  return WKBMultiPoint;
408  return WKBMultiLineString;
409  case WKBMultiPolygon25D:
410  return WKBMultiPolygon;
411  default:
413  }
414 }
415 
417 {
418  return QgsWKBTypes::isSingleType( fromOldWkbType( type ) );
419 }
420 
422 {
423  return QgsWKBTypes::isMultiType( fromOldWkbType( type ) );
424 }
425 
427 {
428  if ( type == WKBUnknown || type == WKBNoGeometry )
429  return 0;
430 
431  QgsWKBTypes::Type wkbType = fromOldWkbType( type );
432  return 2 + ( QgsWKBTypes::hasZ( wkbType ) ? 1 : 0 ) + ( QgsWKBTypes::hasM( wkbType ) ? 1 : 0 );
433 }
434 
436 {
437  switch ( type )
438  {
439  case Point:
440  return "Point";
441  case Line:
442  return "Line";
443  case Polygon:
444  return "Polygon";
445  case UnknownGeometry:
446  return "Unknown geometry";
447  case NoGeometry:
448  return "No geometry";
449  default:
450  return "Invalid type";
451  }
452 }
453 
454 
455 const char *QGis::featureType( QGis::WkbType type )
456 {
457  switch ( type )
458  {
459  case WKBUnknown:
460  return "WKBUnknown";
461  case WKBPoint:
462  return "WKBPoint";
463  case WKBLineString:
464  return "WKBLineString";
465  case WKBPolygon:
466  return "WKBPolygon";
467  case WKBMultiPoint:
468  return "WKBMultiPoint";
469  case WKBMultiLineString:
470  return "WKBMultiLineString";
471  case WKBMultiPolygon:
472  return "WKBMultiPolygon";
473  case WKBNoGeometry:
474  return "WKBNoGeometry";
475  case WKBPoint25D:
476  return "WKBPoint25D";
477  case WKBLineString25D:
478  return "WKBLineString25D";
479  case WKBPolygon25D:
480  return "WKBPolygon25D";
481  case WKBMultiPoint25D:
482  return "WKBMultiPoint25D";
484  return "WKBMultiLineString25D";
485  case WKBMultiPolygon25D:
486  return "WKBMultiPolygon25D";
487  default:
488  return "invalid wkbtype";
489 
490  }
491 }
static const char * QGIS_VERSION
Definition: qgis.h:46
static WkbType flatType(WkbType type)
Map 2d+ to 2d type.
Definition: qgis.cpp:395
qlonglong toLongLong(bool *ok) const
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
static int permissiveToInt(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:221
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:467
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:228
GeometryType
Definition: qgis.h:115
int localeAwareCompare(const QString &other) const
static int wkbDimensions(WkbType type)
Definition: qgis.cpp:426
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:703
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QList< QVariant > toList() const
QDateTime toDateTime() const
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:240
static QGis::UnitType decodeDistanceUnit(const QString &string, bool *ok=0)
Decodes a distance unit from a string.
static double permissiveToDouble(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:214
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:243
QTime toTime() const
static QGis::WkbType fromNewWkbType(QgsWKBTypes::Type type)
Converts from new (post 2.10) WKB type (OGC) to old WKB type.
Definition: qgis.cpp:146
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:476
static const char * vectorGeometryType(GeometryType type)
description strings for geometry types
Definition: qgis.cpp:435
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
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:656
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
qulonglong toULongLong(bool *ok) const
double toDouble(const QString &s, bool *ok) const
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
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:76
int size() const
QLocale system()
static QString encodeUnit(QGis::UnitType unit)
Encodes a distance unit to a string.
static QString toString(QGis::UnitType unit)
Returns a translated string representing a distance unit.
static QgsWKBTypes::Type fromOldWkbType(QGis::WkbType type)
Converts from old (pre 2.10) WKB type (OGR) to new WKB type.
Definition: qgis.cpp:106
static QGis::UnitType stringToDistanceUnit(const QString &string, bool *ok=0)
Converts a translated string to a distance unit.
uint toUInt(bool *ok) const
int toInt(bool *ok) const
bool isNull() const
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:54
static const char * QGIS_DEV_VERSION
Definition: qgis.h:52
static bool isSingleType(WkbType type)
Return if type is a single type.
Definition: qgis.cpp:416
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:238
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:74
static WkbType singleType(WkbType type)
Map multi to single type.
Definition: qgis.cpp:353
const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:57
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified distance units.
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
static const char * featureType(WkbType type)
description strings for feature types
Definition: qgis.cpp:455
static Q_DECL_DEPRECATED QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:197
QDate toDate() const
static WkbType multiType(WkbType type)
Map single to multitype type.
Definition: qgis.cpp:374
const QString PROJECT_SCALES
Definition: qgis.cpp:70
QStringList toStringList() const
static Q_DECL_DEPRECATED UnitType fromTr(const QString &literal, QGis::UnitType defaultType=UnknownUnit)
Provides type value from translated version.
Definition: qgis.cpp:202
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:336
static Q_DECL_DEPRECATED double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:209
bool toBool() const
int toInt(const QString &s, bool *ok, int base) const
UnitType
Map units that qgis supports.
Definition: qgis.h:159
bool isValid() const
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
double toDouble(bool *ok) const
static Q_DECL_DEPRECATED QString toLiteral(QGis::UnitType unit)
Provides the canonical name of the type value.
Definition: qgis.cpp:192
Type type() const
static bool isMultiType(WkbType type)
Return if type is a multi type.
Definition: qgis.cpp:421
static Q_DECL_DEPRECATED UnitType fromLiteral(const QString &literal, QGis::UnitType defaultType=UnknownUnit)
Converts from the canonical name to the type value.
Definition: qgis.cpp:185
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:258
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
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:251
QChar toChar() const
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114