QGIS API Documentation  2.14.0-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 : [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 // 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:
163  return QGis::WKBPoint25D;
165  return QGis::WKBLineString25D;
167  return QGis::WKBPolygon25D;
169  return QGis::WKBMultiPoint25D;
174  default:
175  break;
176  }
177 
178  QgsDebugMsg( QString( "unexpected new wkbType=%1" ).arg( type ) );
179  return static_cast< QGis::WkbType >( type );
180 }
181 
182 
184 {
185  bool ok = false;
186  QGis::UnitType unit = QgsUnitTypes::decodeDistanceUnit( literal, &ok );
187  return ok ? unit : defaultType;
188 }
189 
191 {
192  return QgsUnitTypes::encodeUnit( unit );
193 }
194 
196 {
197  return QgsUnitTypes::toString( unit );
198 }
199 
200 QGis::UnitType QGis::fromTr( const QString& literal, QGis::UnitType defaultType )
201 {
202  bool ok = false;
203  QGis::UnitType unit = QgsUnitTypes::stringToDistanceUnit( literal, &ok );
204  return ok ? unit : defaultType;
205 }
206 
208 {
209  return QgsUnitTypes::fromUnitToUnitFactor( fromUnit, toUnit );
210 }
211 
212 double QGis::permissiveToDouble( QString string, bool &ok )
213 {
214  //remove any thousands separators
215  string.remove( QLocale::system().groupSeparator() );
216  return QLocale::system().toDouble( string, &ok );
217 }
218 
219 int QGis::permissiveToInt( QString string, bool &ok )
220 {
221  //remove any thousands separators
222  string.remove( QLocale::system().groupSeparator() );
223  return QLocale::system().toInt( string, &ok );
224 }
225 
226 void *qgsMalloc( size_t size )
227 {
228  if ( size == 0 || long( size ) < 0 )
229  {
230  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
231  return nullptr;
232  }
233  void *p = malloc( size );
234  if ( !p )
235  {
236  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
237  }
238  return p;
239 }
240 
241 void *qgsCalloc( size_t nmemb, size_t size )
242 {
243  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
244  {
245  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
246  return nullptr;
247  }
248  void *p = qgsMalloc( nmemb * size );
249  if ( p )
250  {
251  memset( p, 0, nmemb * size );
252  }
253  return p;
254 }
255 
256 void qgsFree( void *ptr )
257 {
258  free( ptr );
259 }
260 
261 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
262 {
263  switch ( lhs.type() )
264  {
265  case QVariant::Int:
266  return lhs.toInt() < rhs.toInt();
267  case QVariant::UInt:
268  return lhs.toUInt() < rhs.toUInt();
269  case QVariant::LongLong:
270  return lhs.toLongLong() < rhs.toLongLong();
271  case QVariant::ULongLong:
272  return lhs.toULongLong() < rhs.toULongLong();
273  case QVariant::Double:
274  return lhs.toDouble() < rhs.toDouble();
275  case QVariant::Char:
276  return lhs.toChar() < rhs.toChar();
277  case QVariant::Date:
278  return lhs.toDate() < rhs.toDate();
279  case QVariant::Time:
280  return lhs.toTime() < rhs.toTime();
281  case QVariant::DateTime:
282  return lhs.toDateTime() < rhs.toDateTime();
283  default:
284  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
285  }
286 }
287 
288 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
289 {
290  return ! qgsVariantLessThan( lhs, rhs );
291 }
292 
294 {
295  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
296  path.endsWith( ".zip", Qt::CaseInsensitive ) )
297  return "/vsizip/";
298  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
299  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
300  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
301  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
302  return "/vsitar/";
303  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
304  path.endsWith( ".gz", Qt::CaseInsensitive ) )
305  return "/vsigzip/";
306  else
307  return "";
308 }
309 
311 {
312  switch ( type )
313  {
314  case WKBMultiPoint:
315  return WKBPoint;
316  case WKBMultiLineString:
317  return WKBLineString;
318  case WKBMultiPolygon:
319  return WKBPolygon;
320  case WKBMultiPoint25D:
321  return WKBPoint25D;
323  return WKBLineString25D;
324  case WKBMultiPolygon25D:
325  return WKBPolygon25D;
326  default:
328  }
329 }
330 
332 {
333  switch ( type )
334  {
335  case WKBPoint:
336  return WKBMultiPoint;
337  case WKBLineString:
338  return WKBMultiLineString;
339  case WKBPolygon:
340  return WKBMultiPolygon;
341  case WKBPoint25D:
342  return WKBMultiPoint25D;
343  case WKBLineString25D:
344  return WKBMultiLineString25D;
345  case WKBPolygon25D:
346  return WKBMultiPolygon25D;
347  default:
349  }
350 }
351 
353 {
354  switch ( type )
355  {
356  case WKBPoint25D:
357  return WKBPoint;
358  case WKBLineString25D:
359  return WKBLineString;
360  case WKBPolygon25D:
361  return WKBPolygon;
362  case WKBMultiPoint25D:
363  return WKBMultiPoint;
365  return WKBMultiLineString;
366  case WKBMultiPolygon25D:
367  return WKBMultiPolygon;
368  default:
370  }
371 }
372 
374 {
375  return QgsWKBTypes::isSingleType( fromOldWkbType( type ) );
376 }
377 
379 {
380  return QgsWKBTypes::isMultiType( fromOldWkbType( type ) );
381 }
382 
384 {
385  if ( type == WKBUnknown || type == WKBNoGeometry )
386  return 0;
387 
388  QgsWKBTypes::Type wkbType = fromOldWkbType( type );
389  return 2 + ( QgsWKBTypes::hasZ( wkbType ) ? 1 : 0 ) + ( QgsWKBTypes::hasM( wkbType ) ? 1 : 0 );
390 }
391 
393 {
394  switch ( type )
395  {
396  case Point:
397  return "Point";
398  case Line:
399  return "Line";
400  case Polygon:
401  return "Polygon";
402  case UnknownGeometry:
403  return "Unknown geometry";
404  case NoGeometry:
405  return "No geometry";
406  default:
407  return "Invalid type";
408  }
409 }
410 
411 
412 const char *QGis::featureType( QGis::WkbType type )
413 {
414  switch ( type )
415  {
416  case WKBUnknown:
417  return "WKBUnknown";
418  case WKBPoint:
419  return "WKBPoint";
420  case WKBLineString:
421  return "WKBLineString";
422  case WKBPolygon:
423  return "WKBPolygon";
424  case WKBMultiPoint:
425  return "WKBMultiPoint";
426  case WKBMultiLineString:
427  return "WKBMultiLineString";
428  case WKBMultiPolygon:
429  return "WKBMultiPolygon";
430  case WKBNoGeometry:
431  return "WKBNoGeometry";
432  case WKBPoint25D:
433  return "WKBPoint25D";
434  case WKBLineString25D:
435  return "WKBLineString25D";
436  case WKBPolygon25D:
437  return "WKBPolygon25D";
438  case WKBMultiPoint25D:
439  return "WKBMultiPoint25D";
441  return "WKBMultiLineString25D";
442  case WKBMultiPolygon25D:
443  return "WKBMultiPolygon25D";
444  default:
445  return "invalid wkbtype";
446 
447  }
448 }
static const char * QGIS_VERSION
Definition: qgis.h:42
static WkbType flatType(WkbType type)
Map 2d+ to 2d type.
Definition: qgis.cpp:352
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:219
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:226
GeometryType
Definition: qgis.h:111
int localeAwareCompare(const QString &other) const
static int wkbDimensions(WkbType type)
Definition: qgis.cpp:383
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:703
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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:212
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:241
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:392
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)
Definition: qgis.cpp:288
qulonglong toULongLong(bool *ok) const
double toDouble(const QString &s, bool *ok) const
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:261
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:76
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
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:373
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:310
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:412
static Q_DECL_DEPRECATED QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:195
QDate toDate() const
static WkbType multiType(WkbType type)
Map single to multitype type.
Definition: qgis.cpp:331
const QString PROJECT_SCALES
Definition: qgis.cpp:70
static Q_DECL_DEPRECATED UnitType fromTr(const QString &literal, QGis::UnitType defaultType=UnknownUnit)
Provides type value from translated version.
Definition: qgis.cpp:200
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:293
static Q_DECL_DEPRECATED double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:207
int toInt(const QString &s, bool *ok, int base) const
UnitType
Map units that qgis supports.
Definition: qgis.h:155
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:190
Type type() const
static bool isMultiType(WkbType type)
Return if type is a multi type.
Definition: qgis.cpp:378
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:183
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:256
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
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114