QGIS API Documentation  2.14.11-Essen
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 : sherman@mrcc.com
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
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 // description strings for units
91 // Order must match enum indices
92 const char* QGis::qgisUnitTypes[] =
93 {
94  QT_TRANSLATE_NOOP( "QGis::UnitType", "meters" ),
95  QT_TRANSLATE_NOOP( "QGis::UnitType", "feet" ),
96  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
97  QT_TRANSLATE_NOOP( "QGis::UnitType", "<unknown>" ),
98  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
99  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
100  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
101  QT_TRANSLATE_NOOP( "QGis::UnitType", "nautical miles" )
102 };
103 
105 {
106  switch ( type )
107  {
108  case QGis::WKBPoint:
109  return QgsWKBTypes::Point;
110  case QGis::WKBLineString:
112  case QGis::WKBPolygon:
113  return QgsWKBTypes::Polygon;
114  case QGis::WKBMultiPoint:
120  case QGis::WKBNoGeometry:
122  case QGis::WKBPoint25D:
123  return QgsWKBTypes::PointZ;
126  case QGis::WKBPolygon25D:
127  return QgsWKBTypes::PolygonZ;
134  case QGis::WKBUnknown:
135  return QgsWKBTypes::Unknown;
136  default:
137  break;
138  }
139 
140  QgsDebugMsg( QString( "unexpected old wkbType=%1" ).arg( type ) );
141  return static_cast< QgsWKBTypes::Type >( type );
142 }
143 
145 {
146  switch ( type )
147  {
148  case QgsWKBTypes::Point:
149  return QGis::WKBPoint;
151  return QGis::WKBLineString;
153  return QGis::WKBPolygon;
155  return QGis::WKBMultiPoint;
159  return QGis::WKBMultiPolygon;
161  return QGis::WKBNoGeometry;
162  case QgsWKBTypes::PointZ:
164  return QGis::WKBPoint25D;
167  return QGis::WKBLineString25D;
170  return QGis::WKBPolygon25D;
173  return QGis::WKBMultiPoint25D;
180  default:
181  break;
182  }
183 
184  QgsDebugMsg( QString( "unexpected new wkbType=%1" ).arg( type ) );
185  return static_cast< QGis::WkbType >( type );
186 }
187 
188 
190 {
191  bool ok = false;
192  QGis::UnitType unit = QgsUnitTypes::decodeDistanceUnit( literal, &ok );
193  return ok ? unit : defaultType;
194 }
195 
197 {
198  return QgsUnitTypes::encodeUnit( unit );
199 }
200 
202 {
203  return QgsUnitTypes::toString( unit );
204 }
205 
206 QGis::UnitType QGis::fromTr( const QString& literal, QGis::UnitType defaultType )
207 {
208  bool ok = false;
209  QGis::UnitType unit = QgsUnitTypes::stringToDistanceUnit( literal, &ok );
210  return ok ? unit : defaultType;
211 }
212 
214 {
215  return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit );
216 }
217 
218 double QGis::permissiveToDouble( QString string, bool &ok )
219 {
220  //remove any thousands separators
221  string.remove( QLocale::system().groupSeparator() );
222  return QLocale::system().toDouble( string, &ok );
223 }
224 
225 int QGis::permissiveToInt( QString string, bool &ok )
226 {
227  //remove any thousands separators
228  string.remove( QLocale::system().groupSeparator() );
229  return QLocale::system().toInt( string, &ok );
230 }
231 
232 void *qgsMalloc( size_t size )
233 {
234  if ( size == 0 || long( size ) < 0 )
235  {
236  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
237  return nullptr;
238  }
239  void *p = malloc( size );
240  if ( !p )
241  {
242  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
243  }
244  return p;
245 }
246 
247 void *qgsCalloc( size_t nmemb, size_t size )
248 {
249  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
250  {
251  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
252  return nullptr;
253  }
254  void *p = qgsMalloc( nmemb * size );
255  if ( p )
256  {
257  memset( p, 0, nmemb * size );
258  }
259  return p;
260 }
261 
262 void qgsFree( void *ptr )
263 {
264  free( ptr );
265 }
266 
267 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
268 {
269  // invalid < NULL < any value
270  if ( !lhs.isValid() )
271  return rhs.isValid();
272  else if ( lhs.isNull() )
273  return rhs.isValid() && !rhs.isNull();
274  else if ( !rhs.isValid() || rhs.isNull() )
275  return false;
276 
277  switch ( lhs.type() )
278  {
279  case QVariant::Int:
280  return lhs.toInt() < rhs.toInt();
281  case QVariant::UInt:
282  return lhs.toUInt() < rhs.toUInt();
283  case QVariant::LongLong:
284  return lhs.toLongLong() < rhs.toLongLong();
285  case QVariant::ULongLong:
286  return lhs.toULongLong() < rhs.toULongLong();
287  case QVariant::Double:
288  return lhs.toDouble() < rhs.toDouble();
289  case QVariant::Char:
290  return lhs.toChar() < rhs.toChar();
291  case QVariant::Date:
292  return lhs.toDate() < rhs.toDate();
293  case QVariant::Time:
294  return lhs.toTime() < rhs.toTime();
295  case QVariant::DateTime:
296  return lhs.toDateTime() < rhs.toDateTime();
297  case QVariant::Bool:
298  return lhs.toBool() < rhs.toBool();
299 
300  case QVariant::List:
301  {
302  const QList<QVariant> &lhsl = lhs.toList();
303  const QList<QVariant> &rhsl = rhs.toList();
304 
305  int i, n = qMin( lhsl.size(), rhsl.size() );
306  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
307  ;
308 
309  if ( i == n )
310  return lhsl.size() < rhsl.size();
311  else
312  return qgsVariantLessThan( lhsl[i], rhsl[i] );
313  }
314 
315  case QVariant::StringList:
316  {
317  const QStringList &lhsl = lhs.toStringList();
318  const QStringList &rhsl = rhs.toStringList();
319 
320  int i, n = qMin( lhsl.size(), rhsl.size() );
321  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
322  ;
323 
324  if ( i == n )
325  return lhsl.size() < rhsl.size();
326  else
327  return lhsl[i] < rhsl[i];
328  }
329 
330  default:
331  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
332  }
333 }
334 
335 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
336 {
337  return ! qgsVariantLessThan( lhs, rhs );
338 }
339 
341 {
342  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
343  path.endsWith( ".zip", Qt::CaseInsensitive ) )
344  return "/vsizip/";
345  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
346  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
347  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
348  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
349  return "/vsitar/";
350  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
351  path.endsWith( ".gz", Qt::CaseInsensitive ) )
352  return "/vsigzip/";
353  else
354  return "";
355 }
356 
358 {
359  switch ( type )
360  {
361  case WKBMultiPoint:
362  return WKBPoint;
363  case WKBMultiLineString:
364  return WKBLineString;
365  case WKBMultiPolygon:
366  return WKBPolygon;
367  case WKBMultiPoint25D:
368  return WKBPoint25D;
370  return WKBLineString25D;
371  case WKBMultiPolygon25D:
372  return WKBPolygon25D;
373  default:
375  }
376 }
377 
379 {
380  switch ( type )
381  {
382  case WKBPoint:
383  return WKBMultiPoint;
384  case WKBLineString:
385  return WKBMultiLineString;
386  case WKBPolygon:
387  return WKBMultiPolygon;
388  case WKBPoint25D:
389  return WKBMultiPoint25D;
390  case WKBLineString25D:
391  return WKBMultiLineString25D;
392  case WKBPolygon25D:
393  return WKBMultiPolygon25D;
394  default:
396  }
397 }
398 
400 {
401  switch ( type )
402  {
403  case WKBPoint25D:
404  return WKBPoint;
405  case WKBLineString25D:
406  return WKBLineString;
407  case WKBPolygon25D:
408  return WKBPolygon;
409  case WKBMultiPoint25D:
410  return WKBMultiPoint;
412  return WKBMultiLineString;
413  case WKBMultiPolygon25D:
414  return WKBMultiPolygon;
415  default:
417  }
418 }
419 
421 {
422  return QgsWKBTypes::isSingleType( fromOldWkbType( type ) );
423 }
424 
426 {
427  return QgsWKBTypes::isMultiType( fromOldWkbType( type ) );
428 }
429 
431 {
432  if ( type == WKBUnknown || type == WKBNoGeometry )
433  return 0;
434 
435  QgsWKBTypes::Type wkbType = fromOldWkbType( type );
436  return 2 + ( QgsWKBTypes::hasZ( wkbType ) ? 1 : 0 ) + ( QgsWKBTypes::hasM( wkbType ) ? 1 : 0 );
437 }
438 
440 {
441  switch ( type )
442  {
443  case Point:
444  return "Point";
445  case Line:
446  return "Line";
447  case Polygon:
448  return "Polygon";
449  case UnknownGeometry:
450  return "Unknown geometry";
451  case NoGeometry:
452  return "No geometry";
453  default:
454  return "Invalid type";
455  }
456 }
457 
458 
459 const char *QGis::featureType( QGis::WkbType type )
460 {
461  switch ( type )
462  {
463  case WKBUnknown:
464  return "WKBUnknown";
465  case WKBPoint:
466  return "WKBPoint";
467  case WKBLineString:
468  return "WKBLineString";
469  case WKBPolygon:
470  return "WKBPolygon";
471  case WKBMultiPoint:
472  return "WKBMultiPoint";
473  case WKBMultiLineString:
474  return "WKBMultiLineString";
475  case WKBMultiPolygon:
476  return "WKBMultiPolygon";
477  case WKBNoGeometry:
478  return "WKBNoGeometry";
479  case WKBPoint25D:
480  return "WKBPoint25D";
481  case WKBLineString25D:
482  return "WKBLineString25D";
483  case WKBPolygon25D:
484  return "WKBPolygon25D";
485  case WKBMultiPoint25D:
486  return "WKBMultiPoint25D";
488  return "WKBMultiLineString25D";
489  case WKBMultiPolygon25D:
490  return "WKBMultiPolygon25D";
491  default:
492  return "invalid wkbtype";
493 
494  }
495 }
static const char * QGIS_VERSION
Definition: qgis.h:42
static WkbType flatType(WkbType type)
Map 2d+ to 2d type.
Definition: qgis.cpp:399
qlonglong toLongLong(bool *ok) const
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:239
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:235
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:225
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:232
GeometryType
Definition: qgis.h:111
int localeAwareCompare(const QString &other) const
static int wkbDimensions(WkbType type)
Definition: qgis.cpp:430
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:218
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:247
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:144
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:439
WkbType
Used for symbology operations.
Definition: qgis.h:57
static Q_DECL_DEPRECATED const double DEFAULT_IDENTIFY_RADIUS
Old search radius in % of canvas width.
Definition: qgis.h:223
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:335
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:267
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:104
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:48
static bool isSingleType(WkbType type)
Return if type is a single type.
Definition: qgis.cpp:420
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:230
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:357
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:227
static const char * QGIS_RELEASE_NAME
Definition: qgis.h:46
static const char * featureType(WkbType type)
description strings for feature types
Definition: qgis.cpp:459
static Q_DECL_DEPRECATED QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:201
QDate toDate() const
static WkbType multiType(WkbType type)
Map single to multitype type.
Definition: qgis.cpp:378
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:206
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:340
static Q_DECL_DEPRECATED double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:213
bool toBool() const
int toInt(const QString &s, bool *ok, int base) const
UnitType
Map units that qgis supports.
Definition: qgis.h:155
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:196
Type type() const
static bool isMultiType(WkbType type)
Return if type is a multi type.
Definition: qgis.cpp:425
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:189
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:262
static const int QGIS_VERSION_INT
Definition: qgis.h:44
static double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/outline minimum width in mm.
Definition: qgis.h:243
QChar toChar() const
#define QGSVERSION
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114