QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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
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
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:
166  return QGis::WKBPoint25D;
169  return QGis::WKBLineString25D;
172  return QGis::WKBPolygon25D;
175  return QGis::WKBMultiPoint25D;
182  default:
183  break;
184  }
185 
186  QgsDebugMsg( QString( "unexpected new wkbType=%1" ).arg( type ) );
187  return static_cast< QGis::WkbType >( type );
188 }
189 
190 
192 {
193  bool ok = false;
194  QGis::UnitType unit = QgsUnitTypes::decodeDistanceUnit( literal, &ok );
195  return ok ? unit : defaultType;
196 }
197 
199 {
200  return QgsUnitTypes::encodeUnit( unit );
201 }
202 
204 {
205  return QgsUnitTypes::toString( unit );
206 }
207 
208 QGis::UnitType QGis::fromTr( const QString& literal, QGis::UnitType defaultType )
209 {
210  bool ok = false;
211  QGis::UnitType unit = QgsUnitTypes::stringToDistanceUnit( literal, &ok );
212  return ok ? unit : defaultType;
213 }
214 
216 {
217  return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit );
218 }
219 
220 double QGis::permissiveToDouble( QString string, bool &ok )
221 {
222  //remove any thousands separators
223  string.remove( QLocale::system().groupSeparator() );
224  return QLocale::system().toDouble( string, &ok );
225 }
226 
227 int QGis::permissiveToInt( QString string, bool &ok )
228 {
229  //remove any thousands separators
230  string.remove( QLocale::system().groupSeparator() );
231  return QLocale::system().toInt( string, &ok );
232 }
233 
234 void *qgsMalloc( size_t size )
235 {
236  if ( size == 0 || long( size ) < 0 )
237  {
238  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
239  return nullptr;
240  }
241  void *p = malloc( size );
242  if ( !p )
243  {
244  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
245  }
246  return p;
247 }
248 
249 void *qgsCalloc( size_t nmemb, size_t size )
250 {
251  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
252  {
253  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
254  return nullptr;
255  }
256  void *p = qgsMalloc( nmemb * size );
257  if ( p )
258  {
259  memset( p, 0, nmemb * size );
260  }
261  return p;
262 }
263 
264 void qgsFree( void *ptr )
265 {
266  free( ptr );
267 }
268 
269 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
270 {
271  // invalid < NULL < any value
272  if ( !lhs.isValid() )
273  return rhs.isValid();
274  else if ( lhs.isNull() )
275  return rhs.isValid() && !rhs.isNull();
276  else if ( !rhs.isValid() || rhs.isNull() )
277  return false;
278 
279  switch ( lhs.type() )
280  {
281  case QVariant::Int:
282  return lhs.toInt() < rhs.toInt();
283  case QVariant::UInt:
284  return lhs.toUInt() < rhs.toUInt();
285  case QVariant::LongLong:
286  return lhs.toLongLong() < rhs.toLongLong();
287  case QVariant::ULongLong:
288  return lhs.toULongLong() < rhs.toULongLong();
289  case QVariant::Double:
290  return lhs.toDouble() < rhs.toDouble();
291  case QVariant::Char:
292  return lhs.toChar() < rhs.toChar();
293  case QVariant::Date:
294  return lhs.toDate() < rhs.toDate();
295  case QVariant::Time:
296  return lhs.toTime() < rhs.toTime();
297  case QVariant::DateTime:
298  return lhs.toDateTime() < rhs.toDateTime();
299  case QVariant::Bool:
300  return lhs.toBool() < rhs.toBool();
301 
302  case QVariant::List:
303  {
304  const QList<QVariant> &lhsl = lhs.toList();
305  const QList<QVariant> &rhsl = rhs.toList();
306 
307  int i, n = qMin( lhsl.size(), rhsl.size() );
308  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && lhsl[i].isNull() == rhsl[i].isNull() && lhsl[i] == rhsl[i]; i++ )
309  ;
310 
311  if ( i == n )
312  return lhsl.size() < rhsl.size();
313  else
314  return qgsVariantLessThan( lhsl[i], rhsl[i] );
315  }
316 
317  case QVariant::StringList:
318  {
319  const QStringList &lhsl = lhs.toStringList();
320  const QStringList &rhsl = rhs.toStringList();
321 
322  int i, n = qMin( lhsl.size(), rhsl.size() );
323  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
324  ;
325 
326  if ( i == n )
327  return lhsl.size() < rhsl.size();
328  else
329  return lhsl[i] < rhsl[i];
330  }
331 
332  default:
333  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
334  }
335 }
336 
337 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
338 {
339  return ! qgsVariantLessThan( lhs, rhs );
340 }
341 
343 {
344  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
345  path.endsWith( ".zip", Qt::CaseInsensitive ) )
346  return "/vsizip/";
347  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
348  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
349  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
350  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
351  return "/vsitar/";
352  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
353  path.endsWith( ".gz", Qt::CaseInsensitive ) )
354  return "/vsigzip/";
355  else
356  return "";
357 }
358 
360 {
361  switch ( type )
362  {
363  case WKBMultiPoint:
364  return WKBPoint;
365  case WKBMultiLineString:
366  return WKBLineString;
367  case WKBMultiPolygon:
368  return WKBPolygon;
369  case WKBMultiPoint25D:
370  return WKBPoint25D;
372  return WKBLineString25D;
373  case WKBMultiPolygon25D:
374  return WKBPolygon25D;
375  default:
377  }
378 }
379 
381 {
382  switch ( type )
383  {
384  case WKBPoint:
385  return WKBMultiPoint;
386  case WKBLineString:
387  return WKBMultiLineString;
388  case WKBPolygon:
389  return WKBMultiPolygon;
390  case WKBPoint25D:
391  return WKBMultiPoint25D;
392  case WKBLineString25D:
393  return WKBMultiLineString25D;
394  case WKBPolygon25D:
395  return WKBMultiPolygon25D;
396  default:
398  }
399 }
400 
402 {
403  switch ( type )
404  {
405  case WKBPoint25D:
406  return WKBPoint;
407  case WKBLineString25D:
408  return WKBLineString;
409  case WKBPolygon25D:
410  return WKBPolygon;
411  case WKBMultiPoint25D:
412  return WKBMultiPoint;
414  return WKBMultiLineString;
415  case WKBMultiPolygon25D:
416  return WKBMultiPolygon;
417  default:
419  }
420 }
421 
423 {
424  return QgsWKBTypes::isSingleType( fromOldWkbType( type ) );
425 }
426 
428 {
429  return QgsWKBTypes::isMultiType( fromOldWkbType( type ) );
430 }
431 
433 {
434  if ( type == WKBUnknown || type == WKBNoGeometry )
435  return 0;
436 
437  QgsWKBTypes::Type wkbType = fromOldWkbType( type );
438  return 2 + ( QgsWKBTypes::hasZ( wkbType ) ? 1 : 0 ) + ( QgsWKBTypes::hasM( wkbType ) ? 1 : 0 );
439 }
440 
442 {
443  switch ( type )
444  {
445  case Point:
446  return "Point";
447  case Line:
448  return "Line";
449  case Polygon:
450  return "Polygon";
451  case UnknownGeometry:
452  return "Unknown geometry";
453  case NoGeometry:
454  return "No geometry";
455  default:
456  return "Invalid type";
457  }
458 }
459 
460 
461 const char *QGis::featureType( QGis::WkbType type )
462 {
463  switch ( type )
464  {
465  case WKBUnknown:
466  return "WKBUnknown";
467  case WKBPoint:
468  return "WKBPoint";
469  case WKBLineString:
470  return "WKBLineString";
471  case WKBPolygon:
472  return "WKBPolygon";
473  case WKBMultiPoint:
474  return "WKBMultiPoint";
475  case WKBMultiLineString:
476  return "WKBMultiLineString";
477  case WKBMultiPolygon:
478  return "WKBMultiPolygon";
479  case WKBNoGeometry:
480  return "WKBNoGeometry";
481  case WKBPoint25D:
482  return "WKBPoint25D";
483  case WKBLineString25D:
484  return "WKBLineString25D";
485  case WKBPolygon25D:
486  return "WKBPolygon25D";
487  case WKBMultiPoint25D:
488  return "WKBMultiPoint25D";
490  return "WKBMultiLineString25D";
491  case WKBMultiPolygon25D:
492  return "WKBMultiPolygon25D";
493  default:
494  return "invalid wkbtype";
495 
496  }
497 }
static WkbType flatType(WkbType type)
Map 2d+ to 2d type.
Definition: qgis.cpp:401
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:227
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
Definition: qgswkbtypes.h:478
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:234
GeometryType
Definition: qgis.h:115
int localeAwareCompare(const QString &other) const
static int wkbDimensions(WkbType type)
Definition: qgis.cpp:432
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:714
#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:220
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:249
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:487
static const char * vectorGeometryType(GeometryType type)
description strings for geometry types
Definition: qgis.cpp:441
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:667
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:337
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:269
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.
QString fromUtf8(const char *str, int size)
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:422
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 QString QGIS_VERSION
Definition: qgis.h:46
static WkbType singleType(WkbType type)
Map multi to single type.
Definition: qgis.cpp:359
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 * featureType(WkbType type)
description strings for feature types
Definition: qgis.cpp:461
static Q_DECL_DEPRECATED QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:203
QDate toDate() const
static WkbType multiType(WkbType type)
Map single to multitype type.
Definition: qgis.cpp:380
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:208
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:342
static Q_DECL_DEPRECATED double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:215
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:198
Type type() const
static bool isMultiType(WkbType type)
Return if type is a multi type.
Definition: qgis.cpp:427
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:191
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:264
static QString QGIS_RELEASE_NAME
Definition: qgis.h:50
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