QGIS API Documentation  3.6.0-Noosa (5873452)
qgsmemoryproviderutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmemoryproviderutils.cpp
3  --------------------------
4  begin : May 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsmemoryproviderutils.h"
19 #include "qgsfields.h"
20 #include "qgsvectorlayer.h"
21 
22 QString memoryLayerFieldType( QVariant::Type type )
23 {
24  switch ( type )
25  {
26  case QVariant::Int:
27  return QStringLiteral( "integer" );
28 
29  case QVariant::LongLong:
30  return QStringLiteral( "long" );
31 
32  case QVariant::Double:
33  return QStringLiteral( "double" );
34 
35  case QVariant::String:
36  return QStringLiteral( "string" );
37 
38  case QVariant::Date:
39  return QStringLiteral( "date" );
40 
41  case QVariant::Time:
42  return QStringLiteral( "time" );
43 
44  case QVariant::DateTime:
45  return QStringLiteral( "datetime" );
46 
47  case QVariant::ByteArray:
48  return QStringLiteral( "binary" );
49 
50  case QVariant::Bool:
51  return QStringLiteral( "boolean" );
52 
53  default:
54  break;
55  }
56  return QStringLiteral( "string" );
57 }
58 
60 {
61  QString geomType = QgsWkbTypes::displayString( geometryType );
62  if ( geomType.isNull() )
63  geomType = QStringLiteral( "none" );
64 
65  QStringList parts;
66  if ( crs.isValid() )
67  {
68  parts << QStringLiteral( "crs=" ) + crs.authid();
69  }
70  for ( const auto &field : fields )
71  {
72  const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
73  parts << QStringLiteral( "field=%1:%2%3" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ), memoryLayerFieldType( field.type() ), lengthPrecision );
74  }
75 
76  QString uri = geomType + '?' + parts.join( '&' );
77 
78  return new QgsVectorLayer( uri, name, QStringLiteral( "memory" ) );
79 }
Container of fields for a vector layer.
Definition: qgsfields.h:42
const QgsCoordinateReferenceSystem & crs
QString memoryLayerFieldType(QVariant::Type type)
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
This class represents a coordinate reference system (CRS).
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Creates a new memory layer using the specified parameters.
Represents a vector layer which manages a vector based data sets.
QString authid() const
Returns the authority identifier for the CRS.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.