QGIS API Documentation  2.12.0-Lyon
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 
32 #include <ogr_api.h>
33 
34 // Version constants
35 //
36 
37 // Version string
38 const char* QGis::QGIS_VERSION = VERSION;
39 
40 // development version
41 const char* QGis::QGIS_DEV_VERSION = QGSVERSION;
42 
43 // Version number used for comparing versions using the
44 // "Check QGIS Version" function
45 const int QGis::QGIS_VERSION_INT = VERSION_INT;
46 
47 // Release name
48 const char* QGis::QGIS_RELEASE_NAME = RELEASE_NAME;
49 
50 #if GDAL_VERSION_NUM >= 1800
51 const QString GEOPROJ4 = "+proj=longlat +datum=WGS84 +no_defs";
52 #else
53 const QString GEOPROJ4 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
54 #endif
55 
56 const QString GEOWKT =
57  "GEOGCS[\"WGS 84\", "
58  " DATUM[\"WGS_1984\", "
59  " SPHEROID[\"WGS 84\",6378137,298.257223563, "
60  " AUTHORITY[\"EPSG\",7030]], "
61  " TOWGS84[0,0,0,0,0,0,0], "
62  " AUTHORITY[\"EPSG\",6326]], "
63  " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]], "
64  " UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]], "
65  " AXIS[\"Lat\",NORTH], "
66  " AXIS[\"Long\",EAST], "
67  " AUTHORITY[\"EPSG\",4326]]";
68 
70  "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
71  "1:10000,1:5000,1:2500,1:1000,1:500";
72 
73 const QString GEO_EPSG_CRS_AUTHID = "EPSG:4326";
74 
75 const QString GEO_NONE = "NONE";
76 
77 const double QGis::DEFAULT_IDENTIFY_RADIUS = 0.5;
78 const double QGis::DEFAULT_SEARCH_RADIUS_MM = 2.;
79 
81 const float QGis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
82 
83 const QColor QGis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
84 
86 
88 
89 // description strings for units
90 // Order must match enum indices
91 const char* QGis::qgisUnitTypes[] =
92 {
93  QT_TRANSLATE_NOOP( "QGis::UnitType", "meters" ),
94  QT_TRANSLATE_NOOP( "QGis::UnitType", "feet" ),
95  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
96  QT_TRANSLATE_NOOP( "QGis::UnitType", "<unknown>" ),
97  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
98  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
99  QT_TRANSLATE_NOOP( "QGis::UnitType", "degrees" ),
100  QT_TRANSLATE_NOOP( "QGis::UnitType", "nautical miles" )
101 };
102 
104 {
105  switch ( type )
106  {
107  case QGis::WKBPoint: return QgsWKBTypes::Point;
121  default: break;
122  }
123 
124  QgsDebugMsg( QString( "unexpected old wkbType=%1" ).arg( type ) );
125  return ( QgsWKBTypes::Type ) type;
126 }
127 
129 {
130  switch ( type )
131  {
132  case QgsWKBTypes::Point: return QGis::WKBPoint;
145  default: break;
146  }
147 
148  QgsDebugMsg( QString( "unexpected new wkbType=%1" ).arg( type ) );
149  return ( QGis::WkbType ) type;
150 }
151 
152 
154 {
155  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
156  {
157  if ( literal == qgisUnitTypes[ i ] )
158  {
159  return static_cast<UnitType>( i );
160  }
161  }
162  return defaultType;
163 }
164 
166 {
167  return QString( qgisUnitTypes[ static_cast<int>( unit )] );
168 }
169 
171 {
172  return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
173 }
174 
175 QGis::UnitType QGis::fromTr( const QString& literal, QGis::UnitType defaultType )
176 {
177  for ( unsigned int i = 0; i < ( sizeof( qgisUnitTypes ) / sizeof( qgisUnitTypes[0] ) ); i++ )
178  {
179  if ( literal == QGis::tr( static_cast<UnitType>( i ) ) )
180  {
181  return static_cast<UnitType>( i );
182  }
183  }
184  return defaultType;
185 }
186 
188 {
189 #define DEGREE_TO_METER 111319.49079327358
190 #define FEET_TO_METER 0.3048
191 #define NMILE_TO_METER 1852.0
192 
193  // Unify degree units
194  if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
195  fromUnit = QGis::Degrees;
196  if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
197  toUnit = QGis::Degrees;
198 
199  // Calculate the conversion factor between the specified units
200  if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
201  {
202  switch ( fromUnit )
203  {
204  case QGis::Meters:
205  {
206  if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
207  if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
208  if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
209  break;
210  }
211  case QGis::Feet:
212  {
213  if ( toUnit == QGis::Meters ) return FEET_TO_METER;
214  if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
215  if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
216  break;
217  }
218  case QGis::Degrees:
219  {
220  if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
221  if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
222  if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
223  break;
224  }
225  case QGis::NauticalMiles:
226  {
227  if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
228  if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
229  if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
230  break;
231  }
232  case QGis::UnknownUnit:
233  break;
234  }
235  }
236  return 1.0;
237 }
238 
239 double QGis::permissiveToDouble( QString string, bool &ok )
240 {
241  //remove any thousands separators
242  string.remove( QLocale::system().groupSeparator() );
243  return QLocale::system().toDouble( string, &ok );
244 }
245 
246 int QGis::permissiveToInt( QString string, bool &ok )
247 {
248  //remove any thousands separators
249  string.remove( QLocale::system().groupSeparator() );
250  return QLocale::system().toInt( string, &ok );
251 }
252 
253 void *qgsMalloc( size_t size )
254 {
255  if ( size == 0 || long( size ) < 0 )
256  {
257  QgsDebugMsg( QString( "Negative or zero size %1." ).arg( size ) );
258  return NULL;
259  }
260  void *p = malloc( size );
261  if ( p == NULL )
262  {
263  QgsDebugMsg( QString( "Allocation of %1 bytes failed." ).arg( size ) );
264  }
265  return p;
266 }
267 
268 void *qgsCalloc( size_t nmemb, size_t size )
269 {
270  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
271  {
272  QgsDebugMsg( QString( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
273  return NULL;
274  }
275  void *p = qgsMalloc( nmemb * size );
276  if ( p != NULL )
277  {
278  memset( p, 0, nmemb * size );
279  }
280  return p;
281 }
282 
283 void qgsFree( void *ptr )
284 {
285  free( ptr );
286 }
287 
288 bool qgsVariantLessThan( const QVariant& lhs, const QVariant& rhs )
289 {
290  switch ( lhs.type() )
291  {
292  case QVariant::Int:
293  return lhs.toInt() < rhs.toInt();
294  case QVariant::UInt:
295  return lhs.toUInt() < rhs.toUInt();
296  case QVariant::LongLong:
297  return lhs.toLongLong() < rhs.toLongLong();
298  case QVariant::ULongLong:
299  return lhs.toULongLong() < rhs.toULongLong();
300  case QVariant::Double:
301  return lhs.toDouble() < rhs.toDouble();
302  case QVariant::Char:
303  return lhs.toChar() < rhs.toChar();
304  case QVariant::Date:
305  return lhs.toDate() < rhs.toDate();
306  case QVariant::Time:
307  return lhs.toTime() < rhs.toTime();
308  case QVariant::DateTime:
309  return lhs.toDateTime() < rhs.toDateTime();
310  default:
311  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
312  }
313 }
314 
315 bool qgsVariantGreaterThan( const QVariant& lhs, const QVariant& rhs )
316 {
317  return ! qgsVariantLessThan( lhs, rhs );
318 }
319 
321 {
322  if ( path.startsWith( "/vsizip/", Qt::CaseInsensitive ) ||
323  path.endsWith( ".zip", Qt::CaseInsensitive ) )
324  return "/vsizip/";
325  else if ( path.startsWith( "/vsitar/", Qt::CaseInsensitive ) ||
326  path.endsWith( ".tar", Qt::CaseInsensitive ) ||
327  path.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
328  path.endsWith( ".tgz", Qt::CaseInsensitive ) )
329  return "/vsitar/";
330  else if ( path.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ||
331  path.endsWith( ".gz", Qt::CaseInsensitive ) )
332  return "/vsigzip/";
333  else
334  return "";
335 }
336 
338 {
339  switch ( type )
340  {
341  case WKBMultiPoint: return WKBPoint;
342  case WKBMultiLineString: return WKBLineString;
343  case WKBMultiPolygon: return WKBPolygon;
344  case WKBMultiPoint25D: return WKBPoint25D;
346  case WKBMultiPolygon25D: return WKBPolygon25D;
347  default: return fromNewWkbType( QgsWKBTypes::singleType( fromOldWkbType( type ) ) );
348  }
349 }
350 
352 {
353  switch ( type )
354  {
355  case WKBPoint: return WKBMultiPoint;
356  case WKBLineString: return WKBMultiLineString;
357  case WKBPolygon: return WKBMultiPolygon;
358  case WKBPoint25D: return WKBMultiPoint25D;
360  case WKBPolygon25D: return WKBMultiPolygon25D;
361  default: return fromNewWkbType( QgsWKBTypes::multiType( fromOldWkbType( type ) ) );
362  }
363 }
364 
366 {
367  switch ( type )
368  {
369  case WKBPoint25D: return WKBPoint;
370  case WKBLineString25D: return WKBLineString;
371  case WKBPolygon25D: return WKBPolygon;
372  case WKBMultiPoint25D: return WKBMultiPoint;
374  case WKBMultiPolygon25D: return WKBMultiPolygon;
375  default: return fromNewWkbType( QgsWKBTypes::flatType( fromOldWkbType( type ) ) );
376  }
377 }
378 
380 {
381  return QgsWKBTypes::isSingleType( fromOldWkbType( type ) );
382 }
383 
385 {
386  return QgsWKBTypes::isMultiType( fromOldWkbType( type ) );
387 }
388 
390 {
391  if ( type == WKBUnknown || type == WKBNoGeometry )
392  return 0;
393 
394  QgsWKBTypes::Type wkbType = fromOldWkbType( type );
395  return 2 + ( QgsWKBTypes::hasZ( wkbType ) ? 1 : 0 ) + ( QgsWKBTypes::hasM( wkbType ) ? 1 : 0 );
396 }
397 
399 {
400  switch ( type )
401  {
402  case Point: return "Point";
403  case Line: return "Line";
404  case Polygon: return "Polygon";
405  case UnknownGeometry: return "Unknown geometry";
406  case NoGeometry: return "No geometry";
407  default: return "Invalid type";
408  }
409 }
410 
411 
412 const char *QGis::featureType( QGis::WkbType type )
413 {
414  switch ( type )
415  {
416  case WKBUnknown: return "WKBUnknown";
417  case WKBPoint: return "WKBPoint";
418  case WKBLineString: return "WKBLineString";
419  case WKBPolygon: return "WKBPolygon";
420  case WKBMultiPoint: return "WKBMultiPoint";
421  case WKBMultiLineString: return "WKBMultiLineString";
422  case WKBMultiPolygon: return "WKBMultiPolygon";
423  case WKBNoGeometry: return "WKBNoGeometry";
424  case WKBPoint25D: return "WKBPoint25D";
425  case WKBLineString25D: return "WKBLineString25D";
426  case WKBPolygon25D: return "WKBPolygon25D";
427  case WKBMultiPoint25D: return "WKBMultiPoint25D";
428  case WKBMultiLineString25D: return "WKBMultiLineString25D";
429  case WKBMultiPolygon25D: return "WKBMultiPolygon25D";
430  default: return "invalid wkbtype";
431 
432  }
433 }
static const char * QGIS_VERSION
Definition: qgis.h:42
static WkbType flatType(WkbType type)
Map 2d+ to 2d type.
Definition: qgis.cpp:365
qlonglong toLongLong(bool *ok) const
static double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:222
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:218
static bool isMultiType(Type type)
Definition: qgswkbtypes.cpp:75
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:246
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:253
int localeAwareCompare(const QString &other) const
static int wkbDimensions(WkbType type)
Definition: qgis.cpp:389
static Type multiType(Type type)
Definition: qgswkbtypes.cpp:36
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
UnitType
Map units that qgis supports.
Definition: qgis.h:147
QDateTime toDateTime() const
#define FEET_TO_METER
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:239
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:268
QTime toTime() const
static QGis::WkbType fromNewWkbType(QgsWKBTypes::Type type)
Converts from new (post 2.10) WKB type to old WKB type.
Definition: qgis.cpp:128
#define DEGREE_TO_METER
static const char * vectorGeometryType(GeometryType type)
description strings for geometry types
Definition: qgis.cpp:398
static const double DEFAULT_IDENTIFY_RADIUS
Old search radius in % of canvas width.
Definition: qgis.h:206
GeometryType
Definition: qgis.h:104
WkbType
Used for symbology operations.
Definition: qgis.h:56
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:315
qulonglong toULongLong(bool *ok) const
static Type singleType(Type type)
Definition: qgswkbtypes.cpp:26
double toDouble(const QString &s, bool *ok) const
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Definition: qgis.cpp:288
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:75
QLocale system()
#define NMILE_TO_METER
static QgsWKBTypes::Type fromOldWkbType(QGis::WkbType type)
Converts from old (pre 2.10) WKB type to new WKB type.
Definition: qgis.cpp:103
static Type flatType(Type type)
Definition: qgswkbtypes.cpp:46
uint toUInt(bool *ok) const
static bool hasM(Type type)
Tests whether a WKB type contains m values.
int toInt(bool *ok) const
const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:53
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:379
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:213
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:73
static WkbType singleType(WkbType type)
Map multi to single type.
Definition: qgis.cpp:337
const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:56
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:210
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 QString tr(QGis::UnitType unit)
Provides translated version of the type value.
Definition: qgis.cpp:170
QDate toDate() const
static WkbType multiType(WkbType type)
Map single to multitype type.
Definition: qgis.cpp:351
const QString PROJECT_SCALES
Definition: qgis.cpp:69
static bool isSingleType(Type type)
Definition: qgswkbtypes.cpp:70
static UnitType fromTr(const QString &literal, QGis::UnitType defaultType=UnknownUnit)
Provides type value from translated version.
Definition: qgis.cpp:175
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:320
QString translate(const char *context, const char *sourceText, const char *disambiguation, Encoding encoding)
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:187
int toInt(const QString &s, bool *ok, int base) const
double toDouble(bool *ok) const
static QString toLiteral(QGis::UnitType unit)
Provides the canonical name of the type value.
Definition: qgis.cpp:165
Type type() const
static bool isMultiType(WkbType type)
Return if type is a multi type.
Definition: qgis.cpp:384
static UnitType fromLiteral(const QString &literal, QGis::UnitType defaultType=UnknownUnit)
Converts from the canonical name to the type value.
Definition: qgis.cpp:153
QString toString() const
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:283
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:226
QChar toChar() const